From 6bbbbe65da414c49167996465bc5f7bfe192c4f2 Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Sat, 31 Oct 2015 23:15:34 -0600 Subject: [PATCH] Add bodies list to empire's get_status. This is so the UI can eventually display all the bodies that the current user can use. --- docs/Empire.pod | 21 ++++++ lib/LR.pm | 2 +- lib/Lacuna/DB/Result/Empire.pm | 49 ++++++++++++- lib/Lacuna/RPC.pm | 110 +++++++++++++++------------- lib/Lacuna/RPC/Building/Shipyard.pm | 8 +- lib/Lacuna/RPC/Empire.pm | 2 +- 6 files changed, 134 insertions(+), 58 deletions(-) diff --git a/docs/Empire.pod b/docs/Empire.pod index ee4d3959..231a4657 100644 --- a/docs/Empire.pod +++ b/docs/Empire.pod @@ -214,6 +214,27 @@ B You should probably B call this method directly, as it is a wast "server" : { ... }, "empire" : { "id" : "xxxx", + "bodies" : { + "mine" : [ + # bodies are provided sorted by name already. + { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, + ... + ], + "station" : [ + { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, + ... + ], + "babies" : { + "baby name" : [ + { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, + ... + ], + "another baby name" : [ + { "id" : "xxxx", "name" : "...", "x": "#", "y": "#" }, + ... + ] + } + }, "colonies" : { "id-goes-here" : "Earth", "id-goes-here" : "Mars" diff --git a/lib/LR.pm b/lib/LR.pm index bcaddba5..ee321dce 100644 --- a/lib/LR.pm +++ b/lib/LR.pm @@ -74,7 +74,7 @@ sub _session $session = $_[0]->start_session; $_[0] = $session->id; } - if (ref $_[0] eq 'HASH' && exists $_[0]->{session_id} && ref $_[0]->{session_id}) + if (ref $_[0] eq 'HASH' && exists $_[0]->{session_id} && ref $_[0]->{session_id} eq 'Lacuna::DB::Result::Empire') { # create a session for it. $session = $_[0]->{session_id}->start_session; diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index ad05a27c..0ae88120 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -538,22 +538,66 @@ has rpc_rate => ( sub get_status { my ($self) = @_; - my $planet_rs = $self->planets; + my $planet_rs = $self->planets->search({},{ -order_by => 'name' }); if ($self->alliance_id) { - $planet_rs = Lacuna->db->resultset('Map::Body')->search({-or => { empire_id => $self->id, alliance_id => $self->alliance_id }}); + $planet_rs = Lacuna->db->resultset('Map::Body')-> + search( + {-or => { empire_id => $self->id, alliance_id => $self->alliance_id }}, + { -order_by => 'name' }, + ); } my %planets; my %stations; my %colonies; + my %bodies; + while (my $planet = $planet_rs->next) { $planets{$planet->id} = $planet->name; + my $type = 'mine'; if ($planet->get_type eq 'space station') { $stations{$planet->id} = $planet->name; + $type = 'station' } else { $colonies{$planet->id} = $planet->name; } + + push @{$bodies{$type}}, { + id => $planet->id, + name => $planet->name, + x => $planet->x, + y => $planet->y, #,,, + }; + } + + # shouldn't have to check this once sitter_password goes away. + if ($self->current_session() && + !$self->current_session()->is_sitter()) + { + $planet_rs = Lacuna->db->resultset('Map::Body')-> + search( + { + 'sitterauths.sitter_id' => $self->id, + 'me.class' => { '!=' => 'Lacuna::DB::Result::Map::Body::Planet::Station' }, + }, + { + join => { empire => 'sitterauths' }, + -order_by => 'name', + '+select' => [ qw/empire.name/ ], + '+as' => [ qw/empire_name/ ], + }); + while (my $planet = $planet_rs->next) + { + my $empire_name = $planet->get_column('empire_name'); + push @{$bodies{babies}{$empire_name}}, { + id => $planet->id, + name => $planet->name, + x => $planet->x, + y => $planet->y, #,,, + }; + } } + my $embassy = $self->highest_embassy; my $embassy_id = defined $embassy ? $embassy->id : undef; @@ -571,6 +615,7 @@ sub get_status { planets => \%planets, stations => \%stations, colonies => \%colonies, + bodies => \%bodies, next_colony_cost => $self->next_colony_cost("colony_ship"), next_colony_srcs => $self->next_colony_cost("short_range_colony_ship"), next_station_cost => $self->next_colony_cost("space_station"), diff --git a/lib/Lacuna/RPC.pm b/lib/Lacuna/RPC.pm index e5008a6c..2404ca85 100644 --- a/lib/Lacuna/RPC.pm +++ b/lib/Lacuna/RPC.pm @@ -31,7 +31,7 @@ sub get_session { $opts = { session_id => $session->id } unless ref $opts; if ($opts->{building_id}) { - my $building = $self->get_building($session->empire, + my $building = $self->get_building($session, $opts->{building_id}, %$opts); $session->current_building($building); @@ -47,7 +47,7 @@ sub get_session { } } elsif ($opts->{body_id}) { - my $body = $self->get_body($session->empire, + my $body = $self->get_body($session, $opts->{body_id}); $session->clear_building; $session->current_body($body); @@ -107,6 +107,10 @@ sub get_session { } if ($session->current_body) { + if ($session->current_body->id eq $session->current_empire->home_planet_id) { + $session->current_empire->home_planet($session->current_body); + } + $session->current_body->tick; # do we need to discard the changes? } @@ -115,66 +119,72 @@ sub get_session { } sub get_body { # makes for uniform error handling, and prevents staleness - my ($self, $empire, $body_id) = @_; + my ($self, $session, $body_id) = @_; my $body; if (ref $body_id && $body_id->isa('Lacuna::DB::Result::Map::Body')) { $body = $body_id; - } - else { - ($body) = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->search({ - 'me.id' => $body_id, - },{ - prefetch => 'empire', - }); - } - unless (defined $body) { - confess [1002, 'Body does not exist.', $body_id]; - } - if ($body->empire_id ne $empire->id) { - if ($body->isa('Lacuna::DB::Result::Map::Body::Planet::Station')) { - if ($body->empire->alliance_id eq $empire->alliance_id) { - return $body; - } - } - confess [1010, "Can't manipulate a planet you don't inhabit."]; - } - if ($body->id eq $empire->home_planet_id) { - $empire->home_planet($body); + } else { + $body = Lacuna->db->resultset('Map::Body')-> + search( + { + 'me.id' => $body_id, + -or => [ + { 'me.empire_id' => $session->empire->id }, + { 'me.alliance_id' => $session->empire->alliance_id }, + $session->_is_sitter ? + { + 'sitterauths.sitter_id' => $session->empire->id, + 'me.class' => { '!=' => 'Lacuna::DB::Result::Map::Body::Planet::Station' }, + } : (), + ] + }, + { + join => { 'empire' => 'sitterauths' }, + prefetch => [ 'empire' ], + } + )->first; + confess [ 1002, 'Body does not exist or is not owned by you or any empire you sit.', $body_id ] + unless $body; + } return $body; } sub get_building { # makes for uniform error handling, and prevents staleness - my ($self, $empire, $building_id, %options) = @_; + my ($self, $session, $building_id, %options) = @_; if (ref $building_id && $building_id->isa('Lacuna::DB::Result::Building')) { return $building_id; } - else { - my ($building) = Lacuna->db->resultset('Lacuna::DB::Result::Building')->search({ - 'me.id' => $building_id, - },{ prefetch => 'body' } - ); - unless (defined $building) { - confess [1002, 'Building does not exist.', $building_id]; - } - if ($building->class ne $self->model_class) { - confess [1002, 'That building is not a '.$self->model_class->name]; - } - $building->is_offline unless ($options{skip_offline}); - my $body = $self->get_body($empire, $building->body); - if ($body->empire_id ne $empire->id) { - if ($body->isa('Lacuna::DB::Result::Map::Body::Planet::Station')) { - if ($body->empire->alliance_id eq $empire->alliance_id) { - $building->body($body); - return $building; - } - } - confess [1010, "Can't manipulate a building that you don't own.", $building_id]; - } - $building->body($body); - return $building; - } + my $building = + Lacuna->db->resultset('Building')-> + search( + { + 'me.id' => $building_id, + -or => [ + { 'body.empire_id' => $session->empire->id }, + { 'body.alliance_id' => $session->empire->alliance_id }, + $session->_is_sitter ? + { + 'sitterauths.sitter_id' => $session->empire->id, + 'body.class' => { '!=' => 'Lacuna::DB::Result::Map::Body::Planet::Station' }, + } : (), + ] + }, + { + join => { body => { 'empire' => 'sitterauths' }}, + prefetch => { body => 'empire' }, + } + )->first; + confess [ 1002, 'Building does not exist or is not owned by you or any empire you sit.', $building_id ] + unless $building; + + confess [ 1002, 'That building is not a '. $self->model_class->name ] + unless $building->class eq $self->model_class; + + $building->is_offline unless $options{skip_offline}; + + return $building; } sub format_status { diff --git a/lib/Lacuna/RPC/Building/Shipyard.pm b/lib/Lacuna/RPC/Building/Shipyard.pm index 3651cc51..e5283f8a 100644 --- a/lib/Lacuna/RPC/Building/Shipyard.pm +++ b/lib/Lacuna/RPC/Building/Shipyard.pm @@ -204,7 +204,7 @@ sub build_ship { sub build_ships { my ($self, $session_id, $opts) = @_; - my $session = $self->get_session({session_id => $session_id, building_id => $opts->{building_id} }); + my $session = $self->get_session({session_id => $session_id}); my $empire = $session->current_empire; my $quantity = $opts->{quantity} // 1; @@ -219,9 +219,9 @@ sub build_ships { my $building_view; if ($opts->{building_id}) { if (ref($opts->{building_id}) eq 'ARRAY') { - push @buildings, map { $self->get_building($empire, $_) } @{$opts->{building_id}} + push @buildings, map { $self->get_building($session, $_) } @{$opts->{building_id}} } else { - push @buildings, $self->get_building($empire, $opts->{building_id}); + push @buildings, $self->get_building($session, $opts->{building_id}); } $opts->{body_id} = $buildings[0]->body_id; $building_view = $buildings[0]; @@ -230,7 +230,7 @@ sub build_ships { } else { confess [1001, "Either building id(s) or body id must be provided"]; } - my $body = $self->get_body($empire, $opts->{body_id}); + my $body = $self->get_body($session, $opts->{body_id}); @buildings = grep { $_->level > 0 && $_->efficiency >= 100 } @buildings; my @all_sys = grep { diff --git a/lib/Lacuna/RPC/Empire.pm b/lib/Lacuna/RPC/Empire.pm index 6200a1e9..cb3ff590 100644 --- a/lib/Lacuna/RPC/Empire.pm +++ b/lib/Lacuna/RPC/Empire.pm @@ -1296,7 +1296,7 @@ __PACKAGE__->register_rpc_method_names( get_full_status get_status boost_building boost_storage boost_water boost_energy boost_ore boost_food boost_happiness boost_spy_training view_boosts - authorize_sitters view_authorized_sitters + authorize_sitters view_authorized_sitters deauthorize_sitters ), ); -- 2.51.2