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.400105';
3 13     13   4469 use strict;
  13         21  
  13         291  
4 13     13   99 use v5.10;
  13         33  
5 13     13   37 use Moo::Role;
  13         18  
  13         67  
6 13     13   2576 use Template;
  13         19  
  13         214  
7 13     13   38 use Term::ANSIColor;
  13         17  
  13         606  
8              
9 13     13   49 use BalanceOfPower::Constants ':all';
  13         21  
  13         5084  
10 13     13   65 use BalanceOfPower::Printer;
  13         13  
  13         8635  
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 54 my $self = shift;
22 46         160 my @crises = $self->get_all_crises();
23 46         70 my @hates = ();
24 46         151 foreach my $h ($self->get_hates())
25             {
26 74 100       213 push @hates, $h
27             if(! $self->crisis_exists($h->node1, $h->node2))
28             }
29 46         62 my $crises_to_use = \@crises;
30 46         60 my $hates_to_use = \@hates;
31 46         139 for(my $i = 0; $i < CRISIS_GENERATION_TRIES; $i++)
32             {
33 230         429 ($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 197 my $self = shift;
41 230   50     359 my $hates_to_use = shift || [] ;
42 230   50     311 my $crises_to_use = shift || [];
43 230         185 my @hates = $self->shuffle("Crisis generation: choosing hate", @{ $hates_to_use });
  230         3973  
44 230         253 my @crises = $self->shuffle("Crisis generation: choosing crisis", @{ $crises_to_use});
  230         4153  
45 230         320 my @original_hates = @hates;
46 230         214 my @original_crises = @crises;
47            
48 230         197 my $picked_hate = undef;
49 230         165 my $picked_crisis = undef;
50 230 100       401 if(@hates)
51             {
52 119         137 $picked_hate = shift @hates;
53             }
54 230 100       321 if(@crises)
55             {
56 60         71 $picked_crisis = shift @crises;
57             }
58            
59              
60 230         4206 my $action = $self->random(0, CRISIS_GENERATOR_NOACTION_TOKENS + 3, "Crisis action choose");
61 230 100       725 if($action == 0) #NEW CRISIS
    100          
    100          
    100          
62             {
63 22 100       100 return (\@original_hates, \@original_crises) if ! $picked_hate;
64 11 50       76 if(! $self->war_exists($picked_hate->node1, $picked_hate->node2))
65             {
66 11         61 $self->create_or_escalate_crisis($picked_hate->node1, $picked_hate->node2);
67 11         64 return (\@hates, \@original_crises);
68             }
69             }
70             elsif($action == 1) #ESCALATE
71             {
72 15 100       85 return (\@original_hates, \@original_crises) if ! $picked_crisis;
73 3 100       20 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
74             {
75 2         13 $self->create_or_escalate_crisis($picked_crisis->node1, $picked_crisis->node2);
76             }
77 3         20 return (\@original_hates, \@crises);
78             }
79             elsif($action == 2) #COOL DOWN
80             {
81 26 100       172 return (\@original_hates, \@original_crises) if ! $picked_crisis;
82 4 50       29 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
83             {
84 4         21 $self->cool_down($picked_crisis->node1, $picked_crisis->node2);
85             }
86 4         27 return (\@original_hates, \@crises);
87             }
88             elsif($action == 3) #ELIMINATE
89             {
90 13 100       63 return (\@original_hates, \@original_crises) if ! $picked_crisis;
91 4 50       27 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
92             {
93 4         28 $self->delete_crisis($picked_crisis->node1, $picked_crisis->node2);
94             }
95 4         23 return (\@original_hates, \@crises);
96             }
97             else
98             {
99 154         681 return (\@original_hates, \@original_crises);
100             }
101             }
102             sub create_or_escalate_crisis
103             {
104 13     13 0 19 my $self = shift;
105 13   50     43 my $node1 = shift || "";
106 13   50     29 my $node2 = shift || "";
107 13 100       45 if(my $crisis = $self->crisis_exists($node1, $node2))
108             {
109 2 50       7 if(! $crisis->is_max_crisis)
110             {
111 2         7 $crisis->escalate_crisis();
112 2         11 my $event = { code => 'crisisup',
113             text => "CRISIS BETWEEN $node1 AND $node2 ESCALATES",
114             involved => [$node1, $node2],
115             values => [ $crisis->get_crisis_level() ]
116             };
117 2         7 $self->broadcast_event($event, $node1, $node2);
118             }
119             }
120             else
121             {
122 11         37 $self->add_crisis($node1, $node2);
123 11         99 $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 4     4 0 6 my $self = shift;
131 4         6 my $node1 = shift;
132 4         5 my $node2 = shift;
133 4 50       12 if(my $crisis = $self->crisis_exists($node1, $node2))
134             {
135 4         15 $crisis->cooldown_crisis();
136 4 100       9 if(! $crisis->is_crisis())
137             {
138 3         17 my $event = { code => 'crisisend',
139             text => "CRISIS BETWEEN $node1 AND $node2 ENDED",
140             involved => [$node1, $node2] };
141 3         10 $self->broadcast_event($event, $node1, $node2);
142             }
143             else
144             {
145 1         5 $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