From e48c9cb5cf29c9148c76cc6bee4a53f8cc6b77ef Mon Sep 17 00:00:00 2001 From: lemming552 Date: Tue, 15 May 2012 13:56:07 -0600 Subject: [PATCH 01/13] Table changes. (now to sweep code) --- lib/Lacuna/DB/Result/{Glyphs.pm => Glyph.pm} | 5 +++-- var/upgrades/3.0852.sql | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) rename lib/Lacuna/DB/Result/{Glyphs.pm => Glyph.pm} (75%) create mode 100644 var/upgrades/3.0852.sql diff --git a/lib/Lacuna/DB/Result/Glyphs.pm b/lib/Lacuna/DB/Result/Glyph.pm similarity index 75% rename from lib/Lacuna/DB/Result/Glyphs.pm rename to lib/Lacuna/DB/Result/Glyph.pm index a36fa476..f56184bb 100644 --- a/lib/Lacuna/DB/Result/Glyphs.pm +++ b/lib/Lacuna/DB/Result/Glyph.pm @@ -1,14 +1,15 @@ -package Lacuna::DB::Result::Glyphs; +package Lacuna::DB::Result::Glyph; use Moose; use utf8; no warnings qw(uninitialized); extends 'Lacuna::DB::Result'; -__PACKAGE__->table('glyphs'); +__PACKAGE__->table('glyph'); __PACKAGE__->add_columns( body_id => { data_type => 'int', is_nullable => 0 }, type => { data_type => 'varchar', size => 20, is_nullable => 0 }, + quantity => { data_type => 'int', default_value => 0 }, ); __PACKAGE__->belongs_to('body', 'Lacuna::DB::Result::Map::Body', 'body_id'); diff --git a/var/upgrades/3.0852.sql b/var/upgrades/3.0852.sql new file mode 100644 index 00000000..9cbf87a9 --- /dev/null +++ b/var/upgrades/3.0852.sql @@ -0,0 +1,12 @@ + +CREATE TABLE glyph ( + id INT(11) NOT NULL AUTO_INCREMENT, + body_id INT(11) NOT NULL, + type VARCHAR(20) NOT NULL, + quantity INT(11) NOT NULL DEFAULT 0, + PRIMARY KEY (id), + KEY glyph_idx_body_id (body_id), + CONSTRAINT glyph_fc_body_id FOREIGN KEY (body_id) REFERENCES body (id) +); + +INSERT INTO glyph (body_id, TYPE, quantity) SELECT body_id,TYPE,COUNT(id) FROM glyphs GROUP BY body_id,TYPE; -- 2.51.2 From be402b02d5def16fc3d497b39327f4a6e368c05b Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 21 May 2012 09:43:24 -0600 Subject: [PATCH 02/13] untested --- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 25 +++++++++--- lib/Lacuna/Role/Container.pm | 30 ++------------ lib/Lacuna/Role/Trader.pm | 54 +++++++++++-------------- 3 files changed, 47 insertions(+), 62 deletions(-) diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index e7ccc21d..be0ad190 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -14,7 +14,7 @@ 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('glyphs','Lacuna::DB::Result::Glyphs','body_id'); +__PACKAGE__->has_many('glyph','Lacuna::DB::Result::Glyph','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'); __PACKAGE__->has_many('in_supply_chains', 'Lacuna::DB::Result::SupplyChain','target_id'); @@ -80,11 +80,26 @@ sub claimed_by { # GLYPHS sub add_glyph { - my ($self, $type) = @_; + my ($self, $type, $num_add) = @_; + + $num_add = 1 unless defined($num_add); + + my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ + type => $type, + body_id => $self->id, + })->single; + if (defined($glyph)) { + my $sum = $num_add + $self->glyph->quantity; + $self->glyph->quantity($sum); + return $self->glyph; + } + else { return $self->glyphs->new({ - type => $type, - body_id => $self->id, + type => $type, + body_id => $self->id, + quantity => $num_add, })->insert; + } } # PLANS @@ -128,7 +143,7 @@ sub sanitize { } $self->alliance_id(undef); $self->plans->delete; - $self->glyphs->delete; + $self->glyph->delete; $self->waste_chains->delete; # do indivitual deletes so the remote ends can be titied up too foreach my $chain ($self->out_supply_chains) { diff --git a/lib/Lacuna/Role/Container.pm b/lib/Lacuna/Role/Container.pm index ad98e0c0..8ab0c81c 100644 --- a/lib/Lacuna/Role/Container.pm +++ b/lib/Lacuna/Role/Container.pm @@ -23,21 +23,6 @@ sub format_body_stats_for_log { sub unload { my ($self, $body, $withdraw) = @_; my $payload = $self->payload; - #my $cargo_log = Lacuna->db->resultset('Lacuna::DB::Result::Log::Cargo'); - #$cargo_log->new({ - # message => 'payload to unload', - # body_id => $body->id, - # data => $payload, - # object_type => ref($self), - # object_id => $self->id, - #})->insert; - #$cargo_log->new({ - # message => 'before unload', - # body_id => $body->id, - # data => $self->format_body_stats_for_log($body), - # object_type => ref($self), - # object_id => $self->id, - #})->insert; if (exists $payload->{prisoners}) { foreach my $id (@{$payload->{prisoners}}) { my $prisoner = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($id); @@ -94,18 +79,11 @@ sub unload { delete $payload->{plans}; } if (exists $payload->{glyphs}) { - foreach my $glyph (@{$payload->{glyphs}}) { - $body->add_glyph($glyph); - } - delete $payload->{glyphs}; + foreach my $glyph (@{$payload->{glyphs}}) { + $body->add_glyph($glyph->{name}, $glyph->{quantity}); + } + delete $payload->{glyphs}; } - #$cargo_log->new({ - # message => 'after unload', - # body_id => $body->id, - # data => $self->format_body_stats_for_log($body), - # object_type => ref($self), - # object_id => $self->id, - #})->insert; $self->payload($payload); return $self; } diff --git a/lib/Lacuna/Role/Trader.pm b/lib/Lacuna/Role/Trader.pm index 2c326d2b..acb394c6 100644 --- a/lib/Lacuna/Role/Trader.pm +++ b/lib/Lacuna/Role/Trader.pm @@ -22,40 +22,32 @@ sub my_market { } sub check_payload { - my ($self, $items, $available_cargo_space, $space_exception, $transfer_ship) = @_; - my $body = $self->body; - $space_exception ||= $cargo_exception; + my ($self, $items, $available_cargo_space, $space_exception, $transfer_ship) = @_; + my $body = $self->body; + $space_exception ||= $cargo_exception; - # validate - unless (ref $items eq 'ARRAY') { - confess 'The list of items you want to trade needs to be formatted as an array of hashes.'; - } + # validate + unless (ref $items eq 'ARRAY') { + confess 'The list of items you want to trade needs to be formatted as an array of hashes.'; + } - my $space_used; - my @expanded_items; - - foreach my $item (@{$items}) { - given($item->{type}) { - when ([qw(water energy waste), ORE_TYPES, FOOD_TYPES]) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess $have_exception unless ($body->type_stored($item->{type}) >= $item->{quantity}); - push @expanded_items, $item; - $space_used += $item->{quantity}; - } - when ('glyph') { - if ($item->{glyph_id}) { - my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyphs')->find($item->{glyph_id}); - confess $have_exception unless (defined $glyph && $self->body_id eq $glyph->body_id); - push @expanded_items, $item; - $space_used += 100; - } - elsif ($item->{quantity}) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + my $space_used; + my @expanded_items; - confess [1002, 'you must specify a glyph name if you specify a quantity.'] unless $item->{name}; - my @glyphs = Lacuna->db->resultset('Lacuna::DB::Result::Glyphs')->search({ + foreach my $item (@{$items}) { + given($item->{type}) { + when ([qw(water energy waste), ORE_TYPES, FOOD_TYPES]) { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess $have_exception unless ($body->type_stored($item->{type}) >= $item->{quantity}); + push @expanded_items, $item; + $space_used += $item->{quantity}; + } + when ('glyph') { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess [1002, 'you must specify a glyph name with a quantity.'] unless $item->{name}; + my @glyphs = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ type => $item->{name}, body_id => $self->body_id, }); -- 2.51.2 From f1b77286446809d372e9d3171ff11109f4eaa603 Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 21 May 2012 11:42:54 -0600 Subject: [PATCH 03/13] Glyphs reduced to one row per planet. --- lib/Lacuna/AI/DeLambert.pm | 20 +- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 19 ++ lib/Lacuna/DB/Result/Spies.pm | 10 +- lib/Lacuna/Role/Container.pm | 148 ++++++----- lib/Lacuna/Role/Trader.pm | 334 ++++++++++++------------ lib/Lacuna/Role/TraderRpc.pm | 70 ++--- 6 files changed, 316 insertions(+), 285 deletions(-) diff --git a/lib/Lacuna/AI/DeLambert.pm b/lib/Lacuna/AI/DeLambert.pm index 3da7c045..9e409dd3 100644 --- a/lib/Lacuna/AI/DeLambert.pm +++ b/lib/Lacuna/AI/DeLambert.pm @@ -229,11 +229,13 @@ sub sell_glyph_trade { if ($quantity * $cost_per > 100) { $quantity = int(100 / $cost_per); } - my @glyphs; - for (1..$quantity) { - my $ore = random_element([ORE_TYPES]); - push @glyphs, $ore; - } +# Instead of random assortment, quantity of one glyph + my $ore = random_element([ORE_TYPES]); + my @glyphs = [ { + name => $ore, + quantity => $quantity, + glyph_id => 0, + } ]; if ($quantity) { say "Creating a trade for $quantity glyphs"; $ship->task('Waiting On Trade'); @@ -603,9 +605,11 @@ sub process_email { $asked_for_too_many = 1; } $quantity = 0 if $quantity < 0; - for (1..$quantity) { - push @{$payload->{glyphs}}, lc $glyph; - } + push @{$payload->{glyphs}}, [{ + name => lc $glyph, + quantity => $quantity, + glyph_id => 0, + }]; $total_glyphs += $quantity; } diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index be0ad190..5dfd81d4 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -102,6 +102,25 @@ sub add_glyph { } } +sub use_glyph { + my ($self, $glyph_id, $type, $num_used) = @_; + my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ + id => $glyph_id, + type => $type, + body_id => $self->id, + })->single; + return 0 unless defined($glyph); + if ($glyph->{quantity} > $num_used) { + my $sum = $self->glyph->quantity - $num_used; + $self->glyph->quantity($sum); + } + else { + $num_used = $glyph->quantity; + $glyph->delete; + } + return $num_used; +} + # PLANS sub get_plan { my ($self, $class, $level) = @_; diff --git a/lib/Lacuna/DB/Result/Spies.pm b/lib/Lacuna/DB/Result/Spies.pm index f1d17a99..693346da 100644 --- a/lib/Lacuna/DB/Result/Spies.pm +++ b/lib/Lacuna/DB/Result/Spies.pm @@ -1873,7 +1873,7 @@ sub destroy_glyph { $self->on_body->add_news(70, 'A museum was broken into on %s and a rare artifact was smashed to pieces.', $self->on_body->name); - $glyph->delete; + $on_body->use_glyph($glyph->id, $glyph->type, 1); return $message->id; } @@ -2070,11 +2070,15 @@ sub steal_glyph { direction => 'in', payload => { spies => [ $self->id ], - glyphs => [$glyph->type], + glyphs => [ { + name => $glyph->type, + quantity => $glyph->quantity, + glyph_id => $glyph->id, + } ], }, ); my @table = (['Glyph'],[$glyph->type]); - $glyph->delete; + $on_body->use_glyph($glyph->id, $glyph->type, 1); $self->on_body->empire->send_predefined_message( tags => ['Spies','Alert'], filename => 'ship_stolen.txt', diff --git a/lib/Lacuna/Role/Container.pm b/lib/Lacuna/Role/Container.pm index 8ab0c81c..62e42f42 100644 --- a/lib/Lacuna/Role/Container.pm +++ b/lib/Lacuna/Role/Container.pm @@ -89,93 +89,91 @@ sub unload { } sub format_description_of_payload { - my ($self) = @_; - my $item_arr = []; - my $scratch; - my $payload = $self->payload; - - # essentia - push @{$item_arr}, sprintf('%s essentia.', commify($payload->{essentia})) if ($payload->{essentia}); + my ($self) = @_; + my $item_arr = []; + my $scratch; + my $payload = $self->payload; - # resources - foreach my $resource (keys %{ $payload->{resources}}) { - push @{$item_arr}, sprintf('%s %s', commify($payload->{resources}{$resource}), $resource); - } + # essentia + push @{$item_arr}, sprintf('%s essentia.', commify($payload->{essentia})) if ($payload->{essentia}); + + # resources + foreach my $resource (keys %{ $payload->{resources}}) { + push @{$item_arr}, sprintf('%s %s', commify($payload->{resources}{$resource}), $resource); + } - # glyphs - undef $scratch; - foreach my $glyph (@{$payload->{glyphs}}) { - push @{$scratch}, $glyph.' glyph'; - } - push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + # glyphs + foreach my $glyph (@{$payload->{glyphs}}) { + push @{$item_arr}, sprintf('%s %s', commify($glyph->{quantity}), $glyph->{name}); + } - # ships - my $ships = Lacuna->db->resultset('Lacuna::DB::Result::Ships'); + # ships + my $ships = Lacuna->db->resultset('Lacuna::DB::Result::Ships'); - undef $scratch; - foreach my $id (@{ $payload->{ships}}) { - my $ship = $ships->find($id); - next unless defined $ship; - my $pattern = '%s (speed: %s, stealth: %s, hold size: %s, berth: %s, combat: %s)' ; - push @{$scratch}, - sprintf($pattern, - $ship->type_formatted, - commify($ship->speed), - commify($ship->stealth), - commify($ship->hold_size), - commify($ship->berth_level), - commify($ship->combat)); - } - push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + undef $scratch; + foreach my $id (@{ $payload->{ships}}) { + my $ship = $ships->find($id); + next unless defined $ship; + my $pattern = '%s (speed: %s, stealth: %s, hold size: %s, berth: %s, combat: %s)' ; + push @{$scratch}, + sprintf($pattern, + $ship->type_formatted, + commify($ship->speed), + commify($ship->stealth), + commify($ship->hold_size), + commify($ship->berth_level), + commify($ship->combat)); + } + push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); - # plans - undef $scratch; - foreach my $stats (@{ $payload->{plans}}) { - my $level = $stats->{level}; - if ($stats->{extra_build_level}) { - $level .= '+'.$stats->{extra_build_level}; - } - my $pattern = '%s (%s) plan'; - push @{$scratch}, sprintf($pattern, $stats->{class}->name, $level); + # plans + undef $scratch; + foreach my $stats (@{ $payload->{plans}}) { + my $level = $stats->{level}; + if ($stats->{extra_build_level}) { + $level .= '+'.$stats->{extra_build_level}; } - push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + my $pattern = '%s (%s) plan'; + push @{$scratch}, sprintf($pattern, $stats->{class}->name, $level); + } + push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); - # spies - undef $scratch; - my $spies = Lacuna->db->resultset('Lacuna::DB::Result::Spies'); - if (exists $payload->{spies}) { - foreach my $id (@{$payload->{spies}}) { - my $spy = $spies->find($id); - next unless defined $spy; - push @{$scratch}, 'Level '.$spy->level.' spy named '.$spy->name . ' (transport)'; - } - push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + # spies + undef $scratch; + my $spies = Lacuna->db->resultset('Lacuna::DB::Result::Spies'); + if (exists $payload->{spies}) { + foreach my $id (@{$payload->{spies}}) { + my $spy = $spies->find($id); + next unless defined $spy; + push @{$scratch}, 'Level '.$spy->level.' spy named '.$spy->name . ' (transport)'; } - - # prisoners - undef $scratch; - if (exists $payload->{prisoners}) { - foreach my $id (@{$payload->{prisoners}}) { - my $spy = $spies->find($id); - next unless defined $spy; - push @{$scratch}, - 'Level '.$spy->level.' spy named '.$spy->name . - ' (prisoner) sentence expires '.$spy->format_available_on; - } - push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + } + + # prisoners + undef $scratch; + if (exists $payload->{prisoners}) { + foreach my $id (@{$payload->{prisoners}}) { + my $spy = $spies->find($id); + next unless defined $spy; + push @{$scratch}, + 'Level '.$spy->level.' spy named '.$spy->name . + ' (prisoner) sentence expires '.$spy->format_available_on; } + push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + } - # fetch spies - undef $scratch; - if (exists $payload->{fetch_spies}) { - foreach my $id (@{$payload->{fetch_spies}}) { - my $spy = $spies->find($id); - push @{$scratch}, 'Level '.$spy->level.' spy named '.$spy->name . ' (fetch upon arrival)'; - } - push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + # fetch spies + undef $scratch; + if (exists $payload->{fetch_spies}) { + foreach my $id (@{$payload->{fetch_spies}}) { + my $spy = $spies->find($id); + push @{$scratch}, 'Level '.$spy->level.' spy named '.$spy->name . ' (fetch upon arrival)'; } + push @{$item_arr}, @{consolidate_items($scratch)} if (defined($scratch)); + } - return $item_arr; + return $item_arr; } diff --git a/lib/Lacuna/Role/Trader.pm b/lib/Lacuna/Role/Trader.pm index acb394c6..3f3510b0 100644 --- a/lib/Lacuna/Role/Trader.pm +++ b/lib/Lacuna/Role/Trader.pm @@ -6,204 +6,208 @@ use Lacuna::Constants qw(ORE_TYPES FOOD_TYPES); use Lacuna::Util qw(randint); use Data::Dumper; -my $have_exception = [1011, 'You cannot offer to trade something you do not have.']; -my $cargo_exception = 'You need %s cargo space to trade that.'; -my $offer_nothing_exception = [1013, 'It appears that you have offered nothing.']; -my $ask_nothing_exception = [1013, 'It appears that you have asked for nothing.']; -my $fractional_offer_exception = [1013, 'You cannot offer a fraction of something.']; + my $have_exception = [1011, 'You cannot offer to trade something you do not have.']; + my $cargo_exception = 'You need %s cargo space to trade that.'; + my $offer_nothing_exception = [1013, 'It appears that you have offered nothing.']; + my $ask_nothing_exception = [1013, 'It appears that you have asked for nothing.']; + my $fractional_offer_exception = [1013, 'You cannot offer a fraction of something.']; -sub market { + sub market { return Lacuna->db->resultset('Lacuna::DB::Result::Market'); -} + } -sub my_market { + sub my_market { my $self = shift; return $self->market->search({body_id => $self->body_id, transfer_type => $self->transfer_type }); -} + } -sub check_payload { - my ($self, $items, $available_cargo_space, $space_exception, $transfer_ship) = @_; - my $body = $self->body; - $space_exception ||= $cargo_exception; + sub check_payload { + my ($self, $items, $available_cargo_space, $space_exception, $transfer_ship) = @_; + my $body = $self->body; + $space_exception ||= $cargo_exception; # validate - unless (ref $items eq 'ARRAY') { - confess 'The list of items you want to trade needs to be formatted as an array of hashes.'; - } + unless (ref $items eq 'ARRAY') { + confess 'The list of items you want to trade needs to be formatted as an array of hashes.'; + } - my $space_used; - my @expanded_items; + my $space_used; + my @expanded_items; - foreach my $item (@{$items}) { - given($item->{type}) { - when ([qw(water energy waste), ORE_TYPES, FOOD_TYPES]) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess $have_exception unless ($body->type_stored($item->{type}) >= $item->{quantity}); - push @expanded_items, $item; - $space_used += $item->{quantity}; - } - when ('glyph') { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess [1002, 'you must specify a glyph name with a quantity.'] unless $item->{name}; - my @glyphs = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ + foreach my $item (@{$items}) { + given($item->{type}) { + when ([qw(water energy waste), ORE_TYPES, FOOD_TYPES]) { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess $have_exception unless ($body->type_stored($item->{type}) >= $item->{quantity}); + push @expanded_items, $item; + $space_used += $item->{quantity}; + } + when ('glyph') { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess [1002, 'you must specify a glyph name with a quantity.'] unless $item->{name}; + my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ type => $item->{name}, body_id => $self->body_id, - }); - confess [1002, "You don't have ".$item->{quantity}." glyphs of type ".$item->{name}." you only have ".scalar(@glyphs)] unless scalar(@glyphs) >= $item->{quantity}; - push @expanded_items, map { {type => 'glyph', glyph_id => $_->id} } splice @glyphs, 0, $item->{quantity}; - $space_used += 100 * $item->{quantity}; - } - else { - confess [1002, 'You must specify either a glyph_id, or a quantity if you are pushing a glyph.']; - } - } - when ('plan') { - if ($item->{plan_id}) { - my $plan = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->find($item->{plan_id}); - confess $have_exception unless (defined $plan && $self->body_id eq $plan->body_id); - push @expanded_items, $item; - $space_used += 10000; - } - elsif ($item->{quantity}) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess [1002, 'you must specify a plan_type if you specify a quantity.'] unless $item->{plan_type}; - confess [1002, 'you must specify a level if you specify a quantity.'] unless $item->{level}; - confess [1002, 'you must specify an extra_build_level if you specify a quantity.'] unless defined $item->{extra_build_level}; + })->single; + confess [1002, "You don't have ".$item->{quantity}." glyphs of type ". + $item->{name}." you only have ".$glyph->{quantity}] + unless $glyph->{quantity} >= $item->{quantity}; + push @expanded_items, { + name => $item->{name}, + quantity => $item->{quantity}, + glyph_id => $glyph->{id}, + }; + $space_used += 100 * $item->{quantity}; + } + when ('plan') { + if ($item->{plan_id}) { + my $plan = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->find($item->{plan_id}); + confess $have_exception unless (defined $plan && $self->body_id eq $plan->body_id); + push @expanded_items, $item; + $space_used += 10000; + } + elsif ($item->{quantity}) { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess [1002, 'you must specify a plan_type if you specify a quantity.'] unless $item->{plan_type}; + confess [1002, 'you must specify a level if you specify a quantity.'] unless $item->{level}; + confess [1002, 'you must specify an extra_build_level if you specify a quantity.'] unless defined $item->{extra_build_level}; - my $plan_class = $item->{plan_type}; - $plan_class =~ s/_/::/g; - $plan_class = "Lacuna::DB::Result::Building::$plan_class"; - my @plans = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->search({ + my $plan_class = $item->{plan_type}; + $plan_class =~ s/_/::/g; + $plan_class = "Lacuna::DB::Result::Building::$plan_class"; + my @plans = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->search({ class => $plan_class, body_id => $self->body_id, level => $item->{level}, extra_build_level => $item->{extra_build_level}, }); - confess [1002, "You don't have ".$item->{quantity}." plans of type ".$item->{plan_type}." you only have ".scalar(@plans)] unless scalar(@plans) >= $item->{quantity}; - push @expanded_items, map { {type => 'plan',plan_id => $_->id} } splice @plans, 0, $item->{quantity}; - $space_used += 10000 * $item->{quantity}; - - } - else { - confess [1002, 'You must specify either a plan_id, or a quantity if you are pushing a plan.']; - } - } - when ('prisoner') { - confess [1002, 'You must specify a prisoner_id if you are pushing a prisoner.'] unless $item->{prisoner_id}; - my $prisoner = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($item->{prisoner_id}); - confess $have_exception unless (defined $prisoner && $self->body_id eq $prisoner->on_body_id && $prisoner->task eq 'Captured'); - push @expanded_items, $item; - $space_used += 350; - } - when ('ship') { - if ($item->{ship_id}) { - my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($item->{ship_id}); - confess $have_exception unless (defined $ship && $self->body_id eq $ship->body_id && $ship->task eq 'Docked'); - push @expanded_items, $item; - $space_used += 50000; - } - elsif ($item->{quantity}) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess [1002, 'you must specify a name if you specify a quantity.'] unless $item->{name}; - confess [1002, 'you must specify a ship_type if you specify a quantity.'] unless $item->{ship_type}; - confess [1002, 'you must specify a hold_size if you specify a quantity.'] unless defined $item->{hold_size}; - confess [1002, 'you must specify a speed if you specify a quantity.'] unless defined $item->{speed}; - my $ships_rs = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ + confess [1002, "You don't have ".$item->{quantity}. + " plans of type ".$item->{plan_type}. + " you only have ".scalar(@plans)] unless scalar(@plans) >= $item->{quantity}; + push @expanded_items, map { {type => 'plan',plan_id => $_->id} } splice @plans, 0, $item->{quantity}; + $space_used += 10000 * $item->{quantity}; + } + else { + confess [1002, 'You must specify either a plan_id, or a quantity if you are pushing a plan.']; + } + } + when ('prisoner') { + confess [1002, 'You must specify a prisoner_id if you are pushing a prisoner.'] unless $item->{prisoner_id}; + my $prisoner = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($item->{prisoner_id}); + confess $have_exception unless (defined $prisoner && $self->body_id eq $prisoner->on_body_id && $prisoner->task eq 'Captured'); + push @expanded_items, $item; + $space_used += 350; + } + when ('ship') { + if ($item->{ship_id}) { + my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($item->{ship_id}); + confess $have_exception unless (defined $ship && $self->body_id eq $ship->body_id && $ship->task eq 'Docked'); + push @expanded_items, $item; + $space_used += 50000; + } + elsif ($item->{quantity}) { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess [1002, 'you must specify a name if you specify a quantity.'] unless $item->{name}; + confess [1002, 'you must specify a ship_type if you specify a quantity.'] unless $item->{ship_type}; + confess [1002, 'you must specify a hold_size if you specify a quantity.'] unless defined $item->{hold_size}; + confess [1002, 'you must specify a speed if you specify a quantity.'] unless defined $item->{speed}; + my $ships_rs = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ name => $item->{name}, body_id => $self->body_id, type => $item->{ship_type}, hold_size => $item->{hold_size}, speed => $item->{speed}, task => 'Docked', - }); - - if (defined $transfer_ship) { - $ships_rs = $ships_rs->search({ + }); + if (defined $transfer_ship) { + $ships_rs = $ships_rs->search({ id => {'!=' => $transfer_ship->id}, }); - } - my @ships = $ships_rs->search->all; - confess [1002, "You don't have ".$item->{quantity}." ships of type ".$item->{ship_type}." you only have ".scalar(@ships)] unless @ships && scalar(@ships) >= $item->{quantity}; - push @expanded_items, map { {type => "ship", ship_id => $_->id} } splice @ships, 0, $item->{quantity}; - $space_used += 50000 * $item->{quantity}; - } - else { - confess [1002, 'You must specify a ship_id or a quantity if you are pushing a ship.']; - } - } - } + } + my @ships = $ships_rs->search->all; + confess [1002, "You don't have ".$item->{quantity}." ships of type ".$item->{ship_type}." you only have ".scalar(@ships)] unless @ships && scalar(@ships) >= $item->{quantity}; + push @expanded_items, map { {type => "ship", ship_id => $_->id} } splice @ships, 0, $item->{quantity}; + $space_used += 50000 * $item->{quantity}; + } + else { + confess [1002, 'You must specify a ship_id or a quantity if you are pushing a ship.']; + } + } } + } # push @$items, @expanded_items; - $items = \@expanded_items; - confess $offer_nothing_exception unless $space_used; - confess [1011, sprintf($space_exception,$space_used)] unless ($space_used <= $available_cargo_space); - return $space_used, $items; + $items = \@expanded_items; + confess $offer_nothing_exception unless $space_used; + confess [1011, sprintf($space_exception,$space_used)] unless ($space_used <= $available_cargo_space); + return $space_used, $items; } sub structure_payload { - my ($self, $items, $space_used) = @_; - my $body = $self->body; - my $payload; - my %meta = ( offer_cargo_space_needed => $space_used ); - foreach my $item (@{$items}) { - given($item->{type}) { - when ([qw(water energy waste)]) { - $body->spend_type($item->{type}, $item->{quantity}); - $body->update; - $payload->{resources}{$item->{type}} += $item->{quantity}; - $meta{'has_'.$item->{type}} = 1; - } - when ([ORE_TYPES]) { - $body->spend_type($item->{type}, $item->{quantity}); - $body->update; - $payload->{resources}{$item->{type}} += $item->{quantity}; - $meta{has_ore} = 1; - } - when ([FOOD_TYPES]) { - $body->spend_type($item->{type}, $item->{quantity}); - $body->update; - $payload->{resources}{$item->{type}} += $item->{quantity}; - $meta{has_food} = 1; - } - when ('glyph') { - if ($item->{glyph_id}) { - my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyphs')->find($item->{glyph_id}); - $glyph->delete; - push @{$payload->{glyphs}}, $glyph->type; - $meta{has_glyph} = 1; - } - } - when ('plan') { - if ($item->{plan_id}) { - my $plan = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->find($item->{plan_id}); - $plan->delete; - push @{$payload->{plans}}, { class => $plan->class, level => $plan->level, extra_build_level => $plan->extra_build_level }; - $meta{has_plan} = 1; - } - } - when ('prisoner') { - my $prisoner = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($item->{prisoner_id}); - $prisoner->task('Prisoner Transport'); - $prisoner->update; - push @{$payload->{prisoners}}, $prisoner->id; - $meta{has_prisoner} = 1; - } - when ('ship') { - if ($item->{ship_id}) { - my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($item->{ship_id}); - $ship->task('Waiting On Trade'); - $ship->update; - push @{$payload->{ships}}, $ship->id; - $meta{has_ship} = 1; - } - } + my ($self, $items, $space_used) = @_; + my $body = $self->body; + my $payload; + my %meta = ( offer_cargo_space_needed => $space_used ); + foreach my $item (@{$items}) { + given($item->{type}) { + when ([qw(water energy waste)]) { + $body->spend_type($item->{type}, $item->{quantity}); + $body->update; + $payload->{resources}{$item->{type}} += $item->{quantity}; + $meta{'has_'.$item->{type}} = 1; + } + when ([ORE_TYPES]) { + $body->spend_type($item->{type}, $item->{quantity}); + $body->update; + $payload->{resources}{$item->{type}} += $item->{quantity}; + $meta{has_ore} = 1; + } + when ([FOOD_TYPES]) { + $body->spend_type($item->{type}, $item->{quantity}); + $body->update; + $payload->{resources}{$item->{type}} += $item->{quantity}; + $meta{has_food} = 1; + } + when ('glyph') { + if ($item->{glyph_id}) { + my $num_used = $body->use_glyph($item->{glyph_id}, $item->{name}, $item->{quantity}); + push @{$payload->{glyphs}}, { + name => $item->{name}, + quantity => $item->{quantity}, + glyph_id => $item->{glyph_id}, + }; + $meta{has_glyph} = 1; + } + when ('plan') { + if ($item->{plan_id}) { + my $plan = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->find($item->{plan_id}); + $plan->delete; + push @{$payload->{plans}}, { class => $plan->class, level => $plan->level, extra_build_level => $plan->extra_build_level }; + $meta{has_plan} = 1; } + } + when ('prisoner') { + my $prisoner = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($item->{prisoner_id}); + $prisoner->task('Prisoner Transport'); + $prisoner->update; + push @{$payload->{prisoners}}, $prisoner->id; + $meta{has_prisoner} = 1; + } + when ('ship') { + if ($item->{ship_id}) { + my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($item->{ship_id}); + $ship->task('Waiting On Trade'); + $ship->update; + push @{$payload->{ships}}, $ship->id; + $meta{has_ship} = 1; + } + } } - return ($payload, \%meta); + } + return ($payload, \%meta); } diff --git a/lib/Lacuna/Role/TraderRpc.pm b/lib/Lacuna/Role/TraderRpc.pm index 7f93729d..0412ab60 100644 --- a/lib/Lacuna/Role/TraderRpc.pm +++ b/lib/Lacuna/Role/TraderRpc.pm @@ -241,43 +241,45 @@ sub get_plans { } sub get_glyphs { - my ($self, $session_id, $building_id) = @_; - my $empire = $self->get_empire_by_session($session_id); - my $building = $self->get_building($empire, $building_id); - my $glyphs = $building->body->glyphs; - my @out; - while (my $glyph = $glyphs->next) { - push @out, { - id => $glyph->id, - type => $glyph->type, - }; - } - return { - glyphs => \@out, - cargo_space_used_each => 100, - status => $self->format_status($empire, $building->body), - }; + my ($self, $session_id, $building_id) = @_; + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $glyphs = $building->body->glyph; + my @out; + while (my $glyph = $glyphs->next) { + push @out, { + id => $glyph->id, + type => $glyph->type, + quantity => $glyph->quantity, + }; + } + return { + glyphs => \@out, + cargo_space_used_each => 100, + status => $self->format_status($empire, $building->body), + }; } sub get_glyph_summary { - my ($self, $session_id, $building_id) = @_; - - my $empire = $self->get_empire_by_session($session_id); - my $building = $self->get_building($empire, $building_id); - my $glyphs = $building->body->glyphs; - - my $glyph_summary = {}; - while (my $glyph = $glyphs->next) { - $glyph_summary->{$glyph->type}++; - } - # sort - my @out = map { {name => $_, quantity => $glyph_summary->{$_}}} sort {$a cmp $b} keys %$glyph_summary; - - return { - glyphs => \@out, - cargo_space_used_each => 100, - status => $self->format_status($empire, $building->body), - }; + my ($self, $session_id, $building_id) = @_; + + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $glyphs = $building->body->glyph; + + while (my $glyph = $glyphs->next) { + push @out, { + id => $glyph->id, + type => $glyph->type, + quantity => $glyph->quantity, + }; + } + + return { + glyphs => \@out, + cargo_space_used_each => 100, + status => $self->format_status($empire, $building->body), + }; } -- 2.51.2 From 7a8dc0c25c251d3c8722f71f4ef54554f4749e2d Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 21 May 2012 13:18:57 -0600 Subject: [PATCH 04/13] syntax problems fixed. --- lib/Lacuna/DB/Result/Spies.pm | 2 +- lib/Lacuna/Role/Trader.pm | 209 +++++++++++++++++----------------- lib/Lacuna/Role/TraderRpc.pm | 1 + 3 files changed, 107 insertions(+), 105 deletions(-) diff --git a/lib/Lacuna/DB/Result/Spies.pm b/lib/Lacuna/DB/Result/Spies.pm index 693346da..ed62130b 100644 --- a/lib/Lacuna/DB/Result/Spies.pm +++ b/lib/Lacuna/DB/Result/Spies.pm @@ -1873,7 +1873,7 @@ sub destroy_glyph { $self->on_body->add_news(70, 'A museum was broken into on %s and a rare artifact was smashed to pieces.', $self->on_body->name); - $on_body->use_glyph($glyph->id, $glyph->type, 1); + $self->on_body->use_glyph($glyph->id, $glyph->type, 1); return $message->id; } diff --git a/lib/Lacuna/Role/Trader.pm b/lib/Lacuna/Role/Trader.pm index 3f3510b0..6dfb929a 100644 --- a/lib/Lacuna/Role/Trader.pm +++ b/lib/Lacuna/Role/Trader.pm @@ -12,131 +12,131 @@ use Data::Dumper; my $ask_nothing_exception = [1013, 'It appears that you have asked for nothing.']; my $fractional_offer_exception = [1013, 'You cannot offer a fraction of something.']; - sub market { - return Lacuna->db->resultset('Lacuna::DB::Result::Market'); - } +sub market { + return Lacuna->db->resultset('Lacuna::DB::Result::Market'); +} - sub my_market { - my $self = shift; - return $self->market->search({body_id => $self->body_id, transfer_type => $self->transfer_type }); - } +sub my_market { + my $self = shift; + return $self->market->search({body_id => $self->body_id, transfer_type => $self->transfer_type }); +} - sub check_payload { - my ($self, $items, $available_cargo_space, $space_exception, $transfer_ship) = @_; - my $body = $self->body; - $space_exception ||= $cargo_exception; +sub check_payload { + my ($self, $items, $available_cargo_space, $space_exception, $transfer_ship) = @_; + my $body = $self->body; + $space_exception ||= $cargo_exception; # validate - unless (ref $items eq 'ARRAY') { - confess 'The list of items you want to trade needs to be formatted as an array of hashes.'; - } + unless (ref $items eq 'ARRAY') { + confess [ 9999, 'The list of items you want to trade needs to be formatted as an array of hashes.']; + } - my $space_used; - my @expanded_items; + my $space_used; + my @expanded_items; - foreach my $item (@{$items}) { - given($item->{type}) { - when ([qw(water energy waste), ORE_TYPES, FOOD_TYPES]) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess $have_exception unless ($body->type_stored($item->{type}) >= $item->{quantity}); - push @expanded_items, $item; - $space_used += $item->{quantity}; - } - when ('glyph') { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess [1002, 'you must specify a glyph name with a quantity.'] unless $item->{name}; - my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ + foreach my $item (@{$items}) { + given($item->{type}) { + when ([qw(water energy waste), ORE_TYPES, FOOD_TYPES]) { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess $have_exception unless ($body->type_stored($item->{type}) >= $item->{quantity}); + push @expanded_items, $item; + $space_used += $item->{quantity}; + } + when ('glyph') { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess [1002, 'you must specify a glyph name with a quantity.'] unless $item->{name}; + my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ type => $item->{name}, body_id => $self->body_id, })->single; - confess [1002, "You don't have ".$item->{quantity}." glyphs of type ". - $item->{name}." you only have ".$glyph->{quantity}] + confess [1002, "You don't have ".$item->{quantity}." glyphs of type ". + $item->{name}." you only have ".$glyph->{quantity}] unless $glyph->{quantity} >= $item->{quantity}; - push @expanded_items, { + push (@expanded_items, { name => $item->{name}, quantity => $item->{quantity}, glyph_id => $glyph->{id}, - }; - $space_used += 100 * $item->{quantity}; + }); + $space_used += 100 * $item->{quantity}; + } + when ('plan') { + if ($item->{plan_id}) { + my $plan = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->find($item->{plan_id}); + confess $have_exception unless (defined $plan && $self->body_id eq $plan->body_id); + push @expanded_items, $item; + $space_used += 10000; } - when ('plan') { - if ($item->{plan_id}) { - my $plan = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->find($item->{plan_id}); - confess $have_exception unless (defined $plan && $self->body_id eq $plan->body_id); - push @expanded_items, $item; - $space_used += 10000; - } - elsif ($item->{quantity}) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess [1002, 'you must specify a plan_type if you specify a quantity.'] unless $item->{plan_type}; - confess [1002, 'you must specify a level if you specify a quantity.'] unless $item->{level}; - confess [1002, 'you must specify an extra_build_level if you specify a quantity.'] unless defined $item->{extra_build_level}; + elsif ($item->{quantity}) { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess [1002, 'you must specify a plan_type if you specify a quantity.'] unless $item->{plan_type}; + confess [1002, 'you must specify a level if you specify a quantity.'] unless $item->{level}; + confess [1002, 'you must specify an extra_build_level if you specify a quantity.'] unless defined $item->{extra_build_level}; - my $plan_class = $item->{plan_type}; - $plan_class =~ s/_/::/g; - $plan_class = "Lacuna::DB::Result::Building::$plan_class"; - my @plans = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->search({ + my $plan_class = $item->{plan_type}; + $plan_class =~ s/_/::/g; + $plan_class = "Lacuna::DB::Result::Building::$plan_class"; + my @plans = Lacuna->db->resultset('Lacuna::DB::Result::Plans')->search({ class => $plan_class, body_id => $self->body_id, level => $item->{level}, extra_build_level => $item->{extra_build_level}, }); - confess [1002, "You don't have ".$item->{quantity}. - " plans of type ".$item->{plan_type}. - " you only have ".scalar(@plans)] unless scalar(@plans) >= $item->{quantity}; - push @expanded_items, map { {type => 'plan',plan_id => $_->id} } splice @plans, 0, $item->{quantity}; - $space_used += 10000 * $item->{quantity}; - } - else { - confess [1002, 'You must specify either a plan_id, or a quantity if you are pushing a plan.']; + confess [1002, "You don't have ".$item->{quantity}. + " plans of type ".$item->{plan_type}. + " you only have ".scalar(@plans)] unless scalar(@plans) >= $item->{quantity}; + push @expanded_items, map { {type => 'plan',plan_id => $_->id} } splice @plans, 0, $item->{quantity}; + $space_used += 10000 * $item->{quantity}; + } + else { + confess [1002, 'You must specify either a plan_id, or a quantity if you are pushing a plan.']; + } + } + when ('prisoner') { + confess [1002, 'You must specify a prisoner_id if you are pushing a prisoner.'] unless $item->{prisoner_id}; + my $prisoner = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($item->{prisoner_id}); + confess $have_exception unless (defined $prisoner && $self->body_id eq $prisoner->on_body_id && $prisoner->task eq 'Captured'); + push @expanded_items, $item; + $space_used += 350; + } + when ('ship') { + if ($item->{ship_id}) { + my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($item->{ship_id}); + confess $have_exception unless (defined $ship && $self->body_id eq $ship->body_id && $ship->task eq 'Docked'); + push @expanded_items, $item; + $space_used += 50000; + } + elsif ($item->{quantity}) { + confess $offer_nothing_exception unless ($item->{quantity} > 0); + confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); + confess [1002, 'you must specify a name if you specify a quantity.'] unless $item->{name}; + confess [1002, 'you must specify a ship_type if you specify a quantity.'] unless $item->{ship_type}; + confess [1002, 'you must specify a hold_size if you specify a quantity.'] unless defined $item->{hold_size}; + confess [1002, 'you must specify a speed if you specify a quantity.'] unless defined $item->{speed}; + my $ships_rs = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ + name => $item->{name}, + body_id => $self->body_id, + type => $item->{ship_type}, + hold_size => $item->{hold_size}, + speed => $item->{speed}, + task => 'Docked', + }); + if (defined $transfer_ship) { + $ships_rs = $ships_rs->search({ + id => {'!=' => $transfer_ship->id}, + }); } - } - when ('prisoner') { - confess [1002, 'You must specify a prisoner_id if you are pushing a prisoner.'] unless $item->{prisoner_id}; - my $prisoner = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->find($item->{prisoner_id}); - confess $have_exception unless (defined $prisoner && $self->body_id eq $prisoner->on_body_id && $prisoner->task eq 'Captured'); - push @expanded_items, $item; - $space_used += 350; - } - when ('ship') { - if ($item->{ship_id}) { - my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($item->{ship_id}); - confess $have_exception unless (defined $ship && $self->body_id eq $ship->body_id && $ship->task eq 'Docked'); - push @expanded_items, $item; - $space_used += 50000; - } - elsif ($item->{quantity}) { - confess $offer_nothing_exception unless ($item->{quantity} > 0); - confess $fractional_offer_exception if ($item->{quantity} != int($item->{quantity})); - confess [1002, 'you must specify a name if you specify a quantity.'] unless $item->{name}; - confess [1002, 'you must specify a ship_type if you specify a quantity.'] unless $item->{ship_type}; - confess [1002, 'you must specify a hold_size if you specify a quantity.'] unless defined $item->{hold_size}; - confess [1002, 'you must specify a speed if you specify a quantity.'] unless defined $item->{speed}; - my $ships_rs = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search({ - name => $item->{name}, - body_id => $self->body_id, - type => $item->{ship_type}, - hold_size => $item->{hold_size}, - speed => $item->{speed}, - task => 'Docked', - }); - if (defined $transfer_ship) { - $ships_rs = $ships_rs->search({ - id => {'!=' => $transfer_ship->id}, - }); - } - my @ships = $ships_rs->search->all; - confess [1002, "You don't have ".$item->{quantity}." ships of type ".$item->{ship_type}." you only have ".scalar(@ships)] unless @ships && scalar(@ships) >= $item->{quantity}; - push @expanded_items, map { {type => "ship", ship_id => $_->id} } splice @ships, 0, $item->{quantity}; - $space_used += 50000 * $item->{quantity}; - } - else { - confess [1002, 'You must specify a ship_id or a quantity if you are pushing a ship.']; - } - } + my @ships = $ships_rs->search->all; + confess [1002, "You don't have ".$item->{quantity}." ships of type ".$item->{ship_type}." you only have ".scalar(@ships)] unless @ships && scalar(@ships) >= $item->{quantity}; + push @expanded_items, map { {type => "ship", ship_id => $_->id} } splice @ships, 0, $item->{quantity}; + $space_used += 50000 * $item->{quantity}; + } + else { + confess [1002, 'You must specify a ship_id or a quantity if you are pushing a ship.']; + } + } } } # push @$items, @expanded_items; @@ -180,6 +180,7 @@ sub structure_payload { glyph_id => $item->{glyph_id}, }; $meta{has_glyph} = 1; + } } when ('plan') { if ($item->{plan_id}) { diff --git a/lib/Lacuna/Role/TraderRpc.pm b/lib/Lacuna/Role/TraderRpc.pm index 0412ab60..f1e23e19 100644 --- a/lib/Lacuna/Role/TraderRpc.pm +++ b/lib/Lacuna/Role/TraderRpc.pm @@ -267,6 +267,7 @@ sub get_glyph_summary { my $building = $self->get_building($empire, $building_id); my $glyphs = $building->body->glyph; + my @out; while (my $glyph = $glyphs->next) { push @out, { id => $glyph->id, -- 2.51.2 From 69824f28bd70c0731bc07ff0ea317df598cde66b Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 21 May 2012 16:56:50 -0600 Subject: [PATCH 05/13] progress --- lib/Lacuna/DB/Result/Glyph.pm | 1 + lib/Lacuna/DB/Result/Map/Body/Planet.pm | 6 ++++-- lib/Lacuna/RPC/Building/Archaeology.pm | 16 +++++++++------- lib/Lacuna/Web/Admin.pm | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/Lacuna/DB/Result/Glyph.pm b/lib/Lacuna/DB/Result/Glyph.pm index e7eb4af1..5e25405f 100644 --- a/lib/Lacuna/DB/Result/Glyph.pm +++ b/lib/Lacuna/DB/Result/Glyph.pm @@ -5,6 +5,7 @@ use utf8; no warnings qw(uninitialized); extends 'Lacuna::DB::Result'; +__PACKAGE__->load_components('DynamicSubclass'); __PACKAGE__->table('glyph'); __PACKAGE__->add_columns( body_id => { data_type => 'int', is_nullable => 0 }, diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 5dfd81d4..c8e9282a 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -94,7 +94,7 @@ sub add_glyph { return $self->glyph; } else { - return $self->glyphs->new({ + return $self->glyph->new({ type => $type, body_id => $self->id, quantity => $num_add, @@ -104,13 +104,15 @@ sub add_glyph { sub use_glyph { my ($self, $glyph_id, $type, $num_used) = @_; + + $num_used = 1 unless (defined($num_used)); my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ id => $glyph_id, type => $type, body_id => $self->id, })->single; return 0 unless defined($glyph); - if ($glyph->{quantity} > $num_used) { + if ($glyph->quantity > $num_used) { my $sum = $self->glyph->quantity - $num_used; $self->glyph->quantity($sum); } diff --git a/lib/Lacuna/RPC/Building/Archaeology.pm b/lib/Lacuna/RPC/Building/Archaeology.pm index 6740543a..977c0487 100644 --- a/lib/Lacuna/RPC/Building/Archaeology.pm +++ b/lib/Lacuna/RPC/Building/Archaeology.pm @@ -29,11 +29,12 @@ sub get_glyphs { my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); my @out; - my $glyphs = $building->body->glyphs; + my $glyphs = $building->body->glyph; while (my $glyph = $glyphs->next) { push @out, { id => $glyph->id, type => $glyph->type, + quantity => $glyph->quantity, }; } return { @@ -47,14 +48,15 @@ sub get_glyph_summary { my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); - my $glyphs = $building->body->glyphs; - - my $glyph_summary = {}; + my @out; + my $glyphs = $building->body->glyph; while (my $glyph = $glyphs->next) { - $glyph_summary->{$glyph->type}++; + push @out, { + id => $glyph->id, + type => $glyph->type, + quantity => $glyph->quantity, + }; } - # sort - my @out = map { {name => $_, quantity => $glyph_summary->{$_}}} sort {$a cmp $b} keys %$glyph_summary; return { glyphs => \@out, diff --git a/lib/Lacuna/Web/Admin.pm b/lib/Lacuna/Web/Admin.pm index 947f19f7..b5384224 100644 --- a/lib/Lacuna/Web/Admin.pm +++ b/lib/Lacuna/Web/Admin.pm @@ -424,7 +424,7 @@ sub www_add_resources { sub www_view_glyphs { my ($self, $request, $body_id) = @_; $body_id ||= $request->param('body_id'); - my $glyphs = Lacuna->db->resultset('Lacuna::DB::Result::Glyphs')->search({ body_id => $body_id }, {order_by => ['type'] }); + my $glyphs = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ body_id => $body_id }, {order_by => ['type'] }); my $out = '

View Glyphs

'; $out .= sprintf('Back To Body', $body_id); $out .= ''; -- 2.51.2 From 8f17eb6f2fcddccb9051f6e254237117847580d1 Mon Sep 17 00:00:00 2001 From: lemming552 Date: Tue, 22 May 2012 06:47:28 -0600 Subject: [PATCH 06/13] renaming for now. --- var/upgrades/{3.0852.sql => 3.glyph.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename var/upgrades/{3.0852.sql => 3.glyph.sql} (100%) diff --git a/var/upgrades/3.0852.sql b/var/upgrades/3.glyph.sql similarity index 100% rename from var/upgrades/3.0852.sql rename to var/upgrades/3.glyph.sql -- 2.51.2 From 51115218cd53cadfbb1ab733bac410383b5c37c6 Mon Sep 17 00:00:00 2001 From: lemming552 Date: Wed, 6 Jun 2012 12:05:24 -0600 Subject: [PATCH 07/13] Glyph consolidation work. --- lib/Lacuna/Web/Admin.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Lacuna/Web/Admin.pm b/lib/Lacuna/Web/Admin.pm index ca725810..78afb5a9 100644 --- a/lib/Lacuna/Web/Admin.pm +++ b/lib/Lacuna/Web/Admin.pm @@ -424,14 +424,14 @@ sub www_view_glyphs { my $glyphs = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ body_id => $body_id }, {order_by => ['type'] }); my $out = '

View Glyphs

'; $out .= sprintf('Back To Body', $body_id); - $out .= '
IdTypeAction
'; + $out .= '
IdTypeAction
'; while (my $glyph = $glyphs->next) { - $out .= sprintf('', $glyph->id, $glyph->type, $body_id, $glyph->id); + $out .= sprintf('', $glyph->id, $glyph->type, $glyph->quantity, $body_id, $glyph->id); } $out .= ''; $out .= ''; $out .= ''; -- 2.51.2 From a0abcebf7174b762f23f5d6b3567bd2c94c3ad97 Mon Sep 17 00:00:00 2001 From: lemming552 Date: Wed, 6 Jun 2012 14:16:22 -0600 Subject: [PATCH 08/13] more stuff --- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 5678aaaa..41c7b766 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -132,7 +132,7 @@ sub add_glyph { })->single; if (defined($glyph)) { my $sum = $num_add + $self->glyph->quantity; - $self->glyph->quantity($sum); + $glyph->quantity($sum); return $self->glyph; } else { @@ -149,14 +149,14 @@ sub use_glyph { $num_used = 1 unless (defined($num_used)); my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ - id => $glyph_id, +# id => $glyph_id, type => $type, body_id => $self->id, })->single; return 0 unless defined($glyph); if ($glyph->quantity > $num_used) { my $sum = $self->glyph->quantity - $num_used; - $self->glyph->quantity($sum); + $glyph->quantity($sum); } else { $num_used = $glyph->quantity; -- 2.51.2 From 85329fba3fe145be9bd1f66def7e912716e75529 Mon Sep 17 00:00:00 2001 From: lemming552 Date: Sun, 1 Jul 2012 14:49:44 -0600 Subject: [PATCH 09/13] Not sure why I had dynamic in there. --- lib/Lacuna/DB/Result/Glyph.pm | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Lacuna/DB/Result/Glyph.pm b/lib/Lacuna/DB/Result/Glyph.pm index 5e25405f..e7eb4af1 100644 --- a/lib/Lacuna/DB/Result/Glyph.pm +++ b/lib/Lacuna/DB/Result/Glyph.pm @@ -5,7 +5,6 @@ use utf8; no warnings qw(uninitialized); extends 'Lacuna::DB::Result'; -__PACKAGE__->load_components('DynamicSubclass'); __PACKAGE__->table('glyph'); __PACKAGE__->add_columns( body_id => { data_type => 'int', is_nullable => 0 }, -- 2.51.2 From d3a12bbee0fc1ef911305b91b0813d0f76f47fcb Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 2 Jul 2012 14:05:26 -0600 Subject: [PATCH 10/13] Most functions working correctly now. --- docs/Archaeology.pod | 6 ++- docs/Trade.pod | 13 +---- lib/Lacuna/AI/DeLambert.pm | 6 +-- lib/Lacuna/DB/Result/Building/Archaeology.pm | 54 +++++++++---------- .../Building/Permanent/TheDillonForge.pm | 9 ++-- lib/Lacuna/DB/Result/Building/Trade.pm | 4 +- lib/Lacuna/DB/Result/Map/Body/Planet.pm | 12 ++--- lib/Lacuna/DB/Result/Spies.pm | 4 +- lib/Lacuna/Role/Container.pm | 2 +- lib/Lacuna/Role/Trader.pm | 23 ++++---- 10 files changed, 63 insertions(+), 70 deletions(-) diff --git a/docs/Archaeology.pod b/docs/Archaeology.pod index 61d4deda..70307dca 100644 --- a/docs/Archaeology.pod +++ b/docs/Archaeology.pod @@ -58,6 +58,7 @@ Returns a list of glyphs that have been found by this archaeology ministry. { "id" : "id-goes-here", "type" : "bauxite", + "quantity" : 2 }, ... ] @@ -71,6 +72,7 @@ Returns a summary of all glyphs that may be assembled in this archaeology minist { "glyphs" : [ { + "id" : "id-goes-here", "type" : "bauxite", "quantity" : 2 }, @@ -109,11 +111,11 @@ The unique id of the Archaeology Ministry. =head3 glyphs -An array reference containing an ordered list of up to four glyph ids or up to four glyph types. +An array reference containing an ordered list of up to four glyph types. =head3 quantity -An optional quantity; if left blank it defaults to 1, otherwise specify the number of times to assemble the indicated glyphs up to a maximum of 50. If greater than one, glyphs must specify types, not ids. +An optional quantity; if left blank it defaults to 1, otherwise specify the number of times to assemble the indicated glyphs up to a maximum of 50. diff --git a/docs/Trade.pod b/docs/Trade.pod index 6aa23e94..763ddfdf 100644 --- a/docs/Trade.pod +++ b/docs/Trade.pod @@ -63,14 +63,7 @@ The amount of the resource that you want to trade. =item glyphs -The hash reference for glyphs can either look like this: - - { - "type" : "glyph", - "glyph_id" : "id-goes-here" - } - -Or it can look like this: +The hash reference for glyphs looks like this: { "type" : "glyph", @@ -84,10 +77,6 @@ Or it can look like this: Must be exactly C. -=item glyph_id - -The unique id of the glyph you want to trade. See the C method for a list of your glyphs. - =item name The type of glyph you want to trade (must be an ore type name). diff --git a/lib/Lacuna/AI/DeLambert.pm b/lib/Lacuna/AI/DeLambert.pm index 9e409dd3..88ea8f9f 100644 --- a/lib/Lacuna/AI/DeLambert.pm +++ b/lib/Lacuna/AI/DeLambert.pm @@ -231,10 +231,10 @@ sub sell_glyph_trade { } # Instead of random assortment, quantity of one glyph my $ore = random_element([ORE_TYPES]); - my @glyphs = [ { + my $glyphs = [ { name => $ore, quantity => $quantity, - glyph_id => 0, +# glyph_id => 0, } ]; if ($quantity) { say "Creating a trade for $quantity glyphs"; @@ -243,7 +243,7 @@ sub sell_glyph_trade { my %trade = ( offer_cargo_space_needed => $quantity * 100, has_glyph => 1, - payload => {glyphs => \@glyphs}, + payload => {glyphs => $glyphs}, ask => $cost_per * $quantity, ship_id => $ship->id, body_id => $colony->id, diff --git a/lib/Lacuna/DB/Result/Building/Archaeology.pm b/lib/Lacuna/DB/Result/Building/Archaeology.pm index 80e2e5c5..ccc7c965 100644 --- a/lib/Lacuna/DB/Result/Building/Archaeology.pm +++ b/lib/Lacuna/DB/Result/Building/Archaeology.pm @@ -651,59 +651,57 @@ before finish_work => sub { sub make_plan { my ($self, $glyphs, $quantity) = @_; + $quantity = 1 unless defined($quantity); unless (ref $glyphs eq 'ARRAY' && scalar(@{$glyphs}) < 5) { confess [1009, 'It is not possible to combine more than 4 glyphs.']; } my $plan_class; my $ids; - my $glyphs_rs = $self->body->glyphs; # types + my %count; if ( grep /\D/, @{$glyphs} ) { $plan_class = Lacuna::DB::Result::Plans->check_glyph_recipe($glyphs); if (not $plan_class) { confess [1002, 'The glyphs specified do not fit together in that manner.']; } - my %count; $count{$_} += $quantity for @{$glyphs}; for my $type ( sort keys %count ) { - my @glyphs = Lacuna->db->resultset('Lacuna::DB::Result::Glyphs')->search({ + my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ type => $type, body_id => $self->body_id, - }, { - rows => $count{$type}, - page => 1, - }); - confess [1002, "You don't have $count{$type} glyphs of type $type you only have ".scalar(@glyphs)] unless scalar(@glyphs) >= $count{$type}; - push @{$ids}, map $_->id, @glyphs; + })->single; + unless (defined($glyph)) { + confess [ 1002, "You don't have any glyphs of type $type."]; + } + if ($glyph->quantity < $count{$type}) { + confess [ 1002, + "You don't have $count{$type} glyphs of type $type you only have ".$glyph->quantity]; + } } } - # ids else { - $ids = $glyphs; - - if ($quantity != 1) { - confess [1011, 'You can only assemble one plan from specific glyphs']; - } - - my @glyph_names; - foreach my $id (@{$ids}) { - my $glyph = $glyphs_rs->find($id); - confess [1002, 'You tried to combine a glyph you do not have.'] unless defined $glyph; - push @glyph_names,$glyph->type; - } - - $plan_class = Lacuna::DB::Result::Plans->check_glyph_recipe(\@glyph_names); - if (not $plan_class) { - confess [1002, 'The glyphs specified do not fit together in that manner.']; + confess [1009, "Malformed glyph ARRAY."]; + } + my $min_used = $quantity; + for my $type (@{$glyphs}) { + $count{$type} = $self->body->use_glyph($type, $quantity); + $min_used = $count{$type} if ($min_used < $count{$type}); + } +# Check if all glyphs were used + if ($min_used < $quantity) { + for my $type (@{$glyphs}) { + if ($min_used < $count{$type}) { + $self->body->add_glyph($type, ($count{$type})); + } } } + confess [1002, "Glyphs used before they could be combined!"] if ($min_used == 0); - $glyphs_rs->search({ id => { in => $ids}})->delete; my $plan; - for my $count (1..$quantity) { + for my $count (1..$min_used) { $plan = $self->body->add_plan($plan_class, 1); } return $plan; diff --git a/lib/Lacuna/DB/Result/Building/Permanent/TheDillonForge.pm b/lib/Lacuna/DB/Result/Building/Permanent/TheDillonForge.pm index 619ce72f..d4635312 100644 --- a/lib/Lacuna/DB/Result/Building/Permanent/TheDillonForge.pm +++ b/lib/Lacuna/DB/Result/Building/Permanent/TheDillonForge.pm @@ -152,13 +152,16 @@ before finish_work => sub { my $total_glyphs = 0; for my $glyph (@many_glyphs) { if ($success_percent > rand(100)) { - $self->body->add_to_glyphs({ - type => $glyph, - }); +# $self->body->add_glyph({ +# type => $glyph, +# }); $glyphs_built->{$glyph} = $glyphs_built->{$glyph} ? $glyphs_built->{$glyph}+1 : 1; $total_glyphs++; } } + for my $glyph (keys %{$glyphs_built}) { + $self->body->add_glyph($glyph, $glyphs_built->{$glyph}); + } my @report = map { [ $glyphs_built->{$_}, $_ ]} keys %$glyphs_built; unshift (@report, ['Quantity','Glyph']); diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index b42727e0..9dddeaf2 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -408,8 +408,8 @@ sub push_items { } my $space_used; - ($space_used, $items) = $self->check_payload($items,$ship->hold_size, undef, $ship); - $self->check_payload_ships($items,$target,$options->{stay}); + ($space_used, $items) = $self->check_payload($items, $ship->hold_size, undef, $ship); + $self->check_payload_ships($items, $target, $options->{stay}); my ($payload, $meta) = $self->structure_payload($items, $space_used); foreach my $item (@{$items}) { diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 40e921d7..9a8a342b 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -132,12 +132,12 @@ sub add_glyph { body_id => $self->id, })->single; if (defined($glyph)) { - my $sum = $num_add + $self->glyph->quantity; + my $sum = $num_add + $glyph->quantity; $glyph->quantity($sum); - return $self->glyph; + $glyph->update; } else { - return $self->glyph->new({ + $self->glyph->new({ type => $type, body_id => $self->id, quantity => $num_add, @@ -146,18 +146,18 @@ sub add_glyph { } sub use_glyph { - my ($self, $glyph_id, $type, $num_used) = @_; + my ($self, $type, $num_used) = @_; $num_used = 1 unless (defined($num_used)); my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({ -# id => $glyph_id, type => $type, body_id => $self->id, })->single; return 0 unless defined($glyph); if ($glyph->quantity > $num_used) { - my $sum = $self->glyph->quantity - $num_used; + my $sum = $glyph->quantity - $num_used; $glyph->quantity($sum); + $glyph->update; } else { $num_used = $glyph->quantity; diff --git a/lib/Lacuna/DB/Result/Spies.pm b/lib/Lacuna/DB/Result/Spies.pm index 13add71b..bd992cbd 100644 --- a/lib/Lacuna/DB/Result/Spies.pm +++ b/lib/Lacuna/DB/Result/Spies.pm @@ -1890,7 +1890,7 @@ sub destroy_glyph { $self->on_body->add_news(70, 'A museum was broken into on %s and a rare artifact was smashed to pieces.', $self->on_body->name); - $self->on_body->use_glyph($glyph->id, $glyph->type, 1); + $self->on_body->use_glyph($glyph->type, 1); return $message->id; } @@ -2097,7 +2097,7 @@ sub steal_glyph { }, ); my @table = (['Glyph'],[$glyph->type]); - $on_body->use_glyph($glyph->id, $glyph->type, 1); + $on_body->use_glyph($glyph->type, 1); $self->on_body->empire->send_predefined_message( tags => ['Spies','Alert'], filename => 'ship_stolen.txt', diff --git a/lib/Lacuna/Role/Container.pm b/lib/Lacuna/Role/Container.pm index 62e42f42..897a0946 100644 --- a/lib/Lacuna/Role/Container.pm +++ b/lib/Lacuna/Role/Container.pm @@ -104,7 +104,7 @@ sub format_description_of_payload { # glyphs foreach my $glyph (@{$payload->{glyphs}}) { - push @{$item_arr}, sprintf('%s %s', commify($glyph->{quantity}), $glyph->{name}); + push @{$item_arr}, sprintf('%s %s glyph', commify($glyph->{quantity}), $glyph->{name}); } # ships diff --git a/lib/Lacuna/Role/Trader.pm b/lib/Lacuna/Role/Trader.pm index 6dfb929a..99685fc7 100644 --- a/lib/Lacuna/Role/Trader.pm +++ b/lib/Lacuna/Role/Trader.pm @@ -52,12 +52,11 @@ sub check_payload { body_id => $self->body_id, })->single; confess [1002, "You don't have ".$item->{quantity}." glyphs of type ". - $item->{name}." you only have ".$glyph->{quantity}] - unless $glyph->{quantity} >= $item->{quantity}; + $item->{name}." you only have ".$glyph->quantity] + unless $glyph->quantity >= $item->{quantity}; push (@expanded_items, { name => $item->{name}, quantity => $item->{quantity}, - glyph_id => $glyph->{id}, }); $space_used += 100 * $item->{quantity}; } @@ -172,14 +171,16 @@ sub structure_payload { $meta{has_food} = 1; } when ('glyph') { - if ($item->{glyph_id}) { - my $num_used = $body->use_glyph($item->{glyph_id}, $item->{name}, $item->{quantity}); - push @{$payload->{glyphs}}, { - name => $item->{name}, - quantity => $item->{quantity}, - glyph_id => $item->{glyph_id}, - }; - $meta{has_glyph} = 1; + if ($item->{name}) { + my $num_used = $body->use_glyph($item->{name}, $item->{quantity}); +say('No glyphs used!') unless $num_used; + if ($num_used) { + push @{$payload->{glyphs}}, { + name => $item->{name}, + quantity => $num_used, + }; + $meta{has_glyph} = 1; + } } } when ('plan') { -- 2.51.2 From 83049f950623ec965ef14c76ccd3ec0fdd8e810a Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 2 Jul 2012 15:30:42 -0600 Subject: [PATCH 11/13] Minor kludge in return values of glyphs (name eq type) for now. --- lib/Lacuna/RPC/Building/Archaeology.pm | 2 + lib/Lacuna/Role/Trader.pm | 9 +-- lib/Lacuna/Role/TraderRpc.pm | 78 ++++++++++++++------------ 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/lib/Lacuna/RPC/Building/Archaeology.pm b/lib/Lacuna/RPC/Building/Archaeology.pm index 977c0487..f27da8ed 100644 --- a/lib/Lacuna/RPC/Building/Archaeology.pm +++ b/lib/Lacuna/RPC/Building/Archaeology.pm @@ -33,6 +33,7 @@ sub get_glyphs { while (my $glyph = $glyphs->next) { push @out, { id => $glyph->id, + name => $glyph->name, type => $glyph->type, quantity => $glyph->quantity, }; @@ -54,6 +55,7 @@ sub get_glyph_summary { push @out, { id => $glyph->id, type => $glyph->type, + name => $glyph->name, quantity => $glyph->quantity, }; } diff --git a/lib/Lacuna/Role/Trader.pm b/lib/Lacuna/Role/Trader.pm index 99685fc7..e56e1e03 100644 --- a/lib/Lacuna/Role/Trader.pm +++ b/lib/Lacuna/Role/Trader.pm @@ -54,10 +54,11 @@ sub check_payload { confess [1002, "You don't have ".$item->{quantity}." glyphs of type ". $item->{name}." you only have ".$glyph->quantity] unless $glyph->quantity >= $item->{quantity}; - push (@expanded_items, { - name => $item->{name}, - quantity => $item->{quantity}, - }); + push @expanded_items, $item; +# push (@expanded_items, { +# name => $item->{name}, +# quantity => $item->{quantity}, +# }); $space_used += 100 * $item->{quantity}; } when ('plan') { diff --git a/lib/Lacuna/Role/TraderRpc.pm b/lib/Lacuna/Role/TraderRpc.pm index f1e23e19..52d219f7 100644 --- a/lib/Lacuna/Role/TraderRpc.pm +++ b/lib/Lacuna/Role/TraderRpc.pm @@ -241,46 +241,50 @@ sub get_plans { } sub get_glyphs { - my ($self, $session_id, $building_id) = @_; - my $empire = $self->get_empire_by_session($session_id); - my $building = $self->get_building($empire, $building_id); - my $glyphs = $building->body->glyph; - my @out; - while (my $glyph = $glyphs->next) { - push @out, { - id => $glyph->id, - type => $glyph->type, - quantity => $glyph->quantity, - }; - } - return { - glyphs => \@out, - cargo_space_used_each => 100, - status => $self->format_status($empire, $building->body), - }; + my ($self, $session_id, $building_id) = @_; + + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $glyphs = $building->body->glyph; + + my @out; + while (my $glyph = $glyphs->next) { + push @out, { + id => $glyph->id, + name => $glyph->type, + type => $glyph->type, + quantity => $glyph->quantity, + }; + } + return { + glyphs => \@out, + cargo_space_used_each => 100, + status => $self->format_status($empire, $building->body), + }; } sub get_glyph_summary { - my ($self, $session_id, $building_id) = @_; - - my $empire = $self->get_empire_by_session($session_id); - my $building = $self->get_building($empire, $building_id); - my $glyphs = $building->body->glyph; - - my @out; - while (my $glyph = $glyphs->next) { - push @out, { - id => $glyph->id, - type => $glyph->type, - quantity => $glyph->quantity, - }; - } - - return { - glyphs => \@out, - cargo_space_used_each => 100, - status => $self->format_status($empire, $building->body), - }; + my ($self, $session_id, $building_id) = @_; + + my $empire = $self->get_empire_by_session($session_id); + my $building = $self->get_building($empire, $building_id); + my $glyphs = $building->body->glyph; + + my @out; + while (my $glyph = $glyphs->next) { + push @out, { + id => $glyph->id, + name => $glyph->type, + type => $glyph->type, + quantity => $glyph->quantity, + }; + } + + return { + glyphs => \@out, + cargo_space_used_each => 100, + status => $self->format_status($empire, $building->body), + }; } -- 2.51.2 From 3afc7effdee47a3e1e0aedb5e55341dea3f6afb4 Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 2 Jul 2012 15:47:16 -0600 Subject: [PATCH 12/13] type always returned though. --- lib/Lacuna/RPC/Building/Archaeology.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Lacuna/RPC/Building/Archaeology.pm b/lib/Lacuna/RPC/Building/Archaeology.pm index f27da8ed..17b5a79e 100644 --- a/lib/Lacuna/RPC/Building/Archaeology.pm +++ b/lib/Lacuna/RPC/Building/Archaeology.pm @@ -33,7 +33,7 @@ sub get_glyphs { while (my $glyph = $glyphs->next) { push @out, { id => $glyph->id, - name => $glyph->name, + name => $glyph->type, type => $glyph->type, quantity => $glyph->quantity, }; @@ -54,8 +54,8 @@ sub get_glyph_summary { while (my $glyph = $glyphs->next) { push @out, { id => $glyph->id, + name => $glyph->type, type => $glyph->type, - name => $glyph->name, quantity => $glyph->quantity, }; } -- 2.51.2 From cd60cbc87517314655399c4b043758535873be60 Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 2 Jul 2012 19:09:26 -0600 Subject: [PATCH 13/13] Version changes. --- lib/Lacuna.pm | 2 +- var/upgrades/{3.glyph.sql => 3.0859.sql} | 0 var/www/public/changes.txt | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) rename var/upgrades/{3.glyph.sql => 3.0859.sql} (100%) diff --git a/lib/Lacuna.pm b/lib/Lacuna.pm index 566cc0b4..4fe69b76 100644 --- a/lib/Lacuna.pm +++ b/lib/Lacuna.pm @@ -7,7 +7,7 @@ use Config::JSON; useall __PACKAGE__; -our $VERSION = 3.0858; +our $VERSION = 3.0859; my $config = Config::JSON->new('/data/Lacuna-Server/etc/lacuna.conf'); my $db = Lacuna::DB->connect($config->get('db/dsn'),$config->get('db/username'),$config->get('db/password'), { mysql_enable_utf8 => 1}); diff --git a/var/upgrades/3.glyph.sql b/var/upgrades/3.0859.sql similarity index 100% rename from var/upgrades/3.glyph.sql rename to var/upgrades/3.0859.sql diff --git a/var/www/public/changes.txt b/var/www/public/changes.txt index a5289d3d..ea7b7f0d 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -1,5 +1,6 @@ -3.0899: +3.0859: - Mod: glyphs changed in DB to be more compact. + 3.0858: - Add: Resources per hour for all food/ore types now shown in PCC and SCC - Add: PCC and SCC now show all incoming supply chains -- 2.51.2
IdTypeQuantityAction
%s%sDelete
%s%s%sDelete