lib/BalanceOfPower/Commands.pm

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