| 1 | package BalanceOfPower::Commands; | ||
| 2 | $BalanceOfPower::Commands::VERSION = '0.300010'; | ||
| 3 | use v5.10; | ||
| 4 | use Moo; | ||
| 5 | use IO::Prompter; | ||
| 6 | use Term::ANSIColor; | ||
| 7 | use BalanceOfPower::Player; | ||
| 8 | use BalanceOfPower::Executive; | ||
| 9 | use BalanceOfPower::Constants ":all"; | ||
| 10 | use BalanceOfPower::Utils qw(next_turn prev_turn get_year_turns compare_turns evidence_text); | ||
| 11 | |||
| 12 | with 'BalanceOfPower::Role::Logger'; | ||
| 13 | |||
| 14 | has world => ( | ||
| 15 | is => 'ro' | ||
| 16 | ); | ||
| 17 | has query => ( | ||
| 18 | is => 'rw', | ||
| 19 | default => "" | ||
| 20 | ); | ||
| 21 | has keep_query => ( | ||
| 22 | is => 'rw', | ||
| 23 | default => 0 | ||
| 24 | ); | ||
| 25 | has nation => ( | ||
| 26 | is => 'rw', | ||
| 27 | default => "" | ||
| 28 | ); | ||
| 29 | has active => ( | ||
| 30 | is => 'rw', | ||
| 31 | default => 1 | ||
| 32 | ); | ||
| 33 | |||
| 34 | has active_player => ( | ||
| 35 | is => 'rw', | ||
| 36 | default => "" | ||
| 37 | ); | ||
| 38 | |||
| 39 | has executive => ( | ||
| 40 | is => 'rw', | ||
| 41 | ); | ||
| 42 | |||
| 43 | has latest_result => ( | ||
| 44 | is => 'rw', | ||
| 45 | ); | ||
| 46 | |||
| 47 | sub welcome | ||
| 48 | { | ||
| 49 | my $self = shift; | ||
| 50 | my $welcome_message = <<'WELCOME'; | ||
| 51 | Welcome to Balance of Power, simulation of a real dangerous world! | ||
| 52 | |||
| 53 | Hide in the darkness to gain power and richness while the world burn | ||
| 54 | WELCOME | ||
| 55 | say $welcome_message; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub set_auto_years | ||
| 59 | { | ||
| 60 | my $self = shift; | ||
| 61 | my $auto_years = prompt "Tell a number of years to generate before game start: ", -i; | ||
| 62 | return $auto_years; | ||
| 63 | } | ||
| 64 | sub input_player | ||
| 65 | { | ||
| 66 | my $self = shift; | ||
| 67 | my $player = prompt "Say your name, player: "; | ||
| 68 | if($player) | ||
| 69 | { | ||
| 70 | $self->set_player($player); | ||
| 71 | } | ||
| 72 | else | ||
| 73 | { | ||
| 74 | say "Bad entry"; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub set_player | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | my $player = shift; | ||
| 82 | $self->world->create_player($player); | ||
| 83 | $self->active_player($player); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub get_active_player | ||
| 87 | { | ||
| 88 | my $self = shift; | ||
| 89 | return $self->world->get_player($self->active_player); | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | sub welcome_player | ||
| 94 | { | ||
| 95 | my $self = shift; | ||
| 96 | #TODO: wallet situation will be printed | ||
| 97 | } | ||
| 98 | sub get_prompt_text | ||
| 99 | { | ||
| 100 | my $self = shift; | ||
| 101 | my $player = $self->world->get_player($self->active_player); | ||
| 102 | my $prompt_text = ""; | ||
| 103 | my $controlled = undef; | ||
| 104 | my $ctrl_n = undef; | ||
| 105 | my $influence = undef; | ||
| 106 | if($self->executive) | ||
| 107 | { | ||
| 108 | $controlled = $self->executive->actor; | ||
| 109 | $ctrl_n = $self->world->get_nation($controlled); | ||
| 110 | $influence = $player->influence($controlled); | ||
| 111 | } | ||
| 112 | |||
| 113 | $prompt_text = "[" . $player->name . ", Money: " . $player->money; | ||
| 114 | if($controlled) | ||
| 115 | { | ||
| 116 | $prompt_text .= ", Influence: $influence"; | ||
| 117 | } | ||
| 118 | $prompt_text .= ". Turn is " . $self->world->current_year . "]\n"; | ||
| 119 | if($controlled) | ||
| 120 | { | ||
| 121 | $prompt_text .= "Controlling $controlled (" . "Int:" . $ctrl_n->production_for_domestic . " Exp:" . $ctrl_n->production_for_export . " Prtg:" . $ctrl_n->prestige . " Army:" . $ctrl_n->army . ")\n"; | ||
| 122 | $prompt_text .= "Control orders: " . $player->get_control_order($controlled) . "\n" if $player->get_control_order($controlled); | ||
| 123 | } | ||
| 124 | $prompt_text .= $self->nation ? "(" . $self->nation . " [" . $player->influence($self->nation) . "]) ?" : "?"; | ||
| 125 | return $prompt_text; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub get_query | ||
| 129 | { | ||
| 130 | my $self = shift; | ||
| 131 | if($self->query) | ||
| 132 | { | ||
| 133 | $self->log("[Not interactive query] " . $self->query); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | print color("cyan"); | ||
| 137 | my $input_query = prompt $self->get_prompt_text(); | ||
| 138 | $input_query .= ""; | ||
| 139 | print color("reset"); | ||
| 140 | while ($input_query =~ m/\x08/g) { | ||
| 141 | substr($input_query, pos($input_query)-2, 2, ''); | ||
| 142 | } | ||
| 143 | print "\n"; | ||
| 144 | $self->query($input_query); | ||
| 145 | $self->log("[Interactive query] " . $self->query); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub clear_query | ||
| 149 | { | ||
| 150 | my $self = shift; | ||
| 151 | if($self->keep_query) | ||
| 152 | { | ||
| 153 | $self->keep_query(0); | ||
| 154 | } | ||
| 155 | else | ||
| 156 | { | ||
| 157 | $self->query(undef); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub verify_nation | ||
| 162 | { | ||
| 163 | my $self = shift; | ||
| 164 | my $query = shift; | ||
| 165 | return 0 if (! $query); | ||
| 166 | my @good_nation = grep { $query =~ /^$_$/ } @{$self->world->nation_names}; | ||
| 167 | return @good_nation > 0; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub set_executive | ||
| 171 | { | ||
| 172 | my $self = shift; | ||
| 173 | my $controlled_nation = shift; | ||
| 174 | |||
| 175 | my $exec = BalanceOfPower::Executive->new; | ||
| 176 | $exec->init($self->world); | ||
| 177 | $exec->actor($controlled_nation); | ||
| 178 | $self->executive($exec); | ||
| 179 | return $exec; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub turn_command | ||
| 183 | { | ||
| 184 | my $self = shift; | ||
| 185 | my $query = $self->query; | ||
| 186 | if($query eq "turn") | ||
| 187 | { | ||
| 188 | #$self->nation(undef); | ||
| 189 | $self->query(undef); | ||
| 190 | return { status => 1 }; | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | return { status => 0 }; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | sub report_commands | ||
| 199 | { | ||
| 200 | my $self = shift; | ||
| 201 | my $query = $self->query; | ||
| 202 | |||
| 203 | my $commands = <<'COMMANDS'; | ||
| 204 | Say the name of a nation to select it and obtain its status. | ||
| 205 | Say <nations> for the full list of nations. | ||
| 206 | Say <clear> to un-select nation. | ||
| 207 | |||
| 208 | With a nation selected you can use: | ||
| 209 | <borders> | ||
| 210 | <near> | ||
| 211 | <relations> | ||
| 212 | <events> | ||
| 213 | <status> | ||
| 214 | <history> | ||
| 215 | <plot %attribute%> | ||
| 216 | [year/turn] | ||
| 217 | You can also say one of those commands as: [nation name] [command] | ||
| 218 | |||
| 219 | [year/turn] with no nation selected gives all the events of the year/turns | ||
| 220 | |||
| 221 | say <years> for available range of years | ||
| 222 | |||
| 223 | say <wars> for a list of wars, <crises> for all the ongoing crises | ||
| 224 | |||
| 225 | say <war history> for a list of finished wars | ||
| 226 | |||
| 227 | say <hotspots> gives you wars and crises with also your diplomatic relationship with countries involved | ||
| 228 | |||
| 229 | say <supports> for military supports | ||
| 230 | |||
| 231 | say <rebel supports> for rebel military supports | ||
| 232 | |||
| 233 | say <influences> for influences | ||
| 234 | |||
| 235 | say <distance NATION1-NATION2> for distance between nations | ||
| 236 | |||
| 237 | say <turn> to elaborate events for a new turn | ||
| 238 | |||
| 239 | <orders> display a list of command you can give to your nation for the next turn | ||
| 240 | |||
| 241 | <clearorders> nullify the command issued for the next turn | ||
| 242 | COMMANDS | ||
| 243 | |||
| 244 | my $result = { status => 0 }; | ||
| 245 | |||
| 246 | $query = lc $query; | ||
| 247 | |||
| 248 | if($query eq "quit") { $self->active(0); $result = { status => 1 }; } | ||
| 249 | elsif($query eq "nations") | ||
| 250 | { | ||
| 251 | $query = prompt "?", -menu=>$self->world->nation_names; | ||
| 252 | $self->nation(undef); | ||
| 253 | $self->keep_query(1); | ||
| 254 | $result = { status => 1 }; | ||
| 255 | } | ||
| 256 | elsif($query eq "years") | ||
| 257 | { | ||
| 258 | say "From " . $self->world->first_year . "/1 to " . $self->world->current_year; | ||
| 259 | $result = { status => 1 }; | ||
| 260 | } | ||
| 261 | elsif($query eq "clear") | ||
| 262 | { | ||
| 263 | $self->nation(undef); | ||
| 264 | $result = { status => 1 }; | ||
| 265 | } | ||
| 266 | elsif($query eq "commands") | ||
| 267 | { | ||
| 268 | print $commands; | ||
| 269 | $result = { status => 1 }; | ||
| 270 | } | ||
| 271 | elsif($query eq "wars") | ||
| 272 | { | ||
| 273 | print $self->world->print_wars(); | ||
| 274 | $result = { status => 1 }; | ||
| 275 | } | ||
| 276 | elsif($query eq "crises") | ||
| 277 | { | ||
| 278 | print $self->world->print_all_crises(); | ||
| 279 | $result = { status => 1 }; | ||
| 280 | } | ||
| 281 | elsif($query eq "alliances") | ||
| 282 | { | ||
| 283 | print $self->world->print_allies(undef, 'print'); | ||
| 284 | $result = { status => 1 }; | ||
| 285 | } | ||
| 286 | elsif($query eq "static") | ||
| 287 | { | ||
| 288 | open(my $html, "> report.html"); | ||
| 289 | print {$html} "<html><head></head><body>\n"; | ||
| 290 | print {$html} $self->world->print_hotspots('html'); | ||
| 291 | print {$html} "\n"; | ||
| 292 | print {$html} $self->world->print_allies(undef, 'html'); | ||
| 293 | print {$html} "\n"; | ||
| 294 | print {$html} $self->world->print_influences(undef, 'html'); | ||
| 295 | print {$html} "\n"; | ||
| 296 | print {$html} $self->world->print_military_supports(undef, 'html'); | ||
| 297 | print {$html} "\n"; | ||
| 298 | print {$html} $self->world->print_rebel_military_supports(undef, 'html'); | ||
| 299 | print {$html} "\n"; | ||
| 300 | print {$html} $self->world->print_war_history('html'); | ||
| 301 | print {$html} "\n"; | ||
| 302 | print {$html} $self->world->print_turn_statistics($self->world->get_prev_year(), undef, 'html'); | ||
| 303 | print {$html} "\n"; | ||
| 304 | print {$html} $self->world->print_borders_analysis('Italy', 'html'); | ||
| 305 | print {$html} "\n"; | ||
| 306 | print {$html} $self->world->print_near_analysis('Italy', 'html'); | ||
| 307 | print {$html} "\n"; | ||
| 308 | print {$html} $self->world->print_diplomacy('Italy', 'html'); | ||
| 309 | print {$html} "\n"; | ||
| 310 | print {$html} $self->world->print_formatted_turn_events('1975', undef, 'html'); | ||
| 311 | print {$html} "\n"; | ||
| 312 | print {$html} $self->world->print_nation_events('Italy', '1975', undef, 'html'); | ||
| 313 | print {$html} "\n"; | ||
| 314 | print {$html} "</body></html>"; | ||
| 315 | close($html); | ||
| 316 | $result = { status => 1 }; | ||
| 317 | } | ||
| 318 | elsif($query eq "influences") | ||
| 319 | { | ||
| 320 | print $self->world->print_influences(); | ||
| 321 | $result = { status => 1 }; | ||
| 322 | } | ||
| 323 | elsif($query eq "hotspots") | ||
| 324 | { | ||
| 325 | print $self->world->print_hotspots(); | ||
| 326 | $result = { status => 1 }; | ||
| 327 | } | ||
| 328 | elsif($query eq "war history") | ||
| 329 | { | ||
| 330 | print $self->world->print_war_history(); | ||
| 331 | $result = { status => 1 }; | ||
| 332 | } | ||
| 333 | elsif($query eq "treaties") | ||
| 334 | { | ||
| 335 | print $self->world->print_treaties_table(); | ||
| 336 | $result = { status => 1 }; | ||
| 337 | } | ||
| 338 | elsif($query =~ /^situation( (.*))?$/) | ||
| 339 | { | ||
| 340 | my $order = $2; | ||
| 341 | say $self->world->print_turn_statistics($self->world->get_prev_year(), $order); | ||
| 342 | $result = { status => 1 }; | ||
| 343 | } | ||
| 344 | elsif($query eq "supports") | ||
| 345 | { | ||
| 346 | say $self->world->print_military_supports(); | ||
| 347 | $result = { status => 1 }; | ||
| 348 | } | ||
| 349 | elsif($query eq "rebel supports") | ||
| 350 | { | ||
| 351 | say $self->world->print_rebel_military_supports(); | ||
| 352 | $result = { status => 1 }; | ||
| 353 | } | ||
| 354 | elsif($query =~ /save( (.*))?$/) | ||
| 355 | { | ||
| 356 | my $savefile = $2; | ||
| 357 | $savefile ||= $self->world->savefile; | ||
| 358 | say $self->world->dump_all($savefile); | ||
| 359 | $result = { status => 1 }; | ||
| 360 | } | ||
| 361 | elsif($query =~ /^distance (.*)-(.*)$/) | ||
| 362 | { | ||
| 363 | my $n1 = $self->world->correct_nation_name($1); | ||
| 364 | my $n2 = $self->world->correct_nation_name($2); | ||
| 365 | if($self->verify_nation($n1) && $self->verify_nation($n2)) | ||
| 366 | { | ||
| 367 | say $self->world->print_distance($n1, $n2); | ||
| 368 | $result = { status => 1 }; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | elsif($query =~ /^((.*) )?borders$/) | ||
| 372 | { | ||
| 373 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 374 | if($input_nation) | ||
| 375 | { | ||
| 376 | $self->nation($input_nation); | ||
| 377 | } | ||
| 378 | if($self->nation) | ||
| 379 | { | ||
| 380 | print $self->world->print_borders_analysis($self->nation); | ||
| 381 | $result = { status => 1 }; | ||
| 382 | } | ||
| 383 | else | ||
| 384 | { | ||
| 385 | $result = { status => -1 }; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | elsif($query =~ /^((.*) )?near$/) | ||
| 389 | { | ||
| 390 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 391 | if($input_nation) | ||
| 392 | { | ||
| 393 | $self->nation($input_nation); | ||
| 394 | } | ||
| 395 | if($self->nation) | ||
| 396 | { | ||
| 397 | print $self->world->print_near_analysis($self->nation); | ||
| 398 | $result = { status => 1 }; | ||
| 399 | } | ||
| 400 | else | ||
| 401 | { | ||
| 402 | $result = { status => -1 }; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | elsif($query =~ /^((.*) )?relations$/) | ||
| 406 | { | ||
| 407 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 408 | if($input_nation) | ||
| 409 | { | ||
| 410 | $self->nation($input_nation); | ||
| 411 | } | ||
| 412 | if($self->nation) | ||
| 413 | { | ||
| 414 | print $self->world->print_diplomacy($self->nation); | ||
| 415 | $result = { status => 1 }; | ||
| 416 | } | ||
| 417 | else | ||
| 418 | { | ||
| 419 | $result = { status => -1 }; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | elsif($query =~ /^((.*) )?events( ((\d+)(\/\d+)?))?$/) | ||
| 423 | { | ||
| 424 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 425 | my $input_year = $4; | ||
| 426 | $input_year ||= undef; | ||
| 427 | if($input_nation) | ||
| 428 | { | ||
| 429 | $self->nation($input_nation); | ||
| 430 | } | ||
| 431 | if($self->nation) | ||
| 432 | { | ||
| 433 | if($input_year) | ||
| 434 | { | ||
| 435 | my @turns = get_year_turns($input_year); | ||
| 436 | foreach my $t (@turns) | ||
| 437 | { | ||
| 438 | print $self->world->print_nation_events($self->nation, $t); | ||
| 439 | my $wait = prompt "... press enter to continue ...\n\n" if($t ne $turns[-1]); | ||
| 440 | } | ||
| 441 | $result = { status => 1 }; | ||
| 442 | } | ||
| 443 | else | ||
| 444 | { | ||
| 445 | print $self->world->print_nation_events($self->nation, prev_turn($self->world->current_year) ); | ||
| 446 | $result = { status => 1 }; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | else | ||
| 450 | { | ||
| 451 | if($input_year) | ||
| 452 | { | ||
| 453 | $query = $input_year; | ||
| 454 | $self->keep_query(1); | ||
| 455 | $result = { status => 1 }; | ||
| 456 | } | ||
| 457 | else | ||
| 458 | { | ||
| 459 | $query = prev_turn($self->world->current_year); | ||
| 460 | $self->keep_query(1); | ||
| 461 | $result = { status => 1 }; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | } | ||
| 465 | elsif($query =~ /^((.*) )?status$/) | ||
| 466 | { | ||
| 467 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 468 | if($input_nation) | ||
| 469 | { | ||
| 470 | $query = $input_nation; | ||
| 471 | $self->keep_query(1); | ||
| 472 | } | ||
| 473 | elsif($self->nation) | ||
| 474 | { | ||
| 475 | $query = $self->nation; | ||
| 476 | $self->keep_query(1); | ||
| 477 | } | ||
| 478 | $result = { status => 1 }; | ||
| 479 | } | ||
| 480 | elsif($query =~ /^((.*) )?history$/) | ||
| 481 | { | ||
| 482 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 483 | if($input_nation) | ||
| 484 | { | ||
| 485 | $self->nation($input_nation); | ||
| 486 | } | ||
| 487 | if($self->nation) | ||
| 488 | { | ||
| 489 | print $self->world->print_nation_statistics($self->nation, $self->world->first_year, prev_turn($self->world->current_year)); | ||
| 490 | $result = { status => 1 }; | ||
| 491 | } | ||
| 492 | else | ||
| 493 | { | ||
| 494 | $result = { status => -1 }; | ||
| 495 | } | ||
| 496 | } | ||
| 497 | elsif($query =~ /^((.*) )?plot (.*)$/) | ||
| 498 | { | ||
| 499 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 500 | if($input_nation) | ||
| 501 | { | ||
| 502 | $self->nation($input_nation); | ||
| 503 | } | ||
| 504 | if($self->nation) | ||
| 505 | { | ||
| 506 | print $self->world->plot_nation_factor($self->nation, $3, $self->world->first_year, prev_turn($self->world->current_year)); | ||
| 507 | $result = { status => 1 }; | ||
| 508 | } | ||
| 509 | else | ||
| 510 | { | ||
| 511 | $result = { status => -1 }; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | else | ||
| 515 | { | ||
| 516 | my $nation_query = $self->world->correct_nation_name($query); | ||
| 517 | if($self->verify_nation($nation_query)) #it's a nation | ||
| 518 | { | ||
| 519 | print $self->world->print_nation_actual_situation($nation_query, 1); | ||
| 520 | $self->nation($nation_query); | ||
| 521 | $result = { status => 1 }; | ||
| 522 | } | ||
| 523 | else | ||
| 524 | { | ||
| 525 | my @good_year = (); | ||
| 526 | if($query =~ /^(\d+)(\/\d+)?/) #it's an year or a turn | ||
| 527 | { | ||
| 528 | if((compare_turns($query, $self->world->current_year) == 0 || compare_turns($query, $self->world->current_year) == -1) && | ||
| 529 | compare_turns($query, $self->world->first_year) >= 0) | ||
| 530 | { | ||
| 531 | if($self->nation) | ||
| 532 | { | ||
| 533 | $query = "events $query"; | ||
| 534 | $self->keep_query(1); | ||
| 535 | $result = { status => 1 }; | ||
| 536 | } | ||
| 537 | else | ||
| 538 | { | ||
| 539 | my @turns = get_year_turns($query); | ||
| 540 | foreach my $t (@turns) | ||
| 541 | { | ||
| 542 | say $self->world->print_formatted_turn_events($t); | ||
| 543 | my $wait = prompt "... press enter to continue ...\n" if($t ne $turns[-1]); | ||
| 544 | } | ||
| 545 | $result = { status => 1 }; | ||
| 546 | } | ||
| 547 | } | ||
| 548 | } | ||
| 549 | } | ||
| 550 | } | ||
| 551 | print "\n"; | ||
| 552 | $self->query($query); | ||
| 553 | return $result; | ||
| 554 | } | ||
| 555 | |||
| 556 | sub orders | ||
| 557 | { | ||
| 558 | my $self = shift; | ||
| 559 | if($self->executive && $self->executive->actor) | ||
| 560 | { | ||
| 561 | if($self->get_active_player->influence($self->executive->actor) >= INFLUENCE_COST) | ||
| 562 | { | ||
| 563 | return $self->executive->recognize_command($self->nation, | ||
| 564 | $self->query); | ||
| 565 | } | ||
| 566 | else | ||
| 567 | { | ||
| 568 | return { status => -4 }; | ||
| 569 | } | ||
| 570 | } | ||
| 571 | else | ||
| 572 | { | ||
| 573 | return { status => 0 }; | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | sub stock_commands | ||
| 578 | { | ||
| 579 | my $self = shift; | ||
| 580 | my $query = $self->query; | ||
| 581 | my $result = { status => 0 }; | ||
| 582 | $query = lc $query; | ||
| 583 | if($query =~ /^buy\s+(\d+)(\s+(.*))?$/) | ||
| 584 | { | ||
| 585 | my $stock_nation = $3; | ||
| 586 | if($stock_nation) | ||
| 587 | { | ||
| 588 | $stock_nation = $self->world->correct_nation_name($stock_nation); | ||
| 589 | } | ||
| 590 | else | ||
| 591 | { | ||
| 592 | $stock_nation = $self->nation; | ||
| 593 | } | ||
| 594 | my $q = $1; | ||
| 595 | if($stock_nation) | ||
| 596 | { | ||
| 597 | $result = $self->world->buy_stock($self->active_player, $stock_nation, $q, 1); | ||
| 598 | if($result->{ status } == 1) | ||
| 599 | { | ||
| 600 | $self->get_active_player()->add_stock_order($result->{ command }); | ||
| 601 | } | ||
| 602 | } | ||
| 603 | } | ||
| 604 | if($query =~ /^sell\s+(\d+)(\s+(.*))?$/) | ||
| 605 | { | ||
| 606 | my $stock_nation = $3; | ||
| 607 | if($stock_nation) | ||
| 608 | { | ||
| 609 | $stock_nation = $self->world->correct_nation_name($stock_nation); | ||
| 610 | } | ||
| 611 | else | ||
| 612 | { | ||
| 613 | $stock_nation = $self->nation; | ||
| 614 | } | ||
| 615 | my $q = $1; | ||
| 616 | if($stock_nation) | ||
| 617 | { | ||
| 618 | $result = $self->world->sell_stock($self->active_player, $stock_nation, $q, 1); | ||
| 619 | if($result->{ status } == 1) | ||
| 620 | { | ||
| 621 | $self->get_active_player()->add_stock_order($result->{ command }); | ||
| 622 | } | ||
| 623 | } | ||
| 624 | } | ||
| 625 | elsif($query eq 'market') | ||
| 626 | { | ||
| 627 | say $self->world->print_market; | ||
| 628 | $result = { status => 2 }; | ||
| 629 | } | ||
| 630 | elsif($query eq 'show stocks') | ||
| 631 | { | ||
| 632 | say $self->world->print_stocks($self->active_player); | ||
| 633 | $result = { status => 2 }; | ||
| 634 | } | ||
| 635 | elsif($query eq 'show stock orders') | ||
| 636 | { | ||
| 637 | my $player = $self->get_active_player; | ||
| 638 | say $player->print_stock_orders(); | ||
| 639 | $result = { status => 2 }; | ||
| 640 | } | ||
| 641 | elsif($query eq 'empty stock orders') | ||
| 642 | { | ||
| 643 | my $player = $self->get_active_player; | ||
| 644 | $player->empty_stock_orders(); | ||
| 645 | $result = { status => 1 }; | ||
| 646 | } | ||
| 647 | elsif($query =~ /^remove stock orders( (.*))?/) | ||
| 648 | { | ||
| 649 | my $stock_nation = $2; | ||
| 650 | if($stock_nation) | ||
| 651 | { | ||
| 652 | $stock_nation = $self->world->correct_nation_name($stock_nation); | ||
| 653 | } | ||
| 654 | else | ||
| 655 | { | ||
| 656 | $stock_nation = $self->nation; | ||
| 657 | } | ||
| 658 | if($stock_nation) | ||
| 659 | { | ||
| 660 | my $player = $self->get_active_player; | ||
| 661 | $player->remove_stock_orders($stock_nation); | ||
| 662 | $result = { status => 1 }; | ||
| 663 | } | ||
| 664 | } | ||
| 665 | elsif($query =~ /^stockevents( ((\d+)(\/\d+)?))?$/) | ||
| 666 | { | ||
| 667 | my $input_year = $2; | ||
| 668 | $input_year ||= prev_turn($self->world->current_year); | ||
| 669 | #my @turns = get_year_turns($input_year); | ||
| 670 | #foreach my $t (@turns) | ||
| 671 | #{ | ||
| 672 | print $self->world->print_stock_events($self->active_player, $input_year, "My stock events", 3); | ||
| 673 | # my $wait = prompt "... press enter to continue ...\n\n" if($t ne $turns[-1]); | ||
| 674 | #} | ||
| 675 | #print "\n"; | ||
| 676 | $result = { status => 2 }; | ||
| 677 | } | ||
| 678 | |||
| 679 | return $result; | ||
| 680 | } | ||
| 681 | |||
| 682 | sub control_commands | ||
| 683 | { | ||
| 684 | my $self = shift; | ||
| 685 | my $query = $self->query; | ||
| 686 | my $result = { status => 0 }; | ||
| 687 | $query = lc $query; | ||
| 688 | if($query =~ /^control( (.*))$/) | ||
| 689 | { | ||
| 690 | my $input_nation = $self->world->correct_nation_name($2); | ||
| 691 | my $controlled_nation = undef; | ||
| 692 | if($input_nation) | ||
| 693 | { | ||
| 694 | $controlled_nation = $input_nation; | ||
| 695 | } | ||
| 696 | if(! $controlled_nation) | ||
| 697 | { | ||
| 698 | $controlled_nation = $self->nation; | ||
| 699 | } | ||
| 700 | if($controlled_nation) | ||
| 701 | { | ||
| 702 | if($self->world->at_war($controlled_nation)) | ||
| 703 | { | ||
| 704 | return { status => -2 }; | ||
| 705 | } | ||
| 706 | if($self->world->at_civil_war($controlled_nation)) | ||
| 707 | { | ||
| 708 | return { status => -3 }; | ||
| 709 | } | ||
| 710 | my $player = $self->get_active_player(); | ||
| 711 | if($player->influence($controlled_nation) > 0) | ||
| 712 | { | ||
| 713 | $self->set_executive($controlled_nation); | ||
| 714 | say $self->world->print_nation_actual_situation($controlled_nation, 1); | ||
| 715 | $result = { status => 1 }; | ||
| 716 | } | ||
| 717 | else | ||
| 718 | { | ||
| 719 | $result = { status => -1 }; | ||
| 720 | } | ||
| 721 | } | ||
| 722 | } | ||
| 723 | elsif($query =~ /^uncontrol$/) | ||
| 724 | { | ||
| 725 | $self->executive(undef); | ||
| 726 | $result = { status => 1 }; | ||
| 727 | } | ||
| 728 | elsif($query =~ /^show control orders/) | ||
| 729 | { | ||
| 730 | say $self->get_active_player->print_control_orders(); | ||
| 731 | $result = { status => 1 } | ||
| 732 | } | ||
| 733 | elsif($query eq "orders") | ||
| 734 | { | ||
| 735 | if($self->executive) | ||
| 736 | { | ||
| 737 | say $self->executive->print_orders();; | ||
| 738 | $result = { status => 1 }; | ||
| 739 | } | ||
| 740 | } | ||
| 741 | elsif($query eq 'clearorders') | ||
| 742 | { | ||
| 743 | if($self->executive) | ||
| 744 | { | ||
| 745 | $self->get_active_player->add_control_order($self->executive->actor, undef); | ||
| 746 | $result = { status => 2 }; | ||
| 747 | } | ||
| 748 | } | ||
| 749 | return $result; | ||
| 750 | } | ||
| 751 | |||
| 752 | sub commands | ||
| 753 | { | ||
| 754 | my $self = shift; | ||
| 755 | my $result; | ||
| 756 | $result = $self->turn_command(); | ||
| 757 | if($self->handle_result('turn', $result)) | ||
| 758 | { | ||
| 759 | $self->latest_result($result); | ||
| 760 | return 1; | ||
| 761 | } | ||
| 762 | $result = $self->report_commands(); | ||
| 763 | if($self->handle_result('report', $result)) | ||
| 764 | { | ||
| 765 | $self->latest_result($result); | ||
| 766 | return 1; | ||
| 767 | } | ||
| 768 | $result = $self->stock_commands(); | ||
| 769 | if($self->handle_result('stock', $result)) | ||
| 770 | { | ||
| 771 | $self->latest_result($result); | ||
| 772 | return 1; | ||
| 773 | } | ||
| 774 | $result = $self->control_commands(); | ||
| 775 | if($self->handle_result('control', $result)) | ||
| 776 | { | ||
| 777 | $self->latest_result($result); | ||
| 778 | return 1; | ||
| 779 | } | ||
| 780 | $result = $self->orders(); | ||
| 781 | if($self->handle_result('orders', $result)) | ||
| 782 | { | ||
| 783 | $self->latest_result($result); | ||
| 784 | return 1; | ||
| 785 | } | ||
| 786 | return 0; | ||
| 787 | } | ||
| 788 | |||
| 789 | sub interact | ||
| 790 | { | ||
| 791 | my $self = shift; | ||
| 792 | my $pre_decisions = shift; | ||
| 793 | $pre_decisions = 1 if(! defined $pre_decisions); | ||
| 794 | $self->world->pre_decisions_elaborations() if $pre_decisions; | ||
| 795 | while($self->active) | ||
| 796 | { | ||
| 797 | my $result = undef; | ||
| 798 | $self->clear_query(); | ||
| 799 | $self->get_query(); | ||
| 800 | if(! $self->commands()) | ||
| 801 | { | ||
| 802 | $self->latest_result(undef); | ||
| 803 | say "Bad command"; | ||
| 804 | } | ||
| 805 | } | ||
| 806 | } | ||
| 807 | |||
| 808 | sub handle_result | ||
| 809 | { | ||
| 810 | my $self = shift; | ||
| 811 | my $type = shift; | ||
| 812 | my $result = shift; | ||
| 813 | if($type eq 'turn') | ||
| 814 | { | ||
| 815 | if($result->{status} == 1) | ||
| 816 | { | ||
| 817 | say "Elaborating " . $self->world->current_year . "...\n"; | ||
| 818 | $self->world->decisions(); | ||
| 819 | $self->world->post_decisions_elaborations(); | ||
| 820 | if($self->nation) | ||
| 821 | { | ||
| 822 | say evidence_text($self->world->print_formatted_turn_events($self->world->current_year), $self->nation); | ||
| 823 | } | ||
| 824 | else | ||
| 825 | { | ||
| 826 | say $self->world->print_formatted_turn_events($self->world->current_year); | ||
| 827 | } | ||
| 828 | $self->world->pre_decisions_elaborations(); | ||
| 829 | $self->executive(undef); | ||
| 830 | return 1; | ||
| 831 | } | ||
| 832 | else | ||
| 833 | { | ||
| 834 | return 0; | ||
| 835 | } | ||
| 836 | } | ||
| 837 | elsif($type eq 'report') | ||
| 838 | { | ||
| 839 | if($result->{status} == 1) | ||
| 840 | { | ||
| 841 | return 1; | ||
| 842 | } | ||
| 843 | elsif($result->{status} == -1) | ||
| 844 | { | ||
| 845 | say "No nation selected"; | ||
| 846 | return 1; | ||
| 847 | } | ||
| 848 | else | ||
| 849 | { | ||
| 850 | return 0; | ||
| 851 | } | ||
| 852 | } | ||
| 853 | elsif($type eq 'stock') | ||
| 854 | { | ||
| 855 | if($result->{status} == -11) | ||
| 856 | { | ||
| 857 | say "Requested stock quantity not available"; | ||
| 858 | return 1; | ||
| 859 | } | ||
| 860 | elsif($result->{status} == -12) | ||
| 861 | { | ||
| 862 | say "Not enough money"; | ||
| 863 | return 1; | ||
| 864 | } | ||
| 865 | elsif($result->{status} == -13) | ||
| 866 | { | ||
| 867 | say "You haven't that quantity"; | ||
| 868 | return 1; | ||
| 869 | } | ||
| 870 | elsif($result->{status} == -14) | ||
| 871 | { | ||
| 872 | say "You can't trade during civil war"; | ||
| 873 | return 1; | ||
| 874 | } | ||
| 875 | elsif($result->{status} == -15) | ||
| 876 | { | ||
| 877 | say "You can't buy more than " . MAX_BUY_STOCK . " stocks"; | ||
| 878 | return 1; | ||
| 879 | } | ||
| 880 | elsif($result->{status} == 1) | ||
| 881 | { | ||
| 882 | say "Stock order registered"; | ||
| 883 | return 1; | ||
| 884 | } | ||
| 885 | elsif($result->{status} == 2) | ||
| 886 | { | ||
| 887 | return 1; | ||
| 888 | } | ||
| 889 | else | ||
| 890 | { | ||
| 891 | return 0; | ||
| 892 | } | ||
| 893 | } | ||
| 894 | elsif($type eq 'control') | ||
| 895 | { | ||
| 896 | if($result->{status} == 1) | ||
| 897 | { | ||
| 898 | return 1; | ||
| 899 | } | ||
| 900 | if($result->{status} == 2) | ||
| 901 | { | ||
| 902 | say "Order revoked"; | ||
| 903 | return 1; | ||
| 904 | } | ||
| 905 | elsif($result->{status} == -1) | ||
| 906 | { | ||
| 907 | say "No influence on requested nation"; | ||
| 908 | return 1; | ||
| 909 | } | ||
| 910 | elsif($result->{status} == -2) | ||
| 911 | { | ||
| 912 | say "No control during war"; | ||
| 913 | } | ||
| 914 | elsif($result->{status} == -3) | ||
| 915 | { | ||
| 916 | say "No control during civil war"; | ||
| 917 | } | ||
| 918 | |||
| 919 | else | ||
| 920 | { | ||
| 921 | return 0; | ||
| 922 | } | ||
| 923 | } | ||
| 924 | elsif($type eq 'orders') | ||
| 925 | { | ||
| 926 | if($result->{status} == -1) | ||
| 927 | { | ||
| 928 | say "Command not allowed"; | ||
| 929 | return 1; | ||
| 930 | } | ||
| 931 | elsif($result->{status} == -2) | ||
| 932 | { | ||
| 933 | say "No options available"; | ||
| 934 | return 1; | ||
| 935 | } | ||
| 936 | elsif($result->{status} == -3) | ||
| 937 | { | ||
| 938 | say "Command aborted"; | ||
| 939 | return 1; | ||
| 940 | } | ||
| 941 | elsif($result->{status} == -4) | ||
| 942 | { | ||
| 943 | say "Not enough influence"; | ||
| 944 | return 1; | ||
| 945 | } | ||
| 946 | elsif($result->{status} == 1) | ||
| 947 | { | ||
| 948 | say "Order selected for " . | ||
| 949 | $self->executive->actor . | ||
| 950 | ": " . $result->{command}; | ||
| 951 | my $player = $self->get_active_player(); | ||
| 952 | #$player->add_influence(-1 * INFLUENCE_COST, $self->executive->actor); | ||
| 953 | $player->add_control_order($self->executive->actor, $result->{command}); | ||
| 954 | return 1; | ||
| 955 | } | ||
| 956 | else | ||
| 957 | { | ||
| 958 | return 0; | ||
| 959 | } | ||
| 960 | } | ||
| 961 | } | ||
| 962 | |||
| 963 | |||
| 964 | 1; |