File Coverage

blib/lib/Games/Lacuna/Task/Action/VrbanskBuild.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::VrbanskBuild;
2              
3 1     1   1785 use 5.010;
  1         3  
  1         62  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   418 use Moose -traits => 'NoAutomatic';
  0            
  0            
7             extends qw(Games::Lacuna::Task::Action);
8             with 'Games::Lacuna::Task::Role::Building',
9             'Games::Lacuna::Task::Role::CommonAttributes' => { attributes => ['home_planet'] };
10              
11             use List::Util qw(min);
12              
13             has 'count' => (
14             isa => 'Int',
15             is => 'ro',
16             required => 1,
17             default => 1,
18             documentation=> "Number of halls to be build [Default: 1]",
19             );
20              
21             sub description {
22             return q[Build halls of vrbansk on a given planet];
23             }
24              
25             sub run {
26             my ($self) = @_;
27            
28             my $planet_home = $self->home_planet_data();
29            
30             # Get pcc
31             my $planetarycommand = $self->find_building($planet_home->{id},'PlanetaryCommand');
32             return
33             unless $planetarycommand;
34            
35             my $planetarycommand_object = $self->build_object($planetarycommand);
36            
37             # Get plans
38             my $plan_data = $self->request(
39             object => $planetarycommand_object,
40             method => 'view_plans',
41             );
42            
43             my @halls;
44             foreach my $plan (@{$plan_data->{plans}}) {
45             next
46             unless $plan->{name} eq 'Halls of Vrbansk';
47             next
48             if $plan->{extra_build_level} != 0;
49             push(@halls,$plan_data->{id});
50             last
51             if scalar @halls >= $self->count;
52             }
53            
54             return $self->log('error','Could not find plans for Hall of Vrbansk')
55             if scalar(@halls) == 0;
56              
57             my $buildable_spots = $self->find_buildspot($planet_home);
58            
59             return $self->log('error','Could not find build spots')
60             if scalar @{$buildable_spots} == 0;
61            
62             my $continue = 1;
63             HALL:
64             while ($continue && scalar @halls && scalar @{$buildable_spots}) {
65             my $builspot = pop(@{$buildable_spots});
66             my $vrbansk = pop(@halls);
67            
68             my $new_vrbansk_object = $self->build_object('/hallsofvrbansk', body_id => $planet_home->{id});
69            
70             $self->log('notice',"Building Hall of Vrbansk on %s",$planet_home->{name});
71            
72             $self->request(
73             object => $new_vrbansk_object,
74             method => 'build',
75             params => [ $planet_home->{id}, $builspot->[0],$builspot->[1]],
76             catch => [
77             [
78             1009,
79             qr/That space is already occupied/,
80             sub {
81             $self->log('debug',"Could not build Hall of Vrbansk on %s: Build spot occupied",$planet_home->{name});
82             push(@halls,$vrbansk);
83             return 0;
84             }
85             ],
86             [
87             1009,
88             qr/There's no room left in the build queue/,
89             sub {
90             $self->log('debug',"Could not build Hall of Vrbansk on %s: Build queue full",$planet_home->{name});
91             $continue = 0;
92             return 0;
93             }
94             ],
95             ],
96             );
97             }
98            
99             $self->clear_cache('body/'.$planet_home->{id}.'/buildings');
100             }
101              
102             __PACKAGE__->meta->make_immutable;
103             no Moose;
104             1;