lib/BalanceOfPower/Role/Broker.pm

filter: Statementstmt 14.1% 19 / 134 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 35.7% 5 / 14 Podpod 0.0% 0 / 9 totaltotal 11.2% 24 / 214 CC 33 SCAR 43.2
CC
33
Cov
10%
execute_stock_orders
156.0
buy_stock
110.0
sell_stock
56.0
CRAP 75.1
« lib/BalanceOfPower/Commands/Role/Command.pm ↑ Summary lib/BalanceOfPower/Utils.pm »
1 package BalanceOfPower::Role::Broker;
2 11 $BalanceOfPower::Role::Broker::VERSION = '0.300010';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use BalanceOfPower::Constants ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use BalanceOfPower::Utils qw( prev_turn );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 requires 'get_player';
10 11 requires 'get_nation';
11 11 requires 'get_statistics_value';
12  
13 sub manage_stock
14 {
15 0 ▶ my $self = shift;
Subroutine manage_stock: not called CC=3 SCAR=24.8
Pod undocumented
16 0 my $command = shift;
17 0 my $player = shift;
18 0 my $nation = shift;
19 0 my $q = shift;
20 0 ▶ if($command eq 'buy')
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq 'buy') { } 0 0 0
elsif ($command eq 'sell') { } 0 0 0
21 {
22 0 return $self->buy_stock($player, $nation, $q);
23 }
24 elsif($command eq 'sell')
25 {
26 0 return $self->sell_stock($player, $nation, $q);
27 }
28 }
29  
30 sub buy_stock
31 {
32 0 ▶ my $self = shift;
Subroutine buy_stock: not called CC=10 SCAR=47.0
Pod undocumented
33 0 my $player = shift;
34 0 my $nation = shift;
35 0 my $q = shift;
36 0 ▶ my $dry_run = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
37 0 my $player_obj = $self->get_player($player);
38 0 my $nation_obj = $self->get_nation($nation);
39 0 ▶ if($nation_obj->available_stocks < $q)
Branch: 1 branch
BranchTrueFalseTotal
if ($nation_obj->available_stocks < $q) 0 0 0
40 {
41 0 ▶ $player_obj->register_event("STOCKS IN QUANTITY $q FOR $nation NOT AVAILABLE")
Branch: 1 branch
BranchTrueFalseTotal
unless $dry_run 0 0 0
42 if(! $dry_run);
43 0 return { status => -11 };
44 }
45 0 ▶ if($q > MAX_BUY_STOCK)
Branch: 1 branch
BranchTrueFalseTotal
if ($q > 4) 0 0 0
46 {
47 0 return { status => -15 };
48 }
49 0 ▶ if($self->at_civil_war($nation))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->at_civil_war($nation)) 0 0 0
50 {
51 0 ▶ $player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR")
Branch: 1 branch
BranchTrueFalseTotal
unless $dry_run 0 0 0
52 if(! $dry_run);
53 0 return { status => -14 };
54 }
55 0 ▶ return { status => 1,
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
56 command => "buy $q $nation" } if $dry_run;
57 0 my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d');
58 0 my $global_cost = $unit_cost * $q;
59 0 my $influence = $q * STOCK_INFLUENCE_FACTOR;
60 0 ▶ if($player_obj->money < $global_cost)
Branch: 1 branch
BranchTrueFalseTotal
if ($player_obj->money < $global_cost) 0 0 0
61 {
62 0 ▶ $player_obj->register_event("NOT ENOUGH MONEY TO BUY STOCKS OF $nation FOR $global_cost")
Branch: 1 branch
BranchTrueFalseTotal
unless $dry_run 0 0 0
63 if(! $dry_run);
64 0 return { status => -12 };
65 }
66 0 $player_obj->add_money(-1 * $global_cost);
67 0 $player_obj->add_stocks($q, $nation);
68 0 $player_obj->add_influence($influence, $nation);
69 0 $nation_obj->get_stocks($q);
70  
71 0 $player_obj->register_event("BOUGHT $q STOCKS OF $nation. COST: $global_cost ($unit_cost)");
72 0 return { status => 1 };
73 }
74 sub sell_stock
75 {
76 0 ▶ my $self = shift;
Subroutine sell_stock: not called CC=7 SCAR=40.3
Pod undocumented
77 0 my $player = shift;
78 0 my $nation = shift;
79 0 my $q = shift;
80 0 ▶ my $dry_run = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
81 0 my $player_obj = $self->get_player($player);
82 0 my $nation_obj = $self->get_nation($nation);
83 0 ▶ if($player_obj->stocks($nation) < $q)
Branch: 1 branch
BranchTrueFalseTotal
if ($player_obj->stocks($nation) < $q) 0 0 0
84 {
85 0 ▶ $player_obj->register_event("THERE AREN'T $q STOCKS OF $nation TO SELL")
Branch: 1 branch
BranchTrueFalseTotal
unless $dry_run 0 0 0
86 if(! $dry_run);
87 0 return { status => -13 };
88 }
89 0 ▶ if($self->at_civil_war($nation))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->at_civil_war($nation)) 0 0 0
90 {
91 0 ▶ $player_obj->register_event("NO STOCK TRANSATION WITH $nation. $nation IS IN CIVIL WAR")
Branch: 1 branch
BranchTrueFalseTotal
unless $dry_run 0 0 0
92 if(! $dry_run);
93 0 return { status => -14 };
94 }
95 0 ▶ return { status => 1,
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
96 command => "sell $q $nation" } if $dry_run;
97 0 my $unit_cost = $self->get_statistics_value(prev_turn($self->current_year), $nation, 'w/d');
98 0 my $global_cost = $unit_cost * $q;
99 0 $player_obj->add_money($global_cost);
100 0 $player_obj->add_stocks(-1 * $q, $nation);
101 0 $nation_obj->get_stocks(-1 * $q);
102 0 $player_obj->register_event("SOLD $q STOCKS OF $nation. COST: $global_cost ($unit_cost)");
103 0 return { status => 1 };
104 }
105 sub issue_war_bonds
106 {
107 0 ▶ my $self = shift;
Subroutine issue_war_bonds: not called CC=2 SCAR=17.9
Pod undocumented
108 0 my $n = shift;
109 0 for(@{$self->players})
110 {
111 0 $_->issue_war_bonds($n);
112 }
113 }
114 sub discard_war_bonds
115 {
116 0 ▶ my $self = shift;
Subroutine discard_war_bonds: not called CC=2 SCAR=17.9
Pod undocumented
117 0 my $nation = shift;
118 0 for(@{$self->players})
119 {
120 0 $_->discard_war_bonds($nation);
121 }
122 }
123 sub empty_stocks
124 {
125 0 ▶ my $self = shift;
Subroutine empty_stocks: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my $nation = shift;
127 0 for(@{$self->players})
128 {
129 0 $_->empty_stocks($nation);
130 }
131 }
132 sub cash_war_bonds
133 {
134 0 ▶ my $self = shift;
Subroutine cash_war_bonds: not called CC=2 SCAR=17.9
Pod undocumented
135 0 my $nation = shift;
136 0 for(@{$self->players})
137 {
138 0 $_->cash_war_bonds($nation);
139 }
140 }
141 sub execute_stock_orders
142 {
143 0 ▶ my $self = shift;
Subroutine execute_stock_orders: not called CC=12 SCAR=50.5
Pod undocumented
144 0 ▶ my $dry_run = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
145 0 my %orders;
146 my @acting_players;
147 0 foreach my $player (@{$self->players})
148 {
149 0 my @p_orders = @{$player->stock_orders};
150 0 ▶ if(@p_orders)
Branch: 1 branch
BranchTrueFalseTotal
if (@p_orders) 0 0 0
151 {
152 0 ▶ say "Player " . $player->name . " has stock orders" if($dry_run);
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
153 0 $orders{$player->name} = \@p_orders;
154 0 push @acting_players, $player->name;
155 }
156 0 $player->empty_stock_orders();
157 }
158 0 ▶ say "===" if($dry_run);
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
159 0 ▶ while(@acting_players)
Branch: 1 branch
BranchTrueFalseTotal
if (@acting_players) 0 0 0
160 {
161 0 @acting_players = $self->shuffle("Choosing player to execute stock action", @acting_players);
162 0 my $chosen_player = $acting_players[0];
163 0 ▶ say "Chosen player is $chosen_player" if($dry_run);
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
164 0 my $order = shift @{$orders{$chosen_player}};
165 0 ▶ say "Order is $order" if($dry_run);
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
166 0 $order =~ /^(.*)\s(\d)\s(.*)$/;
167 0 my $command = $1;
168 0 my $q = $2;
169 0 my $nation = $3;
170 0 ▶ $self->manage_stock($command, $chosen_player, $nation, $q) if (! $dry_run);
Branch: 1 branch
BranchTrueFalseTotal
unless $dry_run 0 0 0
171 0 ▶ if(! @{$orders{$chosen_player}})
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$orders{$chosen_player};}) 0 0 0
172 {
173 0 @acting_players = grep { $_ ne $chosen_player } @acting_players;
174 }
175 0 ▶ say "---" if($dry_run);
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
176 }
177 }
178 sub print_stock_events
179 {
180 0 ▶ my $self = shift;
Subroutine print_stock_events: not called CC=1 SCAR=6.9
Pod undocumented
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 11 1;
« lib/BalanceOfPower/Commands/Role/Command.pm ↑ Summary lib/BalanceOfPower/Utils.pm »