From 46b3fe9e312d6358abc63942404b390495f257f8 Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Sun, 3 Apr 2011 16:41:46 -0500 Subject: [PATCH 1/3] Beginning work on Foreign Aid --- lib/Lacuna/DB/Result/Propositions.pm | 1 + .../DB/Result/Propositions/ForeignAid.pm | 16 ++++++++ lib/Lacuna/RPC/Building/Parliament.pm | 39 ++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 lib/Lacuna/DB/Result/Propositions/ForeignAid.pm diff --git a/lib/Lacuna/DB/Result/Propositions.pm b/lib/Lacuna/DB/Result/Propositions.pm index edb801d5..9e2bccd1 100644 --- a/lib/Lacuna/DB/Result/Propositions.pm +++ b/lib/Lacuna/DB/Result/Propositions.pm @@ -33,6 +33,7 @@ __PACKAGE__->typecast_map(type => { EvictMiningPlatform => 'Lacuna::DB::Result::Propositions::EvictMiningPlatform', ExpelMember => 'Lacuna::DB::Result::Propositions::ExpelMember', FireBfg => 'Lacuna::DB::Result::Propositions::FireBfg', + ForeignAid => 'Lacuna::DB::Result::Propositions::ForeignAid', InductMember => 'Lacuna::DB::Result::Propositions::InductMember', InstallModule => 'Lacuna::DB::Result::Propositions::InstallModule', MembersOnlyColonization => 'Lacuna::DB::Result::Propositions::MembersOnlyColonization', diff --git a/lib/Lacuna/DB/Result/Propositions/ForeignAid.pm b/lib/Lacuna/DB/Result/Propositions/ForeignAid.pm new file mode 100644 index 00000000..5e0bfab0 --- /dev/null +++ b/lib/Lacuna/DB/Result/Propositions/ForeignAid.pm @@ -0,0 +1,16 @@ +package Lacuna::DB::Result::Propositions::ForeignAid; + +use Moose; +use utf8; +no warnings qw(uninitialized); +extends 'Lacuna::DB::Result::Propositions'; + +# BUG Need to actually send the foreign aid +before pass => sub { + my ($self) = @_; + my $station = $self->station; + $self->station->add_news(200, $self->scratch->{message}); +}; + +no Moose; +__PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/RPC/Building/Parliament.pm b/lib/Lacuna/RPC/Building/Parliament.pm index 4457ee9d..78e4368e 100644 --- a/lib/Lacuna/RPC/Building/Parliament.pm +++ b/lib/Lacuna/RPC/Building/Parliament.pm @@ -685,8 +685,45 @@ sub propose_expel_member { }; } +sub propose_foreign_aid { + my ($self, $session_id, $building_id, $planet_id, $resources) = @_; + my $empire = $self->get_empire_by_session($session_id); + if ($empire->current_session->is_sitter) { + confess [1015, 'Sitters cannot create propositions.']; + } + my $building = $self->get_building($empire, $building_id); + unless ($building->level >= 16) { + confess [1013, 'Parliament must be level 16 to send out foreign aid packages.',16]; + } + unless ($planet_id) { + confess [1002, 'You must specify a planet id.']; + } + my $planet = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($planet_id); + unless (defined $planet) { + confess [1002, 'Could not find the planet.']; + } + unless ($planet->star->station_id == $self->body_id) { + confess [1009, 'That planet is not in the jurisdiction of this station.']; + } +# BUG Needs to check for available resources + my $proposition = Lacuna->db->resultset('Lacuna::DB::Result::Propositions')->new({ + type => 'Foreign Aid', + name => 'Foreign Aid for {Planet '.$planet->id.' '.$planet->name.'}.', + description => 'Send a foreign aid package of '.$resources.' resources to {Planet '.$planet->id.' '.$planet->name.'} (total cost '.2*$resources.'resources).', + scratch => { planet_id => $planet->id, resources => $resources }, + proposed_by_id => $empire->id, + }); + $proposition->station($building->body); + $proposition->proposed_by($empire); + $proposition->insert; + return { + status => $self->format_status($empire, $building->body), + proposition => $proposition->get_status($empire), + }; +} + -__PACKAGE__->register_rpc_method_names(qw(get_bodies_for_star_in_jurisdiction get_mining_platforms_for_asteroid_in_jurisdiction propose_evict_mining_platform propose_members_only_mining_rights propose_members_only_colonization propose_rename_asteroid propose_rename_uninhabited propose_broadcast_on_network19 get_stars_in_jurisdiction propose_rename_star propose_repeal_law propose_seize_star propose_transfer_station_ownership view_propositions view_laws cast_vote propose_fire_bfg propose_writ propose_elect_new_leader propose_induct_member propose_expel_member)); +__PACKAGE__->register_rpc_method_names(qw(get_bodies_for_star_in_jurisdiction get_mining_platforms_for_asteroid_in_jurisdiction propose_evict_mining_platform propose_members_only_mining_rights propose_members_only_colonization propose_rename_asteroid propose_rename_uninhabited propose_broadcast_on_network19 get_stars_in_jurisdiction propose_rename_star propose_repeal_law propose_seize_star propose_transfer_station_ownership view_propositions view_laws cast_vote propose_fire_bfg propose_writ propose_elect_new_leader propose_induct_member propose_expel_member propose_foreign_aid)); no Moose; __PACKAGE__->meta->make_immutable; -- 2.54.0 From a717d24c3b1fd5fcf84ca4c5265b6d4ddd31a9be Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Sun, 3 Apr 2011 22:17:00 -0500 Subject: [PATCH 2/3] Working foreign aid. --- .../DB/Result/Propositions/ForeignAid.pm | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/Lacuna/DB/Result/Propositions/ForeignAid.pm b/lib/Lacuna/DB/Result/Propositions/ForeignAid.pm index 5e0bfab0..e505a941 100644 --- a/lib/Lacuna/DB/Result/Propositions/ForeignAid.pm +++ b/lib/Lacuna/DB/Result/Propositions/ForeignAid.pm @@ -2,14 +2,32 @@ package Lacuna::DB::Result::Propositions::ForeignAid; use Moose; use utf8; +use DateTime; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Propositions'; -# BUG Need to actually send the foreign aid before pass => sub { my ($self) = @_; - my $station = $self->station; - $self->station->add_news(200, $self->scratch->{message}); + my $resources = $self->scratch->{resources}; + my $planet_id = $self->scratch->{planet_id}; + my $planet = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($planet_id); + if (defined $planet) { + if ($planet->star->station_id == $self->body_id) { + my $finish = DateTime->now; + my $supply_pod = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({ + body_id => $self->station_id, + type => 'supply_pod', + hold_size => $self->scratch->{resource}, + date_available => $finish, + }); + $supply_pod->send(target => $planet); + } + else { + } + } + else { + } + }; no Moose; -- 2.54.0 From b1bb83faaae8cc99511f19707144f19f9adc8291 Mon Sep 17 00:00:00 2001 From: Matthew Musgrove Date: Mon, 4 Apr 2011 21:34:45 -0500 Subject: [PATCH 3/3] API: Added proposing sending foreign aid to planets at Parliament level 16. --- docs/Parliament.pod | 43 ++++++++++++++ .../DB/Result/Propositions/ForeignAid.pm | 59 ++++++++++++++++--- lib/Lacuna/RPC/Building/Parliament.pm | 27 ++++++++- t/Parliament.t | 5 +- var/www/public/changes.txt | 1 + 5 files changed, 124 insertions(+), 11 deletions(-) diff --git a/docs/Parliament.pod b/docs/Parliament.pod index 7dc14b28..df895b3a 100644 --- a/docs/Parliament.pod +++ b/docs/Parliament.pod @@ -720,6 +720,49 @@ The unique id of a mining platform. See C sub { my ($self) = @_; @@ -13,19 +14,61 @@ before pass => sub { my $planet = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($planet_id); if (defined $planet) { if ($planet->star->station_id == $self->body_id) { - my $finish = DateTime->now; - my $supply_pod = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({ - body_id => $self->station_id, - type => 'supply_pod', - hold_size => $self->scratch->{resource}, - date_available => $finish, - }); - $supply_pod->send(target => $planet); + my @types = qw( energy food ore water ); + my @missing; + for my $cost ( @types ) { + my $method = "${cost}_stored"; + unless ( $self->station->$method >= $self->scratch->{"${cost}_cost"} ) { + push @missing, $cost; + } + } + if ( !@missing ) { + $self->station->spend_energy($self->scratch->{energy_cost}); + for my $food ( FOOD_TYPES ) { + if ( $self->scratch->{food_cost} > $self->station->$food ) { + $self->scratch->{food_cost} -= $self->station->$food; + $self->station->spend_food_type($food,$self->station->$food); + } + else { + $self->station->spend_food_type($food,$self->scratch->{food_cost}); + $self->scratch->{food_cost} = 0; + last; + } + } + for my $ore ( ORE_TYPES ) { + if ( $self->scratch->{ore_cost} > $self->station->$ore ) { + $self->scratch->{ore_cost} -= $self->station->$ore; + $self->station->spend_ore_type($ore,$self->station->$ore); + } + else { + $self->station->spend_ore_type($ore,$self->scratch->{ore_cost}); + $self->scratch->{ore_cost} = 0; + last; + } + } + $self->station->spend_water($self->scratch->{water_cost}); + $self->station->update; + my $supply_pod = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({ + body_id => $self->station_id, + type => 'supply_pod', + hold_size => $self->scratch->{resource}, + }); + $supply_pod->date_available(DateTime->now); + $supply_pod->update; + $supply_pod->send(target => $planet); + } + else { + $self->pass_extra_message( + 'Unfortunately by the time the proposition passed, the station did not have enough resources ('. + join(', ',@missing).') to build the supply pod, effectively nullifying the vote.'); + } } else { + $self->pass_extra_message('Unfortunately by the time the proposition passed, the planet was no longer under the jurisdiction of the station, effectively nullifying the vote.'); } } else { + $self->pass_extra_message('Unfortunately by the time the proposition passed, the planet could not be found, effectively nullifying the vote.'); } }; diff --git a/lib/Lacuna/RPC/Building/Parliament.pm b/lib/Lacuna/RPC/Building/Parliament.pm index 78e4368e..a0380477 100644 --- a/lib/Lacuna/RPC/Building/Parliament.pm +++ b/lib/Lacuna/RPC/Building/Parliament.pm @@ -705,12 +705,35 @@ sub propose_foreign_aid { unless ($planet->star->station_id == $self->body_id) { confess [1009, 'That planet is not in the jurisdiction of this station.']; } -# BUG Needs to check for available resources + + my $cost = 2 * $resources; + my @types = qw( energy food ore water ); + my @costs = @types; + my %cost; + # mostly even distribution of resources + while ( my $type = shift @costs ) { + my $cost_per_resource = int($cost / (scalar @costs + 1)); + $cost{$type} = $cost_per_resource; + $cost -= $cost_per_resource; + } + for my $cost ( @types ) { + my $method = "${cost}_stored"; + unless ( $building->body->$method >= $cost{$cost} ) { + confess [1007, "The station does not have enough $cost stored."]; + } + } my $proposition = Lacuna->db->resultset('Lacuna::DB::Result::Propositions')->new({ type => 'Foreign Aid', name => 'Foreign Aid for {Planet '.$planet->id.' '.$planet->name.'}.', description => 'Send a foreign aid package of '.$resources.' resources to {Planet '.$planet->id.' '.$planet->name.'} (total cost '.2*$resources.'resources).', - scratch => { planet_id => $planet->id, resources => $resources }, + scratch => { + planet_id => $planet->id, + resources => $resources, + energy_cost => $cost{energy}, + food_cost => $cost{food}, + ore_cost => $cost{ore}, + water_cost => $cost{water}, + }, proposed_by_id => $empire->id, }); $proposition->station($building->body); diff --git a/t/Parliament.t b/t/Parliament.t index a452b714..1fd7c1d9 100644 --- a/t/Parliament.t +++ b/t/Parliament.t @@ -1,5 +1,5 @@ use lib '../lib'; -use Test::More tests => 24; +use Test::More tests => 25; use Test::Deep; use Data::Dumper; use 5.010; @@ -92,6 +92,9 @@ is($result->{error}{data}, 13, 'members mining rights requires level 13 parliame $result = $tester->post('parliament', 'propose_evict_mining_platform', [$session_id, $par->id]); is($result->{error}{data}, 14, 'evict mining platform requires level 14 parliament'); +$result = $tester->post('parliament', 'propose_foreign_aid', [$session_id, $par->id]); +is($result->{error}{data}, 16, 'sending foreign aid requires level 16 parliament'); + $result = $tester->post('parliament', 'propose_rename_uninhabited', [$session_id, $par->id]); is($result->{error}{data}, 17, 'renaming uninhabited requires level 17 parliament'); diff --git a/var/www/public/changes.txt b/var/www/public/changes.txt index 48f98dcf..2c5538d2 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -2,6 +2,7 @@ - API: Added proposing induction of new alliance members at Parliament level 10. - API: Added proposing expulstion of alliance members at Parliament level 10. - API: Added proposing election of a new alliance leader at Parliament level 11. + - API: Added proposing sending foreign aid to planets at Parliament level 16. - API: Added proposing renaming uninhabited planets at Parliament level 17. - API: Added proposing members only colonization at Parliament level 18. - Fix: Police Station docs now show up under Modules instead of Buildings. -- 2.54.0