From 17c116fbb8cf47cfbf932b739c921a7ebfcf56c6 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Thu, 4 Mar 2010 10:50:27 -0600 Subject: [PATCH] added planet stats to command --- docs/PlanetaryCommand.pod | 31 ++++++++++++ lib/Lacuna/Building/Park.pm | 2 +- lib/Lacuna/Building/PlanetaryCommand.pm | 28 +++++++++++ lib/Lacuna/DB/Building/PlanetaryCommand.pm | 2 +- t/Command.t | 57 ++++++++++++++++++++++ 5 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 t/Command.t diff --git a/docs/PlanetaryCommand.pod b/docs/PlanetaryCommand.pod index 5e4e6f2d..47e9c623 100644 --- a/docs/PlanetaryCommand.pod +++ b/docs/PlanetaryCommand.pod @@ -2,6 +2,37 @@ Planetary Command is accessible via the URL C. +The Planetary Command, or just Command, is the hub of your empire on a planet. It gives you the resources and storage you need to get a foothold on a planet. + The list of methods below represents changes and additions to the methods that all L share. +=head2 view ( session_id, building_id ) + +Command extends the view method to include a C section. + + { + "building" : { ... }, + "status" : { ... }, + "planet" : { + "ore" : { + "gold" : 443, + "bauxite" : 1009, + ... + }, + "water" : 5000, + "x" : -5, + "y" : 4, + "z" : 20, + "name" : "Venus", + "star_id" : "id-goes-here", + "id" : "id-goes-here", + "building_count" : 13, + "size" : 25, + "orbit" : 2, + "image" : "p12" + } + } + +Some of the data you can get from other methods, and is included here simply for ease of use. However, the C vs the C is particular import to the Command. You cannot build more buildings than a planet's size. After you've hit the building count, you have to build up, because you can no longer build out. + =cut \ No newline at end of file diff --git a/lib/Lacuna/Building/Park.pm b/lib/Lacuna/Building/Park.pm index fad81891..2227926e 100644 --- a/lib/Lacuna/Building/Park.pm +++ b/lib/Lacuna/Building/Park.pm @@ -21,7 +21,7 @@ around 'view' => sub { $out->{party}{seconds_remaining} = $building->party_seconds_remaining; } else { - $out->{party}{can_throw} = eval { $building->can_throw_a_party } ? 1 : 0; + $out->{party}{can_throw} = (eval { $building->can_throw_a_party }) ? 1 : 0; } return $out; }; diff --git a/lib/Lacuna/Building/PlanetaryCommand.pm b/lib/Lacuna/Building/PlanetaryCommand.pm index 23e43ee7..ac568c3d 100644 --- a/lib/Lacuna/Building/PlanetaryCommand.pm +++ b/lib/Lacuna/Building/PlanetaryCommand.pm @@ -2,6 +2,7 @@ package Lacuna::Building::PlanetaryCommand; use Moose; extends 'Lacuna::Building'; +use Lacuna::Constants qw(ORE_TYPES); sub app_url { return '/planetarycommand'; @@ -11,6 +12,33 @@ sub model_class { return 'Lacuna::DB::Building::PlanetaryCommand'; } +around 'view' => sub { + my ($orig, $self, $session_id, $building_id) = @_; + my $building = $self->get_building($building_id); + my $empire = $self->get_empire_by_session($session_id); + my $out = $orig->($self, $empire, $building); + my $body = $building->body; + my %ore; + foreach my $type (ORE_TYPES) { + $ore{$type} = $body->$type(); + } + $out->{planet} = { + ore => \%ore, + water => $body->water, + building_count => $body->building_count, + size => $body->size, + orbit => $body->orbit, + x => $body->x, + y => $body->y, + z => $body->z, + star_id => $body->star_id, + name => $body->name, + image => $body->image, + id => $body->id, + }; + return $out; +}; + no Moose; __PACKAGE__->meta->make_immutable; diff --git a/lib/Lacuna/DB/Building/PlanetaryCommand.pm b/lib/Lacuna/DB/Building/PlanetaryCommand.pm index d39b8c3c..45bfa2ad 100644 --- a/lib/Lacuna/DB/Building/PlanetaryCommand.pm +++ b/lib/Lacuna/DB/Building/PlanetaryCommand.pm @@ -12,7 +12,7 @@ sub check_build_prereqs { } sub image { - return 'planetary-command'; + return 'command'; } sub name { diff --git a/t/Command.t b/t/Command.t new file mode 100644 index 00000000..cae8a759 --- /dev/null +++ b/t/Command.t @@ -0,0 +1,57 @@ +use lib '../lib'; +use Test::More tests => 1; +use Test::Deep; +use LWP::UserAgent; +use JSON qw(to_json from_json); +use Lacuna::DB; +use Data::Dumper; +use 5.010; + +my $result; +my $db = Lacuna::DB->new(access_key => $ENV{SIMPLEDB_ACCESS_KEY}, secret_key => $ENV{SIMPLEDB_SECRET_KEY}, cache_servers => [{host=>'127.0.0.1', port=>11211}]); + +my $fed = { + name => 'some rand'.rand(9999999), + species_id => 'human_species', + password => '123qwe', + password1 => '123qwe', +}; +$result = post('empire', 'create', $fed); +my $fed_id = $result->{result}{empire_id}; +my $session_id = $result->{result}{session_id}; +my $home_planet = $result->{result}{status}{empire}{home_planet_id}; +my $empire_id = $result->{result}{status}{empire}{id}; + +$result = post('body','get_buildings', [$session_id, $home_planet]); + +my $id = (keys %{$result->{result}{buildings}})[0]; + +$result = post('planetarycommand', 'view', [$session_id, $id]); + +is($result->{result}{planet}{building_count}, 1, "got building count"); + + +sub post { + my ($url, $method, $params) = @_; + my $content = { + jsonrpc => '2.0', + id => 1, + method => $method, + params => $params, + }; + my $ua = LWP::UserAgent->new; + $ua->timeout(30); + say "REQUEST: ".to_json($content); + my $response = $ua->post('http://localhost:5000/'.$url, + Content_Type => 'application/json', + Content => to_json($content), + Accept => 'application/json', + ); + say "RESPONSE: ".$response->content; + return from_json($response->content); +} + +END { + $db->domain('empire')->find($fed_id)->delete; + $db->domain('session')->find($session_id)->delete; +} -- 2.51.2