lib/BalanceOfPower/Commands.pm

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