lib/BalanceOfPower/Player.pm

filter: Statementstmt 54.5% 103 / 189 Branchbran 54.5% 12 / 22 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 74.2% 26 / 35 Podpod 0.0% 0 / 27 totaltotal 51.0% 141 / 276 CC 18 SCAR 13.9
CC
18
Cov
54%
load
20.0
empty_stocks
6.0
print_stock_orders
6.0
CRAP 4.0
« blib/lib/BalanceOfPower/Commands/MilitaryAid.pm ↑ Summary blib/lib/BalanceOfPower/Commands/DeclareWar.pm »
1 package BalanceOfPower::Player;
2 11 $BalanceOfPower::Player::VERSION = '0.200000';
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  
6 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use BalanceOfPower::Constants ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use BalanceOfPower::Utils qw(as_title);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 with 'BalanceOfPower::Role::Reporter';
11  
12 11 has name => (
13 is => 'ro',
14 default => 'Player'
15 );
16  
17 11 has money => (
18 is => 'rw',
19 default => 0
20 );
21  
22 has wallet => (
23 is => 'ro',
24 1 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 11 );
26  
27 11 has current_year => (
28 is => 'rw'
29 );
30 has stock_orders => (
31 is => 'rw',
32 1 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
33 11 );
34 has control_orders => (
35 is => 'rw',
36 1 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
37 11 );
38  
39 sub init_nation_wallet
40 {
41 1 ▶ my $self = shift;
Subroutine init_nation_wallet: called CC=1 SCAR=0.0
Pod undocumented
42 1 my $nation = shift;
43 1 $self->wallet->{$nation}->{'stocks'} = 0;
44 1 $self->wallet->{$nation}->{'influence'} = 0;
45 1 $self->wallet->{$nation}->{'war bonds'} = 0;
46 }
47 sub add_wallet_element
48 {
49 6 ▶ my $self = shift;
Subroutine add_wallet_element: called CC=2 SCAR=6.9
Pod undocumented
50 6 my $element = shift;
51 6 my $nation = shift;
52 6 my $q = shift;
53 6 ▶ if(exists $self->wallet->{$nation})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->wallet->{$nation}) { } 5 1 6
54 {
55 5 $self->wallet->{$nation}->{$element} += $q;
56 }
57 else
58 {
59 1 $self->init_nation_wallet($nation);
60 1 $self->wallet->{$nation}->{$element} = $q;
61 }
62 }
63 sub get_wallet_element
64 {
65 18 ▶ my $self = shift;
Subroutine get_wallet_element: called CC=2 SCAR=6.9
Pod undocumented
66 18 my $element = shift;
67 18 my $nation = shift;
68 18 ▶ if(exists $self->wallet->{$nation})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->wallet->{$nation}) { } 15 3 18
69 {
70 15 return $self->wallet->{$nation}->{$element};
71 }
72 else
73 {
74 3 return 0;
75 }
76 }
77  
78 sub add_stocks
79 {
80 2 ▶ my $self = shift;
Subroutine add_stocks: called CC=1 SCAR=0.0
Pod undocumented
81 2 my $q = shift;
82 2 my $nation = shift;
83 2 $self->add_wallet_element('stocks', $nation, $q);
84 }
85 sub stocks
86 {
87 10 ▶ my $self = shift;
Subroutine stocks: called CC=1 SCAR=0.0
Pod undocumented
88 10 my $nation = shift;
89 10 $self->get_wallet_element('stocks', $nation);
90 }
91 sub influence
92 {
93 4 ▶ my $self = shift;
Subroutine influence: called CC=1 SCAR=0.0
Pod undocumented
94 4 my $nation = shift;
95 4 $self->get_wallet_element('influence', $nation);
96 }
97 sub add_influence
98 {
99 2 ▶ my $self = shift;
Subroutine add_influence: called CC=1 SCAR=0.0
Pod undocumented
100 2 my $q = shift;
101 2 my $nation = shift;
102 2 $self->add_wallet_element('influence', $nation, $q);
103 }
104 sub add_war_bonds
105 {
106 2 ▶ my $self = shift;
Subroutine add_war_bonds: called CC=1 SCAR=0.0
Pod undocumented
107 2 my $nation = shift;
108 2 $self->add_wallet_element('war bonds', $nation, 1);
109 }
110 sub war_bonds
111 {
112 4 ▶ my $self = shift;
Subroutine war_bonds: called CC=1 SCAR=0.0
Pod undocumented
113 4 my $nation = shift;
114 4 $self->get_wallet_element('war bonds', $nation);
115 }
116 sub add_money
117 {
118 5 ▶ my $self = shift;
Subroutine add_money: called CC=2 SCAR=7.0
Pod undocumented
119 5 my $q = shift;
120 5 $self->money($self->money + $q);
121 5 ▶ $self->money(0) if($self->money < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->money < 0 0 5 5
122 }
123 sub issue_war_bonds
124 {
125 4 ▶ my $self = shift;
Subroutine issue_war_bonds: called CC=3 SCAR=11.1
Pod undocumented
126 4 my $nation = shift;
127 4 ▶ if($self->stocks($nation) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stocks($nation) > 0) 2 2 4
128 {
129 2 ▶ if($self->money > WAR_BOND_COST)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->money > 10) { } 2 0 2
130 {
131 2 $self->add_money(-1 * WAR_BOND_COST);
132 2 $self->add_war_bonds($nation);
133 2 $self->register_event("WAR BOND FOR $nation ISSUED. PAYED " . WAR_BOND_COST);
134 }
135 else
136 {
137 0 $self->register_event("WAR BOND FOR $nation NOT ISSUED. NOT ENOUGH MONEY");
138 }
139 }
140 }
141 sub discard_war_bonds
142 {
143 1 ▶ my $self = shift;
Subroutine discard_war_bonds: called CC=2 SCAR=8.4
Pod undocumented
144 1 my $nation = shift;
145 1 ▶ if($self->war_bonds($nation) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->war_bonds($nation) > 0) 0 1 1
146 {
147 0 $self->wallet->{$nation}->{'war bonds'} = 0;
148 0 $self->register_event("WAR LOST FOR $nation! WAR BONDS FROM $nation HAVE NOW NO VALUE");
149 }
150 }
151 sub cash_war_bonds
152 {
153 1 ▶ my $self = shift;
Subroutine cash_war_bonds: called CC=2 SCAR=7.0
Pod undocumented
154 1 my $nation = shift;
155 1 ▶ if($self->war_bonds($nation) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->war_bonds($nation) > 0) 1 0 1
156 {
157 1 my $gain = $self->wallet->{$nation}->{'war bonds'} * WAR_BOND_GAIN;
158 1 $self->add_money($gain);
159 1 $self->wallet->{$nation}->{'war bonds'} = 0;
160 1 $self->register_event("WAR WON FOR $nation! GAINES $gain FROM WAR BONDS");
161  
162 }
163 }
164 sub empty_stocks
165 {
166 0 ▶ my $self = shift;
Subroutine empty_stocks: not called CC=2 SCAR=17.9
Pod undocumented
167 0 my $nation = shift;
168 0 ▶ if($self->stocks($nation) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stocks($nation) > 0) 0 0 0
169 {
170 0 $self->wallet->{$nation}->{'stocks'} = 0;
171 0 $self->wallet->{$nation}->{'war bonds'} = 0;
172 0 $self->wallet->{$nation}->{'influence'} = 0;
173 0 $self->register_event("INVESTMENTS IN $nation LOST BECAUSE OF REVOLUTION!");
174 }
175 }
176 sub add_stock_order
177 {
178 2 ▶ my $self = shift;
Subroutine add_stock_order: called CC=1 SCAR=0.0
Pod undocumented
179 2 my $order = shift;
180 2 push @{$self->stock_orders}, $order;
181 }
182 sub remove_stock_orders
183 {
184 0 ▶ my $self = shift;
Subroutine remove_stock_orders: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my $nation = shift;
186 0 my @ords = grep { $_ !~ /$nation/ } @{$self->stock_orders};
187 0 $self->stock_orders(\@ords);
188 }
189 sub empty_stock_orders
190 {
191 5 ▶ my $self = shift;
Subroutine empty_stock_orders: called CC=1 SCAR=0.0
Pod undocumented
192 5 $self->stock_orders([]);
193 }
194 sub print_stock_orders
195 {
196 0 ▶ my $self = shift;
Subroutine print_stock_orders: not called CC=2 SCAR=17.9
Pod undocumented
197 0 my $out = as_title("STOCK ORDERS");
198 0 $out .= "\n\n";
199 0 foreach my $ord (@{$self->stock_orders})
200 {
201 0 $out .= $ord . "\n";
202 }
203 0 return $out;
204 }
205  
206 sub add_control_order
207 {
208 1 ▶ my $self = shift;
Subroutine add_control_order: called CC=1 SCAR=0.0
Pod undocumented
209 1 my $nation = shift;
210 1 my $order = shift;
211 1 $self->control_orders->{$nation} = $order;
212 }
213 sub get_control_order
214 {
215 25 ▶ my $self = shift;
Subroutine get_control_order: called CC=2 SCAR=6.9
Pod undocumented
216 25 my $nation = shift;
217 25 ▶ if(exists $self->control_orders->{$nation})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->control_orders->{$nation}) { } 1 24 25
218 {
219 1 return $self->control_orders->{$nation};
220 }
221 else
222 {
223 24 return undef;
224 }
225 }
226 sub remove_control_order
227 {
228 0 ▶ my $self = shift;
Subroutine remove_control_order: not called CC=1 SCAR=6.9
Pod undocumented
229 0 my $nation = shift;
230 0 $self->control_orders->{$nation} = undef;
231 }
232 sub empty_control_orders
233 {
234 5 ▶ my $self = shift;
Subroutine empty_control_orders: called CC=1 SCAR=0.0
Pod undocumented
235 5 $self->control_orders({});
236 }
237 sub print_control_orders
238 {
239 0 ▶ my $self = shift;
Subroutine print_control_orders: not called CC=2 SCAR=17.9
Pod undocumented
240 0 my $out = as_title("CONTROL ORDERS");
241 0 $out .= "\n\n";
242 0 for(keys %{$self->control_orders})
243 {
244 0 my $n = $_;
245 0 $out .= $n . ": " . $self->get_control_order($n) . "\n";
246 }
247 0 return $out;
248 }
249 sub dump_wallet
250 {
251 0 ▶ my $self = shift;
Subroutine dump_wallet: not called CC=2 SCAR=17.9
Pod undocumented
252 0 my $io = shift;
253 0 my $indent = shift;
254 0 foreach my $nation(keys %{$self->wallet})
255 {
256 0 print {$io} $indent . join(";", $nation, $self->stocks($nation), $self->war_bonds($nation), $self->influence($nation));
257 }
258 }
259 sub load_wallet
260 {
261 0 ▶ my $self = shift;
Subroutine load_wallet: not called CC=2 SCAR=17.9
Pod undocumented
262 0 my $data = shift;
263 0 my @lines = split /\n/, $data;
264 0 my %wallet;
265 0 foreach my $line (@lines)
266 {
267 0 $line =~ s/^\s+//;
268 0 chomp $line;
269 0 my ($nation, $stocks, $war_bonds, $influence) = split ";", $line;
270 0 $wallet{$nation}->{'stocks'} = $stocks;
271 0 $wallet{$nation}->{'war bonds'} = $war_bonds;
272 0 $wallet{$nation}->{'influence'} = $influence;
273 }
274 0 return \%wallet;
275 }
276  
277 sub dump
278 {
279 0 ▶ my $self = shift;
Subroutine dump: not called CC=2 SCAR=17.9
Pod undocumented
280 0 my $io = shift;
281 0 ▶ my $indent = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
282 0 print {$io} $indent .
283 join(";", $self->name, $self->money, $self->current_year) . "\n";
284 0 print {$io} $indent . " " . "### WALLET\n";
285 0 $self->dump_wallet($io, " " . $indent);
286 0 print {$io} "\n";
287 0 print {$io} $indent . " " . "### EVENTS\n";
288 0 $self->dump_events($io, " " . $indent);
289 }
290  
291 sub load
292 {
293 0 ▶ my $self = shift;
Subroutine load: not called CC=4 SCAR=30.0
Pod undocumented
294 0 my $data = shift;
295 0 my @player_lines = split /\n/, $data;
296 0 my $player_line = shift @player_lines;
297 0 $player_line =~ s/^\s+//;
298 0 chomp $player_line;
299 0 my ($name, $money, $current_year) = split ";", $player_line;
300 0 my $what = '';
301 0 my $extracted_data;
302 my $wallet;
303 0 foreach my $line (@player_lines)
304 {
305 0 $line =~ s/^\s+//;
306 0 chomp $line;
307 0 ▶ if($line eq '### WALLET')
Branch: 2 branches
BranchTrueFalseTotal
if ($line eq '### WALLET') { } 0 0 0
elsif ($line eq '### EVENTS') { } 0 0 0
308 {
309 0 $what = 'wallet';
310 }
311 elsif($line eq '### EVENTS')
312 {
313 0 $what = 'events';
314 0 $wallet = $self->load_wallet($extracted_data);
315 0 $extracted_data = "";
316 }
317 else
318 {
319 0 $extracted_data .= $line;
320 }
321 }
322 0 my $events = $self->load_events($data);
323 0 return $self->new(name => $name, money => $money, current_year => $current_year,
324 wallet => $wallet,
325 events => $events);
326 }
327  
328  
329  
330 11 1;
« blib/lib/BalanceOfPower/Commands/MilitaryAid.pm ↑ Summary blib/lib/BalanceOfPower/Commands/DeclareWar.pm »