File Coverage

blib/lib/Games/Lacuna/Task/Action/CounterIntelligence.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Games::Lacuna::Task::Action::CounterIntelligence;
2              
3 1     1   1487 use 5.010;
  1         3  
  1         57  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   452 use Moose;
  0            
  0            
7             extends qw(Games::Lacuna::Task::Action);
8             with qw(Games::Lacuna::Task::Role::PlanetRun
9             Games::Lacuna::Task::Role::Stars
10             Games::Lacuna::Task::Role::Intelligence);
11              
12             use List::Util qw(min);
13             use Games::Lacuna::Task::Utils qw(parse_date);
14              
15             sub description {
16             return q[Manage counter intelligence activities (not working due to captcha)];
17             }
18              
19             sub process_planet {
20             my ($self,$planet_stats) = @_;
21            
22             my $timestamp = time();
23            
24             # Get intelligence ministry
25             my ($intelligence_ministry) = $self->find_building($planet_stats->{id},'Intelligence');
26             return
27             unless $intelligence_ministry;
28            
29             my $intelligence_ministry_object = $self->build_object($intelligence_ministry);
30            
31             # Get security ministry
32             my ($security_ministry) = $self->find_building($planet_stats->{id},'Security');
33             my @foreign_spies_active;
34             my $foreign_spies_count = 0;
35             if ($security_ministry) {
36             my $security_ministry_object = $self->build_object($security_ministry);
37            
38             my $foreign_spy_data = $self->paged_request(
39             object => $security_ministry_object,
40             method => 'view_foreign_spies',
41             total => 'spy_count',
42             data => 'spies',
43             );
44            
45             $foreign_spies_count = $foreign_spy_data->{spy_count};
46            
47             # Check if we have active foreign spies (not idle) that can be discovered via security sweep
48             if ($foreign_spy_data->{spy_count} > 0) {
49             $self->log('warn',"There are %i foreign spies on %s",$foreign_spy_data->{spy_count},$planet_stats->{name});
50             foreach my $spy (@{$foreign_spy_data->{spies}}) {
51             my $next_mission = parse_date($spy->{next_mission});
52             if ($next_mission > $timestamp) {
53             $self->log('warn',"%s (%i) on %s is active and vulnerable to a security sweep",$spy->{name},$spy->{level},$planet_stats->{name});
54             push(@foreign_spies_active,$spy->{level})
55             }
56             }
57             }
58             }
59            
60             my $spy_data = $self->paged_request(
61             object => $intelligence_ministry_object,
62             method => 'view_spies',
63             total => 'spy_count',
64             data => 'spies',
65             );
66            
67             # Loop all spies
68             my $defensive_spy_count = 0;
69             my %defensive_spy_assignments;
70             foreach my $spy (@{$spy_data->{spies}}) {
71             # Spy is on this planet
72             if ($spy->{assigned_to}{body_id} == $planet_stats->{id}) {
73             $defensive_spy_assignments{$spy->{assignment}} ||= [];
74             push(@{$defensive_spy_assignments{$spy->{assignment}}},$spy);
75             $defensive_spy_count ++;
76             # Spy is on another planet
77             } else {
78             next
79             unless $spy->{is_available};
80             next
81             unless $spy->{assignment} eq 'Idle';
82            
83             my $assigned_to_type = $self->assigned_to_type($spy->{assigned_to});
84            
85             if ($assigned_to_type ~~ [qw(ally own)]) {
86             $self->log('notice',"Assigning defensive spy %s on %s to counter espionage",$spy->{name},$spy->{assigned_to}{name});
87             my $response = $self->request(
88             object => $intelligence_ministry_object,
89             method => 'assign_spy',
90             params => [$spy->{id},'Counter Espionage'],
91             );
92             } else {
93             $self->log('notice',"Offensive spy %s on %s is currently idle",$spy->{name},$spy->{assigned_to}{name});
94             }
95             }
96             }
97            
98             # Assign local spies
99             foreach my $spy (@{$spy_data->{spies}}) {
100             next
101             unless $spy->{is_available};
102             next
103             unless $spy->{assigned_to}{body_id} == $planet_stats->{id};
104            
105             my $assignment;
106            
107             # Run security sweep
108             if (scalar @foreign_spies_active
109             && ! defined $defensive_spy_assignments{'Security Sweep'}
110             && min(@foreign_spies_active)-1 <= $spy->{level}
111             && $defensive_spy_count > $foreign_spies_count) {
112             $assignment = 'Security Sweep';
113             # Assign to counter espionage
114             } elsif ($spy->{assignment} eq 'Idle') {
115             $assignment = 'Counter Espionage';
116             }
117            
118             # Set new assignment
119             if ($assignment) {
120             $defensive_spy_assignments{$assignment} ||= [];
121             push(@{$defensive_spy_assignments{$assignment}},$spy);
122             $self->log('notice',"Assigning defensive spy %s on %s to %s",$spy->{name},$planet_stats->{name},$assignment);
123             $self->request(
124             object => $intelligence_ministry_object,
125             method => 'assign_spy',
126             params => [$spy->{id},$assignment],
127             );
128             }
129             }
130             }
131              
132             __PACKAGE__->meta->make_immutable;
133             no Moose;
134             1;