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,