diff --git a/docs/SpacePort.pod b/docs/SpacePort.pod index f5d59b32..fa1f7aa9 100644 --- a/docs/SpacePort.pod +++ b/docs/SpacePort.pod @@ -31,7 +31,7 @@ This method is extended to include a list of docked ships. =head2 view_all_ships ( session_id, building_id ) -Shows you the ships that are working in the mining fleet, and available to work in the mining fleet. +Shows you all your ships whatever they are doing. { "ships" : [ @@ -73,6 +73,51 @@ A session id. The unique id of the space port. +=head2 view_foreign_ships ( session_id, building_id ) + +Shows you all the foreign ships that are incoming. However, the list is filtered by the stealth of the ship vs the level of the SpacePort. The formula is: + + 300 * SpacePort Level >= Ship Stealth + +If your SpacePort exceeds the Ship's Stealth, then you'll see it incoming. Otherwise you won't. + + { + "ships" : [ + { + "id" : "id-goes-here", + "name" : "CS3", + "type_human" : "Cargo Ship", + "type" : "cargo_ship", + "date_arrives" : "02 01 2010 10:08:33 +0600", + "from" : { + "id" : "id-goes-here", + "name" : "Earth", + "empire" : { + "id" : "id-goes-here", + "name" : "Earthlings" + } + } + }, + ... + ], + "number_of_ships" : 13, + "status" : { ... } + } + +The C block is only included if + + 100 * SpacePort Level >= Ship Stealth + +=head3 session_id + +A session id. + +=head3 building_id + +The unique id of the space port. + + + =head2 get_ships_for ( session_id, from_body_id, target ) Provides a list of incoming ships and ships that are available to send to a specific target. Use with C. diff --git a/lib/Lacuna/DB/Result/Building/SpacePort.pm b/lib/Lacuna/DB/Result/Building/SpacePort.pm index c350c004..83fc2ce2 100644 --- a/lib/Lacuna/DB/Result/Building/SpacePort.pm +++ b/lib/Lacuna/DB/Result/Building/SpacePort.pm @@ -13,6 +13,17 @@ around 'build_tags' => sub { __PACKAGE__->has_many('ships', 'Lacuna::DB::Result::Ships', 'spaceport_id'); +sub foreign_ships { + my ($self) = @_; + return Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search( + { + foreign_body_id => $self->body_id, + direction => 'out', + task => 'Travelling', + } + ); +} + sub send_probe { my ($self, $star) = @_; return $self->send_ship($star, 'probe'); diff --git a/lib/Lacuna/RPC/Building/SpacePort.pm b/lib/Lacuna/RPC/Building/SpacePort.pm index 44c0d492..5bb88083 100644 --- a/lib/Lacuna/RPC/Building/SpacePort.pm +++ b/lib/Lacuna/RPC/Building/SpacePort.pm @@ -507,6 +507,49 @@ sub view_all_ships { }; } +sub view_foreign_ships { + my ($self, $session_id, $building_id, $page_number) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + $page_number ||= 1; + my @fleet; + my $now = DateTime->now; + my $ships = $building->foreign_ships->search({}, {rows=>25, page=>$page_number, join => 'body' }); + while (my $ship = $ships->next) { + if ($ship->date_available <= $now) { + $ship->body->tick; + } + else { + if ($building->level * 300 >= $ship->stealth) { + my $from = {}; + if ($building->level * 100 >= $ship->stealth) { + $from = { + id => $ship->body->id, + name => $ship->body->name, + empire => { + id => $ship->body->empire->id, + name => $ship->body->empire->name, + }, + }; + } + push @fleet, { + id => $self->id, + name => $self->name, + type_human => $self->type_formatted, + type => $self->type, + date_arrives => $self->date_available_formatted, + from => $from, + }; + } + } + } + return { + status => $self->format_status($empire, $building->body), + number_of_ships => $ships->pager->total_entries, + ships => \@fleet, + }; +} + sub name_ship { my ($self, $session_id, $building_id, $ship_id, $name) = @_; Lacuna::Verify->new(content=>\$name, throws=>[1005, 'Invalid name for a ship.']) @@ -567,7 +610,7 @@ around 'view' => sub { return $out; }; -__PACKAGE__->register_rpc_method_names(qw(get_ships_for send_ship scuttle_ship name_ship fetch_spies send_spies get_available_spy_ships_for_fetch get_available_spy_ships get_my_available_spies send_probe send_spy_pod send_colony_ship send_mining_platform_ship view_ships_travelling view_all_ships)); +__PACKAGE__->register_rpc_method_names(qw(view_foreign_ships get_ships_for send_ship scuttle_ship name_ship fetch_spies send_spies get_available_spy_ships_for_fetch get_available_spy_ships get_my_available_spies send_probe send_spy_pod send_colony_ship send_mining_platform_ship view_ships_travelling view_all_ships)); no Moose; diff --git a/t/SpacePort.t b/t/SpacePort.t index 59b38ddd..28e01ab9 100644 --- a/t/SpacePort.t +++ b/t/SpacePort.t @@ -1,5 +1,5 @@ use lib '../lib'; -use Test::More tests => 8; +use Test::More tests => 9; use Test::Deep; use Data::Dumper; use 5.010; @@ -53,7 +53,10 @@ $result = $tester->post('spaceport', 'view_ships_travelling', [$session_id, $spa is(ref $result->{result}{ships_travelling}, 'ARRAY', "can see travelling ships"); $result = $tester->post('spaceport', 'view_all_ships', [$session_id, $spaceport->id]); -is(ref $result->{result}{ships}, 'ARRAY', "can see all ships"); +is(ref $result->{result}{ships}, 'ARRAY', "can see all my ships"); + +$result = $tester->post('spaceport', 'view_foreign_ships', [$session_id, $spaceport->id]); +is(ref $result->{result}{ships}, 'ARRAY', "can see all foreign ships"); $result = $tester->post('spaceport', 'get_ships_for', [$session_id, $home->id, { body_id => $home->id }]); is(ref $result->{result}{available}, 'ARRAY', "can see what ships are available to send");