From be402b02d5def16fc3d497b39327f4a6e368c05b Mon Sep 17 00:00:00 2001 From: lemming552 Date: Mon, 21 May 2012 09:43:24 -0600 Subject: [PATCH] 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.54.0