File Coverage

lib/Settlers/Event/Monopoly.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 6 66.6
condition 3 9 33.3
subroutine 8 8 100.0
pod 0 2 0.0
total 45 55 81.8


line stmt bran cond sub pod time code
1             package Settlers::Event::Monopoly;
2             $Settlers::Event::Monopoly::VERSION = '0.07';
3 2     2   10 use strict;
  2     1   4  
  2         50  
  1         929  
  1         2  
  1         23  
4 2     2   9 use warnings;
  2     1   4  
  2         46  
  1         5  
  1         3  
  1         25  
5 2     2   735 use Settlers::Game::Trade;
  2     1   6  
  2         411  
  1         5  
  1         2  
  1         353  
6              
7 1     1 0 5 sub new { bless {}, shift }
8              
9             sub calculate
10             {
11 1     1 0 2 my ($self, $player_number, $code, $players, $bank) = @_;
12 1 50 33     16 die 'calculate requires a player number, resource code and players arrayref'
      33        
      33        
13             unless $player_number && $code && $players && ref $players eq 'ARRAY';
14              
15             # loop through all other players and trade away their resource to the player
16 1         3 my %resources_summary = ();
17 1         2 for my $losing_player (@$players)
18             {
19 4 100       12 next if $losing_player->number == $player_number;
20 3 50       11 if (my $amount = $losing_player->resources->{$code})
21             {
22 3         89 my $trade = Settlers::Game::Trade->new($bank, $players, {
23             $player_number => { $code => $amount },
24             $losing_player->number => { $code => -$amount },
25             });
26 3         14 my $resources = $trade->execute;
27 3         11 for my $k (keys %$resources)
28             {
29 6         35 $resources_summary{$k}{$code} += $resources->{$k}{$code};
30             }
31             }
32             }
33 1         5 return \%resources_summary;
34             }
35             1;
36              
37             __END__