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

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 131 CC 12 SCAR 28.0
CC
12
Cov
0%
start_military_support
30.0
start_rebel_military_support
30.0
stop_military_support
12.0
CRAP 16.5
1 package BalanceOfPower::Role::Supporter;
2 $BalanceOfPower::Role::Supporter::VERSION = '0.200000';
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 print_military_supports => 'print_links',
25 reset_supports => 'delete_link_for_node'
26 }
27 );
28 has rebel_military_supports => (
29 is => 'ro',
30 default => sub { BalanceOfPower::Relations::RelPack->new() },
31 handles => { add_rebel_military_support => 'add_link',
32 delete_rebel_military_support => 'delete_link',
33 exists_rebel_military_support => 'exists_link',
34 rebel_supports => 'links_for_node',
35 rebel_supporter => 'links_for_node1',
36 rebel_supported => 'first_link_for_node2',
37 print_rebel_military_supports => 'print_links',
38 reset_rebel_supports => 'delete_link_for_node'
39 }
40 );
41  
42 sub start_military_support
43 {
44 my $self = shift;
45 my $nation1 = shift;
46 my $nation2 = shift;
47 return 0 if($nation1->army < ARMY_FOR_SUPPORT);
48 my $precedent_sup = $self->exists_military_support($nation1->name, $nation2->name);
49 if($precedent_sup)
50 {
51 $precedent_sup->casualities(-1 * ARMY_FOR_SUPPORT);
52 $self->broadcast_event("MILITARY SUPPORT TO " . $nation2->name . " INCREASED BY " . $nation1->name, $nation1->name, $nation2->name);
53 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_INCREASING_SUPPORT);
54 return 1;
55 }
56 if($self->supported($nation2->name))
57 {
58 $self->broadcast_event($nation2->name . " ALREADY SUPPORTED. MILITARY SUPPORT IMPOSSIBILE FOR " . $nation1->name, $nation1->name, $nation2->name);
59 return 0;
60 }
61 my $supported = $self->supported($nation1->name);
62 if($supported)
63 {
64 $self->stop_military_support($self->get_nation($supported->node1), $self->get_nation($supported->node2));
65 }
66 $nation1->add_army(-1 * ARMY_FOR_SUPPORT);
67 $self->add_military_support(
68 BalanceOfPower::Relations::MilitarySupport->new(
69 node1 => $nation1->name,
70 node2 => $nation2->name,
71 army => ARMY_FOR_SUPPORT));
72 $self->broadcast_event("MILITARY SUPPORT TO " . $nation2->name . " STARTED BY " . $nation1->name, $nation1->name, $nation2->name);
73 $self->war_report($nation1->name . " started military support for " . $nation2->name, $nation2->name);
74 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_STARTING_SUPPORT);
75 }
76 sub start_rebel_military_support
77 {
78 my $self = shift;
79 my $nation1 = shift;
80 my $nation2 = shift;
81 return 0 if($nation1->army < REBEL_ARMY_FOR_SUPPORT);
82 return 0 if(! $self->at_civil_war($nation2->name));
83 my $precedent_sup = $self->exists_rebel_military_support($nation1->name, $nation2->name);
84 if($precedent_sup)
85 {
86 $precedent_sup->casualities(-1 * ARMY_FOR_SUPPORT);
87 $self->broadcast_event("REBEL MILITARY SUPPORT AGAINST " . $nation2->name . " INCREASED BY " . $nation1->name, $nation1->name, $nation2->name);
88 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_INCREASING_REBEL_SUPPORT);
89 return 1;
90 }
91 if($self->rebel_supported($nation2->name))
92 {
93 $self->broadcast_event("REBELS IN " . $nation2->name . " ALREADY SUPPORTED. REBEL MILITARY SUPPORT IMPOSSIBILE FOR " . $nation1->name, $nation1->name, $nation2->name);
94 return 0;
95 }
96 $nation1->add_army(-1 * ARMY_FOR_SUPPORT);
97 $self->add_rebel_military_support(
98 BalanceOfPower::Relations::MilitarySupport->new(
99 node1 => $nation1->name,
100 node2 => $nation2->name,
101 army => ARMY_FOR_SUPPORT));
102 $self->broadcast_event("REBEL MILITARY SUPPORT AGAINST " . $nation2->name . " STARTED BY " . $nation1->name, $nation1->name, $nation2->name);
103 $self->change_diplomacy($nation1->name, $nation2->name, DIPLOMACY_FACTOR_STARTING_REBEL_SUPPORT);
104 }
105 sub stop_military_support
106 {
107 my $self = shift;
108 my $node1 = shift;
109 my $node2 = shift;
110 my $avoid_diplomacy = shift;
111 my $milsup = $self->exists_military_support($node1->name, $node2->name);
112 return if (! $milsup);
113 $self->delete_military_support($node1->name, $node2->name);
114 $node1->add_army($milsup->army);
115 $self->broadcast_event("MILITARY SUPPORT FOR " . $node2->name . " STOPPED BY " . $node1->name, $node1->name, $node2->name);
116 $self->war_report($node1->name . " stopped military support for " . $node2->name, $node2->name);
117 if(! $avoid_diplomacy)
118 {
119 $self->change_diplomacy($node1->name, $node2->name, -1 * DIPLOMACY_FACTOR_BREAKING_SUPPORT);
120 }
121 }
122 sub stop_rebel_military_support
123 {
124 my $self = shift;
125 my $node1 = shift;
126 my $node2 = shift;
127 my $milsup = $self->exists_rebel_military_support($node1->name, $node2->name);
128 return if (! $milsup);
129 $self->delete_rebel_military_support($node1->name, $node2->name);
130 $node1->add_army($milsup->army);
131 $self->broadcast_event("REBEL MILITARY SUPPORT AGAINST " . $node2->name . " STOPPED BY " . $node1->name, $node1->name, $node2->name);
132 }
133 sub military_support_garbage_collector
134 {
135 my $self = shift;
136 $self->military_supports->garbage_collector(sub { my $rel = shift; return $rel->army <= 0 });
137 }
138 sub rebel_military_support_garbage_collector
139 {
140 my $self = shift;
141 $self->rebel_military_supports->garbage_collector(sub { my $rel = shift; return $rel->army <= 0 });
142 }
143  
144 1;