File Coverage

lib/BalanceOfPower/Role/Rebel.pm
Criterion Covered Total %
statement 58 83 69.8
branch 8 16 50.0
condition n/a
subroutine 11 13 84.6
pod 0 8 0.0
total 77 120 64.1


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::Rebel;
2             $BalanceOfPower::Role::Rebel::VERSION = '0.400115';
3 13     13   6479 use strict;
  13         27  
  13         407  
4 13     13   158 use v5.10;
  13         43  
5 13     13   61 use Moo::Role;
  13         21  
  13         102  
6              
7 13     13   8046 use BalanceOfPower::CivilWar;
  13         35  
  13         523  
8 13     13   96 use BalanceOfPower::Constants ':all';
  13         22  
  13         17166  
9              
10             requires 'broadcast_event';
11             requires 'war_report';
12             requires 'lose_war';
13             requires 'supported';
14              
15              
16             has civil_wars => (
17             is => 'rw',
18             default => sub { [] }
19             );
20              
21             has civil_memorial => (
22             is => 'rw',
23             default => sub { [] }
24             );
25              
26              
27             sub get_civil_war
28             {
29 704     704 0 728 my $self = shift;
30 704         664 my $nation = shift;
31 704         669 for(@{$self->civil_wars})
  704         1564  
32             {
33 101 100       285 return $_ if $_->is_about($nation);
34             }
35 647         1334 return undef;
36             }
37              
38             sub start_civil_war
39             {
40 5     5 0 13 my $self = shift;
41 5         9 my $nation = shift;
42            
43 5         27 my $rebel_provinces = STARTING_REBEL_PROVINCES->[$nation->size];
44            
45 5         66 my $civwar = BalanceOfPower::CivilWar->new(nation => $nation,
46             rebel_provinces => $rebel_provinces,
47             start_date => $nation->current_year);
48 5         121 $civwar->register_event("Starting army: " . $nation->army);
49 5         47 my $sup = $self->supported($nation->name);
50 5 50       36 if($sup)
51             {
52 0         0 $civwar->register_event("Support to government from " . $sup->node1);
53             }
54 5         110 $self->broadcast_event({ code => "civiloutbreak",
55             text => "CIVIL WAR OUTBREAK IN " . $nation->name,
56             involved => [$nation->name] }, $nation->name);
57 5         76 $self->war_report("Civil war in " . $nation->name . "!", $nation->name);
58 5         47 my $occupied = $self->lose_war($nation->name, 1);
59 5 50       24 if(! $occupied)
60             {
61 5         23 $self->add_civil_war($civwar);
62             }
63             }
64              
65             sub add_civil_war
66             {
67 5     5 0 12 my $self = shift;
68 5         7 my $civwar = shift;
69 5         42 my $already = $self->get_civil_war($civwar->nation->name);
70 5 50       32 if($already)
71             {
72 0         0 say "ERROR: Civil war in " . $civwar->nation->name . " already present!";
73             }
74             else
75             {
76 5         21 push @{$self->civil_wars}, $civwar;
  5         32  
77             }
78             }
79             sub delete_civil_war
80             {
81 2     2 0 4 my $self = shift;
82 2         3 my $nation = shift;
83 2         7 my $cw = $self->get_civil_war($nation);
84 2 50       10 if($cw)
85             {
86 2         21 $cw->end_date($self->current_year);
87            
88 2         2 push @{$self->civil_memorial}, $cw;
  2         9  
89             }
90 2         5 my @civwars = grep { ! $_->is_about($nation) } @{$self->civil_wars};
  2         6  
  2         7  
91 2         49 $self->civil_wars(\@civwars);
92             }
93             sub civil_war_current_year
94             {
95 46     46 0 74 my $self = shift;
96 46         62 for(@{$self->civil_wars})
  46         189  
97             {
98 16         130 $_->current_year($self->current_year);
99             }
100             }
101              
102             sub at_civil_war
103             {
104 505     505 0 525 my $self = shift;
105 505         514 my $n = shift;
106 505 100       932 if($self->get_civil_war($n))
107             {
108 32         124 return 1;
109             }
110             else
111             {
112 473         1819 return 0;
113             }
114             }
115              
116             sub dump_civil_memorial
117             {
118 0     0 0   my $self = shift;
119 0           my $io = shift;
120 0           my $indent = shift;
121 0           foreach my $cw (@{$self->civil_memorial})
  0            
122             {
123 0           print {$io} $cw->dump($io, $indent);
  0            
124             }
125             }
126             sub load_civil_memorial
127             {
128 0     0 0   my $self = shift;
129 0           my $data = shift;
130            
131 0           $data .= "EOF";
132 0           my $war_data = "";
133 0           my @memorial;
134 0           my @lines = split "\n", $data;
135 0           foreach my $l (@lines)
136             {
137 0 0         if($l !~ /^\s/)
138             {
139 0 0         if($war_data)
140             {
141 0           my $cw = BalanceOfPower::CivilWar->load($war_data);
142 0           $cw->load_nation($self);
143 0           push @memorial, $cw;
144 0           $war_data = $l . "\n";
145             }
146             else
147             {
148 0           $war_data = $l . "\n";
149             }
150             }
151             else
152             {
153 0           $war_data .= $l . "\n";
154             }
155             }
156 0           return \@memorial;
157             }
158              
159              
160             1;
161              
162