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