File Coverage

blib/lib/Games/Lacuna/Task/Report/Intelligence.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::Report::Intelligence;
2              
3 1     1   1393 use 5.010;
  1         4  
  1         53  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   406 use Moose::Role;
  0            
  0            
7             with qw(Games::Lacuna::Task::Role::Intelligence
8             Games::Lacuna::Task::Role::Stars);
9              
10             use Games::Lacuna::Task::Utils qw(parse_date);
11              
12             sub report_intelligence {
13             my ($self) = @_;
14            
15             my $table = Games::Lacuna::Task::Table->new(
16             headline=> 'Intelligence Report',
17             columns => ['Planet','Defensive spies','Offensive spies','Idle spies','Foreign spies','Foreign active spies'],
18             );
19            
20             foreach my $planet_id ($self->my_planets) {
21             $self->_report_intelligence_body($planet_id,$table);
22             }
23            
24             return $table;
25             }
26              
27             sub _report_intelligence_body {
28             my ($self,$planet_id,$table) = @_;
29            
30             my $timestamp = time();
31             my $planet_stats = $self->my_body_status($planet_id);
32            
33             # Get security & intelligence ministry
34             my ($security_ministry) = $self->find_building($planet_stats->{id},'Security');
35             my ($intelligence_ministry) = $self->find_building($planet_stats->{id},'Intelligence');
36            
37             return
38             unless $security_ministry && $intelligence_ministry;
39            
40             my @foreign_spies_active;
41             my @foreign_spies;
42            
43             my $security_ministry_object = $self->build_object($security_ministry);
44             my $intelligence_ministry_object = $self->build_object($intelligence_ministry);
45            
46             my $foreign_spy_data = $self->paged_request(
47             object => $security_ministry_object,
48             method => 'view_foreign_spies',
49             total => 'spy_count',
50             data => 'spies',
51             );
52            
53             # Check if we have active foreign spies (not idle) that can be discovered via security sweep
54             if ($foreign_spy_data->{spy_count} > 0) {
55             @foreign_spies = @{$foreign_spy_data->{spies}};
56             foreach my $spy (@{$foreign_spy_data->{spies}}) {
57             my $next_mission = parse_date($spy->{next_mission});
58             if ($next_mission > $timestamp) {
59             push(@foreign_spies_active,$spy)
60             }
61             }
62             }
63            
64             my $my_spy_data = $self->request(
65             object => $intelligence_ministry_object,
66             method => 'view_spies',
67             );
68            
69             my $defensive_spies = 0;
70             my $offensive_spies = 0;
71             my $idle_spies = 0;
72             foreach my $spy (@{$my_spy_data->{spies}}) {
73             if ($spy->{assignment} eq 'Idle') {
74             $idle_spies ++;
75             }
76             my $assigned_type = $self->assigned_to_type($spy->{assigned_to});
77             if ($assigned_type ~~ [qw(own ally)]) {
78             $defensive_spies ++
79             } else {
80             $offensive_spies ++;
81             }
82             }
83            
84             $table->add_row({
85             planet => $planet_stats->{name},
86             defensive_spies => $defensive_spies,
87             offensive_spies => $offensive_spies,
88             idle_spies => $idle_spies,
89             foreign_spies => scalar(@foreign_spies),
90             foreign_active_spies => scalar(@foreign_spies_active),
91             });
92             }
93              
94             no Moose::Role;
95             1;