File Coverage

lib/BalanceOfPower/Role/Broker.pm
Criterion Covered Total %
statement 110 129 85.2
branch 24 46 52.1
condition 5 6 83.3
subroutine 13 14 92.8
pod 0 9 0.0
total 152 204 74.5


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::Broker;
2             $BalanceOfPower::Role::Broker::VERSION = '0.400115';
3 13     13   5798 use strict;
  13         24  
  13         372  
4 13     13   129 use v5.10;
  13         41  
5 13     13   50 use Moo::Role;
  13         23  
  13         88  
6 13     13   3459 use BalanceOfPower::Constants ':all';
  13         22  
  13         7150  
7 13     13   78 use BalanceOfPower::Utils qw( prev_turn );
  13         29  
  13         14769  
8              
9             requires 'get_player';
10             requires 'get_nation';
11             requires 'get_statistics_value';
12              
13             sub manage_stock
14             {
15 2     2 0 4 my $self = shift;
16 2         4 my $command = shift;
17 2         2 my $player = shift;
18 2         5 my $nation = shift;
19 2         4 my $q = shift;
20 2 100       10 if($command eq 'buy')
    50          
21             {
22 1         4 return $self->buy_stock($player, $nation, $q);
23             }
24             elsif($command eq 'sell')
25             {
26 1         6 return $self->sell_stock($player, $nation, $q);
27             }
28             }
29              
30             sub buy_stock
31             {
32 3     3 0 6 my $self = shift;
33 3         4 my $player = shift;
34 3         5 my $nation = shift;
35 3         6 my $q = shift;
36 3   100     10 my $dry_run = shift || 0;
37 3         16 my $player_obj = $self->get_player($player);
38 3         10 my $nation_obj = $self->get_nation($nation);
39 3 50       20 if($nation_obj->available_stocks < $q)
40             {
41 0 0       0 $player_obj->register_event("STOCKS IN QUANTITY $q FOR $nation NOT AVAILABLE")
42             if(! $dry_run);
43 0         0 return { status => -11 };
44             }
45 3 50       10 if($q > MAX_BUY_STOCK)
46             {
47 0         0 return { status => -15 };
48             }
49 3 100       15 if($self->at_civil_war($nation))
50             {
51 1 50       5 $player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR")
52             if(! $dry_run);
53 1         4 return { status => -14 };
54             }
55 2 100       13 return { status => 1,
56             command => "buy $q $nation" } if $dry_run;
57 1         9 my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d');
58 1         4 my $global_cost = $unit_cost * $q;
59 1         3 my $influence = $q * STOCK_INFLUENCE_FACTOR;
60 1 50       8 if($player_obj->money < $global_cost)
61             {
62 0 0       0 $player_obj->register_event("NOT ENOUGH MONEY TO BUY STOCKS OF $nation FOR $global_cost")
63             if(! $dry_run);
64 0         0 return { status => -12 };
65             }
66 1         6 $player_obj->add_money(-1 * $global_cost);
67 1         5 $player_obj->add_stocks($q, $nation);
68 1         5 $player_obj->add_influence($influence, $nation);
69 1         6 $nation_obj->get_stocks($q);
70              
71 1         12 $player_obj->register_event("BOUGHT $q STOCKS OF $nation. COST: $global_cost ($unit_cost)");
72 1         6 return { status => 1 };
73             }
74             sub sell_stock
75             {
76 2     2 0 3 my $self = shift;
77 2         5 my $player = shift;
78 2         2 my $nation = shift;
79 2         4 my $q = shift;
80 2   100     10 my $dry_run = shift || 0;
81 2         9 my $player_obj = $self->get_player($player);
82 2         8 my $nation_obj = $self->get_nation($nation);
83 2 50       12 if($player_obj->stocks($nation) < $q)
84             {
85 0 0       0 $player_obj->register_event("THERE AREN'T $q STOCKS OF $nation TO SELL")
86             if(! $dry_run);
87 0         0 return { status => -13 };
88             }
89 2 50       12 if($self->at_civil_war($nation))
90             {
91 0 0       0 $player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR")
92             if(! $dry_run);
93 0         0 return { status => -14 };
94             }
95 2 100       14 return { status => 1,
96             command => "sell $q $nation" } if $dry_run;
97 1         12 my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d');
98 1         4 my $global_cost = $unit_cost * $q;
99 1         5 $player_obj->add_money($global_cost);
100 1         6 $player_obj->add_stocks(-1 * $q, $nation);
101 1         5 $nation_obj->get_stocks(-1 * $q);
102 1         12 $player_obj->register_event("SOLD $q STOCKS OF $nation. COST: $global_cost ($unit_cost)");
103 1         5 return { status => 1 };
104             }
105             sub issue_war_bonds
106             {
107 22     22 0 38 my $self = shift;
108 22         24 my $n = shift;
109 22         31 for(@{$self->players})
  22         81  
110             {
111 4         25 $_->issue_war_bonds($n);
112             }
113             }
114             sub discard_war_bonds
115             {
116 2     2 0 2 my $self = shift;
117 2         3 my $nation = shift;
118 2         1 for(@{$self->players})
  2         7  
119             {
120 0         0 $_->discard_war_bonds($nation);
121             }
122             }
123             sub empty_stocks
124             {
125 2     2 0 4 my $self = shift;
126 2         6 my $nation = shift;
127 2         5 for(@{$self->players})
  2         10  
128             {
129 1         6 $_->empty_stocks($nation);
130             }
131             }
132             sub cash_war_bonds
133             {
134 2     2 0 3 my $self = shift;
135 2         3 my $nation = shift;
136 2         3 for(@{$self->players})
  2         9  
137             {
138 0         0 $_->cash_war_bonds($nation);
139             }
140             }
141             sub execute_stock_orders
142             {
143 45     45 0 65 my $self = shift;
144 45   50     248 my $dry_run = shift || 0;
145 45         95 my %orders;
146             my @acting_players;
147 45         68 foreach my $player (@{$self->players})
  45         390  
148             {
149 10         25 my @p_orders = @{$player->stock_orders};
  10         46  
150 10 100       48 if(@p_orders)
151             {
152 2 50       7 say "Player " . $player->name . " has stock orders" if($dry_run);
153 2         12 $orders{$player->name} = \@p_orders;
154 2         8 push @acting_players, $player->name;
155             }
156 10         54 $player->empty_stock_orders();
157             }
158 45 50       133 say "===" if($dry_run);
159 45         202 while(@acting_players)
160             {
161 2         51 @acting_players = $self->shuffle("Choosing player to execute stock action", @acting_players);
162 2         4 my $chosen_player = $acting_players[0];
163 2 50       8 say "Chosen player is $chosen_player" if($dry_run);
164 2         4 my $order = shift @{$orders{$chosen_player}};
  2         7  
165 2 50       8 say "Order is $order" if($dry_run);
166 2         10 $order =~ /^(.*)\s(\d)\s(.*)$/;
167 2         6 my $command = $1;
168 2         7 my $q = $2;
169 2         4 my $nation = $3;
170 2 50       14 $self->manage_stock($command, $chosen_player, $nation, $q) if (! $dry_run);
171 2 50       5 if(! @{$orders{$chosen_player}})
  2         10  
172             {
173 2         6 @acting_players = grep { $_ ne $chosen_player } @acting_players;
  2         7  
174             }
175 2 50       13 say "---" if($dry_run);
176             }
177             }
178             sub print_stock_events
179             {
180 0     0 0   my $self = shift;
181 0           my $player = shift;
182 0           my $player_obj = $self->get_player($player);
183 0           my $y = shift;
184 0           my $title = shift;
185 0           my $backlog = shift;
186 0           my $mode = shift;
187 0           return $player_obj->print_turn_events($y, $title, $backlog, $mode);
188             }
189              
190             1;