


when('TRADE_REQUESTED'){
my $myself=$rotmg->getObjectById($rotmg->{'objectId'});
my $bool_wantToTrade=0;
$packet->decompile;
if(defined $admins{$packet->name}){ # admins
$bool_wantToTrade=1;
}else{
my $buyer=$rotmg->getObjectByPlayerName($packet->name);
my $inventory=$buyer->inventory;
my $myPotionCount=sum(values($myself->inventory->getCount(\%potions,'carry')));
my $myNonPotionItemCount=scalar(keys %{$myself->inventory->get('carry',\%potions)});
if($myPotionCount>0 || $myNonPotionItemCount==0){
my $playertext=Rotmg::Packet->new('PLAYER_TEXT');
$playertext->text('/tell ' . $buyer->name . ' I am sold out - check back later ' . $buyer->name . ' - thx!');
$playertext->compile;
$rotmg->sendPacket($playertext);
}else{
$bool_wantToTrade=1;
my $playertext=Rotmg::Packet->new('PLAYER_TEXT');
$playertext->text('/tell ' . $buyer->name . ' The price for all my items is just a single att/def/vit/life/mana potion, ' . $buyer->name . '!');
$playertext->compile;
$rotmg->sendPacket($playertext);
}
}
if($bool_wantToTrade==1){
print '[ALSO REQUESTING TRADE] name:' . $packet->name . "\n";
my $requesttrade=Rotmg::Packet->new('REQUEST_TRADE');
$requesttrade->name($packet->name);
$requesttrade->compile;
$rotmg->sendPacket($requesttrade);
}
}
when('TRADE_START'){
$packet->decompile;
$rotmg->{'boolTrading'}=1;
$rotmg->{'lastTradeStartTime'}=time();
$rotmg->{'currentTradePartnerName'}=$packet->name;
print "\n\n";
print '[TRADESTART] receiving initial inventory from ' . $packet->name . "\n";
# sometimes you get a TRADESTART packet with a person who was actually not the last person you agreed to trade with but the one shortly before!?!?!
# so set the trade variables within the TRADESTART context to be safe ...
my $myItems=$packet->myItems;
my $yourItems=$packet->yourItems;
$rotmg->{'currentTradeOwnInventory'}=$myItems;
$rotmg->{'currentTradeBuyerInventory'}=$yourItems;
my $inventorySlotsToOffer_ref=undef;
if(defined $admins{$packet->name}){
$inventorySlotsToOffer_ref=$myItems->get('carry');
print "\t" . 'OFFERING ALL ... YOU ARE AN ADMIN' . "\n";
}else{
$inventorySlotsToOffer_ref=$myItems->get('carry',\%potions);
}
print '[CHANGETRADE] our offer:' . join(',',keys(%$inventorySlotsToOffer_ref)) . "\n";
my $changetrade=Rotmg::Packet->new('CHANGE_TRADE');
$changetrade->inventorySlotsToOffer($inventorySlotsToOffer_ref);
$changetrade->compile;
$rotmg->sendPacket($changetrade);
}
when('TRADE_ACCEPTED'){
print '[TRADEACCEPTED] buyer suggested a trade ($rotmg->{\'currentTradePartnerName\'}=' . $rotmg->{'currentTradePartnerName'} . ')' . "\n";
$packet->decompile;
my $bool_acceptTrade=0;
if(defined $admins{$rotmg->{'currentTradePartnerName'}}){
print "\t" . 'ACCEPTING TRADE ... YOU ARE AN ADMIN ' . "\n";
$bool_acceptTrade=1;
}else{
my $myOffer=$packet->myOffer;
my $yourOffer=$packet->yourOffer;
my $numberOfPotionsIAmOffering=sum(values($rotmg->{'currentTradeOwnInventory'}->getCount(\%potions,$myOffer)));
print '$numberOfPotionsIAmOffering:' . $numberOfPotionsIAmOffering . ' $numberOfPotionsBuyerIsOffering:' . $numberOfPotionsBuyerIsOffering . "\n";
if($numberOfPotionsIAmOffering==0 && $numberOfPotionsBuyerIsOffering>=1){
print "\t" . 'I GIVE NO POTION ... YOU GIVE 1+ POTION ... WE GOT A DEAL ... ;-))' . "\n";
$bool_acceptTrade=1;
}else{
print "\t" . 'NO DEAL :-((' . "\n";
my $playertext=Rotmg::Packet->new('PLAYER_TEXT');
$playertext->text('/tell ' . $rotmg->{'currentTradePartnerName'} . ' ' . $rotmg->{'currentTradePartnerName'} . ', your offer does not include a att/def/vit/life/mana potion!');
$playertext->compile;
$rotmg->sendPacket($playertext);
}
}
if($bool_acceptTrade==1){
my $accepttrade=Rotmg::Packet->new('ACCEPT_TRADE');
$accepttrade->data($packet->dataBackup); # if the trade is ok just take the raw TRADEACCEPTED data and send it back ...
$rotmg->sendPacket($accepttrade);
}
}