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

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 165 CC 13 SCAR 26.8
CC
13
Cov
0%
start_military_support
30.0
start_rebel_military_support
30.0
stop_military_support
12.0
CRAP 14.5
1 package BalanceOfPower::Role::Supporter;
2 $BalanceOfPower::Role::Supporter::VERSION = '0.300010';
3 use strict;
4 use v5.10;
5 use Moo::Role;
6  
7 use BalanceOfPower::Relations::MilitarySupport;
8 use BalanceOfPower::Constants ':all';
9  
10 requires 'get_nation';
11 requires 'at_civil_war';
12 requires '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 my $self = shift;
43 my $nation1 = shift;
44 my $nation2 = shift;
45 return 0 if($nation1->army < ARMY_FOR_SUPPORT);
46 my $precedent_sup = $self->exists_military_support($nation1->name, $nation2->name);
47 if($precedent_sup)
48 {
49 $precedent_sup->casualities(-1 * ARMY_FOR_SUPPORT);
50 $self->broadcast_event("MILITARY SUPPORT TO " . $nation2->name . " INCREASED BY " . $nation1->name, $nation1->name, $nation2->name);
51 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_INCREASING_SUPPORT);
52 return 1;
53 }
54 if($self->supported($nation2->name))
55 {
56 $self->broadcast_event($nation2->name . " ALREADY SUPPORTED. MILITARY SUPPORT IMPOSSIBILE FOR " . $nation1->name, $nation1->name, $nation2->name);
57 return 0;
58 }
59 my $supported = $self->supported($nation1->name);
60 if($supported)
61 {
62 $self->stop_military_support($self->get_nation($supported->node1), $self->get_nation($supported->node2));
63 }
64 $nation1->add_army(-1 * ARMY_FOR_SUPPORT);
65 $self->add_military_support(
66 BalanceOfPower::Relations::MilitarySupport->new(
67 node1 => $nation1->name,
68 node2 => $nation2->name,
69 army => ARMY_FOR_SUPPORT));
70 $self->broadcast_event("MILITARY SUPPORT TO " . $nation2->name . " STARTED BY " . $nation1->name, $nation1->name, $nation2->name);
71 $self->war_report($nation1->name . " started military support for " . $nation2->name, $nation2->name);
72 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_STARTING_SUPPORT);
73 }
74 sub start_rebel_military_support
75 {
76 my $self = shift;
77 my $nation1 = shift;
78 my $nation2 = shift;
79 return 0 if($nation1->army < REBEL_ARMY_FOR_SUPPORT);
80 return 0 if(! $self->at_civil_war($nation2->name));
81 my $precedent_sup = $self->exists_rebel_military_support($nation1->name, $nation2->name);
82 if($precedent_sup)
83 {
84 $precedent_sup->casualities(-1 * ARMY_FOR_SUPPORT);
85 $self->broadcast_event("REBEL MILITARY SUPPORT AGAINST " . $nation2->name . " INCREASED BY " . $nation1->name, $nation1->name, $nation2->name);
86 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_INCREASING_REBEL_SUPPORT);
87 return 1;
88 }
89 if($self->rebel_supported($nation2->name))
90 {
91 $self->broadcast_event("REBELS IN " . $nation2->name . " ALREADY SUPPORTED. REBEL MILITARY SUPPORT IMPOSSIBILE FOR " . $nation1->name, $nation1->name, $nation2->name);
92 return 0;
93 }
94 $nation1->add_army(-1 * ARMY_FOR_SUPPORT);
95 $self->add_rebel_military_support(
96 BalanceOfPower::Relations::MilitarySupport->new(
97 node1 => $nation1->name,
98 node2 => $nation2->name,
99 army => ARMY_FOR_SUPPORT));
100 $self->broadcast_event("REBEL MILITARY SUPPORT AGAINST " . $nation2->name . " STARTED BY " . $nation1->name, $nation1->name, $nation2->name);
101 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_STARTING_REBEL_SUPPORT);
102 }
103 sub stop_military_support
104 {
105 my $self = shift;
106 my $node1 = shift;
107 my $node2 = shift;
108 my $avoid_diplomacy = shift;
109 my $milsup = $self->exists_military_support($node1->name, $node2->name);
110 return if (! $milsup);
111 $self->delete_military_support($node1->name, $node2->name);
112 $node1->add_army($milsup->army);
113 $self->broadcast_event("MILITARY SUPPORT FOR " . $node2->name . " STOPPED BY " . $node1->name, $node1->name, $node2->name);
114 $self->war_report($node1->name . " stopped military support for " . $node2->name, $node2->name);
115 if(! $avoid_diplomacy)
116 {
117 $self->change_diplomacy($node1->name, $node2->name, -1 * DIPLOMACY_FACTOR_BREAKING_SUPPORT);
118 }
119 }
120 sub stop_rebel_military_support
121 {
122 my $self = shift;
123 my $node1 = shift;
124 my $node2 = shift;
125 my $milsup = $self->exists_rebel_military_support($node1->name, $node2->name);
126 return if (! $milsup);
127 $self->delete_rebel_military_support($node1->name, $node2->name);
128 $node1->add_army($milsup->army);
129 $self->broadcast_event("REBEL MILITARY SUPPORT AGAINST " . $node2->name . " STOPPED BY " . $node1->name, $node1->name, $node2->name);
130 }
131 sub military_support_garbage_collector
132 {
133 my $self = shift;
134 $self->military_supports->garbage_collector(sub { my $rel = shift; return $rel->army <= 0 });
135 }
136 sub rebel_military_support_garbage_collector
137 {
138 my $self = shift;
139 $self->rebel_military_supports->garbage_collector(sub { my $rel = shift; return $rel->army <= 0 });
140 }
141  
142 sub print_military_supports
143 {
144 my $self = shift;
145 my $n = shift;
146 my $mode = shift || 'print';
147 return $self->print_supports($n, "MILITARY SUPPORTS", 0, $mode);
148 }
149 sub print_rebel_military_supports
150 {
151 my $self = shift;
152 my $n = shift;
153 my $mode = shift || 'print';
154 return $self->print_supports($n, "REBEL MILITARY SUPPORTS", 1, $mode);
155 }
156  
157  
158 sub print_supports
159 {
160 my $self = shift;
161 my $n = shift;
162 my $title = shift || "MILITARY SUPPORTS";
163 my $rebel = shift;
164 my $mode = shift || 'print';
165 my @sups;
166 if($rebel)
167 {
168 @sups = $self->rebel_military_supports->links_for_node($n);
169 }
170 else
171 {
172 @sups = $self->military_supports->links_for_node($n);
173 }
174 return BalanceOfPower::Printer::print($mode, $self, 'print_supports',
175 { title => $title,
176 supports => \@sups,
177 } );
178  
179 }
180 1;