blib/lib/BalanceOfPower/Commands/NagTreaty.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 107 CC 13 SCAR 50.0
CC
13
Cov
0%
IA
182.0
get_available_targets
2.0
BEGIN
2.0
CRAP 148.2
1 package BalanceOfPower::Commands::NagTreaty;
2 $BalanceOfPower::Commands::NagTreaty::VERSION = '0.200000';
3 use Moo;
4 use Array::Utils qw(intersect);
5  
6 extends 'BalanceOfPower::Commands::TargetNation';
7 with 'BalanceOfPower::Commands::Role::TreatiesUnderLimit';
8  
9 sub get_available_targets
10 {
11 my $self = shift;
12 my @targets = $self->SUPER::get_available_targets();
13 my $nation = $self->actor;
14 @targets = grep {! $self->world->exists_treaty($nation, $_) } @targets;
15 @targets = grep { $self->world->diplomacy_status($nation, $_) ne 'HATE' } @targets;
16 return $self->nations_under_treaty_limit(@targets);
17 }
18  
19 sub IA
20 {
21 my $self = shift;
22 my $actor = $self->get_nation();
23 my @available_targets = $self->get_available_targets();
24 return undef if @available_targets == 0;
25 my @near = $self->world->near_nations($actor->name, 1);
26  
27 my @friendly_neighbors = $self->world->shuffle("Mixing neighbors to choose about NAG treaty", intersect(@near, @available_targets));
28 my @ordered_friendly_neighbors = ();
29 my $dangerous_neighbor = 0;
30 for(@friendly_neighbors)
31 {
32 my $n = $_;
33 if(! $self->world->exists_treaty($self->name, $n))
34 {
35 my $supporter = $self->world->supported($n);
36 if($supporter)
37 {
38 my $supporter_nation = $supporter->node1;
39 if($supporter_nation eq $actor->name)
40 {
41 #I'm the supporter of this nation!
42 push @ordered_friendly_neighbors, { nation => $n,
43 interest => 0 };
44 }
45 else
46 {
47 if($self->world->crisis_exists($actor->name, $supporter_nation))
48 {
49 push @ordered_friendly_neighbors, { nation => $n,
50 interest => 100 };
51 $dangerous_neighbor = 1;
52 }
53 elsif($self->world->diplomacy_status($actor->name, $supporter_nation) eq 'HATE')
54 {
55 push @ordered_friendly_neighbors, { nation => $n,
56 interest => 10 };
57 }
58 else
59 {
60 push @ordered_friendly_neighbors, { nation => $n,
61 interest => 2 };
62 }
63 }
64 }
65 else
66 {
67 push @ordered_friendly_neighbors, { nation => $n,
68 interest => 1 };
69 }
70 }
71 }
72 if(@ordered_friendly_neighbors > 0 && $dangerous_neighbor)
73 {
74 @ordered_friendly_neighbors = sort { $b->{interest} <=> $a->{interest} } @ordered_friendly_neighbors;
75 return "TREATY NAG WITH " . $ordered_friendly_neighbors[0]->{nation};
76 }
77 else
78 {
79 #Scanning crises
80 my @crises = $self->world->get_crises($actor->name);
81 if(@crises > 0)
82 {
83 foreach my $c ($self->world->shuffle("Mixing crisis for war for " . $actor->name, @crises))
84 {
85 #NAG with enemy supporter
86 my $enemy = $c->destination($actor->name);
87 my $supporter = $self->world->supported($enemy);
88 if($supporter)
89 {
90 my $supporter_nation = $supporter->node1;
91 if($supporter_nation ne $actor->name &&
92 $self->world->diplomacy_status($actor->name, $supporter_nation) ne 'HATE' &&
93 ! $self->world->exists_treaty($actor->name, $supporter_nation))
94 {
95 return "TREATY NAG WITH " . $supporter_nation;
96 }
97 }
98 #NAG with enemy ally
99 my @allies = $self->world->get_allies($enemy);
100 for($self->world->shuffle("Mixing allies of enemy for a NAG", @allies))
101 {
102 my $all = $_->destination($enemy);
103 if($all ne $actor->name &&
104 $self->world->diplomacy_status($actor->name, $all) ne 'HATE' &&
105 ! $self->world->exists_treaty($actor->name, $all))
106 {
107 return "TREATY NAG WITH " . $all;
108 }
109 }
110 }
111 }
112 if(@ordered_friendly_neighbors > 0)
113 {
114 @ordered_friendly_neighbors = sort { $b->{interest} <=> $a->{interest} } @ordered_friendly_neighbors;
115 return "TREATY NAG WITH " . $ordered_friendly_neighbors[0]->{nation};
116 }
117 return undef;
118 }
119 }
120  
121 1;