From 2f365b847a3802519698452e8c24bed3ee6f5e49 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Thu, 29 Dec 2011 15:51:44 -0500 Subject: [PATCH 01/14] Start of code for continuous push for waste disposal --- docs/Trade.pod | 104 ++++++++++++++++- lib/Lacuna/DB/Result/Building/Trade.pm | 143 ++++++++++++++++++++++++ lib/Lacuna/DB/Result/Map/Body/Planet.pm | 22 ++++ lib/Lacuna/DB/Result/WasteChain.pm | 21 ++++ var/upgrades/cp_3.08xx.sql | 14 +++ 5 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 lib/Lacuna/DB/Result/WasteChain.pm create mode 100644 var/upgrades/cp_3.08xx.sql diff --git a/docs/Trade.pod b/docs/Trade.pod index 454414fc..2ea2e927 100644 --- a/docs/Trade.pod +++ b/docs/Trade.pod @@ -530,7 +530,110 @@ The unique id of the trade ministry. The unique id of the body you'll be shipping to. Optional. If included it will calculate the estimated travel time of the ships to this body. +=head2 get_waste_ships ( session_id, building_id ) +Returns a list of the ships that are either working to transport waste or available. + + { + "status" : { ... }, + "ships" : [ + { + "id" : "id-goes-here", + "type" : "scow", + "task" : "Docked", + "name" : "Dumper Truck 1", + "speed" : 600, + "hold_size" : 1234800, + }, + ... + ] + } + + +=head3 session_id + +A session id. + +=head3 building_id + +The unique id of the trade ministry. + +=head2 view_waste_pushes ( session_id, building_id ) + +Returns a list of the waste pushes currently controlled by this Trade Ministry + + { + "status" : { ... }, + "waste_pushes" : [ + { + "id" : "id-goes-here", + "star" : { + "id" : "id-goes-here", + "name" : "Sol", + "x" : 0, + "y" : -123, + ... + }, + "waste_hour" : 10000000, + "percent_transferred" : 95, + } + ] + } + +There will usually only be one waste-push in action, to your local star. + +The B is the amount of waste you wish to transfer per hour. + +The B is the percentage of B that is actually transferred. +This is based on the number of ships you have servicing the waste. If you have no ships then +the B will be zero. If it is below 100 then you need to add ships so that +it is at or above 100. Above 100 and you have more ship capacity than you need, but you will +still only transfer the specified B, it just means some ships will be idle or not +fully filled. + +=head2 add_waste_ship_to_fleet (session_id, building_id, ship_id ) + +Take a scow from your space port and add it to the ships servicing the waste pushes. + + { + "status" : { ... }, + } + +Throws 1009. + +=head3 session_id + +A Session ID. + +=head3 building_id + +The unique ID of the Trade Ministry. + +=head3 ship_id + +The unique ID of the ship you want to add to the fleet. + +=head2 remove_waste_ship_from_fleet (session_id, building_id, ship_id ) + +Remove a ship from servicing you waste management and tell it to return to the space port. + + { + "status" : { ... }, + } + +Throws 1009. + +=head3 session_id + +A Session ID. + +=head3 building_id + +The unique ID of the Trade Ministry. + +=head3 ship_id + +The unique ID of the ship you want to remove from the fleet. =head2 get_stored_resources ( session_id, building_id ) @@ -558,7 +661,6 @@ A session id. The unique id of this building. - =head2 push_items ( session_id, building_id, target_id, items, options ) Allows you to push items between your colonies. diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index 79b1e088..c08a23a3 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -1,6 +1,9 @@ package Lacuna::DB::Result::Building::Trade; use Moose; +use List::Util qw(max min); +use Carp; + use utf8; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building'; @@ -44,6 +47,146 @@ use constant water_consumption => 5; use constant waste_production => 1; +sub waste_chains { + my $self = shift; + return Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({ planet_id => $self->body_id }); +} + +sub supply_ships { + my $self = shift; + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ body_id => $self->body_id, task => 'Supply Chain' }); +} + +sub waste_ships { + my $self = shift; + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ body_id => $self->body_id, task => 'Waste Chain' }); +} + +sub max_chains { + my $self = shift; + return ceil($self->level); +} + +sub add_waste_ship { + my ($self, $ship) = @_; + $ship->task('Waste Chain'); + $ship->update; + $self->recalc_waste_production; + return $self; +} + +sub add_supply_ship { + my ($self, $ship) = @_; + $ship->task('Supply Chain'); + $ship->update; + $self->recalc_supply_production; + return $self; +} + +sub send_waste_ship_home { + my ($self, $star, $ship) = @_; + $ship->send( + target => $star, + direction => 'in', + task => 'Travelling', + ); + $self->recalc_waste_production; + return $self; +} + +sub send_supply_ship_home { + my ($self, $planet, $ship) = @_; + $ship->send( + target => $planet, + direction => 'in', + task => 'Travelling', + ); + $self->recalc_supply_production; + return $self; +} + +sub add_waste_chain { + my ($self, $star, $waste_hour) = @_; + Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->new({ + planet_id => $self->body_id, + star_id => $star->id, + waste_hour => $waste_hour, + })->insert; + $self->recalc_waste_production; + return $self; +} + +sub remove_waste_chain { + my ($self, $waste_chain) = @_; + if ($self->waste_chains->count == 1) { + my $ships = $self->waste_ships; + while (my $ship = $ships->next) { + $self->send_waste_ship_home($waste_chain->star, $ship); + } + } + $waste_chain->delete; + $self->recalc_waste_production; + return $self; +} + +sub recalc_waste_production { + my ($self) = @_; + my $body = $self->body; + + my $ship_speed = 0; + my $ship_capacity = 0; + my $ships = $self->waste_ships; + while (my $ship = $ships->next) { + $ship_capacity += $ship->hold_size; + $ship_speed += $ship->speed; + } + + my $waste_chain_count = $self->waste_chains->count; + my $waste_chains = $self->waste_chains; + my $waste_hour = 0; + my $distance = 0; + while (my $waste_chain = $waste_chains->next) { + $distance += $body->calculate_distance_to_target($waste_chain->star); + $waste_hour += $waste_chain->waist_hour; + } + $distance *= 2; +carp "RECALC: distance=[$distance] ship_speed=[$ship_speed] ship_capacity=[$ship_capacity]"; + + my $trips_per_hour = $distance ? ($ship_speed / $distance) : 0; +carp "RECALC: trips_per_hour=[$trips_per_hour]"; + my $max_waste_hauled_per_hour = $trips_per_hour * $ship_capacity; +carp "RECALC: max_waste_hauled_per_hour=[$max_waste_hauled_per_hour]"; + my $waste_hauled_per_hour = min($waste_hour, $max_waste_hauled_per_hour); +carp "RECALC: waste_hauled_per_hour=[$waste_hauled_per_hour]"; + my $shipping_capacity = $max_waste_hauled_per_hour ? sprintf('%.0f',($waste_hour / $max_waste_hauled_per_hour) * 100) : -1; +carp "RECALC: shipping_capacity=[$shipping_capacity]"; + + $waste_chains->reset; + while (my $waste_chain = $waste_chains->next) { + $waste_chain->percent_ship_capacity($shipping_capacity); + $waste_chain->update; + } + + $body->needs_recalc(1); + $body->update; + return $self; +} + +before delete => sub { + my ($self) = @_; + $self->waste_ships->update({task=>'Docked'}); + $self->waste_chain->delete_all; + $self->body->needs_recalc(1); + $self->body->update; +}; + +before 'can_downgrade' => sub { + my $self = shift; + if ($self->waste_chains->count >= $self->level) { + confess [1013, 'You must cancel one of your supply chains before you can downgrade the Trade Ministry.']; + } +}; + sub add_to_market { my ($self, $offer, $ask, $options) = @_; my $ship = $self->next_available_trade_ship($options->{ship_id}); diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 3c835092..df1be6ac 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -1,6 +1,7 @@ package Lacuna::DB::Result::Map::Body::Planet; use Moose; +use Carp; use utf8; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Map::Body'; @@ -785,6 +786,9 @@ has total_ore_concentration => ( sub recalc_stats { my ($self) = @_; + + carp "#### recalc_stats ####\n"; + my %stats = ( needs_recalc => 0 ); my $buildings = $self->buildings; #reset foods @@ -820,6 +824,24 @@ sub recalc_stats { } } } + if ($building->isa('Lacuna::DB::Result::Building::Trade')) { +carp "### Trade building ###"; + my $waste_chains = Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({planet_id => $self->id}); + while (my $waste_chain = $waste_chains->next) { + my $waste_hour = 0; + if ($waste_chain->percent_transferred > 0) { + if ($waste_chain->percent_transferred >= 100) { + $waste_hour = $waste_chain->waste_hour; + } + else { +carp "[".$waste_chain->waste_hour."][".$waste_chain->percent_transferred."]"; + $waste_hour = sprintf('%.0f',$waste_chain->waste_hour * $waste_chain->percent_transferred / 100); + } + } +carp "### Deduct [$waste_hour] waste per hour at capacity [".$waste_chain->percent_transferred."] ###"; + $stats{waste_hour} -= $waste_hour; + } + } if ($building->isa('Lacuna::DB::Result::Building::Permanent::GasGiantPlatform')) { $gas_giant_platforms += $building->level; } diff --git a/lib/Lacuna/DB/Result/WasteChain.pm b/lib/Lacuna/DB/Result/WasteChain.pm new file mode 100644 index 00000000..e82570d5 --- /dev/null +++ b/lib/Lacuna/DB/Result/WasteChain.pm @@ -0,0 +1,21 @@ +package Lacuna::DB::Result::WasteChain; + +use Moose; +use utf8; +no warnings qw(uninitialized); +extends 'Lacuna::DB::Result'; + +__PACKAGE__->table('waste_chain'); +__PACKAGE__->add_columns( + planet_id => { data_type => 'int', size => 11, is_nullable => 0 }, + star_id => { data_type => 'int', size => 11, is_nullable => 0 }, + waste_hour => { data_type => 'int', size => 11, default_value => 0 }, + percent_transferred => { data_type => 'int', size => 11, default_value => 0 }, +); + +__PACKAGE__->belongs_to('star', 'Lacuna::DB::Result::Map::Star', 'star_id'); +__PACKAGE__->belongs_to('planet', 'Lacuna::DB::Result::Map::Body', 'planet_id'); + + +no Moose; +__PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/var/upgrades/cp_3.08xx.sql b/var/upgrades/cp_3.08xx.sql new file mode 100644 index 00000000..91b2c459 --- /dev/null +++ b/var/upgrades/cp_3.08xx.sql @@ -0,0 +1,14 @@ + +create table waste_chain ( + id int(11) not null auto_increment, + planet_id int(11) not null, + star_id int(11) not null, + waste_hour int(11) not null default 0, + percent_transferred int(11) not null default 0, + primary key (id), + key wc_idx_planet_id (planet_id), + key wc_idx_star_id (star_id), + constraint wc_fk_planet_id foreign key (planet_id) references planet (id), + constraint wc_fk_star_id foreign key (star_id) references star (id) +); + -- 2.51.2 From fddfcdfa797ce38ee96bc37c8d1ed018fd901fcd Mon Sep 17 00:00:00 2001 From: ian docherty Date: Fri, 30 Dec 2011 12:57:02 -0500 Subject: [PATCH 02/14] Updated continuous waste push --- lib/Lacuna/DB/Result/Building/Trade.pm | 23 ++++++++++++ lib/Lacuna/RPC/Building/Trade.pm | 52 ++++++++++++++++++-------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index c08a23a3..8768aea3 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -231,6 +231,29 @@ sub trade_ships { }); } +sub waste_ships { + my ($self) = @_; + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ + task => 'Docked', + type => { 'in' => [qw(scow)] }, + body_id => $self->body_id, + }, + { + order_by=> {-desc => ['hold_size']} + }); +} + +sub waste_chains { + my ($self) = @_; + return Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({ + planet_id => $self->body_id, + star_id => $self->body->star_id, + }, + { + order_by => 'waste_hour', + }); +} + sub next_available_trade_ship { my ($self, $ship_id) = @_; if ($ship_id) { diff --git a/lib/Lacuna/RPC/Building/Trade.pm b/lib/Lacuna/RPC/Building/Trade.pm index d6766049..908c40b5 100644 --- a/lib/Lacuna/RPC/Building/Trade.pm +++ b/lib/Lacuna/RPC/Building/Trade.pm @@ -33,6 +33,41 @@ sub get_trade_ships { }; } +sub get_waste_ships { + my ($self, $session_id, $building_id) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $body = $building->body; + # get the local star + my $target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Star')->find($body->star_id); + my @ships; + my $ships = $building->waste_ships; + while (my $ship = $ships->next) { + push @ships, $ship->get_status($target); + } + return { + status => $self->format_status($empire, $building->body), + ships => \@ships, + }; + +} + +sub view_waste_chain { + my ($self, $session_id, $building_id) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $body = $building->body; + my @waste_chain; + my $chains = $building->waste_chains; + while (my $waste_push = $chains->next) { + push @waste_chain, $waste_push->get_status; + } + return { + status => $self->format_status($empire, $building->body), + waste_chain => \@waste_chain, + }; +} + sub push_items { my ($self, $session_id, $building_id, $target_id, $items, $options) = @_; my $empire = $self->get_empire_by_session($session_id); @@ -146,25 +181,10 @@ sub accept_from_market { $empire->spend_essentia($trade->ask, 'Trade Price')->update; $trade->body->empire->add_essentia($trade->ask, 'Trade Income')->update; - #my $cargo_log = Lacuna->db->resultset('Lacuna::DB::Result::Log::Cargo'); - #$cargo_log->new({ - # message => 'trade ministry offer accepted', - # body_id => $trade->body_id, - # data => $trade->payload, - # object_type => ref($trade), - # object_id => $trade->id, - #})->insert; $offer_ship->send( target => $body, payload => $trade->payload, ); - #$cargo_log->new({ - # message => 'send offer', - # body_id => $offer_ship->foreign_body_id, - # data => $offer_ship->payload, - # object_type => ref($offer_ship), - # object_id => $offer_ship->id, - #})->insert; $trade->body->empire->send_predefined_message( tags => ['Trade','Alert'], @@ -199,7 +219,7 @@ sub add_to_market { -__PACKAGE__->register_rpc_method_names(qw(report_abuse view_my_market view_market accept_from_market withdraw_from_market add_to_market push_items get_trade_ships get_stored_resources get_ships get_ship_summary get_prisoners get_plans get_plan_summary get_glyphs get_glyph_summary)); +__PACKAGE__->register_rpc_method_names(qw(get_waste_ships view_waste_chain add_waste_ship_to_fleet remove_waste_ship_from_fleet report_abuse view_my_market view_market accept_from_market withdraw_from_market add_to_market push_items get_trade_ships get_stored_resources get_ships get_ship_summary get_prisoners get_plans get_plan_summary get_glyphs get_glyph_summary)); no Moose; -- 2.51.2 From a906ddc95f305eeb59158947e6d2eb03ffc64f62 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sat, 28 Jan 2012 17:21:34 -0500 Subject: [PATCH 03/14] Added more Waste Chain code --- docs/Trade.pod | 18 ++++-- lib/Lacuna/DB/Result/Building/Trade.pm | 16 +++-- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 12 ++-- lib/Lacuna/DB/Result/WasteChain.pm | 17 +++++ lib/Lacuna/RPC/Building/Trade.pm | 84 ++++++++++++++++++++++++- 5 files changed, 129 insertions(+), 18 deletions(-) diff --git a/docs/Trade.pod b/docs/Trade.pod index f4e44f9c..09af5aca 100644 --- a/docs/Trade.pod +++ b/docs/Trade.pod @@ -561,13 +561,13 @@ A session id. The unique id of the trade ministry. -=head2 view_waste_pushes ( session_id, building_id ) +=head2 view_waste_chain ( session_id, building_id ) -Returns a list of the waste pushes currently controlled by this Trade Ministry +Returns a list of the waste chains currently controlled by this Trade Ministry { "status" : { ... }, - "waste_pushes" : [ + "waste_chain" : [ { "id" : "id-goes-here", "star" : { @@ -583,7 +583,8 @@ Returns a list of the waste pushes currently controlled by this Trade Ministry ] } -There will usually only be one waste-push in action, to your local star. +There should only be one waste-chain in action, to your local star. There is no point in +setting up a waste-chain to any other star. The B is the amount of waste you wish to transfer per hour. @@ -594,6 +595,15 @@ it is at or above 100. Above 100 and you have more ship capacity than you need, still only transfer the specified B, it just means some ships will be idle or not fully filled. +=head2 update_waste_chain ( session_id, building_id, waste_chain_id, waste_hour) + +Change the B for the Waste Chain specified by B + +Returns the same as the call to B + +Note, the B may be modified as a result of changing the amount of +B if you don't have enough ships to transfer the full amount of waste. + =head2 add_waste_ship_to_fleet (session_id, building_id, ship_id ) Take a scow from your space port and add it to the ships servicing the waste pushes. diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index 1113e674..272fb7e3 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -52,6 +52,17 @@ use constant waste_production => 1; sub waste_chains { my $self = shift; + + # If there is no waste chain, then create a default one + my $waste_chain = Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({ planet_id => $self->body_id }); + if ($waste_chain->count == 0) { + Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->create({ + planet_id => $self->body_id, + star_id => $self->body->star_id, + waste_hour => 0, + percent_transferred => 0, + }); + } return Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({ planet_id => $self->body_id }); } @@ -153,16 +164,11 @@ sub recalc_waste_production { $waste_hour += $waste_chain->waist_hour; } $distance *= 2; -carp "RECALC: distance=[$distance] ship_speed=[$ship_speed] ship_capacity=[$ship_capacity]"; my $trips_per_hour = $distance ? ($ship_speed / $distance) : 0; -carp "RECALC: trips_per_hour=[$trips_per_hour]"; my $max_waste_hauled_per_hour = $trips_per_hour * $ship_capacity; -carp "RECALC: max_waste_hauled_per_hour=[$max_waste_hauled_per_hour]"; my $waste_hauled_per_hour = min($waste_hour, $max_waste_hauled_per_hour); -carp "RECALC: waste_hauled_per_hour=[$waste_hauled_per_hour]"; my $shipping_capacity = $max_waste_hauled_per_hour ? sprintf('%.0f',($waste_hour / $max_waste_hauled_per_hour) * 100) : -1; -carp "RECALC: shipping_capacity=[$shipping_capacity]"; $waste_chains->reset; while (my $waste_chain = $waste_chains->next) { diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 39d2b0e4..6a7f36c7 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -898,7 +898,7 @@ sub recalc_stats { } } if ($building->isa('Lacuna::DB::Result::Building::Trade')) { -carp "### Trade building ###"; + # Calculate the amount of waste to deduct based on the waste_chains my $waste_chains = Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({planet_id => $self->id}); while (my $waste_chain = $waste_chains->next) { my $waste_hour = 0; @@ -907,11 +907,9 @@ carp "### Trade building ###"; $waste_hour = $waste_chain->waste_hour; } else { -carp "[".$waste_chain->waste_hour."][".$waste_chain->percent_transferred."]"; $waste_hour = sprintf('%.0f',$waste_chain->waste_hour * $waste_chain->percent_transferred / 100); } } -carp "### Deduct [$waste_hour] waste per hour at capacity [".$waste_chain->percent_transferred."] ###"; $stats{waste_hour} -= $waste_hour; } } @@ -938,10 +936,10 @@ carp "### Deduct [$waste_hour] waste per hour at capacity [".$waste_chain->perce } # subtract ore consumption - foreach my $type (ORE_TYPES) { - my $method = $type.'_hour'; - $stats{$method} -= sprintf('%.0f', ($total_ore_production_hour) ? $ore_consumption_hour * $stats{$method} / $total_ore_production_hour: 0); - } + foreach my $type (ORE_TYPES) { + my $method = $type.'_hour'; + $stats{$method} -= sprintf('%.0f', ($total_ore_production_hour) ? $ore_consumption_hour * $stats{$method} / $total_ore_production_hour: 0); + } # overall ore production $stats{ore_hour} = $total_ore_production_hour - $ore_consumption_hour; diff --git a/lib/Lacuna/DB/Result/WasteChain.pm b/lib/Lacuna/DB/Result/WasteChain.pm index e82570d5..b06c403a 100644 --- a/lib/Lacuna/DB/Result/WasteChain.pm +++ b/lib/Lacuna/DB/Result/WasteChain.pm @@ -16,6 +16,23 @@ __PACKAGE__->add_columns( __PACKAGE__->belongs_to('star', 'Lacuna::DB::Result::Map::Star', 'star_id'); __PACKAGE__->belongs_to('planet', 'Lacuna::DB::Result::Map::Body', 'planet_id'); +sub get_status { + my ($self) = @_; + + my $star = $self->star; + return { + id => $self->id, + star => { + id => $star->id, + name => $star->name, + x => $star->x, + y => $star->y, + } + waste_hour => $self->waste_hour, + percent_transferred => $self->percent_transferred, + }; +} + no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/RPC/Building/Trade.pm b/lib/Lacuna/RPC/Building/Trade.pm index 908c40b5..65d42dc3 100644 --- a/lib/Lacuna/RPC/Building/Trade.pm +++ b/lib/Lacuna/RPC/Building/Trade.pm @@ -33,6 +33,60 @@ sub get_trade_ships { }; } +sub add_waste_ship_to_fleet { + my ($self, $session_id, $building_id, $ship_id) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id); + unless (defined $ship) { + confess [1002, "Ship not found."]; + } + unless ($ship->task eq 'Docked') { + confess [1009, "That ship is not available."]; + } + unless ($ship->hold_size > 0) { + confess [1009, 'That ship has no cargo hold.']; + } + unless ($ship->body_id eq $building->body_id) { + confess [1013, "You can't manage a ship that is not yours."]; + } + unless ($ship->type eq 'scow') { + confess [1009, 'You can only use a scow to a waste chain.']; + } + $building->add_waste_ship($ship); + return { + status =>$self->format_status($empire, $building->body), + }; +} + +sub remove_waste_ship_from_fleet { + my ($self, $session_id, $building_id, $ship_id) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id); + unless (defined $ship) { + confess [1002, "Ship not found."]; + } + unless ($ship->task eq 'Waste Chain') { + confess [1009, "That ship is not in a Waste Chain."]; + } + unless ($ship->body_id eq $building->body_id) { + confess [1013, "You can't manage a ship that is not yours."]; + } + + my $waste_chain = $building->waste_chains->search({},{rows => 1})->single; + if (defined $waste_chain) { + my $from = $building->body->star; + $building->send_waste_ship_home($from, $ship); + } + else { + $ship->land->update; + } + return { + status => $self->format_status($empire, $building->body), + }; +} + sub get_waste_ships { my ($self, $session_id, $building_id) = @_; my $empire = $self->get_empire_by_session($session_id); @@ -49,7 +103,6 @@ sub get_waste_ships { status => $self->format_status($empire, $building->body), ships => \@ships, }; - } sub view_waste_chain { @@ -68,6 +121,33 @@ sub view_waste_chain { }; } +sub update_waste_chain { + my ($self, $session_id, $building_id, $waste_chain_id, $waste_hour) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $body = $building->body; + unless ($waste_chain_id) { + confess [1002, "You must specify a waste chain id."]; + } + unless (defined $waste_hour) { + confess [1002, "You must specify an amount for waste_hour."]; + } + unless ($waste_hour >= 0) { + confess [1002, "Waste per Hour must be positive or zero."]; + } + + my $chain = $building->waste_chains->find($waste_chain_id); + unless ($chain) { + confess [1002, "That Waste Chain does not exist on this planet."]; + } + $chain->waste_hour(int($waste_hour)); + $chain->update; + $building->recalc_waste_production; + + return $self->view_waste_chain($session_id, $building_id); +} + + sub push_items { my ($self, $session_id, $building_id, $target_id, $items, $options) = @_; my $empire = $self->get_empire_by_session($session_id); @@ -219,7 +299,7 @@ sub add_to_market { -__PACKAGE__->register_rpc_method_names(qw(get_waste_ships view_waste_chain add_waste_ship_to_fleet remove_waste_ship_from_fleet report_abuse view_my_market view_market accept_from_market withdraw_from_market add_to_market push_items get_trade_ships get_stored_resources get_ships get_ship_summary get_prisoners get_plans get_plan_summary get_glyphs get_glyph_summary)); +__PACKAGE__->register_rpc_method_names(qw(get_waste_ships view_waste_chain add_waste_ship_to_fleet remove_waste_ship_from_fleet update_waste_chain report_abuse view_my_market view_market accept_from_market withdraw_from_market add_to_market push_items get_trade_ships get_stored_resources get_ships get_ship_summary get_prisoners get_plans get_plan_summary get_glyphs get_glyph_summary)); no Moose; -- 2.51.2 From 267a8a881e06244f725ae3f3f5f1770e2ac761cb Mon Sep 17 00:00:00 2001 From: ian docherty Date: Mon, 30 Jan 2012 16:29:27 -0500 Subject: [PATCH 04/14] syntax error --- lib/Lacuna/DB/Result/WasteChain.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Lacuna/DB/Result/WasteChain.pm b/lib/Lacuna/DB/Result/WasteChain.pm index b06c403a..97c86cfb 100644 --- a/lib/Lacuna/DB/Result/WasteChain.pm +++ b/lib/Lacuna/DB/Result/WasteChain.pm @@ -27,7 +27,7 @@ sub get_status { name => $star->name, x => $star->x, y => $star->y, - } + }, waste_hour => $self->waste_hour, percent_transferred => $self->percent_transferred, }; -- 2.51.2 From ed88796adb64fbfa340a0a85afbdf58beb85961e Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sun, 18 Mar 2012 06:42:20 -0400 Subject: [PATCH 05/14] Waste chain, minor bug fixes --- lib/Lacuna/DB/Result/Building/Trade.pm | 23 ----------------------- lib/Lacuna/DB/Result/WasteChain.pm | 1 + 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index d241cdfd..89f11df1 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -278,29 +278,6 @@ sub trade_ships { }); } -sub waste_ships { - my ($self) = @_; - return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ - task => 'Docked', - type => { 'in' => [qw(scow)] }, - body_id => $self->body_id, - }, - { - order_by=> {-desc => ['hold_size']} - }); -} - -sub waste_chains { - my ($self) = @_; - return Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({ - planet_id => $self->body_id, - star_id => $self->body->star_id, - }, - { - order_by => 'waste_hour', - }); -} - sub next_available_trade_ship { my ($self, $ship_id) = @_; if ($ship_id) { diff --git a/lib/Lacuna/DB/Result/WasteChain.pm b/lib/Lacuna/DB/Result/WasteChain.pm index 97c86cfb..803493a1 100644 --- a/lib/Lacuna/DB/Result/WasteChain.pm +++ b/lib/Lacuna/DB/Result/WasteChain.pm @@ -25,6 +25,7 @@ sub get_status { star => { id => $star->id, name => $star->name, + color => $star->color, x => $star->x, y => $star->y, }, -- 2.51.2 From b6028db9fd495d817797c2de774c517bef25f7e2 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sun, 1 Apr 2012 14:28:02 -0400 Subject: [PATCH 06/14] Added call to view_waste_chains --- lib/Lacuna/DB/Result/Building/Trade.pm | 2 +- lib/Lacuna/RPC/Building/Trade.pm | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index 89f11df1..c922219b 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -184,7 +184,7 @@ sub recalc_waste_production { before delete => sub { my ($self) = @_; $self->waste_ships->update({task=>'Docked'}); - $self->waste_chain->delete_all; + $self->waste_chains->delete_all; $self->body->needs_recalc(1); $self->body->update; }; diff --git a/lib/Lacuna/RPC/Building/Trade.pm b/lib/Lacuna/RPC/Building/Trade.pm index 444157b4..69904688 100644 --- a/lib/Lacuna/RPC/Building/Trade.pm +++ b/lib/Lacuna/RPC/Building/Trade.pm @@ -105,7 +105,7 @@ sub get_waste_ships { }; } -sub view_waste_chain { +sub view_waste_chains { my ($self, $session_id, $building_id) = @_; my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); @@ -300,7 +300,7 @@ sub add_to_market { -__PACKAGE__->register_rpc_method_names(qw(get_waste_ships view_waste_chain add_waste_ship_to_fleet remove_waste_ship_from_fleet update_waste_chain report_abuse view_my_market view_market accept_from_market withdraw_from_market add_to_market push_items get_trade_ships get_stored_resources get_ships get_ship_summary get_prisoners get_plans get_plan_summary get_glyphs get_glyph_summary)); +__PACKAGE__->register_rpc_method_names(qw(get_waste_ships view_waste_chains add_waste_ship_to_fleet remove_waste_ship_from_fleet update_waste_chain report_abuse view_my_market view_market accept_from_market withdraw_from_market add_to_market push_items get_trade_ships get_stored_resources get_ships get_ship_summary get_prisoners get_plans get_plan_summary get_glyphs get_glyph_summary)); no Moose; -- 2.51.2 From 16ee97c7ddc3ce14bb9bab8d993613a02328f80d Mon Sep 17 00:00:00 2001 From: ian docherty Date: Tue, 24 Apr 2012 15:37:16 -0400 Subject: [PATCH 07/14] Fixed Space Station count for alliances --- bin/summarize_server.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/summarize_server.pl b/bin/summarize_server.pl index 3e0672fe..0dee78ed 100644 --- a/bin/summarize_server.pl +++ b/bin/summarize_server.pl @@ -299,6 +299,7 @@ sub summarize_empires { empire_id => $empire->id, empire_name => $empire->name, alliance_id => $empire->alliance_id, + space_station_count => 0, ); my %map_data = ( empire_id => $empire->id, -- 2.51.2 From 017af16497b095684dd1df559516bc049fd49a8b Mon Sep 17 00:00:00 2001 From: ian docherty Date: Wed, 25 Apr 2012 08:07:11 -0400 Subject: [PATCH 08/14] Fixed exploit which increased Essentia. --- lib/Lacuna/DB/Result/Empire.pm | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 8c1b9d70..6afb6b0a 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -243,6 +243,7 @@ sub add_essentia { my ($self, $value, $note, $transaction_id, $from_id, $from_name) = @_; $from_id = 0 unless defined($from_id); $from_name = "" unless defined($from_name); + $self->discard_changes; $self->essentia( $self->essentia + $value ); Lacuna->db->resultset('Lacuna::DB::Result::Log::Essentia')->new({ empire_id => $self->id, -- 2.51.2 From ae9f57119d77a6f7eee414f9b98a4e8af7d09518 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sat, 28 Apr 2012 11:01:10 -0400 Subject: [PATCH 09/14] Improved the chance of finding a starter planet (temporary fix) --- lib/Lacuna/DB/Result/Empire.pm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 6afb6b0a..c8057d72 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -520,8 +520,10 @@ sub find_home_planet { # then order by distance } - # search - my $possible_planets = $planets->search(\%search, { rows => 10 }); + # search FIXME Note, this is temporary, should create a single query + # that returns all possible planets. 'rows 100' is not guaranteed to + # find a planet. + my $possible_planets = $planets->search(\%search, { rows => 100 }); # find an uncontested planet in the possible planets my $home_planet; -- 2.51.2 From 8b3d509d0d3e916f04e938ae493282300899fda0 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sat, 28 Apr 2012 11:03:15 -0400 Subject: [PATCH 10/14] Added tests, and fixes, for continuous waste push --- docs/Trade.pod | 4 +- lib/Lacuna/DB/Result/Building/Trade.pm | 81 ++++++++++++++++++++------ lib/Lacuna/DB/Result/Map/Body.pm | 10 ++++ lib/Lacuna/RPC/Building/Trade.pm | 13 +++-- t/360_Scows.t | 10 ++-- 5 files changed, 89 insertions(+), 29 deletions(-) diff --git a/docs/Trade.pod b/docs/Trade.pod index 09af5aca..cda8f0d2 100644 --- a/docs/Trade.pod +++ b/docs/Trade.pod @@ -561,7 +561,7 @@ A session id. The unique id of the trade ministry. -=head2 view_waste_chain ( session_id, building_id ) +=head2 view_waste_chains ( session_id, building_id ) Returns a list of the waste chains currently controlled by this Trade Ministry @@ -599,7 +599,7 @@ fully filled. Change the B for the Waste Chain specified by B -Returns the same as the call to B +Returns the same as the call to B Note, the B may be modified as a result of changing the amount of B if you don't have enough ships to transfer the full amount of waste. diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index e3cafc6c..8835c714 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -65,15 +65,66 @@ sub waste_chains { } return Lacuna->db->resultset('Lacuna::DB::Result::WasteChain')->search({ planet_id => $self->body_id }); } - sub supply_ships { my $self = shift; - return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ body_id => $self->body_id, task => 'Supply Chain' }); + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ + body_id => $self->body_id, + task => 'Supply Chain', + }); } +sub all_supply_ships { + my $self = shift; + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ + body_id => $self->body_id, + -or => { + task => 'Supply Chain', + -and => [ + task => 'Docked', + type => { '=', [ + 'cargo_ship', + 'smuggler_ship', + 'freighter', + 'dory', + 'barge', + 'galleon', + 'hulk', + 'hulk_fast', + 'hulk_huge', + ]} + ] + } + }); +} + +# Ships that are currently in a waste chain sub waste_ships { + my ($self) = @_; + + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ + body_id => $self->body_id, + task => 'Waste Chain', + }); +} + +# All ships that are either in a waste chain, or available to be so +sub all_waste_ships { my $self = shift; - return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ body_id => $self->body_id, task => 'Waste Chain' }); + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ + body_id => $self->body_id, + -or => { + task => 'Waste Chain', + -and => [ + task => 'Docked', + type => { '=', [ + 'scow', + 'scow_large', + 'scow_mega', + 'scow_fast', + ]} + ] + } + }); } sub max_chains { @@ -147,32 +198,24 @@ sub recalc_waste_production { my ($self) = @_; my $body = $self->body; - my $ship_speed = 0; - my $ship_capacity = 0; + # Determine the waste/hour/distance for ship + my $ship_wphpd = 0; my $ships = $self->waste_ships; while (my $ship = $ships->next) { - $ship_capacity += $ship->hold_size; - $ship_speed += $ship->speed; + $ship_wphpd += $ship->hold_size * $ship->speed; } - my $waste_chain_count = $self->waste_chains->count; + # Determine the waste/hour for waste chains + my $chain_wphpd = 0; my $waste_chains = $self->waste_chains; - my $waste_hour = 0; - my $distance = 0; while (my $waste_chain = $waste_chains->next) { - $distance += $body->calculate_distance_to_target($waste_chain->star); - $waste_hour += $waste_chain->waist_hour; + $chain_wphpd += $body->calculate_distance_to_target($waste_chain->star) * 2 * $waste_chain->waste_hour; } - $distance *= 2; - - my $trips_per_hour = $distance ? ($ship_speed / $distance) : 0; - my $max_waste_hauled_per_hour = $trips_per_hour * $ship_capacity; - my $waste_hauled_per_hour = min($waste_hour, $max_waste_hauled_per_hour); - my $shipping_capacity = $max_waste_hauled_per_hour ? sprintf('%.0f',($waste_hour / $max_waste_hauled_per_hour) * 100) : -1; + my $shipping_capacity = $chain_wphpd ? sprintf('%.0f',($ship_wphpd / $chain_wphpd) * 100) : 0; $waste_chains->reset; while (my $waste_chain = $waste_chains->next) { - $waste_chain->percent_ship_capacity($shipping_capacity); + $waste_chain->percent_transferred($shipping_capacity); $waste_chain->update; } diff --git a/lib/Lacuna/DB/Result/Map/Body.pm b/lib/Lacuna/DB/Result/Map/Body.pm index 47a35109..09837f4d 100644 --- a/lib/Lacuna/DB/Result/Map/Body.pm +++ b/lib/Lacuna/DB/Result/Map/Body.pm @@ -255,6 +255,16 @@ sub get_type { return $type; } +sub max_berth { + my ($self) = @_; + + my $max_berth = $self->buildings->search({ + class => 'Lacuna::DB::Result::Building::SpacePort', + efficiency => 100, + } )->get_column('level')->max; + return $max_berth ? $max_berth : 0; +} + sub get_status { my ($self) = @_; my %out = ( diff --git a/lib/Lacuna/RPC/Building/Trade.pm b/lib/Lacuna/RPC/Building/Trade.pm index 830555d5..a14585a8 100644 --- a/lib/Lacuna/RPC/Building/Trade.pm +++ b/lib/Lacuna/RPC/Building/Trade.pm @@ -51,8 +51,13 @@ sub add_waste_ship_to_fleet { unless ($ship->body_id eq $building->body_id) { confess [1013, "You can't manage a ship that is not yours."]; } - unless ($ship->type eq 'scow') { - confess [1009, 'You can only use a scow to a waste chain.']; + unless ($ship->type =~ m/^scow/) { + confess [1009, 'You can only add scows to a waste chain.']; + } + my $max_berth = $building->body->max_berth; + + unless ($ship->berth_level <= $max_berth) { + confess [1009, "You don't have a high enough berth for this ship."]; } $building->add_waste_ship($ship); return { @@ -96,7 +101,7 @@ sub get_waste_ships { # get the local star my $target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Star')->find($body->star_id); my @ships; - my $ships = $building->waste_ships; + my $ships = $building->all_waste_ships; while (my $ship = $ships->next) { push @ships, $ship->get_status($target); } @@ -145,7 +150,7 @@ sub update_waste_chain { $chain->update; $building->recalc_waste_production; - return $self->view_waste_chain($session_id, $building_id); + return $self->view_waste_chains($session_id, $building_id); } diff --git a/t/360_Scows.t b/t/360_Scows.t index 43d0a452..730cf6de 100644 --- a/t/360_Scows.t +++ b/t/360_Scows.t @@ -24,6 +24,8 @@ my $uni = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ }); $home->build_building($uni); $uni->finish_upgrade; +$empire->university_level(5); +$empire->update; my $seq = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ x => 0, @@ -118,7 +120,7 @@ $scow2->arrive; $scow2 = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({id=>$scow2->id},{rows=>1})->single; # pull the latest data on this ship $result = $tester->post('spaceport', 'view', [$session_id, $spaceport->id]); -is( $result->{result}{status}{empire}{most_recent_message}{subject}, "Scow Hit Target", 'Scow2 hit target' ); +is( $result->{result}{status}{empire}{most_recent_message}{subject}, "Ship Shot Down", 'Ship Shot Down' ); is( $scow2, undef, 'scow2 is undef' ); @@ -140,11 +142,11 @@ $scow4 = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({id=>$scow4- $result = $tester->post('spaceport', 'send_fleet', [$session_id, [$scow3->id, $scow4->id], { star_id => $home->star_id }]); is(scalar @{$result->{result}{fleet}}, 2, 'fleet sent'); # waste removed cannot be exact, due to variation in clock tick -cmp_ok($scow4->body->waste_stored, "<", 4000, 'correct waste removed'); -cmp_ok($scow4->body->waste_stored, ">", 3900, 'correct waste removed'); +cmp_ok($scow4->body->waste_stored, "<", 4200, 'correct waste removed'); +cmp_ok($scow4->body->waste_stored, ">", 4000, 'correct waste removed'); #is($scow4->body->waste_stored, 3983, 'correct waste removed'); END { - TestHelper->clear_all_test_empires; +# TestHelper->clear_all_test_empires; } -- 2.51.2 From 68d8d3647958c9af4b68d44aef91b69c5e1638f0 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sat, 28 Apr 2012 16:22:08 -0400 Subject: [PATCH 11/14] Final tests for Waste Chains --- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 2 +- t/420_Waste_Chain.t | 215 ++++++++++++++++++++++++ 2 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 t/420_Waste_Chain.t diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index d349cddb..331bce6e 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -876,7 +876,7 @@ has total_ore_concentration => ( sub recalc_stats { my ($self) = @_; - carp "#### recalc_stats ####\n"; +# carp "#### recalc_stats ####\n"; my %stats = ( needs_recalc => 0 ); my $buildings = $self->buildings; diff --git a/t/420_Waste_Chain.t b/t/420_Waste_Chain.t new file mode 100644 index 00000000..7f8cf3b3 --- /dev/null +++ b/t/420_Waste_Chain.t @@ -0,0 +1,215 @@ +use lib '../lib'; + +use strict; +use warnings; + +use Test::More tests => 25; +use Test::Deep; +use Data::Dumper; +use 5.010; +use DateTime; + +use TestHelper; +TestHelper->clear_all_test_empires; + +my $tester = TestHelper->new->generate_test_empire; +my $session_id = $tester->session->id; +my $empire = $tester->empire; +my $home = $empire->home_planet; +my $command = $home->command; + +my $result; + +my $uni = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ + x => 0, + y => -1, + class => 'Lacuna::DB::Result::Building::University', + level => 5, +}); +$home->build_building($uni); +$uni->finish_upgrade; +$empire->university_level(5); +$empire->update; + +my $seq = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ + x => 0, + y => -2, + class => 'Lacuna::DB::Result::Building::Waste::Sequestration', + level => 29, +}); +$home->build_building($seq); +$seq->finish_upgrade; + +my $trade = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ + x => 0, + y => -3, + class => 'Lacuna::DB::Result::Building::Trade', + level => 5, +}); + +$home->build_building($trade); +$trade->finish_upgrade; + +$home->ore_hour(5000); +$home->water_hour(5000); +$home->energy_hour(5000); +$home->algae_production_hour(5000); +$home->ore_capacity(5000); +$home->energy_capacity(5000); +$home->food_capacity(5000); +$home->water_capacity(5000); +$home->waste_capacity(8000); +$home->bauxite_stored(5000); +$home->algae_stored(5000); +$home->energy_stored(5000); +$home->water_stored(5000); +$home->waste_stored(10000000); +$home->needs_recalc(0); +$home->tick; +$home->update; + +cmp_ok($home->waste_stored, "=", 100000, 'correct original waste'); + +$empire->is_isolationist(0); +$empire->update; + +$result = $tester->post('spaceport', 'build', [$session_id, $home->id, 0, 1]); +my $spaceport = $tester->get_building($result->{result}{building}{id}); +$spaceport->finish_upgrade; + +my $shipyard = Lacuna::db->resultset('Lacuna::DB::Result::Building')->new({ + x => 0, + y => 2, + class => 'Lacuna::DB::Result::Building::Shipyard', + level => 20, +}); +$home->build_building($shipyard); +$shipyard->finish_upgrade; + +$home->discard_changes; +$home->waste_stored(1000000); +$home->update; + +my $scow = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({type=>'scow'}); +$shipyard->build_ship($scow); +my $scow_large = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({type=>'scow_large'}); +$shipyard->build_ship($scow_large); + +my $finish = DateTime->now; + +Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({shipyard_id=>$shipyard->id})->update({date_available=>$finish}); + +$result = $tester->post('spaceport', 'view', [$session_id, $spaceport->id]); + +Lacuna->cache->set('captcha', $session_id, { guid => 1111, solution => 1111 }, 60 * 30 ); + +$result = $tester->post('spaceport', 'send_ship', [$session_id, $scow->id, { star_id => $home->star_id } ] ); +ok($result->{result}{ship}{date_arrives}, "scow sent to star id " . $home->star_id); +$scow->discard_changes; + +is( $scow->foreign_star_id, $home->star_id, 'scow is headed to the correct foreign star id' ); +is( $scow->foreign_body_id, undef, 'scow does not have a foreign body id' ); +#diag(Dumper($scow->payload)); + +$scow->arrive; +is( $scow->task, 'Travelling', 'scow is travelling' ); +is( $scow->direction, 'in', 'scow is headed home' ); + +$scow->discard_changes; +$scow->arrive; +is( $scow->task, 'Docked', 'scow is docked' ); +cmp_deeply( $scow->payload, {}, 'no payload' ); + +$result = $tester->post('captcha','solve', [$session_id, 1111, 1111]); +is($result->{result}, 1, 'Solved captcha'); + +$home->discard_changes; +cmp_ok($home->waste_stored, "<", 950_000, 'correct waste removed'); +cmp_ok($home->waste_stored, ">", 940_000, 'correct waste removed'); + +#diag("Waste per hour [".$home->waste_hour."]"); +my $old_waste_per_hour = $home->waste_hour; + +$result = $tester->post('trade','get_waste_ships', [$session_id, $trade->id]); +#diag(Dumper($result->{result}{ships})); +my @temp = map { {task => $_->{task}, type => $_->{type} } } @{$result->{result}{ships}}; +my @comp = ({type=>'scow',task=>'Docked'},{type=>'scow_large',task=>'Docked'}); +cmp_deeply(\@comp,\@temp, "All ships docked"); + +my $original_waste_hour = $result->{result}{status}{body}{waste_hour}; +#diag(Dumper($result->{result})); +#diag("original waste per hour [$original_waste_hour]"); +is($old_waste_per_hour, $original_waste_hour, "Compare waste hour"); +my @ship_types = sort map {$_->{type}} @{$result->{result}{ships}}; +#diag(Dumper(\@ship_types)); +cmp_deeply(\@ship_types, [qw(scow scow_large)], "scow ship types"); +# set up a waste chain with the scow + +$result = $tester->post('trade','view_waste_chains', [$session_id, $trade->id]); +my $waste_chain = $result->{result}{waste_chain}[0]; +#diag(Dumper($waste_chain)); +is($waste_chain->{waste_hour}, 0, "Waste chain is empty"); +is($waste_chain->{percent_transferred}, 0, "Zero percent"); +my $waste_chain_id = $waste_chain->{id}; +$result = $tester->post('trade','add_waste_ship_to_fleet',[$session_id, $trade->id, $scow->id]); +my $new_waste_per_hour = $result->{result}{status}{body}{waste_hour}; +is($old_waste_per_hour, $new_waste_per_hour, "Waste per hour has not yet changed"); + +# add waste to the chain +$result = $tester->post('trade','update_waste_chain', [$session_id, $trade->id, $waste_chain_id, 1000]); +$new_waste_per_hour = $result->{result}{status}{body}{waste_hour}; +#diag("Old waste hour [$old_waste_per_hour], New waste hour [$new_waste_per_hour]"); +is($old_waste_per_hour - $new_waste_per_hour, 1000, "Correct amount of waste deducted"); + +$result = $tester->post('trade','view_waste_chains', [$session_id, $trade->id]); +$waste_chain = $result->{result}{waste_chain}[0]; +cmp_ok($waste_chain->{percent_transferred}, "<", 7300, "Too much shipping capacity"); +cmp_ok($waste_chain->{percent_transferred}, ">", 7200, "Too much shipping capacity"); + +# try to add a scow large (without the requisite berth level) +$result = $tester->post('trade','add_waste_ship_to_fleet',[$session_id, $trade->id, $scow_large->id]); +is($result->{error}{code}, 1009, "Scow large not allowed with berth level"); + +# Increase Space Port level and try again +$spaceport = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ + x => 1, + y => -1, + class => 'Lacuna::DB::Result::Building::SpacePort', + level => 15, +}); + +$home->build_building($spaceport); +$spaceport->finish_upgrade; + +$result = $tester->post('trade','add_waste_ship_to_fleet',[$session_id, $trade->id, $scow_large->id]); +$new_waste_per_hour = $result->{result}{status}{body}{waste_hour}; +#diag("New-new waste per hour [$new_waste_per_hour]"); + +$result = $tester->post('trade','get_waste_ships', [$session_id, $trade->id]); +@temp = map { {task => $_->{task}, type => $_->{type} } } @{$result->{result}{ships}}; +@comp = ({type=>'scow',task=>'Waste Chain'},{type=>'scow_large',task=>'Waste Chain'}); +cmp_deeply(\@comp,\@temp, "All ships in waste chain"); + +# transport enough waste to get to zero per hour +my $total_waste_per_hour = 1000 + $new_waste_per_hour; + +$result = $tester->post('trade','update_waste_chain', [$session_id, $trade->id, $waste_chain_id, $total_waste_per_hour]); +$new_waste_per_hour = $result->{result}{status}{body}{waste_hour}; +#diag("New-new waste per hour [$new_waste_per_hour]"); +is($new_waste_per_hour, 0, "Waste set to zero"); + +# remove one of the ships +$result = $tester->post('trade','remove_waste_ship_from_fleet', [$session_id, $trade->id, $scow->id]); +$new_waste_per_hour = $result->{result}{status}{body}{waste_hour}; +#diag("New-new waste per hour [$new_waste_per_hour]"); +is($new_waste_per_hour, 0, "Waste still at zero after ship removed"); + +# remove all ships +$result = $tester->post('trade','remove_waste_ship_from_fleet', [$session_id, $trade->id, $scow_large->id]); +my $final_waste_per_hour = $result->{result}{status}{body}{waste_hour}; +#diag("Final waste per hour [$final_waste_per_hour]"); +is($final_waste_per_hour, $total_waste_per_hour, "Waste back to normal"); + +END { +# TestHelper->clear_all_test_empires; +} -- 2.51.2 From 9dc0f95eba27e2d62497069f979e0fc0a413d15a Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sat, 28 Apr 2012 16:30:29 -0400 Subject: [PATCH 12/14] Added sql for continuous push (of waste) --- var/upgrades/{cp_3.08xx.sql => 3.0849.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename var/upgrades/{cp_3.08xx.sql => 3.0849.sql} (100%) diff --git a/var/upgrades/cp_3.08xx.sql b/var/upgrades/3.0849.sql similarity index 100% rename from var/upgrades/cp_3.08xx.sql rename to var/upgrades/3.0849.sql -- 2.51.2 From 6d0e8db0848ee6c2b8753e7c8f157499a32148c2 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sat, 28 Apr 2012 16:41:21 -0400 Subject: [PATCH 13/14] Updated changes.txt and bumped version number --- lib/Lacuna.pm | 2 +- var/www/public/changes.txt | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Lacuna.pm b/lib/Lacuna.pm index beaa09ae..8369caf8 100644 --- a/lib/Lacuna.pm +++ b/lib/Lacuna.pm @@ -7,7 +7,7 @@ use Config::JSON; useall __PACKAGE__; -our $VERSION = 3.0848; +our $VERSION = 3.0849; my $config = Config::JSON->new('/data/Lacuna-Server/etc/lacuna.conf'); my $db = Lacuna::DB->connect($config->get('db/dsn'),$config->get('db/username'),$config->get('db/password'), { mysql_enable_utf8 => 1}); diff --git a/var/www/public/changes.txt b/var/www/public/changes.txt index 1e14cb5e..a7d7c389 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -1,3 +1,6 @@ +3.0849: + - Add: API only, continuous push (waste chain) to local star. Note. no UI support yet. + 3.0848: - Add: Hulk Large, Hulk Fast, Scow Fast, Scow Large, and Scow Mega Added. - Fix: Scows and Trade Ships limited to Spaceport Level determined by Berth. -- 2.51.2 From dabb96b4b055dc22d43cbdf1e13377cdb5e7f39a Mon Sep 17 00:00:00 2001 From: JT Smith Date: Mon, 30 Apr 2012 20:57:56 -0500 Subject: [PATCH 14/14] body not planet --- var/upgrades/3.0849.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/var/upgrades/3.0849.sql b/var/upgrades/3.0849.sql index 91b2c459..d97c9432 100644 --- a/var/upgrades/3.0849.sql +++ b/var/upgrades/3.0849.sql @@ -8,7 +8,7 @@ create table waste_chain ( primary key (id), key wc_idx_planet_id (planet_id), key wc_idx_star_id (star_id), - constraint wc_fk_planet_id foreign key (planet_id) references planet (id), + constraint wc_fk_planet_id foreign key (planet_id) references body (id), constraint wc_fk_star_id foreign key (star_id) references star (id) ); -- 2.51.2