File Coverage

blib/lib/Games/Lacuna/Task/Action/WasteRecycle.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::WasteRecycle;
2              
3 1     1   1677 use 5.010;
  1         5  
  1         54  
4             our $VERSION = $Games::Lacuna::Task::VERSION;
5              
6 1     1   508 use Moose;
  0            
  0            
7             extends qw(Games::Lacuna::Task::Action);
8             with 'Games::Lacuna::Task::Role::Waste',
9             'Games::Lacuna::Task::Role::PlanetRun';
10              
11             use List::Util qw(min);
12             use Games::Lacuna::Task::Utils qw(parse_date);
13              
14             our @RESOURCES_RECYCLEABLE = qw(water ore energy);
15              
16             sub description {
17             return q[Recycle waste with the Waste Recycling Center and Waste Exchanger];
18             }
19              
20             sub process_planet {
21             my ($self,$planet_stats) = @_;
22            
23             my $timestamp = time();
24             my %resources;
25             my @recycling_buildings;
26            
27             push (@recycling_buildings,$self->find_building($planet_stats->{id},'WasteRecycling'));
28             push (@recycling_buildings,$self->find_building($planet_stats->{id},'WasteExchanger'));
29            
30             return
31             unless scalar @recycling_buildings;
32            
33             my $waste_stored = $planet_stats->{waste_stored};
34             my $waste_capacity = $planet_stats->{waste_capacity};
35             my $waste_filled = ($waste_stored / $waste_capacity) * 100;
36             my $waste_disposeable = $self->disposeable_waste($planet_stats);
37            
38             my $total_resources = 0;
39             my $total_resources_coeficient = 0;
40             my $total_waste_coeficient = 0;
41            
42             return
43             if $waste_disposeable <= 0;
44            
45             # Get stored resources
46             foreach my $resource (@RESOURCES_RECYCLEABLE) {
47             my $stored = $planet_stats->{$resource.'_stored'}+0;
48             my $capacity = $planet_stats->{$resource.'_capacity'}+0;
49             $resources{$resource} = [ $capacity-$stored, 0, 0];
50             $total_resources += $capacity-$stored;
51             }
52            
53             # Fallback if storage is full
54             if ($total_resources == 0) {
55             foreach my $resource (@RESOURCES_RECYCLEABLE) {
56             my $capacity = $planet_stats->{$resource.'_capacity'}+0;
57             $resources{$resource}[0] = $capacity;
58             $total_resources += $capacity;
59             }
60             }
61            
62             # Calculate ressouces
63             foreach my $resource (@RESOURCES_RECYCLEABLE) {
64             $resources{$resource}[1] = ($resources{$resource}[0] / $total_resources);
65             if ($resources{$resource}[1] > 0
66             && $resources{$resource}[1] < 1) {
67             $resources{$resource}[1] = 1-($resources{$resource}[1]);
68             }
69             $total_resources_coeficient += $resources{$resource}[1];
70             }
71            
72             # Calculate recycling relations
73             foreach my $resource (@RESOURCES_RECYCLEABLE) {
74             $resources{$resource}[2] = ($resources{$resource}[1] / $total_resources_coeficient);
75             }
76            
77             # Loop all recycling buildings
78             foreach my $recycling_building (@recycling_buildings) {
79            
80             last
81             if $waste_disposeable <= 0;
82            
83             # Check recycling is busy
84             if (defined $recycling_building->{work}) {
85             my $work_end = parse_date($recycling_building->{work}{end});
86             if ($work_end > $timestamp) {
87             next;
88             }
89             }
90            
91             my $recycling_object = $self->build_object($recycling_building);
92             my $recycling_data = $self->request(
93             object => $recycling_object,
94             method => 'view',
95             );
96            
97             my $recycle_quantity = min($waste_disposeable,$recycling_data->{recycle}{max_recycle});
98            
99             my %recycle = (map { $_ => int($resources{$_}[2] * $recycle_quantity) } keys %resources);
100            
101             $self->log('notice',"Recycling %i %s, %i %s, %i %s on %s",(map { ($recycle{$_},$_) } @RESOURCES_RECYCLEABLE),$planet_stats->{name});
102            
103             $self->request(
104             object => $recycling_object,
105             method => 'recycle',
106             params => [ (map { $recycle{$_} } @RESOURCES_RECYCLEABLE) ],
107             );
108            
109             $waste_disposeable -= $recycle_quantity;
110            
111             $self->clear_cache('body/'.$planet_stats->{id}.'/buildings');
112             }
113             }
114              
115             __PACKAGE__->meta->make_immutable;
116             no Moose;
117             1;