File Coverage

lib/BalanceOfPower/CivilWar.pm
Criterion Covered Total %
statement 63 102 61.7
branch 25 44 56.8
condition 3 7 42.8
subroutine 10 13 76.9
pod 0 9 0.0
total 101 175 57.7


line stmt bran cond sub pod time code
1             package BalanceOfPower::CivilWar;
2             $BalanceOfPower::CivilWar::VERSION = '0.400115';
3 13     13   58 use strict;
  13         31  
  13         338  
4 13     13   122 use v5.10;
  13         38  
5              
6 13     13   52 use Moo;
  13         15  
  13         80  
7 13     13   3153 use BalanceOfPower::Constants ':all';
  13         31  
  13         22226  
8              
9             with 'BalanceOfPower::Role::Reporter';
10              
11             has nation => (
12             is => 'rw',
13             );
14             has nation_to_load => (
15             is => 'rw',
16             );
17             has rebel_provinces => (
18             is => 'rw',
19             default => 0
20             );
21             has current_year => (
22             is => 'rw'
23             );
24             has start_date => (
25             is => 'ro',
26             default => ""
27             );
28             has end_date => (
29             is => 'rw',
30             default => ""
31             );
32              
33             sub name
34             {
35 47     47 0 68 my $self = shift;
36 47         1038 return "Civil war in " . $self->nation->name;
37             }
38             sub nation_name
39             {
40 48     48 0 64 my $self = shift;
41 48         261 return $self->nation->name;
42             }
43              
44             sub is_about
45             {
46 103     103 0 210 my $self = shift;
47 103         111 my $nation = shift;
48 103         666 return $self->nation->name eq $nation;
49             }
50              
51             sub fight
52             {
53 17     17 0 21 my $self = shift;
54 17         22 my $world = shift;
55 17         54 my $government = $world->random(0, 100, $self->name . ": government fight result");
56 17         61 my $rebels = $world->random(0, 100, $self->name . ": rebels fight result");
57 17         96 $self->nation->register_event("FIGHTING CIVIL WAR");
58 17 100       115 if($self->nation->army >= ARMY_UNIT_FOR_CIVIL_WAR)
59             {
60 2         14 $self->nation->add_army(-1 * ARMY_UNIT_FOR_CIVIL_WAR);
61 2         4 $government += ARMY_HELP_FOR_CIVIL_WAR;
62             }
63 17 50       180 if($self->nation->government eq 'dictatorship')
64             {
65 0         0 $government += DICTATORSHIP_BONUS_FOR_CIVIL_WAR;
66             }
67 17         23 my $reb_sup;
68             my $sup;
69 17 100       49 if($reb_sup = $world->rebel_supported($self->nation_name))
70             {
71 2         4 $rebels += REBEL_SUPPORT_HELP_FOR_CIVIL_WAR;
72             }
73 17 100       49 if($sup = $world->supported($self->nation_name))
74             {
75 2         14 $government += SUPPORT_HELP_FOR_CIVIL_WAR;
76             }
77 17 100       51 if($reb_sup)
78             {
79 2         6 $world->change_diplomacy($self->nation_name, $reb_sup->node1, -1 * DIPLOMACY_MALUS_FOR_REBEL_CIVIL_WAR_SUPPORT, uc($self->name));
80             }
81 17 100 100     61 if($sup && $reb_sup)
82             {
83 1         7 $world->change_diplomacy($sup->node1, $reb_sup->node1, -1 * DIPLOMACY_MALUS_FOR_CROSSED_CIVIL_WAR_SUPPORT, uc($self->name));
84             }
85 17 100       68 if($government > $rebels)
    100          
86             {
87 4 50       16 if($reb_sup)
88             {
89 0         0 $reb_sup->casualities(1);
90 0 0       0 $self->register_event("Rebel military support from " . $reb_sup->node1 . " destroyed") if($reb_sup->army == 0);
91 0         0 $world->rebel_military_support_garbage_collector();
92             }
93 4         21 return $self->battle('government', $world);
94             }
95             elsif($rebels > $government)
96             {
97 11 50       25 if($sup)
98             {
99 0         0 $sup->casualities(1);
100 0 0       0 $self->register_event("Military support from " . $sup->node1 . " destroyed") if($sup->army == 0);
101 0         0 $world->military_support_garbage_collector();
102             }
103 11         34 return $self->battle('rebels', $world);
104             }
105             else
106             {
107 2         7 return undef;
108             }
109             }
110             sub battle
111             {
112 15     15 0 22 my $self = shift;
113 15         19 my $battle_winner = shift;
114 15         19 my $world = shift;
115 15 100       50 if($battle_winner eq 'government')
    50          
116             {
117 4         46 $self->rebel_provinces($self->rebel_provinces() - .5);
118             }
119             elsif($battle_winner eq 'rebels')
120             {
121 11         40 $self->rebel_provinces($self->rebel_provinces() + .5);
122             }
123 15 50       138 if($self->rebel_provinces == 0)
    100          
124             {
125            
126 0         0 return 'government';
127             }
128             elsif($self->rebel_provinces == PRODUCTION_UNITS->[$self->nation->size])
129             {
130 2         8 return 'rebels';
131             }
132 13         39 return undef;
133             }
134              
135             sub win
136             {
137 2     2 0 4 my $self = shift;
138 2         4 my $winner = shift;
139 2         4 my $world = shift;
140 2 50       8 if($winner eq 'rebels')
    0          
141             {
142 2         12 $self->nation->new_government($world);
143 2         8 my $rebsup = $world->rebel_supported($self->nation_name);
144 2 50       10 if($rebsup)
145             {
146 0         0 my $rebel_supporter = $world->get_nation($rebsup->node1);
147 0 0       0 $world->stop_rebel_military_support($rebel_supporter, $self) if $rebel_supporter;
148 0         0 $world->diplomacy_exists($self->nation_name, $rebel_supporter->name)->factor(REBEL_SUPPORTER_WINNER_FRIENDSHIP);
149 0         0 $world->create_treaty($self->nation_name, $rebel_supporter->name, 'alliance');
150 0         0 $world->broadcast_event({ code => 'alliancetreatynew',
151             text => "ALLIANCE BETWEEN " . $self->nation_name . " AND " . $rebel_supporter->name,
152             involved => [$self->nation_name, $rebel_supporter->name],
153             values => ['rebsup'] }, $self->nation_name, $rebel_supporter->name);
154             }
155 2         14 $self->nation->internal_disorder(AFTER_CIVIL_WAR_INTERNAL_DISORDER);
156 2         18 $self->register_event("The rebels won the civil war");
157 2         10 $world->broadcast_event( { code => 'rebwincivil',
158             text => "THE REBELS IN " . $self->nation_name . " WON THE CIVIL WAR",
159             involved => [$self->nation_name] }, $self->nation_name );
160            
161 2         15 $world->empty_stocks($self->nation_name);
162 2         22 $self->nation->available_stocks(START_STOCKS->[$self->nation->size]);
163             }
164             elsif($winner eq 'government')
165             {
166 0           my $rebsup = $world->rebel_supported($self->nation_name);
167 0 0         if($rebsup)
168             {
169 0           my $rebel_supporter = $world->get_nation($rebsup->node1);
170 0 0         $world->stop_rebel_military_support($rebel_supporter, $self->nation) if $rebel_supporter;
171             }
172 0           $self->nation->internal_disorder(AFTER_CIVIL_WAR_INTERNAL_DISORDER);
173 0           $self->register_event("The government won the civil war");
174 0           $world->broadcast_event( { code => 'govwincivil',
175             text => "THE GOVERNMENT OF " . $self->nation_name . " WON THE CIVIL WAR",
176             involved => [$self->nation_name] }, $self->nation_name );
177             }
178             }
179             sub dump
180             {
181 0     0 0   my $self = shift;
182 0           my $io = shift;
183 0   0       my $indent = shift || "";
184 0   0       my $end_date = $self->end_date || "";
185 0           print {$io} $indent .
  0            
186             join(";", $self->nation_name, $self->rebel_provinces, $self->current_year, $self->start_date, $end_date) . "\n";
187 0           $self->dump_events($io, " " . $indent);
188             }
189             sub load
190             {
191 0     0 0   my $self = shift;
192 0           my $data = shift;
193 0           my $cw_line = ( split /\n/, $data )[0];
194 0           $cw_line =~ s/^\s+//;
195 0           chomp $cw_line;
196 0           my ($nation, $rebel_provinces, $current_year, $start_date, $end_date) = split ";", $cw_line;
197 0           $data =~ s/^.*?\n//;
198 0           my $events = $self->load_events($data);
199 0           return $self->new( nation_to_load => $nation, rebel_provinces => $rebel_provinces, current_year => $current_year, events => $events, start_date => $start_date, end_date => $end_date);
200             }
201             sub load_nation
202             {
203 0     0 0   my $self = shift;
204 0           my $world = shift;
205 0           $self->nation($world->get_nation($self->nation_to_load));
206             }
207              
208              
209              
210              
211             1;
212