blib/lib/BalanceOfPower/Commands.pm untested

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