File Coverage

blib/lib/Games/Lacuna/Task/Action/Archaeology.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::Archaeology;
2              
3 1     1   1805 use 5.010;
  1         3  
  1         64  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   517 use Moose;
  0            
  0            
7             extends qw(Games::Lacuna::Task::Action);
8             with qw(Games::Lacuna::Task::Role::PlanetRun);
9              
10             use List::Util qw(max sum);
11             use Games::Lacuna::Client::Types qw(ore_types);
12             use Games::Lacuna::Task::Utils qw(parse_date);
13              
14             sub description {
15             return q[Search for glyphs via Archaeology Ministry];
16             }
17              
18             sub all_glyphs {
19             my ($self) = @_;
20            
21             # Fetch total glyph count from cache
22             my $all_glyphs = $self->get_cache('glyphs');
23            
24             return $all_glyphs
25             if defined $all_glyphs;
26            
27             # Set all glyphs to zero
28             $all_glyphs = { map { $_ => 0 } ore_types() };
29            
30             # Loop all planets
31             PLANETS:
32             foreach my $planet_stats ($self->my_planets) {
33             # Get archaeology ministry
34             my $archaeology_ministry = $self->find_building($planet_stats->{id},'Archaeology');
35            
36             next
37             unless defined $archaeology_ministry;
38            
39             # Get all glyphs
40             my $archaeology_ministry_object = $self->build_object($archaeology_ministry);
41             my $glyph_data = $self->request(
42             object => $archaeology_ministry_object,
43             method => 'get_glyphs',
44             );
45            
46             foreach my $glyph (@{$glyph_data->{glyphs}}) {
47             $all_glyphs->{$glyph->{type}} ||= 0;
48             $all_glyphs->{$glyph->{type}} ++;
49             }
50             }
51            
52             # Write total glyph count to cache
53             $self->set_cache(
54             key => 'glyphs',
55             value => $all_glyphs,
56             max_age => (60*60*24),
57             );
58            
59             return $all_glyphs;
60             }
61              
62             sub process_planet {
63             my ($self,$planet_stats) = @_;
64            
65             my $all_glyphs = $self->all_glyphs;
66            
67             my $total_glyphs = sum(values %{$all_glyphs});
68             my $max_glyphs = max(values %{$all_glyphs});
69             my $timestamp = time();
70            
71             # Get archaeology ministry
72             my $archaeology_ministry = $self->find_building($planet_stats->{id},'Archaeology');
73            
74             return
75             unless defined $archaeology_ministry;
76            
77             # Check archaeology is busy
78             if (defined $archaeology_ministry->{work}) {
79             my $work_end = parse_date($archaeology_ministry->{work}{end});
80             if ($work_end > $timestamp) {
81             return;
82             }
83             }
84            
85             my $archaeology_ministry_object = $self->build_object($archaeology_ministry);
86            
87             # Get searchable ores
88             my $archaeology_view = $self->request(
89             object => $archaeology_ministry_object,
90             method => 'view',
91             );
92            
93             return
94             if defined $archaeology_view->{building}{work}{seconds_remaining};
95            
96             # Get local ores
97             my %ores;
98             foreach my $ore (keys %{$planet_stats->{ore}}) {
99             $ores{$ore} = 1
100             if $planet_stats->{ore}{$ore} > 1;
101             }
102            
103             # Get local ores form mining platforms
104             my $mining_ministry = $self->find_building($planet_stats->{id},'MiningMinistry');
105             if (defined $mining_ministry) {
106             my $mining_ministry_object = $self->build_object($mining_ministry);
107             my $platforms = $self->request(
108             object => $mining_ministry_object,
109             method => 'view_platforms',
110             );
111            
112             if (defined $platforms
113             && $platforms->{platforms}) {
114             foreach my $platform (@{$platforms->{platforms}}) {
115             foreach my $ore (keys %{$platform->{asteroid}{ore}}) {
116             $ores{$ore} = 1
117             if $platform->{asteroid}{ore}{$ore} > 1;
118             }
119             }
120             }
121             }
122            
123             # Get searchable ores
124             my $archaeology_ores = $self->request(
125             object => $archaeology_ministry_object,
126             method => 'get_ores_available_for_processing',
127             );
128            
129             foreach my $ore (keys %ores) {
130             # Local ore
131             if (defined $archaeology_ores->{ore}{$ore}) {
132             $ores{$ore} = $archaeology_ores->{ore}{$ore};
133             # This ore has been imported
134             } else {
135             delete $ores{$ore}
136             }
137             }
138            
139             # Check best suited glyph
140             for my $max_glyph (0..$max_glyphs) {
141             foreach my $ore (keys %ores) {
142             next
143             if $all_glyphs->{$ore} > $max_glyph;
144             $self->log('notice',"Searching for %s glyph on %s",$ore,$planet_stats->{name});
145             $self->request(
146             object => $archaeology_ministry_object,
147             method => 'search_for_glyph',
148             params => [$ore],
149             );
150            
151             #$self->clear_cache('body/'.$planet_stats->{id}.'/buildings');
152            
153             return;
154             }
155             }
156             }
157              
158             __PACKAGE__->meta->make_immutable;
159             no Moose;
160             1;