From 1d100d197bad1460d4e154c3b734a05abfe33fd1 Mon Sep 17 00:00:00 2001 From: Ian Docherty Date: Sun, 1 Jul 2012 16:58:24 -0400 Subject: [PATCH] Start of plan aggregation, added plan in db and modified PlanetaryCommand to show plans. --- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 29 ++++-- lib/Lacuna/DB/Result/Plan.pm | 107 ++++++++++++++++++++ lib/Lacuna/RPC/Building/PlanetaryCommand.pm | 16 ++- var/upgrades/3.plan.sql | 16 +++ 4 files changed, 149 insertions(+), 19 deletions(-) create mode 100644 lib/Lacuna/DB/Result/Plan.pm create mode 100644 var/upgrades/3.plan.sql diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 4c3ee7e8..799a8816 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -14,7 +14,7 @@ use Scalar::Util qw(weaken); no warnings 'uninitialized'; __PACKAGE__->has_many('ships','Lacuna::DB::Result::Ships','body_id'); -__PACKAGE__->has_many('plans','Lacuna::DB::Result::Plans','body_id'); +__PACKAGE__->has_many('plans','Lacuna::DB::Result::Plan','body_id'); __PACKAGE__->has_many('glyphs','Lacuna::DB::Result::Glyphs','body_id'); __PACKAGE__->has_many('waste_chains', 'Lacuna::DB::Result::WasteChain','planet_id'); __PACKAGE__->has_many('out_supply_chains', 'Lacuna::DB::Result::SupplyChain','planet_id'); @@ -142,14 +142,25 @@ sub add_plan { my ($self, $class, $level, $extra_build_level) = @_; # add it - my $plan = $self->plans->new({ - body_id => $self->id, - class => $class, - level => $level, - extra_build_level => $extra_build_level, - })->insert; - - push @{$self->plan_cache}, $plan; + my ($plan) = grep { + $_->class eq $class + and $_->level == $level + and $_->extra_build_level == $extra_build_level, + } @{$self->plan_cache}; + if ($plan) { + $plan->quantity($plan->quantity + 1); + $plan->update; + } + else { + $plan = $self->plans->create({ + body_id => $self->id, + class => $class, + level => $level, + extra_build_level => $extra_build_level, + quantity => 1, + }); + push @{$self->plan_cache}, $plan; + } return $plan; } diff --git a/lib/Lacuna/DB/Result/Plan.pm b/lib/Lacuna/DB/Result/Plan.pm new file mode 100644 index 00000000..8cf6560e --- /dev/null +++ b/lib/Lacuna/DB/Result/Plan.pm @@ -0,0 +1,107 @@ +package Lacuna::DB::Result::Plan; + +use 5.010; +use Moose; +use utf8; +no warnings qw(uninitialized); +extends 'Lacuna::DB::Result'; +use Lacuna::Util qw(format_date); +use DateTime; + +__PACKAGE__->table('plan'); +__PACKAGE__->add_columns( + body_id => { data_type => 'int', size => 11, is_nullable => 0 }, + class => { data_type => 'varchar', size => 255, is_nullable => 0 }, + level => { data_type => 'tinyint', is_nullable => 0 }, + extra_build_level => { data_type => 'tinyint', is_nullable => 1, default_value => 0 }, + quantity => { data_type => 'int', size => 11, is_nullable => 0 }, +); + +sub level_formatted { + my ($self) = @_; + + my $level = $self->level; + if ($self->extra_build_level) { + $level .= '+'.$self->extra_build_level; + } + return $level; +} + +my $recipes = { + 'Lacuna::DB::Result::Building::Permanent::LibraryOfJith' => [qw(anthracite bauxite beryl chalcopyrite)], + 'Lacuna::DB::Result::Building::Permanent::BeeldebanNest' => [qw(anthracite trona kerogen)], + 'Lacuna::DB::Result::Building::Permanent::Sand' => [qw(bauxite)], + 'Lacuna::DB::Result::Building::Permanent::MassadsHenge' => [qw(bauxite trona kerogen monazite)], + 'Lacuna::DB::Result::Building::Permanent::CitadelOfKnope' => [qw(beryl sulfur monazite galena)], + 'Lacuna::DB::Result::Building::Permanent::AmalgusMeadow' => [qw(beryl trona)], + 'Lacuna::DB::Result::Building::Permanent::Lagoon' => [qw(chalcopyrite)], + 'Lacuna::DB::Result::Building::Permanent::GeoThermalVent' => [qw(chalcopyrite sulfur)], + 'Lacuna::DB::Result::Building::Permanent::GratchsGauntlet' => [qw(chromite bauxite gold kerogen)], + 'Lacuna::DB::Result::Building::Permanent::MalcudField' => [qw(fluorite kerogen)], + 'Lacuna::DB::Result::Building::Permanent::KalavianRuins' => [qw(galena gold)], + 'Lacuna::DB::Result::Building::Permanent::Lake' => [qw(goethite)], + 'Lacuna::DB::Result::Building::Permanent::HallsOfVrbansk A' => [qw(goethite halite gypsum trona)], + 'Lacuna::DB::Result::Building::Permanent::OracleOfAnid' => [qw(gold uraninite bauxite goethite)], + 'Lacuna::DB::Result::Building::Permanent::HallsOfVrbansk B' => [qw(gold anthracite uraninite bauxite)], + 'Lacuna::DB::Result::Building::Permanent::Beach1' => [qw(gypsum)], + 'Lacuna::DB::Result::Building::Permanent::Beach9' => [qw(gypsum anthracite)], + 'Lacuna::DB::Result::Building::Permanent::Beach7' => [qw(gypsum chalcopyrite)], + 'Lacuna::DB::Result::Building::Permanent::Beach11' => [qw(gypsum chromite)], + 'Lacuna::DB::Result::Building::Permanent::Beach13' => [qw(gypsum galena)], + 'Lacuna::DB::Result::Building::Permanent::Beach12' => [qw(gypsum goethite)], + 'Lacuna::DB::Result::Building::Permanent::Beach2' => [qw(gypsum gypsum)], + 'Lacuna::DB::Result::Building::Permanent::Beach5' => [qw(gypsum halite)], + 'Lacuna::DB::Result::Building::Permanent::Beach3' => [qw(gypsum magnetite)], + 'Lacuna::DB::Result::Building::Permanent::Beach10' => [qw(gypsum methane)], + 'Lacuna::DB::Result::Building::Permanent::Beach6' => [qw(gypsum rutile)], + 'Lacuna::DB::Result::Building::Permanent::Beach8' => [qw(gypsum sulfur)], + 'Lacuna::DB::Result::Building::Permanent::PantheonOfHagness' => [qw(gypsum trona beryl anthracite)], + 'Lacuna::DB::Result::Building::Permanent::Beach4' => [qw(gypsum uraninite)], + 'Lacuna::DB::Result::Building::Permanent::LapisForest' => [qw(halite anthracite)], + 'Lacuna::DB::Result::Building::Permanent::BlackHoleGenerator' => [qw(kerogen beryl anthracite monazite)], + 'Lacuna::DB::Result::Building::Permanent::HallsOfVrbansk C' => [qw(kerogen methane sulfur zircon)], + 'Lacuna::DB::Result::Building::Permanent::TempleOfTheDrajilites'=> [qw(kerogen rutile chromite chalcopyrite)], + 'Lacuna::DB::Result::Building::Permanent::NaturalSpring' => [qw(magnetite halite)], + 'Lacuna::DB::Result::Building::Permanent::Volcano' => [qw(magnetite uraninite)], + 'Lacuna::DB::Result::Building::Permanent::Grove' => [qw(methane)], + 'Lacuna::DB::Result::Building::Permanent::InterDimensionalRift' => [qw(methane zircon fluorite)], + 'Lacuna::DB::Result::Building::Permanent::TerraformingPlatform' => [qw(methane zircon magnetite beryl)], + 'Lacuna::DB::Result::Building::Permanent::HallsOfVrbansk D' => [qw(monazite fluorite beryl magnetite)], + 'Lacuna::DB::Result::Building::Permanent::CrashedShipSite' => [qw(monazite trona gold bauxite)], +# 'Lacuna::DB::Result::Building::Permanent::KasternsKeep' => [qw(monazite uraninite sulfur trona)], + 'Lacuna::DB::Result::Building::Permanent::Crater' => [qw(rutile)], + 'Lacuna::DB::Result::Building::Permanent::HallsOfVrbansk E' => [qw(rutile chromite chalcopyrite galena)], + 'Lacuna::DB::Result::Building::Permanent::DentonBrambles' => [qw(rutile goethite)], + 'Lacuna::DB::Result::Building::Permanent::GasGiantPlatform' => [qw(sulfur methane galena anthracite)], + 'Lacuna::DB::Result::Building::Permanent::RockyOutcrop' => [qw(trona)], + 'Lacuna::DB::Result::Building::Permanent::AlgaePond' => [qw(uraninite methane)], + 'Lacuna::DB::Result::Building::Permanent::Ravine' => [qw(zircon methane galena fluorite)], +}; + +# given a plan class, return the glyph recipe +# (note, Halls must specify A,B,C,D or E) +# +sub get_glyph_recipe { + my ($class,$plan_class) = @_; + + return $recipes->{$plan_class}; +} + +# Given glyphs, return the plan (if any) +sub check_glyph_recipe { + my ($class, $glyphs) = @_; + + my ($plan_class) = grep {@{$recipes->{$_}} ~~ @$glyphs} keys %$recipes; + if (defined $plan_class) { + # Sort out different Halls recipes + $plan_class =~ s/HallsOfVrbansk.*$/HallsOfVrbansk/; + } + + return $plan_class; +} + + +__PACKAGE__->belongs_to('body', 'Lacuna::DB::Result::Map::Body', 'body_id'); + +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 23e8c6fa..be8eaf27 100644 --- a/lib/Lacuna/RPC/Building/PlanetaryCommand.pm +++ b/lib/Lacuna/RPC/Building/PlanetaryCommand.pm @@ -29,23 +29,19 @@ around 'view' => sub { sub view_plans { my ($self, $session_id, $building_id) = @_; + my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); my $item_hash; my $plans = $building->body->plans; while (my $plan = $plans->next) { - my $key = sprintf("%s-%s-%s", $plan->class->name, $plan->level, $plan->extra_build_level); - if (defined($item_hash->{$key})) { - $item_hash->{$key}->{quantity}++; - } - else { + my $key = sprintf("%s-%s-%s", $plan->class->name, $plan->level, $plan->extra_build_level); $item_hash->{$key} = { - quantity => 1, - name => $plan->class->name, - level => $plan->level, - extra_build_level => $plan->extra_build_level, + quantity => $plan->quantity, + name => $plan->class->name, + level => $plan->level, + extra_build_level => $plan->extra_build_level, }; - } } my $out; @{$out} = sort {$a->{name} cmp $b->{name} || $a->{level} <=> $b->{level} || $b->{extra_build_level} <=> $a->{extra_build_level} } values %{$item_hash}; diff --git a/var/upgrades/3.plan.sql b/var/upgrades/3.plan.sql new file mode 100644 index 00000000..ef8a0135 --- /dev/null +++ b/var/upgrades/3.plan.sql @@ -0,0 +1,16 @@ + +CREATE TABLE plan ( + id INT(11) NOT NULL AUTO_INCREMENT, + body_id INT(11) NOT NULL, + class VARCHAR(255) NOT NULL, + level tinyint not NULL, + extra_build_level tinyint not NULL, + quantity INT(11) NOT NULL DEFAULT 0, + PRIMARY KEY (id), + KEY plan_idx_body_id (body_id), + CONSTRAINT plan_fc_body_id FOREIGN KEY (body_id) REFERENCES body (id) +); + +INSERT INTO plan (body_id,class,level,extra_build_level,quantity) SELECT body_id,class,level,extra_build_level,count(id) from plans group by body_id,class,level,extra_build_level; + +--- INSERT INTO glyph (body_id, TYPE, quantity) SELECT body_id,TYPE,COUNT(id) FROM glyphs GROUP BY body_id,TYPE; -- 2.51.2