lib/BalanceOfPower/Role/CrisisManager.pm

filter: Statementstmt 26.4% 28 / 106 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 58.3% 7 / 12 Podpod 0.0% 0 / 5 totaltotal 17.5% 35 / 200 CC 35 SCAR 49.3
CC
35
Cov
16%
crisis_generator_round
306.0
print_all_crises
90.0
create_or_escalate_crisis
42.0
CRAP 138.8
« blib/lib/BalanceOfPower/Commands/NagTreaty.pm ↑ Summary blib/lib/BalanceOfPower/Role/Recorder.pm »
1 package BalanceOfPower::Role::CrisisManager;
2 11 $BalanceOfPower::Role::CrisisManager::VERSION = '0.300010';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use BalanceOfPower::Constants ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use BalanceOfPower::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 requires 'get_all_crises';
13 11 requires 'get_hates';
14 11 requires 'crisis_exists';
15 11 requires 'war_exists';
16 11 requires 'broadcast_event';
17 11 requires 'add_crisis';
18  
19 sub crisis_generator
20 {
21 0 ▶ my $self = shift;
Subroutine crisis_generator: not called CC=4 SCAR=30.0
Pod undocumented
22 0 my @crises = $self->get_all_crises();
23 0 my @hates = ();
24 0 foreach my $h ($self->get_hates())
25 {
26 0 ▶ push @hates, $h
Branch: 1 branch
BranchTrueFalseTotal
unless $self->crisis_exists($h->node1, $h->node2) 0 0 0
27 if(! $self->crisis_exists($h->node1, $h->node2))
28 }
29 0 my $crises_to_use = \@crises;
30 0 my $hates_to_use = \@hates;
31 0 ▶ for(my $i = 0; $i < CRISIS_GENERATION_TRIES; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 0 0 0
32 {
33 0 ($hates_to_use, $crises_to_use) = $self->crisis_generator_round($hates_to_use, $crises_to_use);
34 }
35  
36 }
37  
38 sub crisis_generator_round
39 {
40 0 ▶ my $self = shift;
Subroutine crisis_generator_round: not called CC=17 SCAR=57.2
Pod undocumented
41 0 ▶ my $hates_to_use = shift || [] ;
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
42 0 ▶ my $crises_to_use = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
43 0 my @hates = $self->shuffle("Crisis generation: choosing hate", @{ $hates_to_use });
44 0 my @crises = $self->shuffle("Crisis generation: choosing crisis", @{ $crises_to_use});
45 0 my @original_hates = @hates;
46 0 my @original_crises = @crises;
47
48 0 my $picked_hate = undef;
49 0 my $picked_crisis = undef;
50 0 ▶ if(@hates)
Branch: 1 branch
BranchTrueFalseTotal
if (@hates) 0 0 0
51 {
52 0 $picked_hate = shift @hates;
53 }
54 0 ▶ if(@crises)
Branch: 1 branch
BranchTrueFalseTotal
if (@crises) 0 0 0
55 {
56 0 $picked_crisis = shift @crises;
57 }
58
59  
60 0 my $action = $self->random(0, CRISIS_GENERATOR_NOACTION_TOKENS + 3, "Crisis action choose");
61 0 ▶ if($action == 0) #NEW CRISIS
Branch: 4 branches
BranchTrueFalseTotal
if ($action == 0) { } 0 0 0
elsif ($action == 1) { } 0 0 0
elsif ($action == 2) { } 0 0 0
elsif ($action == 3) { } 0 0 0
62 {
63 0 ▶ return (\@original_hates, \@original_crises) if ! $picked_hate;
Branch: 1 branch
BranchTrueFalseTotal
unless $picked_hate 0 0 0
64 0 ▶ if(! $self->war_exists($picked_hate->node1, $picked_hate->node2))
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->war_exists($picked_hate->node1, $picked_hate->node2)) 0 0 0
65 {
66 0 $self->create_or_escalate_crisis($picked_hate->node1, $picked_hate->node2);
67 0 return (\@hates, \@original_crises);
68 }
69 }
70 elsif($action == 1) #ESCALATE
71 {
72 0 ▶ return (\@original_hates, \@original_crises) if ! $picked_crisis;
Branch: 1 branch
BranchTrueFalseTotal
unless $picked_crisis 0 0 0
73 0 ▶ if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->war_exists($picked_crisis->node1, $picked_crisis->node2)) 0 0 0
74 {
75 0 $self->create_or_escalate_crisis($picked_crisis->node1, $picked_crisis->node2);
76 }
77 0 return (\@original_hates, \@crises);
78 }
79 elsif($action == 2) #COOL DOWN
80 {
81 0 ▶ return (\@original_hates, \@original_crises) if ! $picked_crisis;
Branch: 1 branch
BranchTrueFalseTotal
unless $picked_crisis 0 0 0
82 0 ▶ if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->war_exists($picked_crisis->node1, $picked_crisis->node2)) 0 0 0
83 {
84 0 $self->cool_down($picked_crisis->node1, $picked_crisis->node2);
85 }
86 0 return (\@original_hates, \@crises);
87 }
88 elsif($action == 3) #ELIMINATE
89 {
90 0 ▶ return (\@original_hates, \@original_crises) if ! $picked_crisis;
Branch: 1 branch
BranchTrueFalseTotal
unless $picked_crisis 0 0 0
91 0 ▶ if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->war_exists($picked_crisis->node1, $picked_crisis->node2)) 0 0 0
92 {
93 0 $self->delete_crisis($picked_crisis->node1, $picked_crisis->node2);
94 }
95 0 return (\@original_hates, \@crises);
96 }
97 else
98 {
99 0 return (\@original_hates, \@original_crises);
100 }
101 }
102 sub create_or_escalate_crisis
103 {
104 0 ▶ my $self = shift;
Subroutine create_or_escalate_crisis: not called CC=6 SCAR=37.4
Pod undocumented
105 0 ▶ my $node1 = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
106 0 ▶ my $node2 = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
107 0 ▶ if(my $crisis = $self->crisis_exists($node1, $node2))
Branch: 1 branch
BranchTrueFalseTotal
if (my $crisis = $self->crisis_exists($node1, $node2)) { } 0 0 0
108 {
109 0 ▶ if(! $crisis->is_max_crisis)
Branch: 1 branch
BranchTrueFalseTotal
if (not $crisis->is_max_crisis) 0 0 0
110 {
111 0 $crisis->escalate_crisis();
112 0 my $event = "CRISIS BETWEEN $node1 AND $node2 ESCALATES";
113 0 ▶ if($crisis->is_max_crisis)
Branch: 1 branch
BranchTrueFalseTotal
if ($crisis->is_max_crisis) 0 0 0
114 {
115 0 $event .= " TO MAX LEVEL";
116 }
117 0 $self->broadcast_event($event, $node1, $node2);
118 }
119 }
120 else
121 {
122 0 $self->add_crisis($node1, $node2);
123 0 $self->broadcast_event("CRISIS BETWEEN $node1 AND $node2 STARTED", $node1, $node2);
124 }
125 }
126 sub cool_down
127 {
128 0 ▶ my $self = shift;
Subroutine cool_down: not called CC=3 SCAR=24.8
Pod undocumented
129 0 my $node1 = shift;
130 0 my $node2 = shift;
131 0 ▶ if(my $crisis = $self->crisis_exists($node1, $node2))
Branch: 1 branch
BranchTrueFalseTotal
if (my $crisis = $self->crisis_exists($node1, $node2)) 0 0 0
132 {
133 0 $crisis->cooldown_crisis();
134 0 ▶ if(! $crisis->is_crisis())
Branch: 1 branch
BranchTrueFalseTotal
if (not $crisis->is_crisis) { } 0 0 0
135 {
136 0 my $event = "CRISIS BETWEEN $node1 AND $node2 ENDED";
137 0 $self->broadcast_event($event, $node1, $node2);
138 }
139 else
140 {
141 0 $self->broadcast_event("CRISIS BETWEEN $node1 AND $node2 COOLED DOWN", $node1, $node2);
142 }
143 }
144 }
145  
146 sub print_all_crises
147 {
148 0 ▶ my $self = shift;
Subroutine print_all_crises: not called CC=9 SCAR=45.0
Pod undocumented
149 0 my $n = shift;
150 0 ▶ my $level = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
151 0 ▶ my $mode = shift || 'print';
Condition0%0/2
shift() || 'print'
l!l
00
MC/DC0%0/1
shift() || 'print'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
152 0 my @crises;
153 my @war_signal;
154 0 foreach my $b ($self->get_all_crises())
155 {
156 0 ▶ if(( ($n && $b->involve($n) || ! $n)) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($n && $b->involve($n) || !$n and $b->get_crisis_level > $level) 0 0 0
Condition0%0/9
$n && $b->involve($n) || !$n and $b->get_crisis_level > $level
!ll&&!rl&&r
000
$n and $b->involve($n) or not $n
l!l&&r!l&&!r
000
$n and $b->involve($n)
!ll&&!rl&&r
000
MC/DC0%0/4
$n && $b->involve($n) || !$n and $b->get_crisis_level > $level
$n $b->involve($n) not $n $b->get_crisis_level > $level
Truth table0%0/8
A and B or not(C) and D
A= $nB= $b->involve($n)C= not $nD= $b->get_crisis_level > $level
ABCDresult
0X0X0
100X0
11X00
0X100
10100
11X11
0X111
10111
157 ( $b->get_crisis_level > $level))
158 {
159 0 push @crises, $b;
160 0 ▶ if($self->war_exists($b->node1, $b->node2))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->war_exists($b->node1, $b->node2)) { } 0 0 0
161 {
162 0 push @war_signal, 1;
163 }
164 else
165 {
166 0 push @war_signal, 0;
167 }
168 }
169 }
170 0 my %nation_codes = reverse %{$self->nation_codes};
171 0 return BalanceOfPower::Printer::print($mode, $self, 'print_all_crises',
172 { crises => \@crises,
173 wars => \@war_signal,
174 nation_codes => \%nation_codes,
175 });
176 }
177 11 1;
178  
« blib/lib/BalanceOfPower/Commands/NagTreaty.pm ↑ Summary blib/lib/BalanceOfPower/Role/Recorder.pm »