From 0618d31ccc2b4f5b171c0e20a9a48c425ab7eb5c Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Wed, 23 Mar 2011 22:31:15 -0500 Subject: [PATCH] - API: Changed recallable to orbiting in the spaceport's get_ships_for results. - API: Added view_ships_orbiting() to spaceport to allow viewing the ships orbiting your planet. --- docs/SpacePort.pod | 63 ++++++++++++++++++++++++- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 15 ++++++ lib/Lacuna/RPC/Building/SpacePort.pm | 31 +++++++++--- t/AlliedDefense.t | 4 +- t/SpacePort.t | 9 ++-- var/www/public/changes.txt | 2 + 6 files changed, 111 insertions(+), 13 deletions(-) diff --git a/docs/SpacePort.pod b/docs/SpacePort.pod index a8ef3a23..cc59d354 100644 --- a/docs/SpacePort.pod +++ b/docs/SpacePort.pod @@ -265,7 +265,7 @@ Provides a list of incoming ships and ships that are available to send to a spec }, ... ], - "recallable" : [ + "orbiting" : [ { "id" : "id-goes-here", "name" : "F13", @@ -304,7 +304,7 @@ Provides a list of incoming ships and ships that are available to send to a spec "status" : { ... } } -The C block tells you about ships that are docked, but are for some reason not available to be used. The C block will be returned only if the target is body and tells you about fighters that are defending and can be recalled. The C block will be returned only if the target is an asteroid that has platforms on it. +The C block tells you about ships that are docked, but are for some reason not available to be used. The C block will be returned only if the target is body and tells you about fighters that are defending and can be recalled. The C block will be returned only if the target is an asteroid that has platforms on it. =head3 session_id @@ -629,6 +629,65 @@ Defaults to 1. An integer representing which page of ships travelling to view. E +=head2 view_ships_orbiting ( session_id, building_id, [ page_number ]) + +Returns a list of the ships that are orbiting this planet. + + { + "status" : { ... }, + "number_of_ships_orbiting" : 30, + "ships_orbiting" : [ + { + "id" : "id-goes-here", + "type" : "spy_shuttle", + "type_human" : "Spy Shuttle", + "task" : "Orbiting", + "from" : { + "id" : "id-goes-here", + "type" : "body", + "name" : "New Earth", + }, + "to" : { + "id" : "id-goes-here", + "type" : "body", + "name" : "Earth", + } + }, + { + "id" : "id-goes-here", + "type" : "fighter", + "type_human" : "Fighter", + "task" : "Defend", + "from" : { + "id" : "id-goes-here", + "type" : "body", + "name" : "New Earth", + }, + "to" : { + "id" : "id-goes-here", + "type" : "body", + "name" : "Earth", + } + }, + ... + ] + } + + +=head3 session_id + +A session id. + +=head3 building_id + +The unique id of the space port. + +=head3 page_number + +Defaults to 1. An integer representing which page of ships orbiting to view. Each page shows 25 ships. + + + =head2 prepare_send_spies ( session_id, on_body_id, to_body_id ) Gathers the information needed to call the C method. diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index c8838a5e..a6fbdf66 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -36,6 +36,21 @@ sub ships_travelling { ); } +sub ships_orbiting { + my ($self, $where, $reverse) = @_; + my $order = '-asc'; + if ($reverse) { + $order = '-desc'; + } + $where->{task} = { in => ['Defend','Orbiting'] }; + return $self->ships->search( + $where, + { + order_by => { $order => 'date_available' }, + } + ); +} + # CLAIM sub claim { diff --git a/lib/Lacuna/RPC/Building/SpacePort.pm b/lib/Lacuna/RPC/Building/SpacePort.pm index 3c070127..21d09ffd 100644 --- a/lib/Lacuna/RPC/Building/SpacePort.pm +++ b/lib/Lacuna/RPC/Building/SpacePort.pm @@ -95,9 +95,9 @@ sub get_ships_for { ); unless ($target->isa('Lacuna::DB::Result::Map::Star')) { - my @recallable; - my $recallable_rs = $ships->search({task => [qw(Defend Orbiting)], body_id => $body->id, foreign_body_id => $target->id }); - while (my $ship = $recallable_rs->next) { + my @orbiting; + my $orbiting_rs = $ships->search({task => [qw(Defend Orbiting)], body_id => $body->id, foreign_body_id => $target->id }); + while (my $ship = $orbiting_rs->next) { $ship->body($body); eval{ $ship->can_recall() }; my $reason = $@; @@ -106,9 +106,9 @@ sub get_ships_for { next; } $ship->body($body); - push @recallable, $ship->get_status($target); + push @orbiting, $ship->get_status($target); } - $out{recallable} = \@recallable; + $out{orbiting} = \@orbiting; } if ($target->isa('Lacuna::DB::Result::Map::Body::Asteroid')) { @@ -415,6 +415,25 @@ sub fetch_spies { +sub view_ships_orbiting { + 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 $body = $building->body; + my @orbiting; + my $ships = $body->ships_orbiting->search(undef, {rows=>25, page=>$page_number}); + while (my $ship = $ships->next) { + $ship->body($body); + push @orbiting, $ship->get_status; + } + return { + status => $self->format_status($empire, $body), + number_of_ships_orbiting => $ships->pager->total_entries, + ships_orbiting => \@orbiting, + }; +} + sub view_ships_travelling { my ($self, $session_id, $building_id, $page_number) = @_; my $empire = $self->get_empire_by_session($session_id); @@ -662,7 +681,7 @@ around 'view' => sub { return $out; }; -__PACKAGE__->register_rpc_method_names(qw(view_foreign_ships get_ships_for send_ship send_fleet recall_ship recall_spies scuttle_ship name_ship prepare_fetch_spies fetch_spies prepare_send_spies send_spies view_ships_travelling view_all_ships)); +__PACKAGE__->register_rpc_method_names(qw(view_foreign_ships get_ships_for send_ship send_fleet recall_ship recall_spies scuttle_ship name_ship prepare_fetch_spies fetch_spies prepare_send_spies send_spies view_ships_orbiting view_ships_travelling view_all_ships)); no Moose; diff --git a/t/AlliedDefense.t b/t/AlliedDefense.t index 52dfc466..057f8501 100644 --- a/t/AlliedDefense.t +++ b/t/AlliedDefense.t @@ -254,7 +254,7 @@ for my $i ( 0 .. 1 ) { $fighter->arrive; $result = $tester->post('spaceport', 'get_ships_for', [$tester{session_id}, $tester{home}->id, { x => $asteroid->{x}, y => $asteroid->{y} } ]); - is( @{ $result->{result}{recallable} }, 1, 'one ship is recallable' ); + is( @{ $result->{result}{orbiting} }, 1, 'one ship is orbiting' ); # Send a fighter to defend the closest planet my $fighter2 = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({body_id => $tester{home}->id, type=>'fighter', task=>'Docked'},{rows=>1})->single; @@ -267,7 +267,7 @@ for my $i ( 0 .. 1 ) { $tester->{fighter2} = $fighter2->id; $result = $tester->post('spaceport', 'get_ships_for', [$tester{session_id}, $tester{home}->id, { x => $planet->{x}, y => $planet->{y} } ]); - is( @{ $result->{result}{recallable} }, 1, 'one ship is recallable' ); + is( @{ $result->{result}{orbiting} }, 1, 'one ship is orbiting' ); $result = $tester->post('spaceport', 'view_all_ships', [$tester{session_id}, $tester->{spaceport_id}]); for my $ship ( @{ $result->{result}{ships} } ) { diff --git a/t/SpacePort.t b/t/SpacePort.t index ef1265ee..97c9c777 100644 --- a/t/SpacePort.t +++ b/t/SpacePort.t @@ -1,5 +1,5 @@ use lib '../lib'; -use Test::More tests => 20; +use Test::More tests => 21; use Test::Deep; use Data::Dumper; use 5.010; @@ -66,7 +66,10 @@ is(ref $result->{result}{ships}, 'ARRAY', "can see all foreign ships"); $result = $tester->post('spaceport', 'get_ships_for', [$session_id, $home->id, { body_id => $enemy->empire->home_planet->id }]); is(ref $result->{result}{available}, 'ARRAY', "can see what ships are available to send"); -is(ref $result->{result}{recallable}, 'ARRAY', "can see what ships are available to recall"); +is(ref $result->{result}{orbiting}, 'ARRAY', "can see what ships are orbiting"); + +$result = $tester->post('spaceport', 'view_ships_orbiting', [$session_id, $spaceport->id]); +is(ref $result->{result}{ships_orbiting}, 'ARRAY', "can see orbiting ships"); my $shipyard = Lacuna::db->resultset('Lacuna::DB::Result::Building')->new({ x => 0, @@ -182,7 +185,7 @@ for my $spy_id ( @$spies ) { } $result = $tester->post('spaceport', 'get_ships_for', [$session_id, $home->id, { body_id => $enemy->empire->home_planet->id }]); -is(ref $result->{result}{recallable}, 'ARRAY', "can see what ships are available to recall"); +is(ref $result->{result}{orbiting}, 'ARRAY', "can see what ships are available to recall"); $result = $tester->post('spaceport', 'prepare_fetch_spies', [$session_id, $enemy->empire->home_planet->id, $home->id ]); is( ref $result->{result}{ships}, 'ARRAY', "can prepare for fetch spies"); diff --git a/var/www/public/changes.txt b/var/www/public/changes.txt index 5b74920e..1c1773c9 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -1,6 +1,8 @@ 3.0300 - Add: Added Mercenaries Guild building. - Fix: Spies require 350 cargo space consistently through the API and docs. + - API: Changed recallable to orbiting in the spaceport's get_ships_for results. + - API: Added view_ships_orbiting() to spaceport to allow viewing the ships orbiting your planet. 3.0201 - Add: Changed crater to dent when on space station. -- 2.51.2