File Coverage

lib/Catan/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 Catan::Event::Monopoly;
2             $Catan::Event::Monopoly::VERSION = '0.03';
3 2     2   12 use strict;
  2     1   5  
  2         53  
  1         792  
  1         2  
  1         22  
4 2     2   10 use warnings;
  2     1   4  
  2         47  
  1         5  
  1         2  
  1         24  
5 2     2   852 use Catan::Game::Trade;
  2     1   5  
  2         414  
  1         5  
  1         1  
  1         326  
6              
7 1     1 0 4 sub new { bless {}, shift }
8              
9             sub calculate
10             {
11 1     1 0 3 my ($self, $player_number, $code, $players, $bank) = @_;
12 1 50 33     21 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         2 my %resources_summary = ();
17 1         3 for my $losing_player (@$players)
18             {
19 4 100       11 next if $losing_player->number == $player_number;
20 3 50       11 if (my $amount = $losing_player->resources->{$code})
21             {
22 3         11 my $trade = Catan::Game::Trade->new($bank, $players, {
23             $player_number => { $code => $amount },
24             $losing_player->number => { $code => -$amount },
25             });
26 3         13 my $resources = $trade->execute;
27 3         10 for my $k (keys %$resources)
28             {
29 6         31 $resources_summary{$k}{$code} += $resources->{$k}{$code};
30             }
31             }
32             }
33 1         4 return \%resources_summary;
34             }
35             1;
36              
37             __END__