From 0de22fc3b6a0004895afd68f8c0ce3983280701c Mon Sep 17 00:00:00 2001 From: JT Smith Date: Tue, 20 Jul 2010 21:34:45 -0500 Subject: [PATCH] fixed some trade bugs --- lib/Lacuna/RPC/Building/Trade.pm | 8 +++++- lib/Lacuna/RPC/Building/Transporter.pm | 6 +++++ lib/Lacuna/Role/TraderRpc.pm | 4 +-- t/Trade.t | 34 ++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lib/Lacuna/RPC/Building/Trade.pm b/lib/Lacuna/RPC/Building/Trade.pm index db894759..491302d4 100644 --- a/lib/Lacuna/RPC/Building/Trade.pm +++ b/lib/Lacuna/RPC/Building/Trade.pm @@ -46,11 +46,14 @@ sub add_trade { sub withdraw_trade { my ($self, $session_id, $building_id, $trade_id) = @_; + unless ($trade_id) { + confess [1002, 'You have not specified a trade to withdraw.']; + } my $cache = Lacuna->cache; if ($cache->get('trade_lock', $trade_id)) { confess [1013, 'A buyer has placed an offer on this trade. Please wait a few moments and try again.']; } - $cache->set('trade_lock',$trade_id,5); + $cache->set('trade_lock',$trade_id,1,5); my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); my $trade = $building->trades->find($trade_id); @@ -65,6 +68,9 @@ sub withdraw_trade { sub accept_trade { my ($self, $session_id, $building_id, $trade_id, $guid, $solution) = @_; + unless ($trade_id) { + confess [1002, 'You have not specified a trade to accept.']; + } my $cache = Lacuna->cache; if ($cache->get('trade_lock', $trade_id)) { confess [1013, 'Another buyer has placed an offer on this trade. Please wait a few moments and try again.']; diff --git a/lib/Lacuna/RPC/Building/Transporter.pm b/lib/Lacuna/RPC/Building/Transporter.pm index d8f50466..7c271c47 100644 --- a/lib/Lacuna/RPC/Building/Transporter.pm +++ b/lib/Lacuna/RPC/Building/Transporter.pm @@ -54,6 +54,9 @@ sub add_trade { sub withdraw_trade { my ($self, $session_id, $building_id, $trade_id) = @_; + unless ($trade_id) { + confess [1002, 'You have not specified a trade to withdraw.']; + } my $cache = Lacuna->cache; if ($cache->get('trade_lock', $trade_id)) { confess [1013, 'A buyer has placed an offer on this trade. Please wait a few moments and try again.']; @@ -74,6 +77,9 @@ sub withdraw_trade { sub accept_trade { my ($self, $session_id, $building_id, $trade_id, $guid, $solution) = @_; + unless ($trade_id) { + confess [1002, 'You have not specified a trade to accept.']; + } my $cache = Lacuna->cache; if ($cache->get('trade_lock', $trade_id)) { confess [1013, 'Another buyer has placed an offer on this trade. Please wait a few moments and try again.']; diff --git a/lib/Lacuna/Role/TraderRpc.pm b/lib/Lacuna/Role/TraderRpc.pm index af099f86..a766d791 100644 --- a/lib/Lacuna/Role/TraderRpc.pm +++ b/lib/Lacuna/Role/TraderRpc.pm @@ -36,8 +36,8 @@ sub view_available_trades { my ($self, $session_id, $building_id, $page_number) = @_; my $empire = $self->get_empire_by_session($session_id); my $building = $self->get_building($empire, $building_id); - my $all_trades = $building->available_trades->search(undef, { rows => 25, page => $page_number }); $page_number ||=1; + my $all_trades = $building->available_trades->search(undef, { rows => 25, page => $page_number }); my @trades; while (my $trade = $all_trades->next) { push @trades, { @@ -55,7 +55,7 @@ sub view_available_trades { trades => \@trades, trade_count => $all_trades->pager->total_entries, page_number => $page_number, - captcha => $building->assign_captcha, + captcha => $building->assign_captcha($empire), status => $self->format_status($empire, $building->body), }; } diff --git a/t/Trade.t b/t/Trade.t index b7a56149..d9372c23 100644 --- a/t/Trade.t +++ b/t/Trade.t @@ -1,5 +1,5 @@ use lib '../lib'; -use Test::More tests => 2; +use Test::More tests => 11; use Test::Deep; use Data::Dumper; use 5.010; @@ -28,7 +28,37 @@ my $trade = $tester->get_building($result->{result}{building}{id}); $trade->finish_upgrade; $result = $tester->post('trade', 'view_my_trades', [$session_id, $trade->id]); -ok(exists $result->{result}, 'can call view_my_trades when there are no trades'); +ok(exists $result->{result}, 'can call view_my_trades'); + +$result = $tester->post('trade', 'get_stored_resources', [$session_id, $trade->id]); +ok(exists $result->{result}, 'can call get_stored_resources'); + +$result = $tester->post('trade', 'view_available_trades', [$session_id, $trade->id]); +ok(exists $result->{result}, 'can call view_available_trades'); + +$result = $tester->post('trade', 'get_ships', [$session_id, $trade->id]); +ok(exists $result->{result}, 'can call get_ships'); + +$result = $tester->post('trade', 'get_prisoners', [$session_id, $trade->id]); +ok(exists $result->{result}, 'can call get_prisoners'); + +$result = $tester->post('trade', 'get_plans', [$session_id, $trade->id]); +ok(exists $result->{result}, 'can call get_plans'); + +$result = $tester->post('trade', 'add_trade', [$session_id, $trade->id]); # no ships +is($result->{error}{code}, 1011, 'can call add_trade'); + +$result = $tester->post('trade', 'accept_trade', [$session_id, $trade->id]); +is($result->{error}{code}, 1002, 'can call accept_trade'); + +$result = $tester->post('trade', 'withdraw_trade', [$session_id, $trade->id]); +is($result->{error}{code}, 1002, 'can call withdraw_trade'); + +$result = $tester->post('trade', 'push_items', [$session_id, $trade->id]); +is($result->{error}{code}, 1002, 'can call push_items'); + + + END { $tester->cleanup; -- 2.54.0