File Coverage

lib/BalanceOfPower/Commands.pm
Criterion Covered Total %
statement 127 544 23.3
branch 79 306 25.8
condition 0 23 0.0
subroutine 19 30 63.3
pod 0 21 0.0
total 225 924 24.3


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