File Coverage

lib/BalanceOfPower/Role/Ruler.pm
Criterion Covered Total %
statement 73 99 73.7
branch 18 26 69.2
condition 3 7 42.8
subroutine 13 15 86.6
pod 0 8 0.0
total 107 155 69.0


line stmt bran cond sub pod time code
1             package BalanceOfPower::Role::Ruler;
2             $BalanceOfPower::Role::Ruler::VERSION = '0.400110';
3 13     13   4234 use strict;
  13         15  
  13         319  
4 13     13   44 use Moo::Role;
  13         16  
  13         59  
5              
6 13     13   2608 use BalanceOfPower::Constants ':all';
  13         16  
  13         5908  
7              
8 13     13   3554 use BalanceOfPower::Relations::Influence;
  13         25  
  13         10719  
9              
10             requires 'broadcast_event';
11             requires 'get_nation';
12              
13             has influences => (
14             is => 'ro',
15             default => sub { BalanceOfPower::Relations::RelPack->new() },
16             handles => { reset_influences => 'delete_link_for_node',
17             add_influence => 'add_link' }
18             );
19             sub influences_garbage_collector
20             {
21 47     47 0 49 my $self = shift;
22 47     6   230 $self->influences->garbage_collector(sub { my $rel = shift; return $rel->status == -1 });
  6         6  
  6         17  
23             }
24             sub is_under_influence
25             {
26 388     388 0 303 my $self = shift;
27 388         245 my $nation = shift;
28             my @rels = $self->influences->query(
29             sub {
30 42     42   29 my $rel = shift;
31 42 100       108 return 0 if($rel->node2 ne $nation);
32 9         13 return $rel->actual_influence()
33 388         1281 }, $nation);
34 388 100       795 if(@rels > 0)
35             {
36 9         21 return $rels[0]->start($nation);
37             }
38             else
39             {
40 379         857 return undef;
41             }
42             }
43             sub print_nation_situation
44             {
45 0     0 0 0 my $self = shift;
46 0         0 my $nation = shift;
47 0         0 my $domination = $self->is_under_influence($nation);
48 0 0       0 return "$nation is under control of $domination" if $domination;
49 0         0 my @influence = $self->has_influence($nation);
50 0 0       0 if(@influence > 0)
51             {
52 0         0 my $out = "";
53 0         0 for(@influence)
54             {
55 0         0 $out .= "$nation has influence on $_\n";
56             }
57 0         0 return $out;
58             }
59             else
60             {
61 0         0 return "$nation is free";
62             }
63              
64             }
65             sub has_influence
66             {
67 334     334 0 255 my $self = shift;
68 334         265 my $nation = shift;
69             my @influences = $self->influences->query(
70             sub {
71 28     28   17 my $rel = shift;
72 28 100       78 return 0 if($rel->node1 ne $nation);
73 10         22 return $rel->actual_influence()
74 334         1321 }, $nation);
75 334         706 my @out = ();
76 334         332 for(@influences)
77             {
78 10         21 push @out, $_->destination($nation);
79             }
80 334         390 return @out;
81             }
82             sub empire
83             {
84 145     145 0 111 my $self = shift;
85 145         108 my $n = shift;
86 145 100       197 if(my $domination = $self->is_under_influence($n))
87             {
88 3         4 my $dominator = $domination;
89 3         5 my @allies = $self->has_influence($dominator);
90 3         4 push @allies, $dominator;
91 3         8 return @allies;
92             }
93             else
94             {
95 142         197 my @allies = $self->has_influence($n);
96 142         146 push @allies, $n;
97 142         273 return @allies;
98             }
99             }
100             sub occupy
101             {
102 3     3 0 3 my $self = shift;
103 3         3 my $nation = shift;
104 3         4 my $occupiers = shift;
105 3         2 my $leader = shift;
106 3   100     12 my $internal_disorder = shift || 0;
107 3         8 $self->get_nation($nation)->occupation($self);
108 3         8 my $occupied_progress = $self->get_nation($nation)->progress;
109              
110 3         4 my @occupiers_array = @{$occupiers};
  3         5  
111 3         7 my $real_leader = $self->is_under_influence($leader);
112 3 50       6 if($real_leader)
113             {
114 0         0 @occupiers_array = grep { $_ ne $real_leader } @occupiers_array;
  0         0  
115 0         0 push @occupiers_array, $real_leader;
116 0         0 $leader = $real_leader;
117             }
118 3         3 foreach my $c (@occupiers_array)
119             {
120 3 50       6 if($c eq $leader)
121             {
122 3 100       57 $self->add_influence(BalanceOfPower::Relations::Influence->new( node1 => $c,
123             node2 => $nation,
124             status => 0,
125             next => $internal_disorder ? 2 : 1,
126             clock => 0 ));
127 3         10 $self->set_diplomacy($nation, $c, DOMINION_DIPLOMACY);
128 3         7 $self->copy_diplomacy($c, $nation);
129 3 100       9 if($self->get_nation($c)->progress < $occupied_progress)
130             {
131 1         3 $self->get_nation($c)->progress($occupied_progress);
132 1         15 $self->broadcast_event({ code => 'acquireprogress',
133             text => "$c ACQUIRES PROGRESS FROM $nation: $occupied_progress",
134             involved => [$c, $nation],
135             values => [$occupied_progress] }, $c, $nation);
136             }
137             }
138             else
139             {
140 0         0 $self->add_influence(BalanceOfPower::Relations::Influence->new( node1 => $c,
141             node2 => $nation,
142             status => 0,
143             clock => 0 ));
144 0         0 $self->set_diplomacy($nation, $c, DIPLOMACY_AFTER_OCCUPATION);
145 0         0 $self->get_nation($c)->grow();
146             }
147 3         20 $self->broadcast_event({code => 'occupy',
148             text => "$c OCCUPIES $nation",
149             involved => [$c, $nation] }, $c, $nation );
150             }
151             }
152             sub situation_clock
153             {
154 47     47 0 51 my $self = shift;
155 47         103 foreach my $i ($self->influences->all())
156             {
157 6         12 my $old_status = $i->status_label;
158 6         14 my $new_status = $i->click();
159 6 50 33     22 if($new_status && $old_status ne $new_status)
160             {
161 6 50       15 if($new_status eq 'dominate')
    100          
162             {
163 0         0 $self->broadcast_event({ code => 'dominate',
164             text => $i->node1 . " DOMINATES " . $i->node2,
165             involved => [$i->node1, $i->node2] }, $i->node1, $i->node2);
166             }
167             elsif($new_status eq 'control')
168             {
169 2         20 $self->broadcast_event({ code => 'control',
170             text => $i->node1 . " CONTROLS " . $i->node2,
171             involved => [$i->node1, $i->node2] }, $i->node1, $i->node2);
172             }
173             }
174             }
175 47         102 $self->influences_garbage_collector();
176             }
177              
178             sub print_influences
179             {
180 0     0 0   my $self = shift;
181 0           my $n = shift;
182 0   0       my $mode = shift || 'print';
183 0           my @inf = $self->influences->links_for_node($n);
184 0           @inf = sort { lc($a->node1) cmp lc($b->node1) } @inf;
  0            
185 0           return BalanceOfPower::Printer::print($mode, $self, 'print_influences',
186             { influences => \@inf } );
187             }
188              
189             1;