diff --git a/lib/Lacuna/RPC/Building/Shipyard.pm b/lib/Lacuna/RPC/Building/Shipyard.pm index c1a0b4bc..c1680f46 100644 --- a/lib/Lacuna/RPC/Building/Shipyard.pm +++ b/lib/Lacuna/RPC/Building/Shipyard.pm @@ -27,6 +27,7 @@ sub view_build_queue { ); while (my $ship = $ships->next) { push @building, { + id => $ship->id, type => $ship->type, type_human => $ship->type_formatted, date_completed => $ship->date_available_formatted, diff --git a/t/TestHelper.pm b/t/TestHelper.pm index 45948909..5d00a00d 100644 --- a/t/TestHelper.pm +++ b/t/TestHelper.pm @@ -46,6 +46,11 @@ has y => ( default => -5, ); +has big_producer => ( + is => 'rw', + default => 0, +); + sub clear_all_test_empires { my ($class, $name) = @_; @@ -116,6 +121,22 @@ sub find_empty_plot { } } +sub build_building { + my ($self, $class, $level) = @_; + + my $home = $self->empire->home_planet; + $self->find_empty_plot; + + my $building = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ + x => $self->x, + y => $self->y, + class => $class, + level => $level - 1, + }); + $home->build_building($building); + $building->finish_upgrade; + return $building; +} sub build_infrastructure { my $self = shift; @@ -123,40 +144,40 @@ sub build_infrastructure { foreach my $type ('Lacuna::DB::Result::Building::Food::Algae','Lacuna::DB::Result::Building::Energy::Hydrocarbon', 'Lacuna::DB::Result::Building::Water::Purification','Lacuna::DB::Result::Building::Ore::Mine') { - # Ensure we only build on an empty plot - $self->find_empty_plot; - - my $building = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ - x => $self->x, - y => $self->y, - class => $type, - level => 20, - }); - $home->build_building($building); - $building->finish_upgrade; + $self->build_building($type, 20); } $home->empire->university_level(30); $home->empire->update; foreach my $type ('Lacuna::DB::Result::Building::Energy::Reserve', 'Lacuna::DB::Result::Building::Food::Reserve','Lacuna::DB::Result::Building::Ore::Storage', 'Lacuna::DB::Result::Building::Water::Storage') { - $self->find_empty_plot; - - my $building = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ - x => $self->x, - y => $self->y, - class => $type, - level => 20, - }); - $home->build_building($building); - $building->finish_upgrade; + + $self->build_building($type, 20); + + } + + if ($self->big_producer) { + $home->ore_hour(50000000); + $home->water_hour(50000000); + $home->energy_hour(50000000); + $home->algae_production_hour(50000000); + $home->ore_capacity(50000000); + $home->energy_capacity(50000000); + $home->food_capacity(50000000); + $home->water_capacity(50000000); + $home->bauxite_stored(50000000); + $home->algae_stored(50000000); + $home->energy_stored(50000000); + $home->water_stored(50000000); + $home->add_happiness(50000000); + } + else { + $home->algae_stored(100_000); + $home->bauxite_stored(100_000); + $home->energy_stored(100_000); + $home->water_stored(100_000); } - $home->algae_stored(100_000); - $home->bauxite_stored(100_000); - $home->energy_stored(100_000); - $home->water_stored(100_000); - $home->tick; return $self; } diff --git a/t/bugs/0003_PurchaseTooManyShips.t b/t/bugs/0003_PurchaseTooManyShips.t new file mode 100644 index 00000000..5125f425 --- /dev/null +++ b/t/bugs/0003_PurchaseTooManyShips.t @@ -0,0 +1,73 @@ +use lib '..','../../lib'; +use Test::More tests => 2; +use Test::Deep; +use 5.010; + +use strict; +use warnings; + +use TestHelper; +TestHelper->clear_all_test_empires; + +# BUG it is possible to purchase more ships than your space port can hold. + +my $tester = TestHelper->new({ big_producer => 1 })->generate_test_empire->build_infrastructure; +my $tester_session_id = $tester->session->id; +my $empire = $tester->empire; +my $home = $empire->home_planet; +$empire->add_essentia(100, 'testing transporter')->update; + +my $trader = TestHelper->new({empire_name => 'TLE Test Trader', big_producer => 1})->generate_test_empire->build_infrastructure; +my $trader_session_id = $trader->session->id; +$trader->empire->add_essentia(100, 'testing transporter')->update; + +# Build an SST and a space port on both the tester and the trader empires + +my $tester_spaceport = $tester->build_building('Lacuna::DB::Result::Building::SpacePort', 2); +my $tester_shipyard = $tester->build_building('Lacuna::DB::Result::Building::Shipyard', 2); +my $tester_transporter = $tester->build_building('Lacuna::DB::Result::Building::Transporter', 10); +my $trader_spaceport = $trader->build_building('Lacuna::DB::Result::Building::SpacePort', 2); +my $trader_shipyard = $trader->build_building('Lacuna::DB::Result::Building::Shipyard', 2); +my $trader_transporter = $trader->build_building('Lacuna::DB::Result::Building::Transporter', 10); + +# build just under the max ships the tester space port can hold +for ( 0 .. 2 ) { + my $dory = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({type=>'dory'}); + $tester_shipyard->build_ship($dory); +} +$tester->finish_ships($tester_shipyard->id); + +# build some ships for the trader to trade + +my @ships; +for ( 0 .. 3 ) { + my $dory = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({type=>'dory'}); + $trader_shipyard->build_ship($dory); + push @ships, $dory; +} +$trader->finish_ships($trader_shipyard->id); + +# Trader puts 4 ships on the SST +my $result = $trader->post('transporter', 'add_to_market', [$trader_session_id, $trader_transporter->id, [ + { type => 'ship', ship_id => $ships[0]->id}, + { type => 'ship', ship_id => $ships[1]->id}, + { type => 'ship', ship_id => $ships[2]->id}, + { type => 'ship', ship_id => $ships[3]->id}, +], 1]); +my $trade_id = $result->{result}{trade_id}; +ok($trade_id, 'there is a trade'); + +Lacuna->cache->set('captcha', $tester_session_id, { guid => 1111, solution => 1111 }, 60 * 15 ); +$tester->post('captcha', 'solve', [$tester_session_id, 1111, 1111]); + +$result = $tester->post('transporter', 'accept_from_market', [$tester_session_id, $tester_transporter->id, $trade_id]); +is($result->{error}{code}, 1009, 'Cannot accept more ships than the space port can accept'); + + + + + + +END { +# TestHelper->clear_all_test_empires; +} diff --git a/t/bugs/0014_BuildingShipsWithNegativeDocks.t b/t/bugs/0014_BuildingShipsWithNegativeDocks.t index cc194b89..489234f6 100644 --- a/t/bugs/0014_BuildingShipsWithNegativeDocks.t +++ b/t/bugs/0014_BuildingShipsWithNegativeDocks.t @@ -11,49 +11,16 @@ TestHelper->clear_all_test_empires; # BUG it is possible to build ships when there are negative docks available -my $tester = TestHelper->new->generate_test_empire->build_infrastructure; +my $tester = TestHelper->new({ big_producer => 1 })->generate_test_empire->build_infrastructure; my $session_id = $tester->session->id; my $empire = $tester->empire; my $home = $empire->home_planet; -$home->ore_hour(50000000); -$home->water_hour(50000000); -$home->energy_hour(50000000); -$home->algae_production_hour(50000000); -$home->ore_capacity(50000000); -$home->energy_capacity(50000000); -$home->food_capacity(50000000); -$home->water_capacity(50000000); -$home->bauxite_stored(50000000); -$home->algae_stored(50000000); -$home->energy_stored(50000000); -$home->water_stored(50000000); -$home->add_happiness(50000000); -$home->needs_recalc(0); -$home->update; - - -$tester->find_empty_plot; -my $result = $tester->post('spaceport', 'build', [$session_id, $home->id, $tester->x, $tester->y]); -my $spaceport = $tester->get_building($result->{result}{building}{id}); -$spaceport->finish_upgrade; - -$tester->find_empty_plot; - -my $shipyard = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ - x => $tester->x, - y => $tester->y, - class => 'Lacuna::DB::Result::Building::Shipyard', - level => 5, -}); -$home->build_building($shipyard); -$shipyard->finish_upgrade; +my $spaceport = $tester->build_building('Lacuna::DB::Result::Building::SpacePort', 1); +my $shipyard = $tester->build_building('Lacuna::DB::Result::Building::Shipyard',5); # Build a trade ministry so we can build dorys -$tester->find_empty_plot; -$result = $tester->post('trade', 'build', [$session_id, $home->id, $tester->x, $tester->y]); -my $trade = $tester->get_building($result->{result}{building}{id}); -$trade->finish_upgrade; +my $trade = $tester->build_building('Lacuna::DB::Result::Building::Trade',1); # build as many ships as the space port can hold @@ -64,8 +31,8 @@ for ( 0 .. 1 ) { push @ships, $dory; } -# now try to post a new one -$result = $tester->post('shipyard', 'build_ship', [$session_id, $shipyard->id, 'dory']); +# now try to build a new ship +my $result = $tester->post('shipyard', 'build_ship', [$session_id, $shipyard->id, 'dory']); is($result->{error}{code}, 1009, 'Cannot build ships if there are zero docks available'); # build more than the space port can hold @@ -73,11 +40,11 @@ my $dory = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({type=>'dory' $shipyard->build_ship($dory); push @ships, $dory; -# now try to post a new ship +# now try to build a new ship $result = $tester->post('shipyard', 'build_ship', [$session_id, $shipyard->id, 'dory']); is($result->{error}{code}, 1009, 'Cannot build ships if there are negative docks available'); END { -# TestHelper->clear_all_test_empires; + TestHelper->clear_all_test_empires; } diff --git a/t/bugs/0024_BuildShipDoesNotReturnShipID.t b/t/bugs/0024_BuildShipDoesNotReturnShipID.t new file mode 100644 index 00000000..79d2041e --- /dev/null +++ b/t/bugs/0024_BuildShipDoesNotReturnShipID.t @@ -0,0 +1,41 @@ +use lib '..','../../lib'; +use Test::More tests => 3; +use Test::Deep; +use 5.010; + +use strict; +use warnings; + +use TestHelper; +TestHelper->clear_all_test_empires; + +# BUG building a ship does not return the ship ID in the return status + +my $tester = TestHelper->new({ big_producer => 1 })->generate_test_empire->build_infrastructure; +my $session_id = $tester->session->id; +my $empire = $tester->empire; +my $home = $empire->home_planet; + +my $spaceport = $tester->build_building('Lacuna::DB::Result::Building::SpacePort', 1); +my $shipyard = $tester->build_building('Lacuna::DB::Result::Building::Shipyard',5); + +# Build a trade ministry so we can build dorys +my $trade = $tester->build_building('Lacuna::DB::Result::Building::Trade',1); + +# now build a new ship +my $result = $tester->post('shipyard', 'build_ship', [$session_id, $shipyard->id, 'dory']); + +my $ships_building = $result->{result}{ships_building}; + +my $first_ship = $ships_building->[0]; +my $ship_id = $first_ship->{id}; +is($first_ship->{type}, 'dory', 'ship is a Dory'); +isnt($ship_id, undef, 'ship ID is defined'); + +# check that the ship exists +my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id); +is($ship->type, 'dory', 'ship is in the database'); + +END { + TestHelper->clear_all_test_empires; +}