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;