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), + }; }