| 1 | package BalanceOfPower::Role::Analyst; | ||
| 2 | $BalanceOfPower::Role::Analyst::VERSION = '0.300010'; | ||
| 3 | use strict; | ||
| 4 | use v5.10; | ||
| 5 | use Moo::Role; | ||
| 6 | use Term::ANSIColor; | ||
| 7 | use BalanceOfPower::Constants ':all'; | ||
| 8 | use BalanceOfPower::Utils qw( prev_turn as_title as_title as_subtitle compare_turns); | ||
| 9 | use BalanceOfPower::Printer; | ||
| 10 | |||
| 11 | use Data::Dumper; | ||
| 12 | |||
| 13 | requires 'diplomacy_exists'; | ||
| 14 | requires 'get_borders'; | ||
| 15 | requires 'supported'; | ||
| 16 | requires 'exists_military_support'; | ||
| 17 | requires 'near_nations'; | ||
| 18 | requires 'routes_for_node'; | ||
| 19 | requires 'get_allies'; | ||
| 20 | requires 'get_crises'; | ||
| 21 | requires 'get_wars'; | ||
| 22 | requires 'print_nation_situation'; | ||
| 23 | requires 'print_nation_statistics_header'; | ||
| 24 | requires 'print_nation_statistics_line'; | ||
| 25 | requires 'get_player'; | ||
| 26 | requires 'print_all_crises'; | ||
| 27 | requires 'print_wars'; | ||
| 28 | |||
| 29 | sub print_nation_actual_situation | ||
| 30 | { | ||
| 31 | my $self = shift; | ||
| 32 | my $nation = shift; | ||
| 33 | my $in_the_middle = shift; | ||
| 34 | my $mode = shift || 'print'; | ||
| 35 | my $attributes_names = ["Size", "Prod.", "Wealth", "W/D", "Growth", "Disor.", "Army", "Prog.", "Pstg."]; | ||
| 36 | my $attributes = ["production", "wealth", "w/d", "growth", "internal disorder", "army", "progress", "prestige"]; | ||
| 37 | |||
| 38 | my $turn; | ||
| 39 | if($in_the_middle) | ||
| 40 | { | ||
| 41 | $turn = prev_turn($self->current_year); | ||
| 42 | } | ||
| 43 | else | ||
| 44 | { | ||
| 45 | $turn = $self->current_year; | ||
| 46 | } | ||
| 47 | my $nation_obj = $self->get_nation($nation); | ||
| 48 | my $under_influence = $self->is_under_influence($nation); | ||
| 49 | my @influence = $self->has_influence($nation); | ||
| 50 | my @ndata = $self->get_nation_statistics_line($nation, $turn, $attributes); | ||
| 51 | my @routes = $self->routes_for_node($nation); | ||
| 52 | my @treaties = $self->get_treaties_for_nation($nation); | ||
| 53 | my @supports = $self->supports($nation); | ||
| 54 | my @rebel_supports = $self->rebel_supports($nation); | ||
| 55 | my $first_row_height; | ||
| 56 | if(@treaties > @supports + @rebel_supports) | ||
| 57 | { | ||
| 58 | $first_row_height = @treaties; | ||
| 59 | } | ||
| 60 | else | ||
| 61 | { | ||
| 62 | $first_row_height = @supports + @rebel_supports; | ||
| 63 | } | ||
| 64 | my @crises = $self->get_crises($nation); | ||
| 65 | my @wars = $self->get_wars($nation); | ||
| 66 | my $second_row_height; | ||
| 67 | if(@crises > @wars) | ||
| 68 | { | ||
| 69 | $second_row_height = @crises; | ||
| 70 | } | ||
| 71 | else | ||
| 72 | { | ||
| 73 | $second_row_height = @wars; | ||
| 74 | } | ||
| 75 | my $latest_order = $self->get_statistics_value($turn, $nation, 'order') ? $self->get_statistics_value($turn, $nation, 'order') : 'NONE'; | ||
| 76 | return BalanceOfPower::Printer::print($mode, $self, 'print_actual_nation_situation', | ||
| 77 | { nation => $nation_obj, | ||
| 78 | under_influence => $under_influence, | ||
| 79 | influence => \@influence, | ||
| 80 | attributes => $attributes_names, | ||
| 81 | nationstats => \@ndata, | ||
| 82 | traderoutes => \@routes, | ||
| 83 | treaties => \@treaties, | ||
| 84 | supports => \@supports, | ||
| 85 | rebel_supports => \@rebel_supports, | ||
| 86 | first_row_height => $first_row_height, | ||
| 87 | crises => \@crises, | ||
| 88 | wars => \@wars, | ||
| 89 | second_row_height => $second_row_height, | ||
| 90 | latest_order => $latest_order, | ||
| 91 | } ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub print_borders_analysis | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | my $nation = shift; | ||
| 98 | my $mode = shift || 'print'; | ||
| 99 | my @borders = $self->near_nations($nation, 1); | ||
| 100 | my %data; | ||
| 101 | |||
| 102 | foreach my $b (@borders) | ||
| 103 | { | ||
| 104 | my $rel = $self->diplomacy_exists($nation, $b); | ||
| 105 | $data{$b}->{'relation'} = $rel; | ||
| 106 | |||
| 107 | my $supps = $self->supported($b); | ||
| 108 | if($supps) | ||
| 109 | { | ||
| 110 | my $supporter = $supps->start($b); | ||
| 111 | $data{$b}->{'support'}->{nation} = $supporter; | ||
| 112 | my $sup_rel = $self->diplomacy_exists($nation, $supporter); | ||
| 113 | $data{$b}->{'support'}->{relation} = $sup_rel; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | return BalanceOfPower::Printer::print($mode, $self, 'print_borders_analysis', | ||
| 117 | { nation => $nation, | ||
| 118 | borders => \%data } ); | ||
| 119 | |||
| 120 | } | ||
| 121 | sub print_near_analysis | ||
| 122 | { | ||
| 123 | my $self = shift; | ||
| 124 | my $nation = shift; | ||
| 125 | my $mode = shift || 'print'; | ||
| 126 | my @data = (); | ||
| 127 | my @near = $self->near_nations($nation, 0); | ||
| 128 | foreach my $b (@near) | ||
| 129 | { | ||
| 130 | my $rel = $self->diplomacy_exists($nation, $b); | ||
| 131 | if(! $self->border_exists($nation, $b)) | ||
| 132 | { | ||
| 133 | if($self->exists_military_support($nation, $b)) | ||
| 134 | { | ||
| 135 | push @data, { nation => $b, | ||
| 136 | relation => $rel, | ||
| 137 | how => 'supported' }; | ||
| 138 | } | ||
| 139 | else | ||
| 140 | { | ||
| 141 | my @foreign_borders = $self->get_borders($b); | ||
| 142 | foreach my $fb (@foreign_borders) | ||
| 143 | { | ||
| 144 | my $other_n = $fb->destination($b); | ||
| 145 | my $sups = $self->supported($other_n); | ||
| 146 | if($sups) | ||
| 147 | { | ||
| 148 | if($sups->start($other_n) eq $nation) | ||
| 149 | { | ||
| 150 | push @data, { nation => $b, | ||
| 151 | relation => $rel, | ||
| 152 | how => $other_n }; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { | ||
| 160 | push @data, { nation => $b, | ||
| 161 | relation => $rel, | ||
| 162 | how => "border" }; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | BalanceOfPower::Printer::print($mode, $self, 'print_near_analysis', | ||
| 166 | { nation => $nation, | ||
| 167 | near => \@data } ); | ||
| 168 | |||
| 169 | } | ||
| 170 | sub print_hotspots | ||
| 171 | { | ||
| 172 | my $self = shift; | ||
| 173 | my $mode = shift || 'print'; | ||
| 174 | my $out = ""; | ||
| 175 | $out .= $self->print_all_crises(undef, 1, $mode); | ||
| 176 | $out .= $self->print_wars(undef, $mode); | ||
| 177 | return $out; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub print_civil_war_report | ||
| 181 | { | ||
| 182 | my $self = shift; | ||
| 183 | my $nation = shift; | ||
| 184 | if (! $self->at_civil_war($nation)) | ||
| 185 | { | ||
| 186 | return "$nation is not fightinh civil war"; | ||
| 187 | } | ||
| 188 | my $out = ""; | ||
| 189 | my $nation_obj = $self->get_nation($nation); | ||
| 190 | $out .= "Rebel provinces: " . $nation_obj->rebel_provinces . "/" . PRODUCTION_UNITS->[$nation_obj->size] . "\n"; | ||
| 191 | $out .= "Army: " . $nation_obj->army . "\n"; | ||
| 192 | my $sup = $self->supported($nation); | ||
| 193 | my $rebsup = $self->rebel_supported($nation); | ||
| 194 | $out .= "Support: " . $sup->print . "\n" if($sup); | ||
| 195 | $out .= "Rebel support: " . $rebsup->print . "\n" if ($rebsup); | ||
| 196 | return $out; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub print_war_history | ||
| 200 | { | ||
| 201 | my $self = shift; | ||
| 202 | my $mode = shift || 'print'; | ||
| 203 | my %wars; | ||
| 204 | my @war_names; | ||
| 205 | foreach my $w (@{$self->memorial}) | ||
| 206 | { | ||
| 207 | if(exists $wars{$w->war_id}) | ||
| 208 | { | ||
| 209 | push @{$wars{$w->war_id}}, $w; | ||
| 210 | } | ||
| 211 | else | ||
| 212 | { | ||
| 213 | $wars{$w->war_id} = [ $w ]; | ||
| 214 | push @war_names, { name => $w->war_id, | ||
| 215 | start => $w->start_date }; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | sub comp | ||
| 219 | { | ||
| 220 | compare_turns($a->{start}, $b->{start}); | ||
| 221 | } | ||
| 222 | @war_names = sort comp @war_names; | ||
| 223 | return BalanceOfPower::Printer::print($mode, $self, 'print_war_history', | ||
| 224 | { wars => \%wars, | ||
| 225 | war_names => \@war_names } ); | ||
| 226 | } | ||
| 227 | sub print_treaties_table | ||
| 228 | { | ||
| 229 | my $self = shift; | ||
| 230 | my $out = sprintf "%-20s %-6s %-5s %-5s %-5s", "Nation", "LIMIT", "ALL", "NAG", "COM"; | ||
| 231 | $out .= "\n"; | ||
| 232 | my @nations = @{$self->nation_names}; | ||
| 233 | for(@nations) | ||
| 234 | { | ||
| 235 | my $n = $_; | ||
| 236 | my $limit = $self->get_nation($n)->treaty_limit; | ||
| 237 | my $alls = $self->get_treaties_for_nation_by_type($n, 'alliance'); | ||
| 238 | my $coms = $self->get_treaties_for_nation_by_type($n, 'commercial'); | ||
| 239 | my $nags = $self->get_treaties_for_nation_by_type($n, 'no aggression'); | ||
| 240 | $out .= sprintf "%-20s %-6s %-5s %-5s %-5s", $n, $limit, $alls, $nags, $coms; | ||
| 241 | $out .= "\n"; | ||
| 242 | } | ||
| 243 | return $out; | ||
| 244 | } | ||
| 245 | sub player_stocks_status | ||
| 246 | { | ||
| 247 | my $self = shift; | ||
| 248 | my $player = shift; | ||
| 249 | my $player_obj = $self->get_player($player); | ||
| 250 | my $stock_value = 0; | ||
| 251 | my %market_data = (); | ||
| 252 | foreach my $nation(keys %{$player_obj->wallet}) | ||
| 253 | { | ||
| 254 | if( $player_obj->stocks($nation) > 0 || $player_obj->influence($nation) > 0) | ||
| 255 | { | ||
| 256 | $market_data{$nation} = { 'stocks' => $player_obj->wallet->{$nation}->{stocks}, | ||
| 257 | 'value' => $self->get_statistics_value(prev_turn($self->current_year), $nation, "w/d"), | ||
| 258 | 'influence' => $player_obj->wallet->{$nation}->{influence}, | ||
| 259 | 'war_bonds' => $player_obj->wallet->{$nation}->{'war_bonds'}, | ||
| 260 | }; | ||
| 261 | $stock_value += $player_obj->wallet->{$nation}->{stocks} * $self->get_statistics_value(prev_turn($self->current_year), $nation, "w/d"); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | my $total_value = $stock_value + $player_obj->money; | ||
| 265 | return { market_data => \%market_data, | ||
| 266 | stock_value => $stock_value, | ||
| 267 | money => $player_obj->money, | ||
| 268 | total_value => $total_value }; | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | sub print_stocks | ||
| 273 | { | ||
| 274 | my $self = shift; | ||
| 275 | my $player = shift; | ||
| 276 | my $mode = shift || 'print'; | ||
| 277 | return BalanceOfPower::Printer::print($mode, $self, 'print_stocks', $self->player_stocks_status($player)); | ||
| 278 | } | ||
| 279 | sub print_all_stocks | ||
| 280 | { | ||
| 281 | my $self = shift; | ||
| 282 | my $mode = shift || 'print'; | ||
| 283 | my %data = (); | ||
| 284 | my @names; | ||
| 285 | for(@{$self->players}) | ||
| 286 | { | ||
| 287 | my $p = $_; | ||
| 288 | push @names, $p->name; | ||
| 289 | $data{$p->name} = $self->player_stocks_status($p->name); | ||
| 290 | } | ||
| 291 | @names = sort { $data{$b}->{total_value} <=> $data{$a}->{total_value} } @names; | ||
| 292 | |||
| 293 | return BalanceOfPower::Printer::print($mode, $self, 'print_ranking', | ||
| 294 | { players_data => \%data, | ||
| 295 | players => \@names }); | ||
| 296 | } | ||
| 297 | sub print_market | ||
| 298 | { | ||
| 299 | my $self = shift; | ||
| 300 | my $mode = shift || 'print'; | ||
| 301 | my @ordered = $self->order_statistics(prev_turn($self->current_year), 'w/d'); | ||
| 302 | my %data = (); | ||
| 303 | my @nations = (); | ||
| 304 | foreach my $stats (@ordered) | ||
| 305 | { | ||
| 306 | my $nation = $self->get_nation($stats->{nation}); | ||
| 307 | push @nations, $stats->{nation}; | ||
| 308 | my $status = ""; | ||
| 309 | if($self->at_war($nation->name)) | ||
| 310 | { | ||
| 311 | $status = "WAR"; | ||
| 312 | } | ||
| 313 | elsif($self->at_civil_war($nation->name)) | ||
| 314 | { | ||
| 315 | $status = "CIVILW"; | ||
| 316 | } | ||
| 317 | $data{$nation->name} = { stocks => $nation->available_stocks, | ||
| 318 | wd => $stats->{value}, | ||
| 319 | status => $status }; | ||
| 320 | } | ||
| 321 | return BalanceOfPower::Printer::print($mode, $self, 'print_market', | ||
| 322 | { market_data => \%data, | ||
| 323 | nations => \@nations } ); | ||
| 324 | } | ||
| 325 | 1; |