| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Settlers::Game::Bank; | 
| 2 |  |  |  |  |  |  | $Settlers::Game::Bank::VERSION = '0.07'; | 
| 3 | 5 |  |  | 5 |  | 48823 | use strict; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 122 |  | 
| 4 | 5 |  |  | 5 |  | 24 | use warnings; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 146 |  | 
| 5 | 5 |  |  | 5 |  | 26 | use List::Util 'shuffle'; | 
|  | 5 |  |  |  |  | 13 |  | 
|  | 5 |  |  |  |  | 5129 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | my %resource_table = ( | 
| 8 |  |  |  |  |  |  | B => 'Settlers::Resource::Brick', | 
| 9 |  |  |  |  |  |  | G => 'Settlers::Resource::Grain', | 
| 10 |  |  |  |  |  |  | L => 'Settlers::Resource::Lumber', | 
| 11 |  |  |  |  |  |  | O => 'Settlers::Resource::Ore', | 
| 12 |  |  |  |  |  |  | W => 'Settlers::Resource::Wool', | 
| 13 |  |  |  |  |  |  | ); | 
| 14 |  |  |  |  |  |  | eval "require $_" for values %resource_table; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 0 |  |  | 0 | 0 | 0 | sub name      { 'The Bank' } | 
| 17 | 696 |  |  | 696 | 0 | 2611 | sub number    { 'bank' } | 
| 18 | 398 |  |  | 398 | 0 | 1132 | sub resources { $_[0]->{resources} } | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub new | 
| 21 |  |  |  |  |  |  | { | 
| 22 | 3 |  |  | 3 | 0 | 29 | my ($class, $args) = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 3 |  |  |  |  | 15 | return bless { | 
| 25 |  |  |  |  |  |  | deck      => deck_build($args), | 
| 26 |  |  |  |  |  |  | resources => resource_build($args), | 
| 27 |  |  |  |  |  |  | }, $class; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 28 |  |  | 28 | 0 | 103 | 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 |  |  |  |  | 5 | my (@codes); | 
| 40 | 3 |  | 33 |  |  | 47 | push @codes, ($args->{knight_count}        || ('KN') x 14); | 
| 41 | 3 |  | 33 |  |  | 23 | push @codes, ($args->{vp_count}            || ('VP') x 5); | 
| 42 | 3 |  | 33 |  |  | 25 | push @codes, ($args->{monopoly_count}      || ('MO') x 2); | 
| 43 | 3 |  | 33 |  |  | 20 | push @codes, ($args->{road_building_count} || ('RB') x 2); | 
| 44 | 3 |  | 33 |  |  | 18 | push @codes, ($args->{year_of_plenty_count}|| ('YP') x 2); | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 3 |  |  |  |  | 143 | return [shuffle @codes]; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub deck_draw | 
| 50 |  |  |  |  |  |  | { | 
| 51 | 9 |  |  | 9 | 0 | 17 | my ($self, $type) = @_; | 
| 52 | 9 | 50 |  |  |  | 18 | die 'no more development cards left!' unless @{$self->deck}; | 
|  | 9 |  |  |  |  | 26 |  | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # if the type of card is in the deck use it instead of drawing | 
| 55 | 9 | 100 |  |  |  | 31 | if ($type) | 
| 56 |  |  |  |  |  |  | { | 
| 57 | 8 |  |  |  |  | 15 | my $has_found = undef; | 
| 58 | 8 |  |  |  |  | 19 | $type = uc $type; | 
| 59 | 8 |  |  |  |  | 16 | my @deck = (); | 
| 60 | 8 |  |  |  |  | 11 | for (@{$self->deck}) | 
|  | 8 |  |  |  |  | 17 |  | 
| 61 |  |  |  |  |  |  | { | 
| 62 | 172 | 100 | 100 |  |  | 489 | if ($_ ne $type || $has_found) | 
| 63 |  |  |  |  |  |  | { | 
| 64 | 164 |  |  |  |  | 270 | push(@deck, $_); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | else | 
| 67 |  |  |  |  |  |  | { | 
| 68 | 8 |  |  |  |  | 13 | $has_found = 1; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | } | 
| 71 | 8 | 50 |  |  |  | 60 | $self->{deck} = [$type, @deck] if $has_found; | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 9 |  |  |  |  | 27 | return shift @{$self->deck}; | 
|  | 9 |  |  |  |  | 34 |  | 
| 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 |  |  | 20 | my $count = $args->{resource_count} || 19; | 
| 81 |  |  |  |  |  |  | return { | 
| 82 | 3 |  |  |  |  | 992 | B => $count, | 
| 83 |  |  |  |  |  |  | G => $count, | 
| 84 |  |  |  |  |  |  | L => $count, | 
| 85 |  |  |  |  |  |  | O => $count, | 
| 86 |  |  |  |  |  |  | W => $count, | 
| 87 |  |  |  |  |  |  | }; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub resource_from_notation | 
| 91 |  |  |  |  |  |  | { | 
| 92 | 172 |  |  | 172 | 0 | 300 | my ($self, $resources) = @_; | 
| 93 | 172 | 50 | 33 |  |  | 805 | die 'resource from notation requires a hashref of resource codes and amounts' | 
| 94 |  |  |  |  |  |  | unless $resources && ref $resources eq 'HASH'; | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 172 |  |  |  |  | 267 | my @resource_objects = (); | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 172 |  |  |  |  | 413 | for my $code (keys %$resources) | 
| 99 |  |  |  |  |  |  | { | 
| 100 | 292 |  |  |  |  | 449 | my $amount = $resources->{$code}; | 
| 101 | 292 |  |  |  |  | 577 | my $class = $resource_table{ uc $code }; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 292 | 50 |  |  |  | 544 | die "invalid resource code $_->{code}" unless $class; | 
| 104 | 292 |  |  |  |  | 902 | push @resource_objects, $class->new($amount); | 
| 105 |  |  |  |  |  |  | } | 
| 106 | 172 |  |  |  |  | 471 | 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('Settlers::Game::Player') | 
| 123 |  |  |  |  |  |  | && $details && ref $details eq 'HASH'; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 0 |  |  |  |  |  | my $trade = Settlers::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__ |