File Coverage

blib/lib/Games/Lacuna/Task/Action/ReportIncoming.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::ReportIncoming;
2              
3 1     1   1420 use 5.010;
  1         3  
  1         137  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   456 use Moose;
  0            
  0            
7             extends qw(Games::Lacuna::Task::Action);
8             with qw(Games::Lacuna::Task::Role::Notify
9             Games::Lacuna::Task::Role::PlanetRun);
10              
11             use Games::Lacuna::Task::Utils qw(parse_date format_date);
12              
13             sub description {
14             return q[Report incoming foreign ships];
15             }
16              
17             has 'known_incoming' => (
18             is => 'rw',
19             isa => 'ArrayRef',
20             lazy_build => 1,
21             traits => ['Array','NoGetopt'],
22             handles => {
23             add_known_incoming => 'push',
24             }
25             );
26              
27             has 'new_incoming' => (
28             is => 'rw',
29             isa => 'ArrayRef',
30             default => sub { [] },
31             traits => ['Array','NoGetopt'],
32             handles => {
33             add_new_incoming => 'push',
34             has_new_incoming => 'count',
35             }
36             );
37              
38             sub _build_known_incoming {
39             my ($self) = @_;
40            
41             my $incoming = $self->get_cache('report/known_incoming');
42             $incoming ||= [];
43            
44             return $incoming;
45             }
46              
47             after 'run' => sub {
48             my ($self) = @_;
49            
50             if ($self->has_new_incoming) {
51            
52             $self->add_known_incoming(map { $_->{id} } @{$self->new_incoming});
53            
54             my $message = join ("\n",map {
55             sprintf('%s: %s from %s arrives at %s',$_->{planet},$_->{ship},$_->{from_empire},format_date($_->{arrives}))
56             } @{$self->new_incoming});
57            
58             my $empire_name = $self->empire_name;
59            
60             $self->notify(
61             "[$empire_name] Incoming ship(s) detected!",
62             $message
63             );
64            
65             $self->set_cache(
66             key => 'report/known_incoming',
67             value => $self->known_incoming,
68             max_age => (60*60*24*7), # Cache one week
69             );
70             }
71             };
72              
73             sub process_planet {
74             my ($self,$planet_stats) = @_;
75            
76             return
77             unless defined($planet_stats->{incoming_enemy_ships});
78            
79             # Get space port
80             my $spaceport = $self->find_building($planet_stats->{id},'SpacePort');
81            
82             return
83             unless $spaceport;
84            
85             my $spaceport_object = $self->build_object($spaceport);
86            
87             # Get all incoming ships
88             my $ships_data = $self->paged_request(
89             object => $spaceport_object,
90             method => 'view_foreign_ships',
91             total => 'number_of_ships',
92             data => 'ships',
93             );
94            
95             my @incoming_ships;
96            
97             foreach my $ship (@{$ships_data->{ships}}) {
98             my $from;
99             if (defined $ship->{from}
100             && defined $ship->{from}{empire}) {
101             # My own ship
102             next
103             if ($ship->{from}{empire}{id} == $planet_stats->{empire}{id});
104             $from = $ship->{from}{empire}{name};
105             }
106            
107             # Ignore cargo ships since they are probably carrying out a trade
108             # (not dories since they can be quite stealthy and therefore can be used to carry spies)
109             next
110             if ($ship->{type} ~~ [qw(hulk cargo_ship galleon barge freighter)]);
111            
112             my $arrives = parse_date($ship->{date_arrives});
113            
114             my $incoming = {
115             arrives_delta => ((time() - $arrives) / 60),
116             arrives => $arrives,
117             planet => $planet_stats->{name},
118             ship => $ship->{type},
119             from_empire => ($from || 'unknown'),
120             id => $ship->{id},
121             };
122            
123             $self->log('warn','Incoming %s ship from %s arriving in %s minutes detected on %s',$incoming->{ship},$incoming->{from_empire},$incoming->{arrives_delta} ,$planet_stats->{name});
124            
125             # Check if we already know this ship
126             next
127             if $ship->{id} ~~ $self->known_incoming;
128            
129             $self->add_new_incoming($incoming);
130             }
131             }
132              
133             __PACKAGE__->meta->make_immutable;
134             no Moose;
135             1;