lib/BalanceOfPower/Role/Diplomat.pm

filter: Statementstmt 96.3% 239 / 248 Branchbran 86.9% 40 / 46 Conditioncond 80.0% 20 / 25 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 97.4% 38 / 39 Podpod 0.0% 0 / 29 totaltotal 85.7% 344 / 401 CC 45 SCAR 11.9
CC
45
Cov
94%
get_hates
7.0
change_diplomacy
6.0
crisis_exists
5.5
CRAP 3.3
« lib/BalanceOfPower/Commands/DeclareWar.pm ↑ Summary lib/BalanceOfPower/Commands/TargetRoute.pm »
1 package BalanceOfPower::Role::Diplomat;
2 11 $BalanceOfPower::Role::Diplomat::VERSION = '0.200000';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use BalanceOfPower::Constants ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use BalanceOfPower::Relations::Friendship;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use BalanceOfPower::Relations::Treaty;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use BalanceOfPower::Relations::RelPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 has diplomatic_relations => (
14 is => 'ro',
15 14 ▶ default => sub { BalanceOfPower::Relations::RelPack->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 11 handles => { add_diplomacy => 'add_link',
17 _diplomacy_exists => 'exists_link',
18 update_diplomacy => 'update_link',
19 get_diplomatic_relations => 'links_for_node' }
20 );
21 has treaties => (
22 is => 'ro',
23 14 ▶ default => sub { BalanceOfPower::Relations::RelPack->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 11 handles => { add_treaty => 'add_link',
25 print_treaties => 'print_links',
26 exists_treaty => 'exists_link',
27 get_treaties_for_nation => 'links_for_node',
28 reset_treaties => 'delete_link_for_node',
29 delete_treaty => 'delete_link', }
30 );
31  
32 11 requires 'random';
33 11 requires 'distance';
34 11 requires 'border_exists';
35 11 requires 'broadcast_event';
36 11 requires 'is_under_influence';
37  
38 sub init_diplomacy
39 {
40 13 ▶ my $self = shift;
Subroutine init_diplomacy: called CC=5 SCAR=16.1
Pod undocumented
41 13 my @nations = @{$self->nation_names};
42 13 foreach my $n1 (@nations)
43 {
44 60 foreach my $n2 (@nations)
45 {
46 292 ▶ if($n1 ne $n2 && ! $self->_diplomacy_exists($n1, $n2))
Branch: 1 branch
BranchTrueFalseTotal
if ($n1 ne $n2 and not $self->_diplomacy_exists($n1, $n2)) 116 176 292
Condition100%3/3
$n1 ne $n2 and not $self->_diplomacy_exists($n1, $n2)
!ll&&!rl&&r
60116116
MC/DC100%2/2
$n1 ne $n2 and not $self->_diplomacy_exists($n1, $n2)
$n1 ne $n2 not $self->_diplomacy_exists($n1, $n2)
Truth table100%3/3
A and not(B)
A= $n1 ne $n2B= not $self->_diplomacy_exists($n1, $n2)
ABresult
0X0
100
111
47 {
48 116 my $minimum_friendship = 0;
49 116 my $rel = BalanceOfPower::Relations::Friendship->new( node1 => $n1,
50 node2 => $n2,
51 factor => $self->calculate_random_friendship($n1, $n2));
52 116 $self->add_diplomacy($rel);
53 }
54 }
55 }
56 }
57  
58 #Random friendship is function of the distance
59 sub calculate_random_friendship
60 {
61 116 ▶ my $self = shift;
Subroutine calculate_random_friendship: called CC=3 SCAR=11.0
Pod undocumented
62 116 my $nation1 = shift;
63 116 my $nation2 = shift;
64 116 my $distance = $self->distance($nation1, $nation2);
65 116 ▶ $distance = 3 if $distance > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $distance > 3 47 69 116
66  
67 116 my $middle = 50;
68  
69 116 my $polar_factor = ( 4 - $distance ) * 5;
70 116 my $random_floor = ( ( 3 - $distance ) * 5 ) + 25;
71  
72 116 my $side = $self->random(0, 1, "Side for friendship between $nation1 and $nation2");
73 116 ▶ $side = $side == 0 ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$side == 0 ? : 58 58 116
74 116 my $random_factor = $self->random(0, $random_floor, "Random factor for friendship between $nation1 and $nation2 [floor: $random_floor]");
75  
76 116 my $friendship = $middle + ( $side * ( $polar_factor + $random_factor ) );
77 116 return $friendship;
78 }
79  
80  
81 sub init_random_alliances
82 {
83 1 ▶ my $self = shift;
Subroutine init_random_alliances: called CC=3 SCAR=11.0
Pod undocumented
84 1 my @nations = @{$self->nation_names};
85 1 ▶ for(my $i = 0; $i < STARTING_ALLIANCES; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 7) 7 1 8
86 {
87 7 my $n1 = $nations[$self->random(0, $#nations, "Nation1 for random alliance")];
88 7 my $n2 = $nations[$self->random(0, $#nations, "Nation2 for random alliance")];
89 7 ▶ if($n1 ne $n2)
Branch: 1 branch
BranchTrueFalseTotal
if ($n1 ne $n2) 6 1 7
90 {
91 6 $self->add_alliance($n1, $n2);
92 6 $self->broadcast_event("ALLIANCE BETWEEN $n1 AND $n2 CREATED", $n1, $n2);
93 }
94 }
95 }
96 sub reroll_diplomacy
97 {
98 2 ▶ my $self = shift;
Subroutine reroll_diplomacy: called CC=2 SCAR=7.1
Pod undocumented
99 2 my $nation = shift;
100 2 my @rels = $self->get_diplomatic_relations($nation);
101 2 for(@rels)
102 {
103 0 $_->factor($self->random(0 ,100, "Reroll diplomacy for " . $_->node1 . ", " . $_->node2));
104 }
105 }
106  
107 sub diplomacy_exists
108 {
109 1034 ▶ my $self = shift;
Subroutine diplomacy_exists: called CC=2 SCAR=7.1
Pod undocumented
110 1034 my $n1 = shift;
111 1034 my $n2 = shift;
112 1034 my $r = $self->_diplomacy_exists($n1, $n2);
113 1034 ▶ if(! defined $r)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r) 0 1034 1034
114 {
115 0 say "ERROR! $n1, $n2";
116 }
117 1034 return $r;
118 }
119  
120 sub get_hates
121 {
122 42 ▶ my $self = shift;
Subroutine get_hates: called CC=7 SCAR=19.5
Pod undocumented
123 42 my $nation = shift;
124 42 ▶ my @hates = $self->diplomatic_relations->query( sub { my $rel = shift; return $rel->status eq 'HATE' });
Subroutine __ANON__: called CC=1 SCAR=0.0
125 42 my @out = ();
126 42 foreach my $r (@hates)
127 {
128 99 ▶ if(($nation && $r->has_node($nation)) || (! $nation))
Branch: 1 branch
BranchTrueFalseTotal
if ($nation and $r->has_node($nation) or not $nation) 95 4 99
Condition100%6/6
$nation and $r->has_node($nation) or not $nation
l!l&&r!l&&!r
4914
$nation and $r->has_node($nation)
!ll&&!rl&&r
9144
MC/DC33%1/3
$nation and $r->has_node($nation) or not $nation
$nation $r->has_node($nation) not $nation
Truth table60%3/5
A and B or not(C)
A= $nationB= $r->has_node($nation)C= not $nation
ABCresult
11X1
0X11
1011
0X00
1000
129 {
130 95 ▶ if(! $self->is_under_influence($r->node1) && ! $self->is_under_influence($r->node2))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_under_influence($r->node1) or $self->is_under_influence($r->node2)) 88 7 95
Condition100%3/3
$self->is_under_influence($r->node1) or $self->is_under_influence($r->node2)
l!l&&r!l&&!r
3488
MC/DC100%2/2
$self->is_under_influence($r->node1) or $self->is_under_influence($r->node2)
$self->is_under_influence($r->node1) $self->is_under_influence($r->node2)
Truth table100%3/3
A or B
A= $self->is_under_influence($r->node1)B= $self->is_under_influence($r->node2)
ABresult
1X1
011
000
131 {
132 88 push @out, $r;
133 }
134 }
135 }
136 42 return @out;
137 }
138 sub get_nations_with_status
139 {
140 22 ▶ my $self = shift;
Subroutine get_nations_with_status: called CC=3 SCAR=11.0
Pod undocumented
141 22 my $nation = shift;
142 22 my $status = shift;
143 22 my @st_array = @{$status};
144 22 my @relations = $self->get_diplomatic_relations($nation);
145 22 my @out = ();
146 22 for(@relations)
147 {
148 88 my $r = $_;
149 88 ▶ if(grep{ $_ eq $r->status } @st_array)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $r->status;} @st_array) 24 64 88
150 {
151 24 push @out, $r->destination($nation);
152 }
153 }
154 22 return @out;
155 }
156  
157 sub get_friends
158 {
159 15 ▶ my $self = shift;
Subroutine get_friends: called CC=1 SCAR=0.0
Pod undocumented
160 15 my $nation = shift;
161 15 return $self->get_nations_with_status($nation, ['FRIENDSHIP', 'ALLIANCE', 'INFLUENCE PRESENT']);
162 }
163 sub set_diplomacy
164 {
165 39 ▶ my $self = shift;
Subroutine set_diplomacy: called CC=2 SCAR=7.0
Pod undocumented
166 39 my $node1 = shift;
167 39 my $node2 = shift;
168 39 my $new_factor = shift;
169 39 my $r = $self->diplomacy_exists($node1, $node2);
170 39 ▶ return undef if(!$r ); #Should never happen
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 39 39
171 39 $r->factor($new_factor);
172 39 return $r;
173 }
174 sub copy_diplomacy
175 {
176 4 ▶ my $self = shift;
Subroutine copy_diplomacy: called CC=3 SCAR=11.0
Pod undocumented
177 4 my $nation_from = shift;
178 4 my $nation_to = shift;
179 4 my @relations = $self->get_diplomatic_relations($nation_from);
180 4 for(@relations)
181 {
182 13 my $r = $_;
183 13 my $other = $r->destination($nation_from);
184 13 ▶ if($other ne $nation_to)
Branch: 1 branch
BranchTrueFalseTotal
if ($other ne $nation_to) 9 4 13
185 {
186 9 $self->set_diplomacy($nation_to, $other, $r->factor);
187 }
188 }
189  
190  
191 }
192  
193 sub change_diplomacy
194 {
195 53 ▶ my $self = shift;
Subroutine change_diplomacy: called CC=6 SCAR=17.9
Pod undocumented
196 53 my $node1 = shift;
197 53 my $node2 = shift;
198 53 my $dipl = shift;
199 53 my $r = $self->diplomacy_exists($node1, $node2);
200 53 ▶ return if(!$r ); #Should never happen
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 53 53
201 53 ▶ return if $r->status eq 'ALLIANCE' || $r->status eq 'INFLUENCE PRESENT';
Branch: 1 branch
BranchTrueFalseTotal
if $r->status eq "ALLIANCE" or $r->status eq "INFLUENCE PRESENT" 1 52 53
Condition66%2/3
$r->status eq "ALLIANCE" or $r->status eq "INFLUENCE PRESENT"
l!l&&r!l&&!r
1052
MC/DC50%1/2
$r->status eq "ALLIANCE" or $r->status eq "INFLUENCE PRESENT"
$r->status eq "ALLIANCE" $r->status eq "INFLUENCE PRESENT"
Truth table66%2/3
A or B
A= $r->status eq "ALLIANCE"B= $r->status eq "INFLUENCE PRESENT"
ABresult
1X1
011
000
202 52 my $present_status = $r->status;
203 52 $r->change_factor($dipl);
204 52 my $actual_status = $r->status;
205 52 ▶ if($present_status ne $actual_status)
Branch: 1 branch
BranchTrueFalseTotal
if ($present_status ne $actual_status) 9 43 52
206 {
207 9 $self->broadcast_event("RELATION BETWEEN $node1 AND $node2 CHANGED FROM $present_status TO $actual_status", $node1, $node2);
208 9 ▶ if($actual_status eq 'HATE')
Branch: 1 branch
BranchTrueFalseTotal
if ($actual_status eq "HATE") 1 8 9
209 {
210 1 $self->diplomatic_breakdown($node1, $node2);
211 }
212 }
213 }
214 sub diplomacy_status
215 {
216 58 ▶ my $self = shift;
Subroutine diplomacy_status: called CC=1 SCAR=0.0
Pod undocumented
217 58 my $n1 = shift;
218 58 my $n2 = shift;
219 58 my $r = $self->diplomacy_exists($n1, $n2);
220 58 return $r->status;
221 }
222  
223 sub diplomatic_breakdown
224 {
225 1 ▶ my $self = shift;
Subroutine diplomatic_breakdown: called CC=2 SCAR=7.1
Pod undocumented
226 1 my $n1 = shift;
227 1 my $n2 = shift;
228 1 my $treaty = $self->exists_treaty($n1, $n2);
229 1 ▶ if($treaty)
Branch: 1 branch
BranchTrueFalseTotal
if ($treaty) 0 1 1
230 {
231 0 $self->broadcast_event($treaty->short_tag . " TREATY BETWEEN $n1 AND $n2 BROKEN", $n1, $n2);
232 }
233 1 $self->stop_military_support($self->get_nation($n1), $self->get_nation($n2), 1);
234 1 $self->stop_military_support($self->get_nation($n2), $self->get_nation($n1), 1);
235 }
236  
237 sub diplomacy_for_node
238 {
239 174 ▶ my $self = shift;
Subroutine diplomacy_for_node: called CC=3 SCAR=11.0
Pod undocumented
240 174 my $node = shift;
241 174 my %relations;
242 174 foreach my $n (@{$self->nation_names})
243 {
244 838 ▶ if($n ne $node)
Branch: 1 branch
BranchTrueFalseTotal
if ($n ne $node) 664 174 838
245 {
246 664 my $real_r = $self->diplomacy_exists($node, $n);
247 664 $relations{$n} = $real_r->factor;
248 }
249 }
250 174 return %relations;;
251 }
252 sub print_diplomacy
253 {
254 2 ▶ my $self = shift;
Subroutine print_diplomacy: called CC=4 SCAR=13.9
Pod undocumented
255 2 my $n = shift;
256 2 my $out;
257 my @outnodes;
258 2 foreach my $f ($self->diplomatic_relations->all())
259 {
260 20 ▶ if($f->has_node($n))
Branch: 1 branch
BranchTrueFalseTotal
if ($f->has_node($n)) 8 12 20
261 {
262 8 my $real_r = $self->diplomacy_exists($n, $f->destination($n));
263 8 push @outnodes, $real_r;
264 }
265 }
266 2 foreach my $rr (sort { $a->factor <=> $b->factor} @outnodes)
267 {
268 8 $out .= $rr->print($n) . "\n";
269 }
270 2 return $out;
271 }
272  
273  
274 sub diplomatic_pressure
275 {
276 9 ▶ my $self = shift;
Subroutine diplomatic_pressure: called CC=2 SCAR=6.9
Pod undocumented
277 9 my $nation1 = shift;
278 9 my $nation2 = shift;
279 9 my @friends = $self->get_friends($nation1);
280 9 $self->change_diplomacy($nation1, $nation2, DIPLOMATIC_PRESSURE_FACTOR);
281 9 $self->broadcast_event("DIPLOMATIC PRESSURE OF $nation1 ON $nation2", $nation1, $nation2);
282 9 for(@friends)
283 {
284 6 my $f = $_;
285 6 $self->change_diplomacy($f, $nation2, DIPLOMATIC_PRESSURE_FACTOR);
286 }
287 }
288  
289  
290 #Functions to manage relationships as crises
291 sub add_crisis
292 {
293 21 ▶ my $self = shift;
Subroutine add_crisis: called CC=2 SCAR=6.9
Pod undocumented
294 21 my $nation1 = shift;
295 21 my $nation2 = shift;
296 21 my $rel = $self->diplomacy_exists($nation1, $nation2);
297 21 ▶ if($rel->get_crisis_level == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->get_crisis_level == 0) 20 1 21
298 {
299 20 $rel->escalate_crisis();
300 }
301 }
302 sub delete_crisis
303 {
304 8 ▶ my $self = shift;
Subroutine delete_crisis: called CC=1 SCAR=0.0
Pod undocumented
305 8 my $nation1 = shift;
306 8 my $nation2 = shift;
307 8 my $rel = $self->diplomacy_exists($nation1, $nation2);
308 8 $rel->crisis_level(0);
309 }
310 sub crisis_exists
311 {
312 163 ▶ my $self = shift;
Subroutine crisis_exists: called CC=5 SCAR=17.1
Pod undocumented
313 163 ▶ my $nation1 = shift || "";
Condition50%1/2
shift() || ''
l!l
1630
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
314 163 ▶ my $nation2 = shift || "";
Condition50%1/2
shift() || ''
l!l
1630
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
315 163 my $rel = $self->diplomacy_exists($nation1, $nation2);
316 163 ▶ if(! $rel)
Branch: 1 branch
BranchTrueFalseTotal
unless ($rel) 0 163 163
317 {
318 0 say "ERROR: $nation1 <-> $nation2";
319 0 return undef;
320 }
321 163 ▶ if($rel->get_crisis_level > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->get_crisis_level > 0) { } 93 70 163
322 {
323 93 return $rel;
324 }
325 else
326 {
327 70 return undef;
328 }
329 }
330 sub get_crises
331 {
332 32 ▶ my $self = shift;
Subroutine get_crises: called CC=1 SCAR=0.0
Pod undocumented
333 32 my $nation = shift;
334 32 my @crises = $self->get_diplomatic_relations($nation);
335 32 @crises = grep { $_->get_crisis_level > 0 } @crises;
336 32 return @crises;
337 }
338 sub get_all_crises
339 {
340 81 ▶ my $self = shift;
Subroutine get_all_crises: called CC=1 SCAR=0.0
Pod undocumented
341 81 my @rels = $self->diplomatic_relations->all();
342 81 return grep { $_->is_crisis() } @rels;
343 }
344 sub reset_crises
345 {
346 6 ▶ my $self = shift;
Subroutine reset_crises: called CC=2 SCAR=6.9
Pod undocumented
347 6 my $nation = shift;
348 6 my @rels = $self->get_diplomatic_relations($nation);
349 6 for(@rels)
350 {
351 13 $_->crisis_level(0);
352 }
353 }
354  
355 #Functions to manage treaties
356 sub create_treaty
357 {
358 13 ▶ my $self = shift;
Subroutine create_treaty: called CC=1 SCAR=0.0
Pod undocumented
359 13 my $nation1 = shift;
360 13 my $nation2 = shift;
361 13 my $type = shift;
362 13 $self->add_treaty(BalanceOfPower::Relations::Treaty->new(
363 node1 => $nation1,
364 node2 => $nation2,
365 type => $type ));
366 }
367 sub exists_treaty_by_type
368 {
369 103 ▶ my $self = shift;
Subroutine exists_treaty_by_type: called CC=4 SCAR=13.9
Pod undocumented
370 103 my $nation1 = shift;
371 103 my $nation2 = shift;
372 103 my $type = shift;
373 103 my $rel = $self->exists_treaty($nation1, $nation2);
374 103 ▶ if( $rel && ($rel->type eq $type || $rel->type eq 'alliance')) #Alliance means both treaties are active
Branch: 1 branch
BranchTrueFalseTotal
if ($rel and $rel->type eq $type || $rel->type eq 'alliance') { } 23 80 103
Condition66%4/6
$rel and $rel->type eq $type || $rel->type eq "alliance"
!ll&&!rl&&r
80023
$rel->type eq $type or $rel->type eq "alliance"
l!l&&r!l&&!r
2030
MC/DC33%1/3
$rel and $rel->type eq $type || $rel->type eq "alliance"
$rel $rel->type eq $type $rel->type eq "alliance"
Truth table75%3/4
A and B or C
A= $relB= $rel->type eq $typeC= $rel->type eq "alliance"
ABCresult
0XX0
1000
11X1
1011
375 {
376 23 return $rel;
377 }
378 else
379 {
380 80 return undef;
381 }
382 }
383 sub get_treaties_for_nation_by_type
384 {
385 41 ▶ my $self = shift;
Subroutine get_treaties_for_nation_by_type: called CC=1 SCAR=0.0
Pod undocumented
386 41 my $nation = shift;
387 41 my $type = shift;
388 41 my @treaties = $self->get_treaties_for_nation($nation);
389 41 return grep { $_->type eq $type } @treaties;
390 }
391  
392 #Functions to manage alliances
393 sub add_alliance
394 {
395 8 ▶ my $self = shift;
Subroutine add_alliance: called CC=1 SCAR=0.0
Pod undocumented
396 8 my $nation1 = shift;
397 8 my $nation2 = shift;
398 8 $self->create_treaty($nation1, $nation2, 'alliance');
399 8 $self->set_diplomacy($nation1, $nation2, ALLIANCE_FRIENDSHIP_FACTOR);
400 }
401 sub print_allies
402 {
403 1 ▶ my $self = shift;
Subroutine print_allies: called CC=3 SCAR=11.0
Pod undocumented
404 1 my @treaties = $self->treaties->all();
405 1 my $out = "";
406 1 for(@treaties)
407 {
408 4 ▶ if($_->type eq 'alliance')
Branch: 1 branch
BranchTrueFalseTotal
if ($_->type eq "alliance") 4 0 4
409 {
410 4 $out .= $_->print . "\n";
411 }
412 }
413 1 return $out;
414 }
415  
416 sub exists_alliance
417 {
418 0 ▶ my $self = shift;
Subroutine exists_alliance: not called CC=1 SCAR=6.9
Pod undocumented
419 0 my $nation1 = shift;
420 0 my $nation2 = shift;
421 0 return $self->exists_treaty_by_type($nation1, $nation2, 'alliance');
422 }
423  
424 sub get_allies
425 {
426 26 ▶ my $self = shift;
Subroutine get_allies: called CC=1 SCAR=0.0
Pod undocumented
427 26 my $nation = shift;
428 26 return $self->get_treaties_for_nation_by_type($nation, 'alliance');
429 }
430  
431  
432 11 1;
« lib/BalanceOfPower/Commands/DeclareWar.pm ↑ Summary lib/BalanceOfPower/Commands/TargetRoute.pm »