lib/BalanceOfPower/Role/Historian.pm

filter: Statementstmt 12.6% 31 / 246 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 32.1% 9 / 28 Podpod 0.0% 0 / 18 totaltotal 11.7% 40 / 342 CC 38 SCAR 30.4
CC
38
Cov
11%
plot_nation_factor
72.0
print_crises
42.0
print_turn_statistics
30.0
CRAP 20.9
« lib/BalanceOfPower/Relations/RelPack.pm ↑ Summary blib/lib/BalanceOfPower/Commands/TargetNation.pm »
1 package BalanceOfPower::Role::Historian;
2 11 $BalanceOfPower::Role::Historian::VERSION = '0.300010';
3 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use BalanceOfPower::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use BalanceOfPower::Utils qw( get_year_turns as_title from_to_turns );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use BalanceOfPower::Constants ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 11 with 'BalanceOfPower::Role::Reporter';
15  
16 has statistics => (
17 is => 'rw',
18 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 11 );
20  
21 11 requires 'get_nation';
22  
23 sub get_statistics_value
24 {
25 0 ▶ my $self = shift;
Subroutine get_statistics_value: not called CC=4 SCAR=30.0
Pod undocumented
26 0 my $turn = shift;
27 0 my $nation = shift;
28 0 my $value = shift;
29 0 ▶ if($turn && exists $self->statistics->{$turn})
Branch: 1 branch
BranchTrueFalseTotal
if ($turn and exists $self->statistics->{$turn}) { } 0 0 0
Condition0%0/3
$turn and exists $self->statistics->{$turn}
!ll&&!rl&&r
000
MC/DC0%0/2
$turn and exists $self->statistics->{$turn}
$turn exists $self->statistics->{$turn}
Truth table0%0/3
A and B
A= $turnB= exists $self->statistics->{$turn}
ABresult
0X0
100
111
30 {
31 0 ▶ if($nation)
Branch: 1 branch
BranchTrueFalseTotal
if ($nation) { } 0 0 0
32 {
33 0 return $self->statistics->{$turn}->{$nation}->{$value};
34 }
35 else
36 {
37 0 return $self->statistics->{$turn}->{$value};
38 }
39 }
40 else
41 {
42 0 return undef;
43 }
44 }
45 sub set_statistics_value
46 {
47 0 ▶ my $self = shift;
Subroutine set_statistics_value: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my $nation = shift;
49 0 my $value_name = shift;
50 0 my $value = shift;
51 0 ▶ if($nation)
Branch: 1 branch
BranchTrueFalseTotal
if ($nation) { } 0 0 0
52 {
53 0 $self->statistics->{$nation->current_year}->{$nation->name}->{$value_name} = $value;
54 }
55 else
56 {
57 0 $self->statistics->{$self->current_year}->{$value_name} = $value;
58 }
59 }
60  
61 sub print_nation_statistics
62 {
63 0 ▶ my $self = shift;
Subroutine print_nation_statistics: not called CC=3 SCAR=24.8
Pod undocumented
64 0 my $nation = shift;
65 0 my $first_turn = shift;
66 0 my $last_turn = shift;
67 0 ▶ my $mode = shift || 'print';
Condition0%0/2
shift() || 'print'
l!l
00
MC/DC0%0/1
shift() || 'print'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
68 0 my $attributes_names = ["Size", "Prod.", "Wealth", "W/D", "Growth", "Disor.", "Army", "Prog.", "Pstg."];
69 0 my $attributes = ["production", "wealth", "w/d", "growth", "internal disorder", "army", "progress", "prestige"];
70 0 my %data = ();
71  
72 0 foreach my $t (from_to_turns($first_turn, $last_turn))
73 {
74 0 my @ndata = $self->get_nation_statistics_line($nation, $t, $attributes);
75 0 $data{$t} = \@ndata;
76 }
77 0 return BalanceOfPower::Printer::print($mode, $self, 'print_nation_statistics',
78 { nation => $nation,
79 attributes => $attributes_names,
80 statistics => \%data,
81 } );
82 }
83 sub print_nation_factor
84 {
85 0 ▶ my $self = shift;
Subroutine print_nation_factor: not called CC=3 SCAR=24.8
Pod undocumented
86 0 my $nation = shift;
87 0 my $factor = shift;
88 0 my $first_turn = shift;
89 0 my $last_turn = shift;
90 0 my $out = as_title($nation . " - " . $factor . " - column of values" . "\n===\n");
91 0 foreach my $t (from_to_turns($first_turn, $last_turn))
92 {
93 0 ▶ if(defined $self->get_statistics_value($t, $nation, $factor))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_statistics_value($t, $nation, $factor)) { } 0 0 0
94 {
95 0 $out .= $self->get_statistics_value($t, $nation, $factor) . "\n";
96 }
97 else
98 {
99 0 $out .= "*** UNAVAILABLE ***" . "\n";
100 }
101 }
102 0 return $out;
103 }
104  
105 sub plot_nation_factor
106 {
107 0 ▶ my $self = shift;
Subroutine plot_nation_factor: not called CC=8 SCAR=42.8
Pod undocumented
108 0 my $nation = shift;
109 0 my $factor = shift;
110 0 my $first_turn = shift;
111 0 my $last_turn = shift;
112
113 0 my $graph_height = 12;
114  
115 0 my @data = ();
116 0 my $valid = 0;
117 0 foreach my $t (from_to_turns($first_turn, $last_turn))
118 {
119 0 ▶ if(defined $self->get_statistics_value($t, $nation, $factor))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_statistics_value($t, $nation, $factor)) { } 0 0 0
120 {
121 0 push @data, $self->get_statistics_value($t, $nation, $factor);
122 0 $valid = 1;
123 }
124 else
125 {
126 0 push @data, undef;
127 }
128 }
129 0 ▶ return "" if(! $valid);
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 0 0 0
130 0 my $min = min @data;
131 0 my $max = max @data;
132 0 my $step = ($max - $min) / $graph_height;
133 0 my @lines = ();
134 0 my $out = as_title($nation . " - " . $factor . " - graph " . "\n===\n");
135 0 $out .= "Min. value: $min Max. value: $max\n\n";
136 0 ▶ for(my $i = 0; $i <= $graph_height; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $graph_height) 0 0 0
137 {
138 0 $lines[$i] = "";
139 0 my $level = $step * ( $graph_height - $i);
140 0 for(@data)
141 {
142 0 my $v = $_;
143 0 ▶ if(! $v)
Branch: 1 branch
BranchTrueFalseTotal
if (not $v) { } 0 0 0
144 {
145 0 $lines[$i] .= " ";
146 }
147 else
148 {
149 #if(($v - $min) < $step * ( $graph_height - ($i-1)) &&
150 0 ▶ if(($v - $min) >= $level)
Branch: 1 branch
BranchTrueFalseTotal
if ($v - $min >= $level) { } 0 0 0
151 {
152 0 $lines[$i] .= "o";
153 }
154 else
155 {
156 0 $lines[$i] .= " ";
157 }
158 }
159 }
160 0 my $level_label = int($level * 100) / 100 + $min;
161 0 $out .= $lines[$i] . "...$level_label" ."\n";
162 }
163 0 return $out . "\n";
164 }
165  
166  
167 sub print_nation_statistics_header
168 {
169 0 ▶ if(DEBT_ALLOWED)
Subroutine print_nation_statistics_header: not called CC=1 SCAR=6.9
Pod undocumented
170 {
171 return "Size\tProd.\tWealth\tW/D\tGrowth\tDebt\tDisor.\tArmy\tProg.\tPstg.";
172 }
173 else
174 {
175 0 return "Size\tProd.\tWealth\tW/D\tGrowth\tDisor.\tArmy\tProg.\tPstg.";
176 }
177 }
178 sub get_nation_statistics_line
179 {
180 0 ▶ my $self = shift;
Subroutine get_nation_statistics_line: not called CC=2 SCAR=17.9
Pod undocumented
181 0 my $nation = shift;
182 0 my $y = shift;
183 0 my $characteristics = shift;
184 0 my @out = ();
185 0 push @out, $self->get_nation($nation)->size;
186 0 for(@{$characteristics})
187 {
188 0 push @out, $self->get_statistics_value($y, $nation, $_);
189 }
190 0 return @out;
191 }
192 sub print_nation_statistics_line
193 {
194 0 ▶ my $self = shift;
Subroutine print_nation_statistics_line: not called CC=2 SCAR=17.9
Pod undocumented
195 0 my $nation = shift;
196 0 my $y = shift;
197 0 my $out = "";
198 0 $out .= $self->get_nation($nation)->size . "\t";
199 0 ▶ if(! defined $self->get_statistics_value($y, $nation, 'production'))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get_statistics_value($y, $nation, "production")) 0 0 0
200 {
201 0 $out .= "### no statistics available ###";
202 0 return $out;
203 }
204 0 $out .= $self->get_statistics_value($y, $nation, 'production') . "\t";
205 0 $out .= $self->get_statistics_value($y, $nation, 'wealth') . "\t";
206 0 $out .= $self->get_statistics_value($y, $nation, 'w/d') . "\t";
207 0 $out .= $self->get_statistics_value($y, $nation, 'growth') . "\t";
208 0 if(DEBT_ALLOWED)
209 {
210 $out .= $self->get_statistics_value($y, $nation, 'debt') . "\t";
211 }
212 0 $out .= $self->get_statistics_value($y, $nation, 'internal disorder') . "\t";
213 0 $out .= $self->get_statistics_value($y, $nation, 'army') . "\t";
214 0 $out .= $self->get_statistics_value($y, $nation, 'progress') . "\t";
215 0 $out .= $self->get_statistics_value($y, $nation, 'prestige') . "\t";
216 0 return $out;
217 }
218  
219  
220  
221 sub print_formatted_turn_events
222 {
223 0 ▶ my $self = shift;
Subroutine print_formatted_turn_events: not called CC=2 SCAR=17.9
Pod undocumented
224 0 my $y = shift;
225 0 my $title = shift;
226 0 ▶ my $mode = shift || 'print';
Condition0%0/2
shift() || 'print'
l!l
00
MC/DC0%0/1
shift() || 'print'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
227 0 my $out = "";
228 0 $out .= $self->print_turn_events($y, $title, 0, $mode);
229 0 return $out;
230 }
231  
232 sub print_nation_events
233 {
234 0 ▶ my $self = shift;
Subroutine print_nation_events: not called CC=2 SCAR=17.9
Pod undocumented
235 0 my $nation_name = shift;
236 0 my $y = shift;
237 0 my $title = shift;
238 0 ▶ my $mode = shift || 'print';
Condition0%0/2
shift() || 'print'
l!l
00
MC/DC0%0/1
shift() || 'print'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
239 0 my $nation = $self->get_nation($nation_name);
240 0 return $nation->print_turn_events($y, $title, 0, $mode);
241 }
242  
243 sub print_turn_statistics
244 {
245 0 ▶ my $self = shift;
Subroutine print_turn_statistics: not called CC=5 SCAR=34.0
Pod undocumented
246 0 my $y = shift;
247 0 my $order = shift;
248 0 ▶ my $mode = shift || 'print';
Condition0%0/2
shift() || 'print'
l!l
00
MC/DC0%0/1
shift() || 'print'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
249 0 my @nations = @{$self->nation_names};
250 0 my $attributes_names = ["Size", "Prod.", "Wealth", "W/D", "Disor.", "Army", "Prog.", "Pstg."];
251 0 my $attributes = ["production", "wealth", "w/d", "internal disorder", "army", "progress", "prestige"];
252 0 my %data = ();
253 0 my @names;
254 0 ▶ if($order)
Branch: 1 branch
BranchTrueFalseTotal
if ($order) { } 0 0 0
255 {
256 0 my @ordered = $self->order_statistics($y, lc $order);
257 0 for(@ordered)
258 {
259 0 push @names, $_->{nation};
260 }
261 }
262 else
263 {
264 0 @names = @nations;
265 }
266 0 for(@names)
267 {
268 0 my @ndata = $self->get_nation_statistics_line($_, $y, $attributes);
269 0 $data{$_} = \@ndata;
270 }
271 0 return BalanceOfPower::Printer::print($mode, $self, 'print_turn_statistics',
272 { year => $y,
273 order => $order,
274 attributes => $attributes_names,
275 statistics => \%data,
276 names => \@names, });
277 }
278  
279 sub print_overall_statistics
280 {
281 0 ▶ my $self =shift;
Subroutine print_overall_statistics: not called CC=3 SCAR=24.8
Pod undocumented
282 0 my $first_year = shift;
283 0 my $last_year = shift;
284 0 my @nations = @{$self->nation_names};
285 0 my $out = "Overall medium values\n";
286 0 $out .= "Year\tProd.\tWealth\tInt.Dis\n";
287 0 foreach my $y ($first_year..$last_year)
288 {
289 0 foreach my $t (get_year_turns($y))
290 {
291 0 my ($prod, $wealth, $disorder) = $self->medium_statistics($t, @nations);
292 0 $out .= "$y\t$prod\t$wealth\t$disorder\n";
293 }
294 }
295 0 return $out;
296 }
297 sub medium_statistics
298 {
299 0 ▶ my $self = shift;
Subroutine medium_statistics: not called CC=3 SCAR=24.8
Pod undocumented
300 0 my $year = shift;
301 0 my @nations = @{$self->nation_names};
302 0 my $total_production = 0;
303 0 my $total_wealth = 0;
304 0 my $total_disorder = 0;
305 0 foreach my $t (get_year_turns($year))
306 {
307 0 foreach my $n (@nations)
308 {
309 0 $total_production += $self->get_statistics_value($t, $n, 'production');
310 0 $total_wealth += $self->get_statistics_value($t, $n, 'wealth');
311 0 $total_disorder += $self->get_statistics_value($t, $n, 'internal disorder');
312 }
313 }
314 0 my $medium_production = int(($total_production / @nations)*100)/100;
315 0 my $medium_wealth = int(($total_wealth / @nations)*100)/100;
316 0 my $medium_disorder = int(($total_disorder / @nations)*100)/100;
317 0 return ($medium_production, $medium_wealth, $medium_disorder);
318 }
319  
320 sub order_statistics
321 {
322 0 ▶ my $self = shift;
Subroutine order_statistics: not called CC=3 SCAR=24.8
Pod undocumented
323 0 my $turn = shift;
324 0 my $value = shift;
325 0 my @nations = @{$self->nation_names};
326 0 my @ordered;
327 0 foreach my $n (@nations)
328 {
329 0 my $val = $self->get_statistics_value($turn, $n, $value);
330 0 ▶ if(! defined $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
331 {
332 0 return ();
333 }
334 0 push @ordered, { nation => $n, value => $val };
335 }
336 0 @ordered = sort { $b->{value} <=> $a->{value} } @ordered;
337 0 return @ordered;
338 }
339  
340 sub print_crises
341 {
342 0 ▶ my $self = shift;
Subroutine print_crises: not called CC=6 SCAR=37.4
Pod undocumented
343 0 my $year = shift;
344 0 my $out = "";
345 0 foreach my $t (get_year_turns($year))
346 {
347 0 my $header = 0;
348 0 foreach my $e (@{$self->events->{$t}})
349 {
350 0 ▶ if($e =~ /^CRISIS/)
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^CRISIS/) 0 0 0
351 {
352 0 ▶ if(! $header)
Branch: 1 branch
BranchTrueFalseTotal
unless ($header) 0 0 0
353 {
354 0 $header = 1;
355 0 $out .= "$t\n";
356 }
357 0 $out .= " " . $e . "\n";
358 }
359 }
360 0 ▶ if($header)
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 0 0 0
361 {
362 0 $out .= "\n";
363 }
364 }
365 0 return $out;
366 }
367 sub print_defcon_statistics
368 {
369 0 ▶ my $self = shift;
Subroutine print_defcon_statistics: not called CC=3 SCAR=24.8
Pod undocumented
370 0 my $first_year = shift;
371 0 my $last_year = shift;
372 0 my $out = "Year\tCrises\tWars\n";
373 0 foreach my $y ($first_year..$last_year)
374 {
375 0 foreach my $t (get_year_turns($y))
376 {
377 0 my $crises = $self->get_statistics_value($t, undef, 'crises');
378 0 my $wars = $self->get_statistics_value($t, undef, 'wars');
379 0 $out .= "$t\t$crises\t$wars\n";
380 }
381 }
382 0 return $out;
383 }
384  
385 sub dump_statistics
386 {
387 0 ▶ my $self = shift;
Subroutine dump_statistics: not called CC=2 SCAR=17.9
Pod undocumented
388 0 my $io = shift;
389 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
390 0 my $dump = Data::Dumper->new([$self->statistics]);
391 0 $dump->Indent(0);
392 0 print {$io} $indent . $dump->Dump . "\n";
393 }
394 sub load_statistics
395 {
396 0 ▶ my $self = shift;
Subroutine load_statistics: not called CC=1 SCAR=6.9
Pod undocumented
397 0 my $data = shift;
398 0 my $VAR1;
399 0 eval ( $data );
400 0 $self->statistics($VAR1);
401 }
402  
403  
404 11 1;
405  
406  
407  
408  
« lib/BalanceOfPower/Relations/RelPack.pm ↑ Summary blib/lib/BalanceOfPower/Commands/TargetNation.pm »