From 5f8d998dd8b20126dad3d06ab3fc41185d0bac8d Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sat, 23 Jun 2012 17:01:18 -0400 Subject: [PATCH 1/4] Some improvements in calculating supply chains --- docs/PlanetaryCommand.pod | 18 +- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 390 ++++++++++++-------- lib/Lacuna/RPC/Building/PlanetaryCommand.pm | 2 + lib/Lacuna/RPC/Building/Shipyard.pm | 2 +- 4 files changed, 248 insertions(+), 164 deletions(-) diff --git a/docs/PlanetaryCommand.pod b/docs/PlanetaryCommand.pod index ffc2ccf8..eeeb82b5 100644 --- a/docs/PlanetaryCommand.pod +++ b/docs/PlanetaryCommand.pod @@ -49,8 +49,22 @@ Command extends the view method to include a C section. "waste_capacity" : 13000, "water_stored" : 9929, "water_hour" : 295, - "water_capacity" : 51050 - } + "water_capacity" : 51050, + "food" : { + "algae_hour" : 24, + "algae_stored" :1322, + "bean_hour" : 1, + "bean_stored" :42, + .. + } + "ore" : { + "anthracite_hour" : 333, + "anthracite_stored" : 34444, + "bauxite_hour" : 22, + "bauxite_stored" : 1235, + ... + } + } } diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 783cead2..1f2482c5 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -217,14 +217,40 @@ before abandon => sub { $self->sanitize; }; +sub get_ore_status { + my ($self) = @_; + + my $out; + foreach my $type (ORE_TYPES) { + my $arg = "${type}_hour"; + $out->{$arg} = $self->$arg; + $arg = "${type}_stored"; + $out->{$arg} = $self->$arg; + } + return $out; +} + +sub get_food_status { + my ($self) = @_; + + my $out; + foreach my $type (FOOD_TYPES) { + my $arg = "${type}_production_hour"; + $out->{"${type}_hour"} = $self->$arg; + $arg = "${type}_stored"; + $out->{$arg} = $self->$arg; + } + return $out; +} + around get_status => sub { my ($orig, $self, $empire) = @_; my $out = $orig->($self); - my %ore; + my $ore; foreach my $type (ORE_TYPES) { - $ore{$type} = $self->$type(); + $ore->{$type} = $self->$type(); } - $out->{ore} = \%ore; + $out->{ore} = $ore; $out->{water} = $self->water; if ($self->empire_id) { $out->{empire} = { @@ -428,10 +454,10 @@ use constant water => 0; # BUILDINGS has population => ( - is => 'ro', - lazy => 1, - builder => '_build_population', -); + is => 'ro', + lazy => 1, + builder => '_build_population', + ); sub _build_population { my ($self) = @_; @@ -446,10 +472,10 @@ sub _build_population { } has building_count => ( - is => 'rw', - lazy => 1, - builder => '_build_building_count', -); + is => 'rw', + lazy => 1, + builder => '_build_building_count', + ); sub _build_building_count { my ($self) = @_; @@ -480,112 +506,112 @@ sub find_building { } has command => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::PlanetaryCommand'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); has oversight => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::Oversight'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); has archaeology => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::Archaeology'); return undef unless defined $building; -# #$building->body($self); + # #$building->body($self); return $building; - }, -); + }, + ); has mining_ministry => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::Ore::Ministry'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); has network19 => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::Network19'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); has development => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::Development'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); has refinery => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::Ore::Refinery'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); has spaceport => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::SpacePort'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); has embassy => ( - is => 'rw', - lazy => 1, - default => sub { + is => 'rw', + lazy => 1, + default => sub { my $self = shift; my $building = $self->get_building_of_class('Lacuna::DB::Result::Building::Embassy'); return undef unless defined $building; #$building->body($self); return $building; - }, -); + }, + ); sub is_space_free { my ($self, $x, $y) = @_; @@ -673,34 +699,34 @@ sub has_room_in_build_queue { use constant operating_resource_names => qw(food_hour energy_hour ore_hour water_hour); has future_operating_resources => ( - is => 'rw', - clearer => 'clear_future_operating_resources', - lazy => 1, - default => sub { + is => 'rw', + clearer => 'clear_future_operating_resources', + lazy => 1, + default => sub { my $self = shift; - + # get current my %future; foreach my $method ($self->operating_resource_names) { - $future{$method} = $self->$method; + $future{$method} = $self->$method; } - + # adjust for what's already in build queue my @queued_builds = @{$self->builds}; foreach my $build (@queued_builds) { - $build->body($self); - my $other = $build->stats_after_upgrade; - foreach my $method ($self->operating_resource_names) { - $future{$method} += $other->{$method} - $build->$method; - } + $build->body($self); + my $other = $build->stats_after_upgrade; + foreach my $method ($self->operating_resource_names) { + $future{$method} += $other->{$method} - $build->$method; + } } return \%future; - }, -); + }, + ); sub has_resources_to_operate { my ($self, $building) = @_; - + # get future my $future = $self->future_operating_resources; @@ -710,7 +736,7 @@ sub has_resources_to_operate { # check our ability to sustain ourselves foreach my $method ($self->operating_resource_names) { my $delta = $after->{$method} - $building->$method; - # don't allow it if it sucks resources && its sucking more than we're producing + # don't allow it if it sucks resources && its sucking more than we're producing if ($delta < 0 && $future->{$method} + $delta < 0) { my $resource = $method; $resource =~ s/(\w+)_hour/$1/; @@ -722,13 +748,13 @@ sub has_resources_to_operate { sub has_resources_to_operate_after_building_demolished { my ($self, $building) = @_; - + # get future my $planet = $self->future_operating_resources; # check our ability to sustain ourselves foreach my $method ($self->operating_resource_names) { - # don't allow it if it sucks resources && its sucking more than we're producing + # don't allow it if it sucks resources && its sucking more than we're producing if ($planet->{$method} - $building->$method < 0) { my $resource = $method; $resource =~ s/(\w+)_hour/$1/; @@ -811,7 +837,7 @@ sub found_colony { $self->usable_as_starter_enabled(0); $self->last_tick(DateTime->now); $self->update; -# Excavators get cleared when being checked for results. + # Excavators get cleared when being checked for results. # award medal my $type = ref $self; @@ -825,11 +851,11 @@ sub found_colony { # add command building my $command = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ - x => 0, - y => 0, - class => 'Lacuna::DB::Result::Building::PlanetaryCommand', - level => $empire->growth_affinity - 1, - }); + x => 0, + y => 0, + class => 'Lacuna::DB::Result::Building::PlanetaryCommand', + level => $empire->growth_affinity - 1, + }); $self->build_building($command); $command->finish_upgrade; @@ -837,7 +863,7 @@ sub found_colony { foreach my $crater (@craters) { $crater->finish_work->update; } - + # add starting resources $self->needs_recalc(1); $self->tick; @@ -846,10 +872,10 @@ sub found_colony { $self->add_water(700); $self->add_ore(700); $self->update; - + # newsworthy $self->add_news(75,'%s founded a new colony on %s.', $empire->name, $self->name); - + return $self; } @@ -871,34 +897,34 @@ sub convert_to_station { # clean it my @all_buildings = @{$self->building_cache}; $self->delete_buildings(\@all_buildings); - + # add command building my $command = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ - x => 0, - y => 0, - class => 'Lacuna::DB::Result::Building::Module::StationCommand', - }); + x => 0, + y => 0, + class => 'Lacuna::DB::Result::Building::Module::StationCommand', + }); $self->build_building($command); $command->finish_upgrade; - + # add parliament my $parliament = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ - x => -1, - y => 0, - class => 'Lacuna::DB::Result::Building::Module::Parliament', - }); + x => -1, + y => 0, + class => 'Lacuna::DB::Result::Building::Module::Parliament', + }); $self->build_building($parliament); $parliament->finish_upgrade; - + # add warehouse my $warehouse = Lacuna->db->resultset('Lacuna::DB::Result::Building')->new({ - x => 1, - y => 0, - class => 'Lacuna::DB::Result::Building::Module::Warehouse', - }); + x => 1, + y => 0, + class => 'Lacuna::DB::Result::Building::Module::Warehouse', + }); $self->build_building($warehouse); $warehouse->finish_upgrade; - + # add starting resources $self->tick; $self->add_algae(2500); @@ -906,25 +932,25 @@ sub convert_to_station { $self->add_water(2500); $self->add_rutile(2500); $self->update; - + # newsworthy $self->add_news(100,'%s deployed a space station at %s.', $empire->name, $self->name); - + return $self; } has total_ore_concentration => ( - is => 'ro', - lazy => 1, - default => sub { + is => 'ro', + lazy => 1, + default => sub { my $self = shift; my $tally = 0; foreach my $type (ORE_TYPES) { - $tally += $self->$type; + $tally += $self->$type; } return $tally; - }, -); + }, + ); sub is_food { my ($self, $resource) = @_; @@ -984,7 +1010,7 @@ sub recalc_stats { } $stats{max_berth} = 1; #calculate building production - my ($gas_giant_platforms, $terraforming_platforms, $station_command, $pantheon_of_hagness, $total_ore_production_hour, $ore_production_hour, $ore_consumption_hour) = 0; + my ($gas_giant_platforms, $terraforming_platforms, $station_command, $pantheon_of_hagness, $ore_production_hour, $ore_consumption_hour) = 0; foreach my $building (@{$self->building_cache}) { $stats{waste_capacity} += $building->waste_capacity; $stats{water_capacity} += $building->water_capacity; @@ -1003,7 +1029,7 @@ sub recalc_stats { $stats{$method} += $building->$method(); } if ($building->isa('Lacuna::DB::Result::Building::SpacePort') and $building->efficiency == 100) { - $stats{max_berth} = $building->level if ($building->level > $stats{max_berth}); + $stats{max_berth} = $building->level if ($building->level > $stats{max_berth}); } if ($building->isa('Lacuna::DB::Result::Building::Ore::Ministry')) { my $platforms = Lacuna->db->resultset('Lacuna::DB::Result::MiningPlatforms')->search({planet_id => $self->id}); @@ -1011,7 +1037,6 @@ sub recalc_stats { foreach my $type (ORE_TYPES) { my $method = $type.'_hour'; $stats{$method} += $platform->$method(); - $total_ore_production_hour += $platform->$method(); } } } @@ -1029,14 +1054,12 @@ sub recalc_stats { my $output_chains = Lacuna->db->resultset('Lacuna::DB::Result::SupplyChain')->search({planet_id => $self->id}); while (my $out_chain = $output_chains->next) { my $percent = $out_chain->percent_transferred; - $percent = $percent > 100 ? 100 : $percent; - $percent *= $building->efficiency / 100; + $percent = $percent > 100 ? 100 : $percent; + $percent *= $building->efficiency / 100; + my $resource_hour = sprintf('%.0f',$out_chain->resource_hour * $percent / 100); - my $resource_name = $self->resource_name($out_chain->resource_type); + my $resource_name = $self->resource_name($out_chain->resource_type); $stats{$resource_name} -= $resource_hour; - if ($self->is_ore($out_chain->resource_type)) { - $total_ore_production_hour -= $resource_hour; - } } } if ($building->isa('Lacuna::DB::Result::Building::Permanent::GasGiantPlatform')) { @@ -1055,10 +1078,10 @@ sub recalc_stats { # active supply chains sent *to* this planet my $input_chains = Lacuna->db->resultset('Lacuna::DB::Result::SupplyChain')->search({ - target_id => $self->id, - stalled => 0, - },{prefetch => 'building'} - ); + target_id => $self->id, + stalled => 0, + },{prefetch => 'building'} + ); while (my $in_chain = $input_chains->next) { my $percent = $in_chain->percent_transferred; @@ -1067,9 +1090,6 @@ sub recalc_stats { my $resource_hour = sprintf('%.0f',$in_chain->resource_hour * $percent / 100); my $resource_name = $self->resource_name($in_chain->resource_type); $stats{$resource_name} += $resource_hour; - if ($self->is_ore($in_chain->resource_type)) { - $total_ore_production_hour += $resource_hour; - } } # local ore production @@ -1077,17 +1097,27 @@ sub recalc_stats { my $method = $type.'_hour'; my $domestic_ore_hour = sprintf('%.0f',$self->$type * $ore_production_hour / $self->total_ore_concentration); $stats{$method} += $domestic_ore_hour; - $total_ore_production_hour += $domestic_ore_hour; } - # subtract ore consumption + # For all ore production that is positive, deduct a percentage for ore consumption + my $positive_ore_hour = 0; foreach my $type (ORE_TYPES) { - my $method = $type.'_hour'; - $stats{$method} -= sprintf('%.0f', ($total_ore_production_hour) ? $ore_consumption_hour * $stats{$method} / $total_ore_production_hour: 0); + my $resource_name = $self->resource_name($type); + if ($stats{$resource_name} > 0) { + $positive_ore_hour += $stats{$resource_name}; + } } - - # overall ore production - $stats{ore_hour} = $total_ore_production_hour - $ore_consumption_hour; + my $reduce_factor = 1; + if ($positive_ore_hour >= $ore_consumption_hour) { + $reduce_factor = $ore_consumption_hour / $positive_ore_hour; + } + foreach my $type (ORE_TYPES) { + my $resource_name = $self->resource_name($type); + if ($stats{$resource_name} > 0) { + $stats{$resource_name} -= $stats{$resource_name} * $reduce_factor; + } + } + $stats{ore_hour} = $positive_ore_hour - $ore_consumption_hour; # deal with negative amounts stored $self->water_stored = 0 if $self->water_stored < 0; @@ -1125,10 +1155,10 @@ sub recalc_stats { } } $stats{plots_available} = $max_plots - $self->building_count; -# Decrease happiness production if short on plots. + # Decrease happiness production if short on plots. if ($stats{plots_available} < 0) { my $plot_tax = int(50 * 1.62 ** (abs($stats{plots_available})-1)); -# Set max to at least -10k + # Set max to at least -10k my $neg_hr = $self->happiness > 100_000 ? -1 * $self->happiness/10 : -10_000; if ( $stats{happiness_hour} < 0 and $stats{happiness_hour} > $neg_hr) { @@ -1301,7 +1331,7 @@ sub tick_to { my $seconds = $now->epoch - $self->last_tick->epoch; my $tick_rate = $seconds / 3600; $self->last_tick($now); -# Check to see if downward spiral still happening in happiness from negative plots. + # Check to see if downward spiral still happening in happiness from negative plots. $self->needs_recalc(1) if ($self->happiness < 0 and $self->happiness_hour < -20_000); if ($self->needs_recalc) { $self->recalc_stats; @@ -1323,8 +1353,7 @@ sub tick_to { } else { $arch->last_check($now); - $arch->update; - } + } } # happiness $self->add_happiness(sprintf('%.0f', $self->happiness_hour * $tick_rate)); @@ -1353,27 +1382,53 @@ sub tick_to { $self->add_water(sprintf('%.0f', $self->water_hour * $tick_rate)); } + + # OK, tricky stuff coming up! + # + # TERMS: + # + # $self->ore_hour + # $self->ore_hour - the net amount of ore produced on this colony per hour + # it also includes any ore imported via supply chains and any exported by supply chains. + # + # $ore_consumption_hour - the amount of generic ore type consumed by buildings + # consumption comes out of stored ore. + # + # $self->xxx_hour - the net amount of a specific ore (e.g. xxx = gold) produced/consumed by + # this colony. Includes all local production, platform production, incoming supply + # chains and outgoing supply chains. + # + # ALGORITHM: + # + # Every tick, we need to calculate the amount of each ore + # + # Where a xxx_hour ore rate is specified then calculate the resulting +ve or -ve change in + # the amount of ore stored for that specific ore type. If -ve and ore stored goes to zero + # then don't 'complain', this just means that a supply chain will become stalled which is + # handled elsewhere. + # + # For $ore_consumption_hour we remove from stored ore proportionate to the amount of ore + # stored of each type. In this case if a stored ore goes to zero then we *do* complain + # and part of that complaint might be to damage buildings. + # + # ore - my $ore_hour = 0; foreach my $type (ORE_TYPES) { my $hour_method = $type.'_hour'; - $ore_hour += $self->$hour_method; - if ($self->$hour_method < 0 ) { # if it gets negative, spend out of storage + if ($self->$hour_method < 0 ) { $self->spend_ore_type($type, sprintf('%.0f',abs($self->$hour_method) * $tick_rate)); } - else { + elsif ($self->$hour_method > 0 ) { $self->add_ore_type($type, sprintf('%.0f', $self->$hour_method * $tick_rate)); } } - if ($ore_hour == 0 && $self->ore_hour != 0) { - if ($self->ore_hour < 0) { - $self->spend_ore(sprintf('%.0f', abs($self->ore_hour) * $tick_rate)); - } - else { - $self->add_ore(sprintf('%.0f', $self->ore_hour * $tick_rate)); - } + if ($self->ore_hour > 0) { + $self->add_ore(sprintf('%.0f', $self->ore_hour * $tick_rate)); } - + if ($self->ore_hour < 0) { + $self->spend_ore(sprintf('%.0f', abs($self->ore_hour) * $tick_rate, 'complain')); + } + # food my %food; my $food_produced; @@ -1497,13 +1552,13 @@ sub add_type { my $empire = $self->empire; if (defined $empire && !$empire->skip_resource_warnings && !$empire->check_for_repeat_message('complaint_overflow'.$self->id)) { $empire->send_predefined_message( - filename => 'complaint_overflow.txt', - params => [$type, $self->id, $self->name], - repeat_check=> 'complaint_overflow'.$self->id, - tags => ['Complaint','Alert'], - ); + filename => 'complaint_overflow.txt', + params => [$type, $self->id, $self->name], + repeat_check=> 'complaint_overflow'.$self->id, + tags => ['Complaint','Alert'], + ); } - + } $self->$method($value); return $self; @@ -1539,13 +1594,18 @@ sub add_ore_type { } sub spend_ore_type { - my ($self, $type, $amount_spent) = @_; + my ($self, $type, $amount_spent, $complain) = @_; my $amount_stored = $self->type_stored($type); if ($amount_spent > $amount_stored && $amount_spent > 0) { my $difference = $amount_spent - $amount_stored; $self->spend_happiness($difference); $self->type_stored($type, 0); - $self->complain_about_lack_of_resources('ore') if ((($difference * 100) / $amount_spent) > 5); # help avoid rounding errors causing messages + + if ($complain && + ($difference * 100) / $amount_spent > 5) { + + $self->complain_about_lack_of_resources('ore'); + } } else { $self->type_stored($type, $amount_stored - $amount_spent ); @@ -1761,11 +1821,11 @@ sub spend_ore { foreach my $type (ORE_TYPES) { $ore_stored += $self->type_stored($type); } - + # spend proportionally and save if ($ore_stored) { foreach my $type (ORE_TYPES) { - $self->spend_ore_type($type, sprintf('%.0f', ($ore_consumed * $self->type_stored($type)) / $ore_stored)); + $self->spend_ore_type($type, sprintf('%.0f', ($ore_consumed * $self->type_stored($type)) / $ore_stored),'complain'); } } return $self; @@ -1802,13 +1862,19 @@ sub add_food_type { } sub spend_food_type { - my ($self, $type, $amount_spent) = @_; + my ($self, $type, $amount_spent, $complain) = @_; my $amount_stored = $self->type_stored($type); if ($amount_spent > 0 && $amount_spent > $amount_stored) { my $difference = $amount_spent - $amount_stored; $self->spend_happiness($difference); $self->type_stored($type, 0); - $self->complain_about_lack_of_resources('food') if ((($difference * 100) / $amount_spent) > 5); # help avoid rounding errors causing messages + + # Complain about lack of resources if required but avoid rounding errors + if ($complain && + ($difference * 100) / $amount_spent > 5) { + + $self->complain_about_lack_of_resources('food'); + } } else { $self->type_stored($type, $amount_stored - $amount_spent ); @@ -2052,7 +2118,9 @@ sub spend_food { # spend proportionally and save if ($food_stored) { foreach my $type (FOOD_TYPES) { - $self->spend_food_type($type, sprintf('%.0f', ($food_consumed * $self->type_stored($type)) / $food_stored)); + # We 'complain' about lack of food if we are spending out of generic food + # we don't complain about specific foods, because we can always substitute. + $self->spend_food_type($type, sprintf('%.0f', ($food_consumed * $self->type_stored($type)) / $food_stored),'complain'); } } diff --git a/lib/Lacuna/RPC/Building/PlanetaryCommand.pm b/lib/Lacuna/RPC/Building/PlanetaryCommand.pm index fdb9a999..5870f9e5 100644 --- a/lib/Lacuna/RPC/Building/PlanetaryCommand.pm +++ b/lib/Lacuna/RPC/Building/PlanetaryCommand.pm @@ -19,6 +19,8 @@ around 'view' => sub { my $building = $self->get_building($empire, $building_id, skip_offline => 1); my $out = $orig->($self, $empire, $building); $out->{planet} = $building->body->get_status($empire); + $out->{ore} = $building->body->get_ore_status; + $out->{food} = $building->body->get_food_status; $out->{next_colony_cost} = $empire->next_colony_cost; return $out; }; diff --git a/lib/Lacuna/RPC/Building/Shipyard.pm b/lib/Lacuna/RPC/Building/Shipyard.pm index 187db1a6..09704303 100644 --- a/lib/Lacuna/RPC/Building/Shipyard.pm +++ b/lib/Lacuna/RPC/Building/Shipyard.pm @@ -116,7 +116,7 @@ sub get_buildable { next unless ($tag ~~ \@tags); } my $can = eval{$building->can_build_ship($ship)}; - my $reason = $@; + my $reason = $@; $buildable{$type} = { attributes => { speed => $building->set_ship_speed($ship), -- 2.51.2 From 14eb2d2e120d8716789792b27d0102812b1ade8f Mon Sep 17 00:00:00 2001 From: fireartist Date: Thu, 28 Jun 2012 11:05:19 +0100 Subject: [PATCH 2/4] docs didn't match code - also {planet}{ore} clashed with already existing data --- docs/PlanetaryCommand.pod | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/PlanetaryCommand.pod b/docs/PlanetaryCommand.pod index eeeb82b5..59541661 100644 --- a/docs/PlanetaryCommand.pod +++ b/docs/PlanetaryCommand.pod @@ -14,6 +14,20 @@ Command extends the view method to include a C section. "building" : { ... }, "status" : { ... }, "next_colony_cost" : 750000, # the amount of happiness required to settle your next colony + "food" : { + "algae_hour" : 24, + "algae_stored" :1322, + "bean_hour" : 1, + "bean_stored" :42, + .. + }, + "ore" : { + "anthracite_hour" : 333, + "anthracite_stored" : 34444, + "bauxite_hour" : 22, + "bauxite_stored" : 1235, + ... + }, "planet" : { "id" : "id-goes-here", "x" : -4, @@ -49,22 +63,8 @@ Command extends the view method to include a C section. "waste_capacity" : 13000, "water_stored" : 9929, "water_hour" : 295, - "water_capacity" : 51050, - "food" : { - "algae_hour" : 24, - "algae_stored" :1322, - "bean_hour" : 1, - "bean_stored" :42, - .. - } - "ore" : { - "anthracite_hour" : 333, - "anthracite_stored" : 34444, - "bauxite_hour" : 22, - "bauxite_stored" : 1235, - ... - } - } + "water_capacity" : 51050 + } } -- 2.51.2 From 32c4b631f576bb48a963f1904fc2f424e1e685b4 Mon Sep 17 00:00:00 2001 From: fireartist Date: Thu, 28 Jun 2012 13:50:45 +0100 Subject: [PATCH 3/4] new view_incoming_supply_chains API method --- docs/PlanetaryCommand.pod | 35 +++++++++++++++++++ docs/StationCommand.pod | 35 +++++++++++++++++++ .../Result/Building/Module/StationCommand.pm | 6 ++++ .../DB/Result/Building/PlanetaryCommand.pm | 6 ++++ lib/Lacuna/DB/Result/SupplyChain.pm | 21 +++++++++++ lib/Lacuna/RPC/Building/PlanetaryCommand.pm | 4 ++- lib/Lacuna/RPC/Building/StationCommand.pm | 4 ++- lib/Lacuna/Role/IncomingSupplyChains.pm | 25 +++++++++++++ 8 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 lib/Lacuna/Role/IncomingSupplyChains.pm diff --git a/docs/PlanetaryCommand.pod b/docs/PlanetaryCommand.pod index 59541661..5f1f87e1 100644 --- a/docs/PlanetaryCommand.pod +++ b/docs/PlanetaryCommand.pod @@ -97,4 +97,39 @@ A session id. The unique id of the PCC. +=head2 view_incoming_supply_chains ( session_id, building_id ) + +Returns a list of all incoming supply chains feeding this planet. + + { + "status" : { ... }, + "incoming_supply_chains" : [ + { + "id" : "id-goes-here", + "from_body" : { + "id" : "id-goes-here", + "name" : "Mars", + "x" : 0, + "y" : -123, + "image" : "station" + }, + "resource_hour" : 10000000, + "resource_type" : 'water', + "percent_transferred" : 95, + "stalled" : 0, + }, + ... + ] + } + + +=head3 session_id + +A session id. + +=head3 building_id + +The unique id of the PCC. + + =cut diff --git a/docs/StationCommand.pod b/docs/StationCommand.pod index a1c01f8b..d7572180 100644 --- a/docs/StationCommand.pod +++ b/docs/StationCommand.pod @@ -80,4 +80,39 @@ A session id. The unique id of the PCC. +=head2 view_incoming_supply_chains ( session_id, building_id ) + +Returns a list of all incoming supply chains feeding this planet. + + { + "status" : { ... }, + "incoming_supply_chains" : [ + { + "id" : "id-goes-here", + "from_body" : { + "id" : "id-goes-here", + "name" : "Mars", + "x" : 0, + "y" : -123, + "image" : "station" + }, + "resource_hour" : 10000000, + "resource_type" : 'water', + "percent_transferred" : 95, + "stalled" : 0, + }, + ... + ] + } + + +=head3 session_id + +A session id. + +=head3 building_id + +The unique id of the PCC. + + =cut \ No newline at end of file diff --git a/lib/Lacuna/DB/Result/Building/Module/StationCommand.pm b/lib/Lacuna/DB/Result/Building/Module/StationCommand.pm index a4fa7bbf..29e7aa70 100644 --- a/lib/Lacuna/DB/Result/Building/Module/StationCommand.pm +++ b/lib/Lacuna/DB/Result/Building/Module/StationCommand.pm @@ -14,6 +14,12 @@ use constant ore_consumption => 90; use constant water_consumption => 110; use constant energy_consumption => 110; +sub incoming_supply_chains { + my ($self) = @_; + + return Lacuna->db->resultset('Lacuna::DB::Result::SupplyChain')->search({ target_id => $self->body_id }); +} + no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/DB/Result/Building/PlanetaryCommand.pm b/lib/Lacuna/DB/Result/Building/PlanetaryCommand.pm index 6cbbeb8b..6bf1bf5e 100644 --- a/lib/Lacuna/DB/Result/Building/PlanetaryCommand.pm +++ b/lib/Lacuna/DB/Result/Building/PlanetaryCommand.pm @@ -65,6 +65,12 @@ around produces_food_items => sub { return $foods; }; +sub incoming_supply_chains { + my ($self) = @_; + + return Lacuna->db->resultset('Lacuna::DB::Result::SupplyChain')->search({ target_id => $self->body_id }); +} + no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/DB/Result/SupplyChain.pm b/lib/Lacuna/DB/Result/SupplyChain.pm index f34a9c72..93730fe7 100644 --- a/lib/Lacuna/DB/Result/SupplyChain.pm +++ b/lib/Lacuna/DB/Result/SupplyChain.pm @@ -40,6 +40,27 @@ sub get_status { }; } +sub get_incoming_status { + my ($self) = @_; + + my $planet = $self->planet; + return { + id => $self->id, + from_body => { + id => $planet->id, + name => $planet->name, + x => $planet->x, + y => $planet->y, + image => $planet->image, + }, + building_id => $self->building_id, + resource_hour => $self->resource_hour, + resource_type => $self->resource_type, + stalled => $self->stalled, + percent_transferred => $self->percent_transferred, + }; +} + no Moose; __PACKAGE__->meta->make_immutable(inline_constructor => 0); diff --git a/lib/Lacuna/RPC/Building/PlanetaryCommand.pm b/lib/Lacuna/RPC/Building/PlanetaryCommand.pm index 5870f9e5..23e8c6fa 100644 --- a/lib/Lacuna/RPC/Building/PlanetaryCommand.pm +++ b/lib/Lacuna/RPC/Building/PlanetaryCommand.pm @@ -5,6 +5,8 @@ use utf8; no warnings qw(uninitialized); extends 'Lacuna::RPC::Building'; +with 'Lacuna::Role::IncomingSupplyChains'; + sub app_url { return '/planetarycommand'; } @@ -54,7 +56,7 @@ sub view_plans { } } -__PACKAGE__->register_rpc_method_names(qw(view_plans)); +__PACKAGE__->register_rpc_method_names(qw(view_plans view_incoming_supply_chains)); diff --git a/lib/Lacuna/RPC/Building/StationCommand.pm b/lib/Lacuna/RPC/Building/StationCommand.pm index b1534039..27c0b4ec 100644 --- a/lib/Lacuna/RPC/Building/StationCommand.pm +++ b/lib/Lacuna/RPC/Building/StationCommand.pm @@ -5,6 +5,8 @@ use utf8; no warnings qw(uninitialized); extends 'Lacuna::RPC::Building'; +with 'Lacuna::Role::IncomingSupplyChains'; + sub app_url { return '/stationcommand'; } @@ -52,7 +54,7 @@ sub view_plans { } } -__PACKAGE__->register_rpc_method_names(qw(view_plans)); +__PACKAGE__->register_rpc_method_names(qw(view_plans view_incoming_supply_chains)); diff --git a/lib/Lacuna/Role/IncomingSupplyChains.pm b/lib/Lacuna/Role/IncomingSupplyChains.pm new file mode 100644 index 00000000..c32fe55a --- /dev/null +++ b/lib/Lacuna/Role/IncomingSupplyChains.pm @@ -0,0 +1,25 @@ +package Lacuna::Role::IncomingSupplyChains; + +use Moose::Role; + + +sub view_incoming_supply_chains { + my ($self, $session_id, $building_id) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + unless ($building) { + confess [1002, "Cannot find that building."]; + } + + my @supply_chains; + my $chains = $building->incoming_supply_chains; + while (my $chain = $chains->next) { + push @supply_chains, $chain->get_incoming_status; + } + return { + status => $self->format_status($empire, $building->body), + supply_chains => \@supply_chains, + }; +} + +1; -- 2.51.2 From bb6f89fb4bd9139a9cf13573f617907433714724 Mon Sep 17 00:00:00 2001 From: fireartist Date: Thu, 28 Jun 2012 13:51:47 +0100 Subject: [PATCH 4/4] remove unused vars - saves a method call --- lib/Lacuna/RPC/Building/Trade.pm | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Lacuna/RPC/Building/Trade.pm b/lib/Lacuna/RPC/Building/Trade.pm index 8506561f..a53cf7f2 100644 --- a/lib/Lacuna/RPC/Building/Trade.pm +++ b/lib/Lacuna/RPC/Building/Trade.pm @@ -212,7 +212,6 @@ sub view_supply_chains { } my $max_chains = $building->level * 3; - my $body = $building->body; my @supply_chains; my $chains = $building->supply_chains; while (my $chain = $chains->next) { @@ -233,7 +232,6 @@ sub view_waste_chains { unless ($building) { confess [1002, "Cannot find that building."]; } - my $body = $building->body; my @waste_chains; my $chains = $building->waste_chains; while (my $waste_push = $chains->next) { -- 2.51.2