lib/BalanceOfPower/Role/Historian.pm

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