File Coverage

lib/BalanceOfPower/Role/Supporter.pm
Criterion Covered Total %
statement 67 106 63.2
branch 11 24 45.8
condition 0 8 0.0
subroutine 11 16 68.7
pod 0 9 0.0
total 89 163 54.6


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::Supporter;
2             $BalanceOfPower::Role::Supporter::VERSION = '0.400115';
3 13     13   6014 use strict;
  13         27  
  13         374  
4 13     13   128 use v5.10;
  13         38  
5 13     13   61 use Moo::Role;
  13         19  
  13         86  
6              
7 13     13   7590 use BalanceOfPower::Relations::MilitarySupport;
  13         45  
  13         483  
8 13     13   85 use BalanceOfPower::Constants ':all';
  13         24  
  13         25525  
9              
10             requires 'get_nation';
11             requires 'war_report';
12             requires 'civil_war_report';
13              
14             has military_supports => (
15             is => 'ro',
16             default => sub { BalanceOfPower::Relations::RelPack->new() },
17             handles => { add_military_support => 'add_link',
18             delete_military_support => 'delete_link',
19             exists_military_support => 'exists_link',
20             already_in_military_support => 'first_link_for_node',
21             supports => 'links_for_node',
22             supporter => 'links_for_node1',
23             supported => 'first_link_for_node2',
24             reset_supports => 'delete_link_for_node'
25             }
26             );
27             has rebel_military_supports => (
28             is => 'ro',
29             default => sub { BalanceOfPower::Relations::RelPack->new() },
30             handles => { add_rebel_military_support => 'add_link',
31             delete_rebel_military_support => 'delete_link',
32             exists_rebel_military_support => 'exists_link',
33             rebel_supports => 'links_for_node',
34             rebel_supporter => 'links_for_node1',
35             rebel_supported => 'first_link_for_node2',
36             reset_rebel_supports => 'delete_link_for_node'
37             }
38             );
39              
40             sub start_military_support
41             {
42 7     7 0 16 my $self = shift;
43 7         12 my $nation1 = shift;
44 7         12 my $nation2 = shift;
45 7 50       42 return 0 if($nation1->army < ARMY_FOR_SUPPORT);
46 7         49 my $precedent_sup = $self->exists_military_support($nation1->name, $nation2->name);
47 7 50       30 if($precedent_sup)
48             {
49 0         0 $precedent_sup->casualities(-1 * ARMY_FOR_SUPPORT);
50 0         0 $self->broadcast_event( { code => 'supincreased',
51             text => "MILITARY SUPPORT TO " . $nation2->name . " INCREASED BY " . $nation1->name,
52             involved => [$nation1->name, $nation2->name] },
53             $nation1->name, $nation2->name);
54 0         0 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_INCREASING_SUPPORT, "INCREASED MILITARY SUPPORT FROM " . $nation1->name);
55 0         0 return 1;
56             }
57 7 50       45 if($self->supported($nation2->name))
58             {
59 0         0 $self->broadcast_event( { code => 'supfailed',
60             text => $nation2->name . " ALREADY SUPPORTED. MILITARY SUPPORT IMPOSSIBLE FOR " . $nation1->name,
61             involved => [$nation1->name, $nation2->name] }, $nation1->name, $nation2->name);
62 0         0 return 0;
63             }
64 7         37 my $supported = $self->supported($nation1->name);
65 7 50       44 if($supported)
66             {
67 0         0 $self->stop_military_support($self->get_nation($supported->node1), $self->get_nation($supported->node2));
68             }
69 7         39 $nation1->add_army(-1 * ARMY_FOR_SUPPORT);
70 7         123 $self->add_military_support(
71             BalanceOfPower::Relations::MilitarySupport->new(
72             node1 => $nation1->name,
73             node2 => $nation2->name,
74             army => ARMY_FOR_SUPPORT));
75 7         138 $self->broadcast_event({ code => 'supstarted',
76             text => "MILITARY SUPPORT TO " . $nation2->name . " STARTED BY " . $nation1->name,
77             involved => [$nation1->name, $nation2->name] },
78             $nation1->name, $nation2->name);
79 7         89 $self->war_report($nation1->name . " started military support for " . $nation2->name, $nation2->name);
80 7         79 $self->civil_war_report($nation1->name . " started military support for " . $nation2->name, $nation2->name);
81 7         79 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_STARTING_SUPPORT, "STARTED MILITARY SUPPORT FROM ".$nation1->name);
82             }
83             sub start_rebel_military_support
84             {
85 1     1 0 2 my $self = shift;
86 1         8 my $nation1 = shift;
87 1         2 my $nation2 = shift;
88 1 50       6 return 0 if($nation1->army < REBEL_ARMY_FOR_SUPPORT);
89 1         9 my $precedent_sup = $self->exists_rebel_military_support($nation1->name, $nation2->name);
90 1 50       4 if($precedent_sup)
91             {
92 0         0 $precedent_sup->casualities(-1 * ARMY_FOR_SUPPORT);
93 0         0 $self->broadcast_event({ code => 'rebsupincreased' ,
94             text => "REBEL MILITARY SUPPORT AGAINST " . $nation2->name . " INCREASED BY " . $nation1->name,
95             involved => [$nation1->name, $nation2->name] }, $nation1->name, $nation2->name);
96 0         0 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_INCREASING_REBEL_SUPPORT, "INCREASED REBEL MILITARY SUPPORT FROM " . $nation1->name);
97 0         0 return 1;
98             }
99 1 50       6 if($self->rebel_supported($nation2->name))
100             {
101 0         0 $self->broadcast_event({ code => 'rebsupfailed',
102             text => "REBELS IN " . $nation2->name . " ALREADY SUPPORTED. REBEL MILITARY SUPPORT IMPOSSIBILE FOR " . $nation1->name,
103             involved => [$nation1->name, $nation2->name] }, $nation1->name, $nation2->name);
104 0         0 return 0;
105             }
106 1         8 $nation1->add_army(-1 * ARMY_FOR_SUPPORT);
107 1         15 $self->add_rebel_military_support(
108             BalanceOfPower::Relations::MilitarySupport->new(
109             node1 => $nation1->name,
110             node2 => $nation2->name,
111             army => ARMY_FOR_SUPPORT));
112 1         19 $self->broadcast_event({ code => 'rebsupstarted',
113             text => "REBEL MILITARY SUPPORT AGAINST " . $nation2->name . " STARTED BY " . $nation1->name,
114             involved => [$nation1->name, $nation2->name] }, $nation1->name, $nation2->name);
115 1         14 $self->civil_war_report($nation1->name . " started rebel military support in " . $nation2->name, $nation2->name);
116 1         14 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_STARTING_REBEL_SUPPORT, "STARTED REBEL MILITARY SUPPORT FROM " . $nation1->name);
117             }
118             sub stop_military_support
119             {
120 7     7 0 10 my $self = shift;
121 7         11 my $node1 = shift;
122 7         9 my $node2 = shift;
123 7         8 my $avoid_diplomacy = shift;
124 7         36 my $milsup = $self->exists_military_support($node1->name, $node2->name);
125 7 100       66 return if (! $milsup);
126 1         9 $self->delete_military_support($node1->name, $node2->name);
127 1         5 $node1->add_army($milsup->army);
128 1         20 $self->broadcast_event( { code => 'supstopped',
129             text => "MILITARY SUPPORT FOR " . $node2->name . " STOPPED BY " . $node1->name,
130             involved => [$node1->name, $node2->name] }, $node1->name, $node2->name);
131 1         10 $self->war_report($node1->name . " stopped military support for " . $node2->name, $node2->name);
132 1         9 $self->civil_war_report($node1->name . " stopped military support for " . $node2->name, $node2->name);
133 1 50       4 if(! $avoid_diplomacy)
134             {
135 1         8 $self->change_diplomacy($node1->name, $node2->name, -1 * DIPLOMACY_FACTOR_BREAKING_SUPPORT, "STOPPED MILITARY SUPPORT FROM " . $node1->name);
136             }
137             }
138             sub stop_rebel_military_support
139             {
140 1     1 0 2 my $self = shift;
141 1         1 my $node1 = shift;
142 1         2 my $node2 = shift;
143 1         9 my $milsup = $self->exists_rebel_military_support($node1->name, $node2->name);
144 1 50       4 return if (! $milsup);
145 1         7 $self->delete_rebel_military_support($node1->name, $node2->name);
146 1         6 $node1->add_army($milsup->army);
147 1         12 $self->civil_war_report($node1->name . " stopped rebel military support in " . $node2->name, $node2->name);
148 1         18 $self->broadcast_event({ code => 'rebsupstopped',
149             text => "REBEL MILITARY SUPPORT AGAINST " . $node2->name . " STOPPED BY " . $node1->name,
150             involved => [$node1->name, $node2->name] }, $node1->name, $node2->name);
151             }
152             sub military_support_garbage_collector
153             {
154 22     22 0 27 my $self = shift;
155 22     8   182 $self->military_supports->garbage_collector(sub { my $rel = shift; return $rel->army <= 0 });
  8         12  
  8         41  
156             }
157             sub rebel_military_support_garbage_collector
158             {
159 0     0 0   my $self = shift;
160 0           for($self->rebel_military_supports->all)
161             {
162 0           say $_->node1 . " => " . $_->node2;
163 0 0         if(! $self->at_civil_war($_->node2))
164             {
165 0           $self->stop_rebel_military_support($self->get_nation($_->node1), $self->get_nation($_->node2));
166             }
167             }
168 0     0     $self->rebel_military_supports->garbage_collector(sub { my $rel = shift; return $rel->army <= 0 });
  0            
  0            
169             }
170              
171             sub print_military_supports
172             {
173 0     0 0   my $self = shift;
174 0           my $n = shift;
175 0   0       my $mode = shift || 'print';
176 0           return $self->print_supports($n, "MILITARY SUPPORTS", 0, $mode);
177             }
178             sub print_rebel_military_supports
179             {
180 0     0 0   my $self = shift;
181 0           my $n = shift;
182 0   0       my $mode = shift || 'print';
183 0           return $self->print_supports($n, "REBEL MILITARY SUPPORTS", 1, $mode);
184             }
185              
186              
187             sub print_supports
188             {
189 0     0 0   my $self = shift;
190 0           my $n = shift;
191 0   0       my $title = shift || "MILITARY SUPPORTS";
192 0           my $rebel = shift;
193 0   0       my $mode = shift || 'print';
194 0           my @sups;
195 0 0         if($rebel)
196             {
197 0           @sups = $self->rebel_military_supports->links_for_node($n);
198             }
199             else
200             {
201 0           @sups = $self->military_supports->links_for_node($n);
202             }
203 0           return BalanceOfPower::Printer::print($mode, $self, 'print_supports',
204             { title => $title,
205             supports => \@sups,
206             } );
207              
208             }
209             1;