blib/lib/BalanceOfPower/Role/CrisisManager.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 165 CC 22 SCAR 48.0
CC
22
Cov
0%
crisis_generator_round
240.0
create_or_escalate_crisis
20.0
cool_down
12.0
CRAP 121.0
1 package BalanceOfPower::Role::CrisisManager;
2 $BalanceOfPower::Role::CrisisManager::VERSION = '0.200000';
3 use strict;
4 use Moo::Role;
5 use Term::ANSIColor;
6  
7 use BalanceOfPower::Constants ':all';
8 use BalanceOfPower::Utils qw(as_title);
9  
10 requires 'get_all_crises';
11 requires 'get_hates';
12 requires 'crisis_exists';
13 requires 'war_exists';
14 requires 'broadcast_event';
15 requires 'add_crisis';
16  
17 sub crisis_generator
18 {
19 my $self = shift;
20 my @crises = $self->get_all_crises();
21 my @hates = ();
22 foreach my $h ($self->get_hates())
23 {
24 push @hates, $h
25 if(! $self->crisis_exists($h->node1, $h->node2))
26 }
27 my $crises_to_use = \@crises;
28 my $hates_to_use = \@hates;
29 for(my $i = 0; $i < CRISIS_GENERATION_TRIES; $i++)
30 {
31 ($hates_to_use, $crises_to_use) = $self->crisis_generator_round($hates_to_use, $crises_to_use);
32 }
33  
34 }
35  
36 sub crisis_generator_round
37 {
38 my $self = shift;
39 my $hates_to_use = shift || [] ;
40 my $crises_to_use = shift || [];
41 my @hates = $self->shuffle("Crisis generation: choosing hate", @{ $hates_to_use });
42 my @crises = $self->shuffle("Crisis generation: choosing crisis", @{ $crises_to_use});
43 my @original_hates = @hates;
44 my @original_crises = @crises;
45
46 my $picked_hate = undef;
47 my $picked_crisis = undef;
48 if(@hates)
49 {
50 $picked_hate = shift @hates;
51 }
52 if(@crises)
53 {
54 $picked_crisis = shift @crises;
55 }
56
57  
58 my $action = $self->random(0, CRISIS_GENERATOR_NOACTION_TOKENS + 3, "Crisis action choose");
59 if($action == 0) #NEW CRISIS
60 {
61 return (\@original_hates, \@original_crises) if ! $picked_hate;
62 if(! $self->war_exists($picked_hate->node1, $picked_hate->node2))
63 {
64 $self->create_or_escalate_crisis($picked_hate->node1, $picked_hate->node2);
65 return (\@hates, \@original_crises);
66 }
67 }
68 elsif($action == 1) #ESCALATE
69 {
70 return (\@original_hates, \@original_crises) if ! $picked_crisis;
71 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
72 {
73 $self->create_or_escalate_crisis($picked_crisis->node1, $picked_crisis->node2);
74 }
75 return (\@original_hates, \@crises);
76 }
77 elsif($action == 2) #COOL DOWN
78 {
79 return (\@original_hates, \@original_crises) if ! $picked_crisis;
80 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
81 {
82 $self->cool_down($picked_crisis->node1, $picked_crisis->node2);
83 }
84 return (\@original_hates, \@crises);
85 }
86 elsif($action == 3) #ELIMINATE
87 {
88 return (\@original_hates, \@original_crises) if ! $picked_crisis;
89 if(! $self->war_exists($picked_crisis->node1, $picked_crisis->node2))
90 {
91 $self->delete_crisis($picked_crisis->node1, $picked_crisis->node2);
92 }
93 return (\@original_hates, \@crises);
94 }
95 else
96 {
97 return (\@original_hates, \@original_crises);
98 }
99 }
100 sub create_or_escalate_crisis
101 {
102 my $self = shift;
103 my $node1 = shift || "";
104 my $node2 = shift || "";
105 if(my $crisis = $self->crisis_exists($node1, $node2))
106 {
107 if(! $crisis->is_max_crisis)
108 {
109 $crisis->escalate_crisis();
110 my $event = "CRISIS BETWEEN $node1 AND $node2 ESCALATES";
111 if($crisis->is_max_crisis)
112 {
113 $event .= " TO MAX LEVEL";
114 }
115 $self->broadcast_event($event, $node1, $node2);
116 }
117 }
118 else
119 {
120 $self->add_crisis($node1, $node2);
121 $self->broadcast_event("CRISIS BETWEEN $node1 AND $node2 STARTED", $node1, $node2);
122 }
123 }
124 sub cool_down
125 {
126 my $self = shift;
127 my $node1 = shift;
128 my $node2 = shift;
129 if(my $crisis = $self->crisis_exists($node1, $node2))
130 {
131 $crisis->cooldown_crisis();
132 if(! $crisis->is_crisis())
133 {
134 my $event = "CRISIS BETWEEN $node1 AND $node2 ENDED";
135 $self->broadcast_event($event, $node1, $node2);
136 }
137 else
138 {
139 $self->broadcast_event("CRISIS BETWEEN $node1 AND $node2 COOLED DOWN", $node1, $node2);
140 }
141 }
142 }
143  
144 sub print_all_crises
145 {
146 my $self = shift;
147 my $n = shift;
148 my $out;
149 $out .= as_title("CRISES\n===\n");
150 foreach my $b ($self->get_all_crises())
151 {
152 if($self->war_exists($b->node1, $b->node2))
153 {
154 $out .= color("red bold") . $b->print_crisis() . color("reset") . "\n";
155 }
156 else
157 {
158 $out .= $b->print_crisis() . "\n";
159 }
160 }
161 return $out;
162 }
163  
164 1;
165