| 1 | package BalanceOfPower::Role::Analyst; | ||
| 2 | $BalanceOfPower::Role::Analyst::VERSION = '0.200000'; | ||
| 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 | |||
| 10 | requires 'diplomacy_exists'; | ||
| 11 | requires 'get_borders'; | ||
| 12 | requires 'supported'; | ||
| 13 | requires 'exists_military_support'; | ||
| 14 | requires 'near_nations'; | ||
| 15 | requires 'routes_for_node'; | ||
| 16 | requires 'get_allies'; | ||
| 17 | requires 'get_crises'; | ||
| 18 | requires 'get_wars'; | ||
| 19 | requires 'print_nation_situation'; | ||
| 20 | requires 'print_nation_statistics_header'; | ||
| 21 | requires 'print_nation_statistics_line'; | ||
| 22 | requires 'get_player'; | ||
| 23 | |||
| 24 | sub print_nation_actual_situation | ||
| 25 | { | ||
| 26 | my $self = shift; | ||
| 27 | my $nation = shift; | ||
| 28 | my $in_the_middle = shift; | ||
| 29 | my $turn = shift; | ||
| 30 | if($in_the_middle) | ||
| 31 | { | ||
| 32 | $turn = prev_turn($self->current_year); | ||
| 33 | } | ||
| 34 | else | ||
| 35 | { | ||
| 36 | $turn = $self->current_year; | ||
| 37 | } | ||
| 38 | my $nation_obj = $self->get_nation($nation); | ||
| 39 | my $out = as_title("$nation\n===\n"); | ||
| 40 | $out .= $nation_obj->print_attributes(); | ||
| 41 | $out .= "\n"; | ||
| 42 | $out .= $self->print_nation_situation($nation); | ||
| 43 | $out .= "\n"; | ||
| 44 | $out .= "\n"; | ||
| 45 | $out .= $self->print_nation_statistics_header() . "\n"; | ||
| 46 | $out .= $self->print_nation_statistics_line($nation, $turn) . "\n\n"; | ||
| 47 | $out .= as_title("TRADEROUTES\n---\n"); | ||
| 48 | foreach my $tr ($self->routes_for_node($nation)) | ||
| 49 | { | ||
| 50 | $out .= $tr->print($nation) . "\n"; | ||
| 51 | } | ||
| 52 | $out .= "\n"; | ||
| 53 | my $allies_support_title = sprintf "%-35s %-35s", "TREATIES [" . $nation_obj->treaty_limit . "]" , "SUPPORTS"; | ||
| 54 | $allies_support_title .="\n"; | ||
| 55 | $allies_support_title .= sprintf "%-35s %-35s", "---", "---"; | ||
| 56 | $allies_support_title .="\n"; | ||
| 57 | $out .= as_title($allies_support_title); | ||
| 58 | my @allies = $self->get_treaties_for_nation($nation); | ||
| 59 | my @supports = $self->supports($nation); | ||
| 60 | my @rebel_supports = $self->rebel_supports($nation); | ||
| 61 | for(my $i = 0; ;$i++) | ||
| 62 | { | ||
| 63 | last if(@allies == 0 && @supports == 0 && @rebel_supports == 0); | ||
| 64 | my $allies_text = ""; | ||
| 65 | if(@allies) | ||
| 66 | { | ||
| 67 | my $a = shift @allies; | ||
| 68 | $allies_text = $a->print; | ||
| 69 | } | ||
| 70 | my $support_text = ""; | ||
| 71 | if(@supports) | ||
| 72 | { | ||
| 73 | my $s = shift @supports; | ||
| 74 | $support_text = $s->print; | ||
| 75 | } | ||
| 76 | else | ||
| 77 | { | ||
| 78 | if(@rebel_supports) | ||
| 79 | { | ||
| 80 | my $rs = shift @rebel_supports; | ||
| 81 | $support_text = "REB: " . $rs->print; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | $out .= sprintf "%-35s %-35s", $allies_text, $support_text; | ||
| 85 | $out .="\n"; | ||
| 86 | } | ||
| 87 | $out .= "\n"; | ||
| 88 | my $crises_wars_title = sprintf "%-35s %-35s", "CRISES", "WARS"; | ||
| 89 | $crises_wars_title .="\n"; | ||
| 90 | $crises_wars_title .= sprintf "%-35s %-35s", "---", "---"; | ||
| 91 | $crises_wars_title .="\n"; | ||
| 92 | $out .= as_title($crises_wars_title); | ||
| 93 | my @crises = $self->get_crises($nation); | ||
| 94 | my @wars = $self->get_wars($nation); | ||
| 95 | for(my $i = 0; ;$i++) | ||
| 96 | { | ||
| 97 | last if(@crises == 0 && @wars == 0); | ||
| 98 | my $crisis_text = ""; | ||
| 99 | if(@crises) | ||
| 100 | { | ||
| 101 | my $c = shift @crises; | ||
| 102 | $crisis_text = $c->print_crisis(0); | ||
| 103 | } | ||
| 104 | my $war_text = ""; | ||
| 105 | if(@wars) | ||
| 106 | { | ||
| 107 | my $w = shift @wars; | ||
| 108 | $war_text = $w->print; | ||
| 109 | } | ||
| 110 | $out .= sprintf "%-35s %-35s", $crisis_text, $war_text; | ||
| 111 | $out .="\n"; | ||
| 112 | } | ||
| 113 | $out .= "\n"; | ||
| 114 | my $order = $self->get_statistics_value($turn, $nation, 'order') ? $self->get_statistics_value($turn, $nation, 'order') : 'NONE'; | ||
| 115 | $out .= "Latest given order: $order\n"; | ||
| 116 | return $out; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub print_borders_analysis | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | my $nation = shift; | ||
| 123 | my @borders = $self->near_nations($nation, 1); | ||
| 124 | |||
| 125 | my $out = ""; | ||
| 126 | foreach my $b (@borders) | ||
| 127 | { | ||
| 128 | $out .= as_title("# " . $b . " #") . "\n"; | ||
| 129 | my $rel = $self->diplomacy_exists($nation, $b); | ||
| 130 | $out .= " Relations: " . $rel->print_status() . " " . | ||
| 131 | $rel->print_crisis_bar() . "\n"; | ||
| 132 | my $supps = $self->supported($b); | ||
| 133 | if($supps) | ||
| 134 | { | ||
| 135 | $out .= as_subtitle(" Military support in the country:\n"); | ||
| 136 | my $supporter = $supps->start($b); | ||
| 137 | my $sup_rel = $self->diplomacy_exists($nation, $supporter); | ||
| 138 | if($sup_rel) | ||
| 139 | { | ||
| 140 | $out .= " $supporter (" . $sup_rel->print_status(); | ||
| 141 | if($sup_rel->is_crisis()) | ||
| 142 | { | ||
| 143 | $out .= " " . $sup_rel->print_crisis_bar; | ||
| 144 | } | ||
| 145 | sub war_current_year | ||
| 146 | { | ||
| 147 | my $self = shift; | ||
| 148 | for($self->wars->all) | ||
| 149 | { | ||
| 150 | $_->current_year($self->current_year); | ||
| 151 | } | ||
| 152 | } $out .= ")"; | ||
| 153 | } | ||
| 154 | $out .= "\n"; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | return $out; | ||
| 158 | } | ||
| 159 | sub print_near_analysis | ||
| 160 | { | ||
| 161 | my $self = shift; | ||
| 162 | my $nation = shift; | ||
| 163 | my @near = $self->near_nations($nation, 0); | ||
| 164 | my $out = ""; | ||
| 165 | foreach my $b (@near) | ||
| 166 | { | ||
| 167 | $out .= as_title($b) . " " . $self->diplomacy_exists($nation, $b)->print_status(); | ||
| 168 | if(! $self->border_exists($nation, $b)) | ||
| 169 | { | ||
| 170 | if($self->exists_military_support($nation, $b)) | ||
| 171 | { | ||
| 172 | $out .= " (supported)\n"; | ||
| 173 | } | ||
| 174 | else | ||
| 175 | { | ||
| 176 | $out .= "\n"; | ||
| 177 | my @foreign_borders = $self->get_borders($b); | ||
| 178 | foreach my $fb (@foreign_borders) | ||
| 179 | { | ||
| 180 | my $other_n = $fb->destination($b); | ||
| 181 | my $sups = $self->supported($other_n); | ||
| 182 | for($sups) | ||
| 183 | { | ||
| 184 | if($sups->start($other_n) eq $nation) | ||
| 185 | { | ||
| 186 | $out .= " Military support from: $other_n\n"; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | $out .= "\n"; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | return $out; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub print_hotspots | ||
| 201 | { | ||
| 202 | my $self = shift; | ||
| 203 | my $out = ""; | ||
| 204 | my @crises = $self->get_all_crises(); | ||
| 205 | $out .= as_title("CRISES") . "\n"; | ||
| 206 | for(@crises) | ||
| 207 | { | ||
| 208 | my $c = $_; | ||
| 209 | if(! $self->war_exists($c->node1, $c->node2)) | ||
| 210 | { | ||
| 211 | $out .= as_subtitle($c->print_crisis()) . "\n"; | ||
| 212 | #$out .= " " . $self->diplomacy_exists($self->player_nation, $c->node1)->print() . "\n" if($self->player_nation ne $c->node1); | ||
| 213 | #$out .= " " . $self->diplomacy_exists($self->player_nation, $c->node2)->print(). "\n" if($self->player_nation ne $c->node2); | ||
| 214 | $out .= "\n"; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | $out .= "\n"; | ||
| 218 | $out .= as_title("WARS") . "\n"; | ||
| 219 | my @wars = $self->wars->all(); | ||
| 220 | for(@wars) | ||
| 221 | { | ||
| 222 | my $w = $_; | ||
| 223 | $out .= $w->print() . "\n"; | ||
| 224 | #$out .= " " . $self->diplomacy_exists($self->player_nation, $w->node1)->print() . "\n" if($self->player_nation ne $w->node1); | ||
| 225 | #$out .= " " . $self->diplomacy_exists($self->player_nation, $w->node2)->print(). "\n" if($self->player_nation ne $w->node2); | ||
| 226 | $out .= "\n"; | ||
| 227 | } | ||
| 228 | $out .= "\n"; | ||
| 229 | $out .= as_title("CIVIL WARS") . "\n"; | ||
| 230 | foreach my $n (@{$self->nation_names}) | ||
| 231 | { | ||
| 232 | if($self->at_civil_war($n)) | ||
| 233 | { | ||
| 234 | $out .= "$n is fighting civil war\n"; | ||
| 235 | #$out .= " " . $self->diplomacy_exists($self->player_nation, $n)->print() . "\n" if($self->player_nation ne $n); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | return $out; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub print_civil_war_report | ||
| 242 | { | ||
| 243 | my $self = shift; | ||
| 244 | my $nation = shift; | ||
| 245 | if (! $self->at_civil_war($nation)) | ||
| 246 | { | ||
| 247 | return "$nation is not fightinh civil war"; | ||
| 248 | } | ||
| 249 | my $out = ""; | ||
| 250 | my $nation_obj = $self->get_nation($nation); | ||
| 251 | $out .= "Rebel provinces: " . $nation_obj->rebel_provinces . "/" . PRODUCTION_UNITS->[$nation_obj->size] . "\n"; | ||
| 252 | $out .= "Army: " . $nation_obj->army . "\n"; | ||
| 253 | my $sup = $self->supported($nation); | ||
| 254 | my $rebsup = $self->rebel_supported($nation); | ||
| 255 | $out .= "Support: " . $sup->print . "\n" if($sup); | ||
| 256 | $out .= "Rebel support: " . $rebsup->print . "\n" if ($rebsup); | ||
| 257 | return $out; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub print_war_history | ||
| 261 | { | ||
| 262 | my $self = shift; | ||
| 263 | my $out .= as_title("WAR HISTORY\n\n"); | ||
| 264 | my %wars; | ||
| 265 | my @war_names; | ||
| 266 | foreach my $w (@{$self->memorial}) | ||
| 267 | { | ||
| 268 | if(exists $wars{$w->war_id}) | ||
| 269 | { | ||
| 270 | $wars{$w->war_id} .= $w->print_history . "\n"; | ||
| 271 | } | ||
| 272 | else | ||
| 273 | { | ||
| 274 | $wars{$w->war_id} = $w->print_history . "\n"; | ||
| 275 | push @war_names, { name => $w->war_id, | ||
| 276 | start => $w->start_date }; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | sub comp | ||
| 280 | { | ||
| 281 | compare_turns($a->{start}, $b->{start}); | ||
| 282 | } | ||
| 283 | for(sort comp @war_names) | ||
| 284 | { | ||
| 285 | $out .= "### WAR " . $_->{name} . "\n"; | ||
| 286 | $out .= $wars{$_->{name}}; | ||
| 287 | } | ||
| 288 | return $out; | ||
| 289 | } | ||
| 290 | sub print_treaties_table | ||
| 291 | { | ||
| 292 | my $self = shift; | ||
| 293 | my $out = sprintf "%-20s %-6s %-5s %-5s %-5s", "Nation", "LIMIT", "ALL", "NAG", "COM"; | ||
| 294 | $out .= "\n"; | ||
| 295 | my @nations = @{$self->nation_names}; | ||
| 296 | for(@nations) | ||
| 297 | { | ||
| 298 | my $n = $_; | ||
| 299 | my $limit = $self->get_nation($n)->treaty_limit; | ||
| 300 | my $alls = $self->get_treaties_for_nation_by_type($n, 'alliance'); | ||
| 301 | my $coms = $self->get_treaties_for_nation_by_type($n, 'commercial'); | ||
| 302 | my $nags = $self->get_treaties_for_nation_by_type($n, 'no aggression'); | ||
| 303 | $out .= sprintf "%-20s %-6s %-5s %-5s %-5s", $n, $limit, $alls, $nags, $coms; | ||
| 304 | $out .= "\n"; | ||
| 305 | } | ||
| 306 | return $out; | ||
| 307 | } | ||
| 308 | sub print_stocks | ||
| 309 | { | ||
| 310 | my $self = shift; | ||
| 311 | my $player = shift; | ||
| 312 | my $player_obj = $self->get_player($player); | ||
| 313 | my $stock_value = 0; | ||
| 314 | my $out = ""; | ||
| 315 | $out .= as_title(sprintf "%-15s %-10s %-10s %-10s %-10s", "NATION", "Q", "VALUE", "INFLUENCE", "WAR BONDS"); | ||
| 316 | $out .= "\n"; | ||
| 317 | foreach my $nation(keys %{$player_obj->wallet}) | ||
| 318 | { | ||
| 319 | if( $player_obj->stocks($nation) > 0 || $player_obj->influence($nation) > 0) | ||
| 320 | { | ||
| 321 | $out .= sprintf "%-15s %-10s %-10s %-10s %-10s", $nation, $player_obj->wallet->{$nation}->{stocks}, $self->get_statistics_value(prev_turn($self->current_year), $nation, "w/d"), $player_obj->wallet->{$nation}->{influence}, $player_obj->wallet->{$nation}->{'war bonds'} ; | ||
| 322 | $out .= "\n"; | ||
| 323 | $stock_value += $player_obj->wallet->{$nation}->{stocks} * $self->get_statistics_value(prev_turn($self->current_year), $nation, "w/d"); | ||
| 324 | } | ||
| 325 | } | ||
| 326 | $out .= "\n"; | ||
| 327 | $out .= "Stock value: " . $stock_value . "\n"; | ||
| 328 | $out .= "Money: " . $player_obj->money . "\n"; | ||
| 329 | my $total_value = $stock_value + $player_obj->money; | ||
| 330 | $out .= "Total value: " . $total_value . "\n"; | ||
| 331 | return $out; | ||
| 332 | } | ||
| 333 | sub print_market | ||
| 334 | { | ||
| 335 | my $self = shift; | ||
| 336 | my $out = ""; | ||
| 337 | $out .= as_title(sprintf "%-20s %-10s %-10s %-10s", "NATION", "STOCK", "VALUE", "STATUS"); | ||
| 338 | $out .= "\n"; | ||
| 339 | my @ordered = $self->order_statistics(prev_turn($self->current_year), 'w/d'); | ||
| 340 | foreach my $stats (@ordered) | ||
| 341 | { | ||
| 342 | my $nation = $self->get_nation($stats->{nation}); | ||
| 343 | my $status = ""; | ||
| 344 | if($self->at_war($nation->name)) | ||
| 345 | { | ||
| 346 | $status = "WAR"; | ||
| 347 | } | ||
| 348 | elsif($self->at_civil_war($nation->name)) | ||
| 349 | { | ||
| 350 | $status = "CIVILW"; | ||
| 351 | } | ||
| 352 | $out .= sprintf "%-20s %-10s %-10s %-10s", $nation->name, $nation->available_stocks, $stats->{value}, $status; | ||
| 353 | $out .= "\n"; | ||
| 354 | } | ||
| 355 | return $out; | ||
| 356 | } | ||
| 357 | 1; |