File Coverage

lib/BalanceOfPower/Role/CrisisManager.pm
Criterion Covered Total %
statement 82 96 85.4
branch 33 42 78.5
condition 4 18 22.2
subroutine 11 12 91.6
pod 0 5 0.0
total 130 173 75.1


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::CrisisManager;
2             $BalanceOfPower::Role::CrisisManager::VERSION = '0.400110';
3 13     13   4388 use strict;
  13         19  
  13         287  
4 13     13   145 use v5.10;
  13         27  
5 13     13   41 use Moo::Role;
  13         15  
  13         63  
6 13     13   2566 use Template;
  13         17  
  13         204  
7 13     13   39 use Term::ANSIColor;
  13         10  
  13         572  
8              
9 13     13   46 use BalanceOfPower::Constants ':all';
  13         15  
  13         5306  
10 13     13   87 use BalanceOfPower::Printer;
  13         17  
  13         8334  
11              
12             requires 'get_all_crises';
13             requires 'get_hates';
14             requires 'crisis_exists';
15             requires 'war_exists';
16             requires 'broadcast_event';
17             requires 'add_crisis';
18              
19             sub crisis_generator
20             {
21 46     46 0 46 my $self = shift;
22 46         121 my @crises = $self->get_all_crises();
23 46         53 my @hates = ();
24 46         128 foreach my $h ($self->get_hates())
25             {
26 105 100       244 push @hates, $h
27             if(! $self->crisis_exists($h->node1, $h->node2))
28             }
29 46         55 my $crises_to_use = \@crises;
30 46         47 my $hates_to_use = \@hates;
31 46         112 for(my $i = 0; $i < CRISIS_GENERATION_TRIES; $i++)
32             {
33 230         385 ($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 230     230 0 192 my $self = shift;
41 230   50     368 my $hates_to_use = shift || [] ;
42 230   50     323 my $crises_to_use = shift || [];
43 230         189 my @hates = $self->shuffle("Crisis generation: choosing hate", @{ $hates_to_use });
  230         3920  
44 230         295 my @crises = $self->shuffle("Crisis generation: choosing crisis", @{ $crises_to_use});
  230         4182  
45 230         288 my @original_hates = @hates;
46 230         221 my @original_crises = @crises;
47            
48 230         210 my $picked_hate = undef;
49 230         150 my $picked_crisis = undef;
50 230 100       347 if(@hates)
51             {
52 150         147 $picked_hate = shift @hates;
53             }
54 230 100       325 if(@crises)
55             {
56 87         79 $picked_crisis = shift @crises;
57             }
58            
59              
60 230         4017 my $action = $self->random(0, CRISIS_GENERATOR_NOACTION_TOKENS + 3, "Crisis action choose");
61 230 100       699 if($action == 0) #NEW CRISIS
    100          
    100          
    100          
62             {
63 16 100       61 return (\@original_hates, \@original_crises) if ! $picked_hate;
64 10 50       56 if(! $self->war_exists($picked_hate->node1, $picked_hate->node2))
65             {
66 10         45 $self->create_or_escalate_crisis($picked_hate->node1, $picked_hate->node2);
67 10         55 return (\@hates, \@original_crises);
68             }
69             }
70             elsif($action == 1) #ESCALATE
71             {
72 26 100       120 return (\@original_hates, \@original_crises) if ! $picked_crisis;
73 8 50       46 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
74             {
75 8         30 $self->create_or_escalate_crisis($picked_crisis->node1, $picked_crisis->node2);
76             }
77 8         45 return (\@original_hates, \@crises);
78             }
79             elsif($action == 2) #COOL DOWN
80             {
81 17 100       83 return (\@original_hates, \@original_crises) if ! $picked_crisis;
82 5 50       34 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
83             {
84 5         32 $self->cool_down($picked_crisis->node1, $picked_crisis->node2);
85             }
86 5         31 return (\@original_hates, \@crises);
87             }
88             elsif($action == 3) #ELIMINATE
89             {
90 21 100       106 return (\@original_hates, \@original_crises) if ! $picked_crisis;
91 8 50       47 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
92             {
93 8         39 $self->delete_crisis($picked_crisis->node1, $picked_crisis->node2);
94             }
95 8         35 return (\@original_hates, \@crises);
96             }
97             else
98             {
99 150         694 return (\@original_hates, \@original_crises);
100             }
101             }
102             sub create_or_escalate_crisis
103             {
104 18     18 0 22 my $self = shift;
105 18   50     35 my $node1 = shift || "";
106 18   50     36 my $node2 = shift || "";
107 18 100       43 if(my $crisis = $self->crisis_exists($node1, $node2))
108             {
109 8 100       21 if(! $crisis->is_max_crisis)
110             {
111 7         23 $crisis->escalate_crisis();
112 7         37 my $event = { code => 'crisisup',
113             text => "CRISIS BETWEEN $node1 AND $node2 ESCALATES",
114             involved => [$node1, $node2],
115             values => [ $crisis->get_crisis_level() ]
116             };
117 7         20 $self->broadcast_event($event, $node1, $node2);
118             }
119             }
120             else
121             {
122 10         34 $self->add_crisis($node1, $node2);
123 10         78 $self->broadcast_event( { code => 'crisisstart',
124             text => "CRISIS BETWEEN $node1 AND $node2 STARTED",
125             involved => [$node1, $node2] }, $node1, $node2);
126             }
127             }
128             sub cool_down
129             {
130 5     5 0 9 my $self = shift;
131 5         7 my $node1 = shift;
132 5         9 my $node2 = shift;
133 5 50       14 if(my $crisis = $self->crisis_exists($node1, $node2))
134             {
135 5         22 $crisis->cooldown_crisis();
136 5 100       15 if(! $crisis->is_crisis())
137             {
138 3         19 my $event = { code => 'crisisend',
139             text => "CRISIS BETWEEN $node1 AND $node2 ENDED",
140             involved => [$node1, $node2] };
141 3         11 $self->broadcast_event($event, $node1, $node2);
142             }
143             else
144             {
145 2         11 $self->broadcast_event({ code => 'crisisdown',
146             text => "CRISIS BETWEEN $node1 AND $node2 COOLED DOWN",
147             involved => [$node1, $node2],
148             values => [ $crisis->get_crisis_level() ]
149             }, $node1, $node2);
150             }
151             }
152             }
153              
154             sub print_all_crises
155             {
156 0     0 0   my $self = shift;
157 0           my $n = shift;
158 0   0       my $level = shift || 0;
159 0   0       my $mode = shift || 'print';
160 0           my @crises;
161             my @war_signal;
162 0           foreach my $b ($self->get_all_crises())
163             {
164 0 0 0       if(( ($n && $b->involve($n) || ! $n)) &&
      0        
165             ( $b->get_crisis_level > $level))
166             {
167 0           push @crises, $b;
168 0 0         if($self->war_exists($b->node1, $b->node2))
169             {
170 0           push @war_signal, 1;
171             }
172             else
173             {
174 0           push @war_signal, 0;
175             }
176             }
177             }
178 0           my %nation_codes = reverse %{$self->nation_codes};
  0            
179 0           return BalanceOfPower::Printer::print($mode, $self, 'print_all_crises',
180             { crises => \@crises,
181             wars => \@war_signal,
182             nation_codes => \%nation_codes,
183             });
184             }
185             1;
186