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.400110';
3 13     13   4278 use strict;
  13         19  
  13         302  
4 13     13   102 use v5.10;
  13         29  
5 13     13   38 use Moo::Role;
  13         17  
  13         62  
6              
7 13     13   5977 use BalanceOfPower::CivilWar;
  13         22  
  13         359  
8 13     13   65 use BalanceOfPower::Constants ':all';
  13         16  
  13         12388  
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 671     671 0 554 my $self = shift;
30 671         479 my $nation = shift;
31 671         455 for(@{$self->civil_wars})
  671         1142  
32             {
33 102 100       199 return $_ if $_->is_about($nation);
34             }
35 614         990 return undef;
36             }
37              
38             sub start_civil_war
39             {
40 5     5 0 9 my $self = shift;
41 5         6 my $nation = shift;
42            
43 5         17 my $rebel_provinces = STARTING_REBEL_PROVINCES->[$nation->size];
44            
45 5         44 my $civwar = BalanceOfPower::CivilWar->new(nation => $nation,
46             rebel_provinces => $rebel_provinces,
47             start_date => $nation->current_year);
48 5         82 $civwar->register_event("Starting army: " . $nation->army);
49 5         31 my $sup = $self->supported($nation->name);
50 5 50       16 if($sup)
51             {
52 0         0 $civwar->register_event("Support to government from " . $sup->node1);
53             }
54 5         49 $self->broadcast_event({ code => "civiloutbreak",
55             text => "CIVIL WAR OUTBREAK IN " . $nation->name,
56             involved => [$nation->name] }, $nation->name);
57 5         43 $self->war_report("Civil war in " . $nation->name . "!", $nation->name);
58 5         30 my $occupied = $self->lose_war($nation->name, 1);
59 5 50       14 if(! $occupied)
60             {
61 5         16 $self->add_civil_war($civwar);
62             }
63             }
64              
65             sub add_civil_war
66             {
67 5     5 0 15 my $self = shift;
68 5         7 my $civwar = shift;
69 5         27 my $already = $self->get_civil_war($civwar->nation->name);
70 5 50       15 if($already)
71             {
72 0         0 say "ERROR: Civil war in " . $civwar->nation->name . " already present!";
73             }
74             else
75             {
76 5         5 push @{$self->civil_wars}, $civwar;
  5         24  
77             }
78             }
79             sub delete_civil_war
80             {
81 2     2 0 2 my $self = shift;
82 2         3 my $nation = shift;
83 2         4 my $cw = $self->get_civil_war($nation);
84 2 50       6 if($cw)
85             {
86 2         9 $cw->end_date($self->current_year);
87            
88 2         2 push @{$self->civil_memorial}, $cw;
  2         6  
89             }
90 2         3 my @civwars = grep { ! $_->is_about($nation) } @{$self->civil_wars};
  2         4  
  2         4  
91 2         8 $self->civil_wars(\@civwars);
92             }
93             sub civil_war_current_year
94             {
95 46     46 0 57 my $self = shift;
96 46         51 for(@{$self->civil_wars})
  46         117  
97             {
98 16         55 $_->current_year($self->current_year);
99             }
100             }
101              
102             sub at_civil_war
103             {
104 472     472 0 370 my $self = shift;
105 472         349 my $n = shift;
106 472 100       701 if($self->get_civil_war($n))
107             {
108 32         86 return 1;
109             }
110             else
111             {
112 440         1194 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