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