From 9ca4859f98dfe48e4fee596086a65db0800d44b5 Mon Sep 17 00:00:00 2001 From: Ian Docherty Date: Mon, 2 Jul 2012 11:08:58 -0400 Subject: [PATCH] Aggregate plans, now works for trade --- lib/Lacuna/DB/Result/Empire.pm | 2 +- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 12 ++++++-- lib/Lacuna/DB/Result/Plan.pm | 12 ++++++++ lib/Lacuna/RPC/Body.pm | 6 ++-- lib/Lacuna/RPC/Building.pm | 2 +- lib/Lacuna/RPC/Building/PlanetaryCommand.pm | 16 +++++------ lib/Lacuna/Role/TraderRpc.pm | 32 ++++++--------------- t/TestHelper.pm | 14 ++++----- 8 files changed, 50 insertions(+), 46 deletions(-) diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index 3ff13021..e68ef212 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -503,7 +503,7 @@ sub found { $self->stage('founded'); $self->update; $self->home_planet($home_planet); - weaken($self->{_relationship_data}{home_planet}); +# weaken($self->{_relationship_data}{home_planet}); $self->add_probe($home_planet->star_id, $home_planet->id); diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 799a8816..f8820d7c 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -33,6 +33,14 @@ sub _build_plan_cache { return \@plans; } +# Sort plans by name (asc), by level (asc), by extra_build_level (desc) +sub sorted_plans { + my ($self) = @_; + + my @sorted_plans = sort {$a->class->name cmp $b->class->name || $a->level cmp $b->level || $b->extra_build_level cmp $a->extra_build_level } @{$self->plan_cache}; + return \@sorted_plans; +} + sub delete_building { my ($self, $building) = @_; @@ -1266,8 +1274,8 @@ sub tick { # synchronize completion of tasks foreach my $key (sort keys %todo) { my ($object, $job) = ($todo{$key}{object}, $todo{$key}{type}); - $object->body($self); - weaken($object->{_relationship_data}{body}); +# $object->body($self); +# weaken($object->{_relationship_data}{body}); if ($job eq 'ship built') { $self->tick_to($object->date_available); $object->finish_construction; diff --git a/lib/Lacuna/DB/Result/Plan.pm b/lib/Lacuna/DB/Result/Plan.pm index 8cf6560e..5d975c5d 100644 --- a/lib/Lacuna/DB/Result/Plan.pm +++ b/lib/Lacuna/DB/Result/Plan.pm @@ -100,6 +100,18 @@ sub check_glyph_recipe { return $plan_class; } +# Delete one plan +sub delete_one_plan { + my ($self) = @_; + + if ($self->quantity <= 1) { + $self->delete; + } + else { + $self->quantity($self->quantity - 1); + $self->update; + } +} __PACKAGE__->belongs_to('body', 'Lacuna::DB::Result::Map::Body', 'body_id'); diff --git a/lib/Lacuna/RPC/Body.pm b/lib/Lacuna/RPC/Body.pm index 8fc3c1db..e7cb9dd1 100644 --- a/lib/Lacuna/RPC/Body.pm +++ b/lib/Lacuna/RPC/Body.pm @@ -386,12 +386,14 @@ sub get_buildable { } # plans + # my %plans; - my $plan_rs = $body->plans->search({level => 1},{ order_by => { -asc => 'extra_build_level' }}); - while (my $plan = $plan_rs->next) { + my @buildable_plans = sort {$a->extra_build_level <=> $b->extra_build_level} grep{$_->level == 1} @{$body->plan_cache}; + for my $plan (@buildable_plans) { push @buildable, $plan->class->controller_class; $plans{$plan->class} = $plan->extra_build_level; } + foreach my $class (uniq @buildable) { $properties{class} = $class->model_class; my $building = $building_rs->new(\%properties); diff --git a/lib/Lacuna/RPC/Building.pm b/lib/Lacuna/RPC/Building.pm index 73c6c56c..c1caf44b 100644 --- a/lib/Lacuna/RPC/Building.pm +++ b/lib/Lacuna/RPC/Building.pm @@ -173,7 +173,7 @@ sub build { if ($plan->extra_build_level) { $building->level( $plan->extra_build_level); } - $plan->delete; + $plan->delete_one_plan; } else { $body->spend_food($cost->{food}, 0); diff --git a/lib/Lacuna/RPC/Building/PlanetaryCommand.pm b/lib/Lacuna/RPC/Building/PlanetaryCommand.pm index be8eaf27..6a937e26 100644 --- a/lib/Lacuna/RPC/Building/PlanetaryCommand.pm +++ b/lib/Lacuna/RPC/Building/PlanetaryCommand.pm @@ -32,23 +32,21 @@ sub view_plans { 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); - $item_hash->{$key} = { + + my @out; + my $sorted_plans = $building->body->sorted_plans; + foreach my $plan (@$sorted_plans) { + my $item = { quantity => $plan->quantity, name => $plan->class->name, level => $plan->level, extra_build_level => $plan->extra_build_level, }; + push @out, $item; } - my $out; - @{$out} = sort {$a->{name} cmp $b->{name} || $a->{level} <=> $b->{level} || $b->{extra_build_level} <=> $a->{extra_build_level} } values %{$item_hash}; - return { status => $self->format_status($empire, $building->body), - plans => $out, + plans => \@out, } } diff --git a/lib/Lacuna/Role/TraderRpc.pm b/lib/Lacuna/Role/TraderRpc.pm index 7f93729d..54b9660a 100644 --- a/lib/Lacuna/Role/TraderRpc.pm +++ b/lib/Lacuna/Role/TraderRpc.pm @@ -177,33 +177,19 @@ sub get_plan_summary { my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); - my $plans = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->search( - {body_id => $building->body_id} - ); - - my $plan_summary = {}; - while (my $plan = $plans->next) { - my $key = sprintf("%s~%s~%02u~%02u", $plan->class->name, $plan->class, $plan->level, $plan->extra_build_level); - $plan_summary->{$key}++; - } - - # Sort - my @plans = map { {$_ => $plan_summary->{$_}} } sort {$a cmp $b} keys %$plan_summary; my @out; - for my $plan (@plans) { - my ($key,$quantity) = %$plan; - my ($name,$class,$level,$extra) = split /~/, $key; - my $plan_type = $class; + my $sorted_plans = $building->body->sorted_plans; + foreach my $plan (@$sorted_plans) { + my $plan_type = $plan->class->name; $plan_type =~ s/Lacuna::DB::Result::Building:://; $plan_type =~ s/::/_/g; - push @out, { - name => $name, + name => $plan->class->name, plan_type => $plan_type, - level => int($level), - extra_build_level => int($extra), - quantity => $quantity, + level => int($plan->level), + extra_build_level => int($plan->extra_build_level), + quantity => $plan->quantity, }; } return { @@ -214,7 +200,7 @@ sub get_plan_summary { } -sub get_plans { +sub get_plans_delete_me_i_am_not_used { my ($self, $session_id, $building_id) = @_; my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); @@ -240,7 +226,7 @@ sub get_plans { }; } -sub get_glyphs { +sub get_glyphs_delete_me_i_am_not_used { my ($self, $session_id, $building_id) = @_; my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); diff --git a/t/TestHelper.pm b/t/TestHelper.pm index 6f83c68d..f5279364 100644 --- a/t/TestHelper.pm +++ b/t/TestHelper.pm @@ -116,11 +116,13 @@ sub generate_test_empire { sub get_building { my ($self, $building_id) = @_; - my $building = Lacuna->db->resultset('Lacuna::DB::Result::Building')->find($building_id); + + $self->empire->home_planet->clear_building_cache; + + my ($building) = grep {$_->id == $building_id} @{$self->empire->home_planet->building_cache}; unless (defined $building) { confess 'Building does not exist.'; } - $building->body($self->empire->home_planet); return $building; } @@ -132,13 +134,9 @@ sub find_empty_plot { # Ensure we only build on an empty plot EXISTING_BUILDING: while (1) { - my $building = Lacuna->db->resultset('Lacuna::DB::Result::Building')->search({ - x => $self->x, - y => $self->y, - body_id => $home->id, - }); + my ($building) = grep {$_->x == $self->x and $_->y == $self->y} @{$home->building_cache}; - last EXISTING_BUILDING if $building == 0; + last EXISTING_BUILDING if not $building; $self->x($self->x + 1); if ($self->x == 6) { $self->x(-5); -- 2.51.2