File Coverage

blib/lib/Games/Lacuna/Task/Role/Building.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::Building;
2              
3 1     1   1537 use 5.010;
  1         6  
  1         60  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   498 use Moose::Role;
  0            
  0            
7              
8             use Games::Lacuna::Task::Utils qw(parse_date);
9              
10             sub check_upgrade_building {
11             my ($self,$planet_stats,$building_data) = @_;
12            
13             my $building_object = $self->build_object($building_data);
14             my $building_detail = $self->request(
15             object => $building_object,
16             method => 'view',
17             );
18            
19             return 0
20             unless $building_detail->{building}{upgrade}{can};
21            
22             # Check if we really can afford the upgrade
23             return 0
24             unless $self->can_afford($planet_stats,$building_detail->{'building'}{upgrade}{cost});
25            
26             # Check if upgraded building is sustainable
27             {
28             no warnings 'once';
29             foreach my $resource (@Games::Lacuna::Task::Constants::RESOURCES) {
30             my $resource_difference = -1 * ($building_detail->{'building'}{$resource.'_hour'} - $building_detail->{'building'}{upgrade}{production}{$resource.'_hour'});
31             return 0
32             if ($planet_stats->{$resource.'_hour'} + $resource_difference <= 0);
33             }
34             }
35            
36             return 1;
37             }
38              
39             sub upgrade_building {
40             my ($self,$planet_stats,$building_data) = @_;
41            
42             my $building_object = $self->build_object($building_data);
43            
44             $self->log('notice',"Upgrading %s on %s",$building_data->{'name'},$planet_stats->{name});
45            
46             # Upgrade request
47             $self->request(
48             object => $building_object,
49             method => 'upgrade',
50             );
51            
52             $self->clear_cache('body/'.$planet_stats->{id}.'/buildings');
53            
54             return 1;
55             }
56              
57             sub find_buildspot {
58             my ($self,$body) = @_;
59            
60             my $body_data = $self->my_body_status($body);
61            
62             return []
63             unless $body_data;
64            
65             my @occupied;
66             foreach my $building_data ($self->buildings_body($body_data)) {
67             push (@occupied,$building_data->{x}.';'.$building_data->{y});
68             }
69            
70             my @buildable;
71             for my $x (-5..5) {
72             for my $y (-5..5) {
73             next
74             if $x.';'.$y ~~ @occupied;
75             push(@buildable,[$x,$y]);
76             }
77             }
78            
79             return \@buildable;
80             }
81              
82             sub build_queue_size {
83             my ($self,$body) = @_;
84            
85             my @buildings = $self->buildings_body($body);
86             my $timestamp = time();
87            
88             my $building_count = 0;
89            
90             # Get build queue size
91             foreach my $building_data (@buildings) {
92             if (defined $building_data->{pending_build}) {
93             my $date_end = parse_date($building_data->{pending_build}{end});
94             $building_count ++
95             if $timestamp < $date_end;
96             }
97             }
98            
99             return $building_count;
100             }
101              
102             no Moose::Role;
103             1;
104              
105             =encoding utf8
106              
107             =head1 NAME
108              
109             Games::Lacuna::Task::Role::Building - Helper methods for buildings
110              
111             =head1 SYNOPSIS
112              
113             package Games::Lacuna::Task::Action::MyTask;
114             use Moose;
115             extends qw(Games::Lacuna::Task::Action);
116             with qw(Games::Lacuna::Task::Role::Building);
117              
118             =head1 DESCRIPTION
119              
120             This role provides building-related helper methods.
121              
122             =head1 METHODS
123              
124             =head3 find_buildspot
125              
126             my $avaliable_buildspots = $self->find_buildspot($planet_id);
127              
128             Returns all available build spots as an Array Reference.
129              
130             =head3 upgrade_building
131              
132             my $upgrade_ok = $self->upgrade_building($planet_stats,$building_data);
133              
134             Tries to upgrade the given building while performing various checks.
135              
136             =head3 build_queue_size
137              
138             my $count = $self->build_queue_size($planet_stats);
139              
140             Calculates the build queue size
141              
142             =head3 check_upgrade_building
143              
144             my $is_upgradeable = $self->check_upgrade_building($planet_stats,$building_data);
145              
146             Checks if a building is upgradeable
147              
148             =cut