File Coverage

lib/Catan/Game/Bank.pm
Criterion Covered Total %
statement 40 62 64.5
branch 4 18 22.2
condition 7 35 20.0
subroutine 11 15 73.3
pod 0 12 0.0
total 62 142 43.6


line stmt bran cond sub pod time code
1             package Catan::Game::Bank;
2             $Catan::Game::Bank::VERSION = '0.02';
3 5     5   48267 use strict;
  5         10  
  5         136  
4 5     5   24 use warnings;
  5         9  
  5         142  
5 5     5   23 use List::Util 'shuffle';
  5         11  
  5         5211  
6              
7             my %resource_table = (
8             B => 'Catan::Resource::Brick',
9             G => 'Catan::Resource::Grain',
10             L => 'Catan::Resource::Lumber',
11             O => 'Catan::Resource::Ore',
12             W => 'Catan::Resource::Wool',
13             );
14             eval "require $_" for values %resource_table;
15              
16 0     0 0 0 sub name { 'The Bank' }
17 5     5 0 14 sub number { 'bank' }
18 3     3 0 9 sub resources { $_[0]->{resources} }
19              
20             sub new
21             {
22 3     3 0 27 my ($class, $args) = @_;
23              
24 3         54 return bless {
25             deck => deck_build($args),
26             resources => resource_build($args),
27             }, $class;
28             }
29              
30 4     4 0 21 sub deck { $_[0]->{deck} }
31              
32 0     0 0 0 sub deck_remaining { scalar @{$_->{deck}} }
  0         0  
33              
34             sub deck_build
35             {
36 3     3 0 7 my $args = shift;
37              
38             # NB the 5-6 player game has 1 extra MO, RB, YP. 5 extra KN.
39 3         6 my (@codes);
40 3   33     39 push @codes, ($args->{knight_count} || ('KN') x 14);
41 3   33     23 push @codes, ($args->{vp_count} || ('VP') x 5);
42 3   33     19 push @codes, ($args->{monopoly_count} || ('MO') x 2);
43 3   33     18 push @codes, ($args->{road_building_count} || ('RB') x 2);
44 3   33     20 push @codes, ($args->{year_of_plenty_count}|| ('YP') x 2);
45              
46 3         139 return [shuffle @codes];
47             }
48              
49             sub deck_draw
50             {
51 1     1 0 3 my ($self, $type) = @_;
52 1 50       2 die 'no more development cards left!' unless @{$self->deck};
  1         3  
53              
54             # if the type of card is in the deck use it instead of drawing
55 1 50       5 if ($type)
56             {
57 0         0 my $has_found = undef;
58 0         0 $type = uc $type;
59 0         0 my @deck = ();
60 0         0 for (@{$self->deck})
  0         0  
61             {
62 0 0 0     0 if ($_ ne $type || $has_found)
63             {
64 0         0 push(@deck, $_);
65             }
66             else
67             {
68 0         0 $has_found = 1;
69             }
70             }
71 0 0       0 $self->{deck} = [$type, @deck] if $has_found;
72             }
73 1         2 return shift @{$self->deck};
  1         4  
74             }
75              
76             sub resource_build
77             {
78 3     3 0 7 my $args = shift;
79             # NB the 5-6 player game has 25 of each resource
80 3   50     22 my $count = $args->{resource_count} || 19;
81             return {
82 3         59 B => $count,
83             G => $count,
84             L => $count,
85             O => $count,
86             W => $count,
87             };
88             }
89              
90             sub resource_from_notation
91             {
92 10     10 0 14 my ($self, $resources) = @_;
93 10 50 33     51 die 'resource from notation requires a hashref of resource codes and amounts'
94             unless $resources && ref $resources eq 'HASH';
95              
96 10         14 my @resource_objects = ();
97              
98 10         24 for my $code (keys %$resources)
99             {
100 26         39 my $amount = $resources->{$code};
101 26         39 my $class = $resource_table{ uc $code };
102              
103 26 50       50 die "invalid resource code $_->{code}" unless $class;
104 26         100 push @resource_objects, $class->new($amount);
105             }
106 10         41 return \@resource_objects;
107             }
108              
109             sub resource_by_code
110             {
111 0     0 0   my ($self, $code) = @_;
112             die 'resource by code requires a valid resource code!'
113 0 0 0       unless $code && exists $resource_table{ uc $code };
114              
115 0           return $resource_table{uc $code};
116             }
117              
118             sub resource_concede
119             {
120 0     0 0   my ($self, $player, $details) = @_;
121 0 0 0       die 'resource_concede requires a player and resources hashref'
      0        
      0        
122             unless $player && $player->isa('Catan::Game::Player')
123             && $details && ref $details eq 'HASH';
124              
125 0           my $trade = Catan::Game::Trade->new($self, [$player], { $player->number => $details});
126              
127             # check all amounts are negative as the player is supposed to be CONCEDING resources
128 0           for (@{$trade->resources($player->number)})
  0            
129             {
130 0 0         die 'resource_concede amounts must be negative!' unless $_->amount < 0;
131             }
132 0           return $trade->execute;
133             }
134              
135             1;
136              
137             __END__