diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index c39709dc..2256b2ff 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -61,6 +61,7 @@ sub add_to_market { my ($payload, $meta) = $self->structure_payload($offer, $space_used); $ship->task('Waiting On Trade'); $ship->update; + my $body = $self->body; my %trade = ( %{$meta}, payload => $payload, @@ -68,13 +69,36 @@ sub add_to_market { ship_id => $ship->id, body_id => $self->body_id, transfer_type => $self->transfer_type, + x => $body->x, + y => $body->y, + speed => $ship->speed, + trade_range => max (250, $self->level * 20), ); return Lacuna->db->resultset('Lacuna::DB::Result::Market')->new(\%trade)->insert; } sub transfer_type { my $self = shift; - return $self->body->zone; + return 'trade'; +} + +sub available_market { + my $self = shift; + my $minus_x = 0 - $self->body->x; + my $minus_y = 0 - $self->body->y; + + return $self->market->search({ + -and => [ + body_id => {'!=' => $self->body_id}, + \[ "transfer_type = ? and ceil(pow(pow(me.x - $minus_x, 2) + pow(me.y - $minus_y, 2), 0.5)) < trade_range", [transfer_type => $self->transfer_type]], + ]},{ + '+select' => [ + { ceil => \"pow(pow(me.x + $minus_x,2) + pow(me.y + $minus_y,2), 0.5)", '-as' => 'distance' }, + ], + '+as' => [ + 'distance', + ], + }); } sub trade_ships { diff --git a/lib/Lacuna/DB/Result/Building/Transporter.pm b/lib/Lacuna/DB/Result/Building/Transporter.pm index 47536beb..19c1eb6e 100644 --- a/lib/Lacuna/DB/Result/Building/Transporter.pm +++ b/lib/Lacuna/DB/Result/Building/Transporter.pm @@ -72,6 +72,16 @@ sub transfer_type { return 'transporter'; } +sub available_market { + my $self = shift; + return $self->market->search( + { + body_id => {'!=' => $self->body_id}, + transfer_type => $self->transfer_type, + }, + ) +} + sub determine_available_cargo_space { my ($self) = @_; return 12500 * $self->level * $self->body->empire->trade_affinity; diff --git a/lib/Lacuna/DB/Result/Market.pm b/lib/Lacuna/DB/Result/Market.pm index cfbeeaa1..fe9ef0d8 100644 --- a/lib/Lacuna/DB/Result/Market.pm +++ b/lib/Lacuna/DB/Result/Market.pm @@ -25,9 +25,14 @@ __PACKAGE__->add_columns( has_prisoner => { data_type => 'tinyint', default_value => 0 }, has_glyph => { data_type => 'tinyint', default_value => 0 }, has_plan => { data_type => 'tinyint', default_value => 0 }, + x => { data_type => 'int', default_value => 0 }, + y => { data_type => 'int', default_value => 0 }, + speed => { data_type => 'int', default_value => 0 }, + trade_range => { data_type => 'int', default_value => 0 }, ); __PACKAGE__->belongs_to('body', 'Lacuna::DB::Result::Map::Body', 'body_id'); +__PACKAGE__->belongs_to('ship', 'Lacuna::DB::Result::Ships', 'ship_id'); sub sqlt_deploy_hook { my ($self, $sqlt_table) = @_; diff --git a/lib/Lacuna/Role/Trader.pm b/lib/Lacuna/Role/Trader.pm index 9ae2de95..2c326d2b 100644 --- a/lib/Lacuna/Role/Trader.pm +++ b/lib/Lacuna/Role/Trader.pm @@ -21,16 +21,6 @@ sub my_market { return $self->market->search({body_id => $self->body_id, transfer_type => $self->transfer_type }); } -sub available_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; diff --git a/lib/Lacuna/Role/TraderRpc.pm b/lib/Lacuna/Role/TraderRpc.pm index 7c194bc4..1af5b75a 100644 --- a/lib/Lacuna/Role/TraderRpc.pm +++ b/lib/Lacuna/Role/TraderRpc.pm @@ -36,9 +36,13 @@ sub view_market { my $building = $self->get_building($empire, $building_id); $page_number ||=1; my $all_trades = $building->available_market->search( - undef, - { rows => 25, page => $page_number, join => 'body', order_by => 'ask' } - ); + undef,{ + rows => 25, + page => $page_number, + join => 'body', + order_by => 'ask', + } + ); if ($filter && $filter ~~ [qw(food ore water waste energy glyph prisoner ship plan)]) { $all_trades = $all_trades->search({ 'has_'.$filter => 1 }); } @@ -48,18 +52,26 @@ sub view_market { $trade->delete; next; } + my $delivery; + if ($trade->transfer_type eq 'transporter') { + $delivery = {duration => 0}; + } + else { + $delivery = {duration => $trade->ship->calculate_travel_time($building->body)}; + } push @trades, { - id => $trade->id, - date_offered => $trade->date_offered_formatted, - ask => $trade->ask, - offer => $trade->format_description_of_payload, - body => { - id => $trade->body_id, + id => $trade->id, + date_offered => $trade->date_offered_formatted, + ask => $trade->ask, + offer => $trade->format_description_of_payload, + body => { + id => $trade->body_id, }, - empire => { - id => $trade->body->empire->id, - name => $trade->body->empire->name, + empire => { + id => $trade->body->empire->id, + name => $trade->body->empire->name, }, + delivery => => $delivery, }; } return { diff --git a/var/upgrades/3.0836.sql b/var/upgrades/3.0836.sql new file mode 100644 index 00000000..93a1de59 --- /dev/null +++ b/var/upgrades/3.0836.sql @@ -0,0 +1,9 @@ +alter table market add column speed int(11) not null default 0; +alter table market add column x int(11) not null default 0; +alter table market add column y int(11) not null default 0; +alter table market add column trade_range int(11) not null default 0; + +update market set transfer_type='trade' where transfer_type != 'transporter'; +update market,body set market.x = body.x, market.y = body.y where market.body_id=body.id; +update market,ships set market.speed = ships.speed where market.ship_id=ships.id; +update market set trade_range=500 where transfer_type='trade'; -- 2.51.2 From 4bb58795be2d137e2eced54d2552f73f3a64829e Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sun, 22 Jan 2012 06:34:34 -0500 Subject: [PATCH 2/5] Updated changes.txt, pod and version number for new trade method --- docs/Trade.pod | 5 ++++- lib/Lacuna.pm | 2 +- var/www/public/changes.txt | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/Trade.pod b/docs/Trade.pod index 454414fc..3697b3f6 100644 --- a/docs/Trade.pod +++ b/docs/Trade.pod @@ -428,11 +428,14 @@ Displays a list of trades available at the present time. "gold glyph" ], "body" : { - "id" : "id-goes-here" # use with get_trade_ships() to determine travel time + "id" : "id-goes-here" }, "empire" : { "id" : "id-goes-here", "name" : "Earthlings" + }, + "delivery: : { + "duration" : 3600, # travel time in seconds } }, ... diff --git a/lib/Lacuna.pm b/lib/Lacuna.pm index a1a49c6c..f8218bd6 100644 --- a/lib/Lacuna.pm +++ b/lib/Lacuna.pm @@ -7,7 +7,7 @@ use Config::JSON; useall __PACKAGE__; -our $VERSION = 3.0836; +our $VERSION = 3.0837; 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/www/public/changes.txt b/var/www/public/changes.txt index 7e492413..8e5f287d 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -1,3 +1,6 @@ +3.0837: + - Mod: Trade Ministry now trades within a set distance rather than just within zone + 3.0836: - Fix: Better resource checks when storage is reduced. - Fix: Supply Pods can only take what is available on a planet when launched. -- 2.51.2 From 29eaeb76a3f7a00863d77342cdbb46c689b14435 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sun, 22 Jan 2012 13:05:20 -0500 Subject: [PATCH 3/5] DeLambert now know how many trades are in range --- lib/Lacuna/AI/DeLambert.pm | 51 ++++++++++++++++++++------ lib/Lacuna/DB/Result/Building/Trade.pm | 4 +- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/lib/Lacuna/AI/DeLambert.pm b/lib/Lacuna/AI/DeLambert.pm index 59081ff9..6b5bb312 100644 --- a/lib/Lacuna/AI/DeLambert.pm +++ b/lib/Lacuna/AI/DeLambert.pm @@ -187,6 +187,12 @@ sub sell_glyph_trade { say "#### SELL GLYPH TRADE ####"; my $scratchpad = $self->scratch->pad; + my $trade_min = $colony->get_building_of_class('Lacuna::DB::Result::Building::Trade'); + + if (not $trade_min) { + say " ERROR: No trade ministry found on ".$colony->name; + return; + } if (not defined $scratchpad->{sell_glyph_probability}) { $scratchpad->{sell_glyph_probability} = 1; @@ -210,15 +216,18 @@ sub sell_glyph_trade { my $ship = $self->get_trade_ship($colony); return unless $ship; - # check how many trades there are in the zone at the moment - my $trades_in_zone = Lacuna->db->resultset('Lacuna::DB::Result::Market')->search({ - transfer_type => $colony->zone, + # check how many trades there are in range at the moment + my $trades_in_zone = $trade_min->available_market({ + transfer_type => 'trade', has_glyph => 1, 'body.empire_id' => -9, },{ join => 'body', })->count; - if ($trades_in_zone >= $scratchpad->{sell_max_glyph_trades_in_zone}) { + my $my_trades = $trade_min->my_market({ + has_plan => 1, + })->count; + if ($trades_in_zone + $my_trades >= $scratchpad->{sell_max_glyph_trades_in_zone}) { return; } my $quantity = randint(1,$scratchpad->{sell_glyph_max_batch}); @@ -242,7 +251,11 @@ sub sell_glyph_trade { ask => $cost_per * $quantity, ship_id => $ship->id, body_id => $colony->id, - transfer_type => $colony->zone, + transfer_type => 'trade', + x => $colony->x, + y => $colony->y, + speed => $ship->speed, + trade_range => 600, ); Lacuna->db->resultset('Lacuna::DB::Result::Market')->create(\%trade); } @@ -254,6 +267,13 @@ sub sell_plan_trade { say "#### SELL PLAN TRADE ####"; + my $trade_min = $colony->get_building_of_class('Lacuna::DB::Result::Building::Trade'); + + if (not $trade_min) { + say " ERROR: No trade ministry found on ".$colony->name; + return; + } + my $scratchpad = $self->scratch->pad; if (randint(1,100) <= $scratchpad->{sell_plan_probability}) { @@ -264,16 +284,20 @@ sub sell_plan_trade { return; } - # check how many trades there are in the zone at the moment - my $trades_in_zone = Lacuna->db->resultset('Lacuna::DB::Result::Market')->search({ - transfer_type => $colony->zone, + # check how many trades there are in the zone at the moment (including our own) + my $trades_in_zone = $trade_min->available_market({ + transfer_type => 'trade', has_plan => 1, 'body.empire_id' => -9, },{ join => 'body', })->count; - if ($trades_in_zone >= $scratchpad->{sell_max_plan_trades_in_zone}) { - say "Already enough trades ($trades_in_zone) in zone!"; + my $my_trades = $trade_min->my_market({ + has_plan => 1, + })->count; + + if ($trades_in_zone + $my_trades >= $scratchpad->{sell_max_plan_trades_in_zone}) { + say "Already enough trades ($trades_in_zone + $my_trades) in zone!"; return; } my $level = randint($scratchpad->{sell_plan_min_level},$scratchpad->{sell_plan_max_level}); @@ -307,7 +331,11 @@ sub sell_plan_trade { ask => $cost_per * $quantity, ship_id => $ship->id, body_id => $colony->id, - transfer_type => $colony->zone, + transfer_type => 'trade', + x => $colony->x, + y => $colony->y, + speed => $ship->speed, + trade_range => 600, ); Lacuna->db->resultset('Lacuna::DB::Result::Market')->create(\%trade); } @@ -886,3 +914,4 @@ small print. no Moose; __PACKAGE__->meta->make_immutable; + diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index 2256b2ff..8040d84a 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -2,6 +2,8 @@ package Lacuna::DB::Result::Building::Trade; use Moose; use utf8; +use List::Util qw(max); + no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building'; @@ -90,7 +92,7 @@ sub available_market { return $self->market->search({ -and => [ body_id => {'!=' => $self->body_id}, - \[ "transfer_type = ? and ceil(pow(pow(me.x - $minus_x, 2) + pow(me.y - $minus_y, 2), 0.5)) < trade_range", [transfer_type => $self->transfer_type]], + \[ "transfer_type = ? and ceil(pow(pow(me.x + $minus_x, 2) + pow(me.y + $minus_y, 2), 0.5)) < trade_range", [transfer_type => $self->transfer_type]], ]},{ '+select' => [ { ceil => \"pow(pow(me.x + $minus_x,2) + pow(me.y + $minus_y,2), 0.5)", '-as' => 'distance' }, -- 2.51.2 From f5e1f4935d5158a69fd9cd4abed4cece88164f06 Mon Sep 17 00:00:00 2001 From: ian docherty Date: Sun, 22 Jan 2012 14:47:14 -0500 Subject: [PATCH 4/5] fixed DeL bug, not adding trades --- lib/Lacuna/AI/DeLambert.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Lacuna/AI/DeLambert.pm b/lib/Lacuna/AI/DeLambert.pm index 6b5bb312..bf38e848 100644 --- a/lib/Lacuna/AI/DeLambert.pm +++ b/lib/Lacuna/AI/DeLambert.pm @@ -225,7 +225,7 @@ sub sell_glyph_trade { join => 'body', })->count; my $my_trades = $trade_min->my_market({ - has_plan => 1, + has_glyph => 1, })->count; if ($trades_in_zone + $my_trades >= $scratchpad->{sell_max_glyph_trades_in_zone}) { return; -- 2.51.2 From 9f1d4fc147cb27f379221a1e4fa33a99d28875ca Mon Sep 17 00:00:00 2001 From: ian docherty Date: Thu, 26 Jan 2012 14:43:45 -0500 Subject: [PATCH 5/5] Stopped DeLambert from creating too many trades in the area --- lib/Lacuna/AI/DeLambert.pm | 27 +++++++------------------- lib/Lacuna/DB/Result/Building/Trade.pm | 25 ++++++++++++++++++------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/Lacuna/AI/DeLambert.pm b/lib/Lacuna/AI/DeLambert.pm index bf38e848..8f6393be 100644 --- a/lib/Lacuna/AI/DeLambert.pm +++ b/lib/Lacuna/AI/DeLambert.pm @@ -217,17 +217,11 @@ sub sell_glyph_trade { return unless $ship; # check how many trades there are in range at the moment - my $trades_in_zone = $trade_min->available_market({ - transfer_type => 'trade', - has_glyph => 1, - 'body.empire_id' => -9, - },{ - join => 'body', + my $trades_in_zone = $trade_min->local_market({ + has_glyph => 1, })->count; - my $my_trades = $trade_min->my_market({ - has_glyph => 1, - })->count; - if ($trades_in_zone + $my_trades >= $scratchpad->{sell_max_glyph_trades_in_zone}) { + if ($trades_in_zone >= $scratchpad->{sell_max_glyph_trades_in_zone}) { + say "Already enough trades ($trades_in_zone) in zone!"; return; } my $quantity = randint(1,$scratchpad->{sell_glyph_max_batch}); @@ -285,19 +279,12 @@ sub sell_plan_trade { } # check how many trades there are in the zone at the moment (including our own) - my $trades_in_zone = $trade_min->available_market({ - transfer_type => 'trade', - has_plan => 1, - 'body.empire_id' => -9, - },{ - join => 'body', - })->count; - my $my_trades = $trade_min->my_market({ + my $trades_in_zone = $trade_min->local_market({ has_plan => 1, })->count; - if ($trades_in_zone + $my_trades >= $scratchpad->{sell_max_plan_trades_in_zone}) { - say "Already enough trades ($trades_in_zone + $my_trades) in zone!"; + if ($trades_in_zone >= $scratchpad->{sell_max_plan_trades_in_zone}) { + say "Already enough trades ($trades_in_zone) in zone!"; return; } my $level = randint($scratchpad->{sell_plan_min_level},$scratchpad->{sell_plan_max_level}); diff --git a/lib/Lacuna/DB/Result/Building/Trade.pm b/lib/Lacuna/DB/Result/Building/Trade.pm index 8040d84a..a39a02f6 100644 --- a/lib/Lacuna/DB/Result/Building/Trade.pm +++ b/lib/Lacuna/DB/Result/Building/Trade.pm @@ -3,6 +3,7 @@ package Lacuna::DB::Result::Building::Trade; use Moose; use utf8; use List::Util qw(max); +use Data::Dumper; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building'; @@ -84,22 +85,34 @@ sub transfer_type { return 'trade'; } -sub available_market { - my $self = shift; - my $minus_x = 0 - $self->body->x; - my $minus_y = 0 - $self->body->y; +# all trades within range (including those on this colony) +sub local_market { + my ($self, $args) = @_; + + my $minus_x = -$self->body->x; + my $minus_y = -$self->body->y; return $self->market->search({ + %$args, -and => [ - body_id => {'!=' => $self->body_id}, \[ "transfer_type = ? and ceil(pow(pow(me.x + $minus_x, 2) + pow(me.y + $minus_y, 2), 0.5)) < trade_range", [transfer_type => $self->transfer_type]], - ]},{ + ] + },{ '+select' => [ { ceil => \"pow(pow(me.x + $minus_x,2) + pow(me.y + $minus_y,2), 0.5)", '-as' => 'distance' }, ], '+as' => [ 'distance', ], + join => 'body', + }); +} + +# available market. All trades within range that are not our own +sub available_market { + my ($self) = @_; + return $self->local_market({ + body_id => {'!=' => $self->body_id}, }); }