lib/BalanceOfPower/Player.pm

filter: Statementstmt 12.2% 23 / 188 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 14.2% 5 / 35 Podpod 0.0% 0 / 27 totaltotal 10.1% 28 / 277 CC 19 SCAR 18.0
CC
19
Cov
11%
load
20.0
issue_war_bonds
12.0
load_wallet
12.0
CRAP 6.1
« lib/BalanceOfPower/Commands/Role/TreatiesUnderLimit.pm ↑ Summary blib/lib/BalanceOfPower/Role/WebMaster.pm »
1 package BalanceOfPower::Player;
2 11 $BalanceOfPower::Player::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  
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 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 11 );
26  
27 11 has current_year => (
28 is => 'rw'
29 );
30 has stock_orders => (
31 is => 'rw',
32 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 11 );
34 has control_orders => (
35 is => 'rw',
36 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 11 );
38  
39 sub init_nation_wallet
40 {
41 0 ▶ my $self = shift;
Subroutine init_nation_wallet: not called CC=1 SCAR=6.9
Pod undocumented
42 0 my $nation = shift;
43 0 $self->wallet->{$nation}->{'stocks'} = 0;
44 0 $self->wallet->{$nation}->{'influence'} = 0;
45 0 $self->wallet->{$nation}->{'war_bonds'} = 0;
46 }
47 sub add_wallet_element
48 {
49 0 ▶ my $self = shift;
Subroutine add_wallet_element: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my $element = shift;
51 0 my $nation = shift;
52 0 my $q = shift;
53 0 ▶ if(exists $self->wallet->{$nation})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->wallet->{$nation}) { } 0 0 0
54 {
55 0 $self->wallet->{$nation}->{$element} += $q;
56 }
57 else
58 {
59 0 $self->init_nation_wallet($nation);
60 0 $self->wallet->{$nation}->{$element} = $q;
61 }
62 }
63 sub get_wallet_element
64 {
65 0 ▶ my $self = shift;
Subroutine get_wallet_element: not called CC=2 SCAR=17.9
Pod undocumented
66 0 my $element = shift;
67 0 my $nation = shift;
68 0 ▶ if(exists $self->wallet->{$nation})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->wallet->{$nation}) { } 0 0 0
69 {
70 0 return $self->wallet->{$nation}->{$element};
71 }
72 else
73 {
74 0 return 0;
75 }
76 }
77  
78 sub add_stocks
79 {
80 0 ▶ my $self = shift;
Subroutine add_stocks: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $q = shift;
82 0 my $nation = shift;
83 0 $self->add_wallet_element('stocks', $nation, $q);
84 }
85 sub stocks
86 {
87 0 ▶ my $self = shift;
Subroutine stocks: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $nation = shift;
89 0 $self->get_wallet_element('stocks', $nation);
90 }
91 sub influence
92 {
93 0 ▶ my $self = shift;
Subroutine influence: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $nation = shift;
95 0 $self->get_wallet_element('influence', $nation);
96 }
97 sub add_influence
98 {
99 0 ▶ my $self = shift;
Subroutine add_influence: not called CC=1 SCAR=6.9
Pod undocumented
100 0 my $q = shift;
101 0 my $nation = shift;
102 0 $self->add_wallet_element('influence', $nation, $q);
103 }
104 sub add_war_bonds
105 {
106 0 ▶ my $self = shift;
Subroutine add_war_bonds: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my $nation = shift;
108 0 $self->add_wallet_element('war_bonds', $nation, 1);
109 }
110 sub war_bonds
111 {
112 0 ▶ my $self = shift;
Subroutine war_bonds: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my $nation = shift;
114 0 $self->get_wallet_element('war_bonds', $nation);
115 }
116 sub add_money
117 {
118 0 ▶ my $self = shift;
Subroutine add_money: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my $q = shift;
120 0 $self->money($self->money + $q);
121 0 ▶ $self->money(0) if($self->money < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->money < 0 0 0 0
122 }
123 sub issue_war_bonds
124 {
125 0 ▶ my $self = shift;
Subroutine issue_war_bonds: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my $nation = shift;
127 0 ▶ if($self->stocks($nation) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stocks($nation) > 0) 0 0 0
128 {
129 0 ▶ if($self->money > WAR_BOND_COST)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->money > 10) { } 0 0 0
130 {
131 0 $self->add_money(-1 * WAR_BOND_COST);
132 0 $self->add_war_bonds($nation);
133 0 $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 0 ▶ my $self = shift;
Subroutine discard_war_bonds: not called CC=2 SCAR=17.9
Pod undocumented
144 0 my $nation = shift;
145 0 ▶ if($self->war_bonds($nation) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->war_bonds($nation) > 0) 0 0 0
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 0 ▶ my $self = shift;
Subroutine cash_war_bonds: not called CC=2 SCAR=17.9
Pod undocumented
154 0 my $nation = shift;
155 0 ▶ if($self->war_bonds($nation) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->war_bonds($nation) > 0) 0 0 0
156 {
157 0 my $gain = $self->wallet->{$nation}->{'war_bonds'} * WAR_BOND_GAIN;
158 0 $self->add_money($gain);
159 0 $self->wallet->{$nation}->{'war_bonds'} = 0;
160 0 $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 0 ▶ my $self = shift;
Subroutine add_stock_order: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my $order = shift;
180 0 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 0 ▶ my $self = shift;
Subroutine empty_stock_orders: not called CC=1 SCAR=6.9
Pod undocumented
192 0 $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 0 ▶ my $self = shift;
Subroutine add_control_order: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my $nation = shift;
210 0 my $order = shift;
211 0 $self->control_orders->{$nation} = $order;
212 }
213 sub get_control_order
214 {
215 0 ▶ my $self = shift;
Subroutine get_control_order: not called CC=2 SCAR=17.9
Pod undocumented
216 0 my $nation = shift;
217 0 ▶ if(exists $self->control_orders->{$nation})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->control_orders->{$nation}) { } 0 0 0
218 {
219 0 return $self->control_orders->{$nation};
220 }
221 else
222 {
223 0 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 0 ▶ my $self = shift;
Subroutine empty_control_orders: not called CC=1 SCAR=6.9
Pod undocumented
235 0 $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)) . "\n";
257 }
258 }
259 sub load_wallet
260 {
261 0 ▶ my $self = shift;
Subroutine load_wallet: not called CC=3 SCAR=24.8
Pod undocumented
262 0 my $data = shift;
263 0 my %wallet = ();
264 0 ▶ return \%wallet if(! $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
265 0 my @lines = split /\n/, $data;
266 0 foreach my $line (@lines)
267 {
268 0 $line =~ s/^\s+//;
269 0 chomp $line;
270 0 my ($nation, $stocks, $war_bonds, $influence) = split ";", $line;
271 0 $wallet{$nation}->{'stocks'} = $stocks;
272 0 $wallet{$nation}->{'war_bonds'} = $war_bonds;
273 0 $wallet{$nation}->{'influence'} = $influence;
274 }
275 0 return \%wallet;
276 }
277  
278 sub dump
279 {
280 0 ▶ my $self = shift;
Subroutine dump: not called CC=2 SCAR=17.9
Pod undocumented
281 0 my $io = shift;
282 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
283 0 print {$io} $indent .
284 join(";", $self->name, $self->money, $self->current_year) . "\n";
285 0 print {$io} $indent . " " . "### WALLET\n";
286 0 $self->dump_wallet($io, " " . $indent);
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 . "\n";
320 }
321 }
322 0 my $events = $self->load_events($extracted_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;
« lib/BalanceOfPower/Commands/Role/TreatiesUnderLimit.pm ↑ Summary blib/lib/BalanceOfPower/Role/WebMaster.pm »