File Coverage

blib/lib/Games/Lacuna/Task/Role/Storage.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::Role::Storage;
2              
3 1     1   1358 use 5.010;
  1         3  
  1         55  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   444 use Moose::Role;
  0            
  0            
7              
8             use Games::Lacuna::Client::Types qw(ore_types food_types is_food_type is_ore_type);
9              
10             sub resource_type {
11             my ($self,$type) = @_;
12            
13             given ($type) {
14             when ([qw(waste water ore food energy happiness essentia)]) {
15             return $_;
16             }
17             when ([ ore_types() ]) {
18             return 'ore'
19             }
20             when ([ food_types() ]) {
21             return 'food'
22             }
23             }
24             }
25              
26             sub check_stored {
27             my ($self,$planet_stats,$resource) = @_;
28            
29             given ($resource) {
30             when ([qw(waste water ore food energy)]) {
31             return $planet_stats->{$_.'_stored'};
32             }
33             when ('happiness') {
34             return $planet_stats->{$_};
35             }
36             when ([ ore_types() ]) {
37             my $ores = $self->ore_stored($planet_stats->{id});
38             return $ores->{$_}
39             }
40             when ([ food_types() ]) {
41             my $foods = $self->food_stored($planet_stats->{id});
42             return $foods->{$_}
43             }
44             }
45             return;
46             }
47              
48             sub plans_stored {
49             my ($self,$planet_id) = @_;
50            
51             $planet_id = $planet_id->{id}
52             if ref($planet_id) eq 'HASH';
53            
54             my $cache_key = 'body/'.$planet_id.'/plans';
55            
56             my $plans = $self->get_cache($cache_key);
57            
58             return $plans
59             if defined $plans;
60            
61             my $command = $self->find_building($planet_id,'PlanetaryCommand');
62             $command ||= $self->find_building($planet_id,'StationCommand');
63            
64             my $command_object = $self->build_object($command);
65             my $response = $self->request(
66             object => $command_object,
67             method => 'view_plans',
68             );
69            
70             $plans = $response->{plans};
71            
72             $self->set_cache(
73             key => $cache_key,
74             value => $plans,
75             );
76            
77             return $plans;
78             }
79              
80             sub ore_stored {
81             my ($self,$planet_id) = @_;
82             $self->_resource_stored($planet_id,'ore','OreStorage');
83             }
84              
85             sub food_stored {
86             my ($self,$planet_id) = @_;
87             $self->_resource_stored($planet_id,'food','FoodReserve');
88             }
89              
90             sub _resource_stored {
91             my ($self,$planet_id,$resource,$building_name) = @_;
92            
93             $planet_id = $planet_id->{id}
94             if ref($planet_id) eq 'HASH';
95            
96             my $cache_key = 'body/'.$planet_id.'/storage/'.$resource;
97            
98             my $stored = $self->get_cache($cache_key);
99            
100             return $stored
101             if defined $stored;
102            
103             my $storage_builiding = $self->find_building($planet_id,$building_name);
104            
105             return
106             unless $storage_builiding;
107            
108             my $storage_builiding_object = $self->build_object($storage_builiding);
109            
110             my ($resource_subtype,@dump_params);
111            
112             my $response = $self->request(
113             object => $storage_builiding_object,
114             method => 'view',
115             );
116            
117             $stored = $response->{lc($resource).'_stored'};
118            
119             $self->set_cache(
120             key => $cache_key,
121             value => $stored,
122             max_age => 600,
123             );
124            
125             return $stored;
126             }
127              
128              
129             no Moose::Role;
130             1;
131              
132             =encoding utf8
133              
134             =head1 NAME
135              
136             Games::Lacuna::Task::Role::Storage - Storage helper methods
137              
138             =head1 SYNOPSIS
139              
140             package Games::Lacuna::Task::Action::MyTask;
141             use Moose;
142             extends qw(Games::Lacuna::Task::Action);
143             with qw(Games::Lacuna::Task::Role::Storage);
144            
145             =head1 DESCRIPTION
146              
147             This role provides helper method to query storage buildings.
148              
149             =head1 METHODS
150              
151             =head2 resource_type
152              
153             my $type = $self->resource_type('magnetite');
154             # $type is 'ore'
155              
156             Returns the type of the requested resource
157              
158             =head2 check_stored
159              
160             my $quantity1 = $self->resource_type($planet_stats,'magnetite');
161             my $quantity2 = $self->resource_type($planet_stats,'water');
162              
163             Returns the stored quantity for the given resource
164              
165             =head2 food_stored
166              
167             $self->food_stored($planet_id);
168              
169             Returns a hashref of all stored foods
170              
171             =head2 ore_stored
172              
173             $self->ore_stored($planet_id);
174              
175             Returns a hashref of all stored ores
176              
177             =head2 plans_stored
178              
179             $self->ore_stored($planet_id);
180              
181             Returns an arrayref of all stored plans
182              
183             =head2 _resource_stored
184              
185             $self->_resource_stored($planet_id,'ore','OreStorage');
186              
187             Helper method to query storage building for details.
188              
189             =cut