diff --git a/docs/Development.pod b/docs/Development.pod index 5925c0a0..f181f880 100644 --- a/docs/Development.pod +++ b/docs/Development.pod @@ -19,36 +19,28 @@ This method is extended to include details about what's in your build queue. "name" : "Planetary Commmand", "to_level" : 9, "seconds_remaining" : 537, + "x" : 0, + "y" : 0 }, { "building_id" : "building-id-goes-here", "name" : "Wheat Farm", "to_level" : 15, "seconds_remaining" : 9748, + "x" : -1, + "y" : 4 } - ] + ], + "subsidy_cost" : 8 # the essentia cost to subsidize the build queue } -=head2 subsidize_build_queue ( session_id, building_id, amount ) +=head2 subsidize_build_queue ( session_id, building_id ) -Allows a player to speed up the build process, by spending essentia. Returns: +Allows a player to instantly finish any buildings in their build queue. The cost is returned by the C method. Returns: { "status" : { get_status() }, - "build_queue" : [ - { - "building_id" : "building-id-goes-here", - "name" : "Planetary Commmand", - "to_level" : 9, - "seconds_remaining" : 537, - }, - { - "building_id" : "building-id-goes-here", - "name" : "Wheat Farm", - "to_level" : 15, - "seconds_remaining" : 9748, - } - ] + "essentia_spent" : 8 } =head3 session_id @@ -58,9 +50,5 @@ A session id. =head3 building_id The id of the development ministry. - -=head3 amount - -The amount of essentia to spend. One essentia is worth 10 minutes of build time. =cut \ No newline at end of file diff --git a/lib/Lacuna/Building/Development.pm b/lib/Lacuna/Building/Development.pm index bca838a6..0bdc437d 100644 --- a/lib/Lacuna/Building/Development.pm +++ b/lib/Lacuna/Building/Development.pm @@ -17,23 +17,25 @@ around 'view' => sub { my $building = $empire->get_building($self->model_domain, $building_id); my $out = $orig->($self, $empire, $building); $out->{build_queue} = $building->format_build_queue; + $out->{subsidy_cost} = $building->calculate_subsidy; return $out; }; sub subsidize_build_queue { - my ($self, $session_id, $building_id, $amount) = @_; - if ($amount < 0) { - confess [1009, "You can't subsidize that little.", $amount]; - } + my ($self, $session_id, $building_id) = @_; my $empire = $self->get_empire_by_session($session_id); - if ($empire->essentia < $amount) { + my $building = $empire->get_building($self->model_class, $building_id); + my $subsidy = $building->calculate_subsidy; + if ($empire->essentia < $subsidy) { confess [1011, "You don't have enough essentia."]; } - my $building = $empire->get_building($self->model_class, $building_id); - $building->subsidize_build_queue($amount); + $empire->spend_essentia($subsidy); + $empire->trigger_full_update(skip_put=>1); + $empire->put; + $building->subsidize_build_queue; return { - build_queue => $building->format_build_queue, - status => $empire->get_status, + status => $empire->get_status, + essentia_spent => $subsidy, }; } diff --git a/lib/Lacuna/DB/Building/Development.pm b/lib/Lacuna/DB/Building/Development.pm index 720a5d65..aca7153c 100644 --- a/lib/Lacuna/DB/Building/Development.pm +++ b/lib/Lacuna/DB/Building/Development.pm @@ -4,15 +4,25 @@ use Moose; extends 'Lacuna::DB::Building'; sub subsidize_build_queue { - my ($self, $amount) = @_; + my ($self) = @_; + $self->body->tick; + my $builds = $self->simpledb->domain('build_queue')->search(where=>{body_id=>$self->body_id}); + while (my $build = $builds->next) { + $build->finish_build; + } +} + + +sub calculate_subsidy { + my ($self) = @_; + my $levels = 0; my $builds = $self->simpledb->domain('build_queue')->search(where=>{body_id=>$self->body_id}); while (my $build = $builds->next) { - $build->date_complete->subtract(seconds=>($amount * 600)); - $build->put; + $levels += $build->building->level; } - my $empire = $self->empire; - $empire->spend_essentia($amount); - $empire->trigger_full_update; + my $cost = int($levels / 3); + $cost = 1 if $cost < 1; + return $cost; } sub format_build_queue { @@ -26,6 +36,8 @@ sub format_build_queue { name => $target->name, to_level => ($target->level + 1), seconds_remaining => $build->seconds_remaining, + x => $target->x, + y => $target->y, }; } return \@queue; diff --git a/t/Development.t b/t/Development.t index 0db9488d..18207775 100644 --- a/t/Development.t +++ b/t/Development.t @@ -1,5 +1,5 @@ use lib '../lib'; -use Test::More tests => 1; +use Test::More tests => 2; use Test::Deep; use Data::Dumper; use 5.010; @@ -7,19 +7,49 @@ use 5.010; use TestHelper; my $tester = TestHelper->new->generate_test_empire; my $session_id = $tester->session->id; - +my $empire = $tester->empire; +my $home = $empire->home_planet; my $result; +$empire->add_essentia(10)->put; -my $command = $tester->empire->home_planet->command; -$command->level(5); -$command->put; +my $uni = Lacuna::DB::Building::University->new( + simpledb => $tester->db, + x => 0, + y => -1, + class => 'Lacuna::DB::Building::University', + date_created => DateTime->now, + body_id => $home->id, + body => $home, + empire_id => $empire->id, + empire => $empire, + level => 2, +); +$home->build_building($uni); +$uni->finish_upgrade; -$result = $tester->post('development', 'build', [$session_id, $tester->empire->home_planet_id, 3, 3]); +$home->ore_capacity(5000); +$home->energy_capacity(5000); +$home->food_capacity(5000); +$home->water_capacity(5000); +$home->bauxite_stored(5000); +$home->algae_stored(5000); +$home->energy_stored(5000); +$home->water_stored(5000); +$home->ore_hour(5000); +$home->energy_hour(5000); +$home->algae_production_hour(5000); +$home->water_hour(5000); +$home->put; -$result = $tester->post('development', 'view', [$session_id, $result->{result}{building}{id}]); +$result = $tester->post('development', 'build', [$session_id, $tester->empire->home_planet_id, 3, 3]); +my $id = $result->{result}{building}{id}; +$result = $tester->post('development', 'view', [$session_id, $id]); is($result->{result}{build_queue}[0]{name}, 'Development Ministry', "got build queue"); +$result = $tester->post('development', 'subsidize_build_queue', [$session_id, $id]); +ok($result->{result}{essentia_spent}, 'subsidy worked'); + END { $tester->cleanup; }