MagicBot
8 months ago
5 changed files with 132 additions and 109 deletions
@ -0,0 +1,129 @@ |
|||||||
|
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
|
||||||
|
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
|
||||||
|
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
|
||||||
|
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
|
||||||
|
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
|
||||||
|
// Magicbane Emulator Project © 2013 - 2022
|
||||||
|
// www.magicbane.com
|
||||||
|
|
||||||
|
package engine.net.client.handlers; |
||||||
|
|
||||||
|
import engine.Enum; |
||||||
|
import engine.exception.MsgSendException; |
||||||
|
import engine.net.Dispatch; |
||||||
|
import engine.net.DispatchMessage; |
||||||
|
import engine.net.client.ClientConnection; |
||||||
|
import engine.net.client.msg.AcceptTradeRequestMsg; |
||||||
|
import engine.net.client.msg.ClientNetMsg; |
||||||
|
import engine.net.client.msg.OpenTradeWindowMsg; |
||||||
|
import engine.net.client.msg.UpdateVaultMsg; |
||||||
|
import engine.objects.Account; |
||||||
|
import engine.objects.CharacterItemManager; |
||||||
|
import engine.objects.PlayerCharacter; |
||||||
|
import engine.server.MBServerStatics; |
||||||
|
|
||||||
|
import static engine.math.FastMath.sqr; |
||||||
|
import static engine.objects.CharacterItemManager.canTrade; |
||||||
|
|
||||||
|
public class AcceptTradeRequestMsgHandler extends AbstractClientMsgHandler { |
||||||
|
|
||||||
|
public AcceptTradeRequestMsgHandler() { |
||||||
|
super(AcceptTradeRequestMsg.class); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException { |
||||||
|
|
||||||
|
// Member variable declaration
|
||||||
|
|
||||||
|
AcceptTradeRequestMsg msg; |
||||||
|
|
||||||
|
// Member variable assignment
|
||||||
|
|
||||||
|
msg = (AcceptTradeRequestMsg) baseMsg; |
||||||
|
|
||||||
|
PlayerCharacter source = (PlayerCharacter) origin.getPlayerCharacter(); |
||||||
|
PlayerCharacter target = PlayerCharacter.getFromCache(msg.getTargetID()); |
||||||
|
|
||||||
|
Dispatch dispatch; |
||||||
|
|
||||||
|
if (source == null || !source.isAlive()) |
||||||
|
return false; |
||||||
|
|
||||||
|
if (target == null || !target.isAlive()) |
||||||
|
return false; |
||||||
|
|
||||||
|
if (source.charItemManager.tradingWith != null) |
||||||
|
return false; |
||||||
|
|
||||||
|
if (!canTrade(source, target)) |
||||||
|
return false; |
||||||
|
|
||||||
|
// verify characterTarget is in range
|
||||||
|
if (source.getLoc().distanceSquared2D(target.getLoc()) > sqr(MBServerStatics.TRADE_RANGE)) |
||||||
|
return false; |
||||||
|
|
||||||
|
// TODO uncomment this block after we determine when we
|
||||||
|
// setBankOpen(false) and setVaultOpen(false)
|
||||||
|
/* |
||||||
|
* CharacterItemManager cim1 = source.getCharItemManager(); |
||||||
|
* CharacterItemManager cim2 = characterTarget.getCharItemManager(); if (cim1 == |
||||||
|
* null) return false; if (cim2 == null) return false; if (cim1.isBankOpen()) |
||||||
|
* return false; if (cim2.isVaultOpen()) return false; |
||||||
|
*/ |
||||||
|
ClientConnection sourceConn = origin; |
||||||
|
ClientConnection targetConn = target.getClientConnection(); |
||||||
|
|
||||||
|
if (sourceConn == null) |
||||||
|
return false; |
||||||
|
|
||||||
|
if (targetConn == null) |
||||||
|
return false; |
||||||
|
|
||||||
|
CharacterItemManager toTradeWith = target.charItemManager; |
||||||
|
|
||||||
|
if (toTradeWith == null) |
||||||
|
return false; |
||||||
|
|
||||||
|
Account sourceAccount = source.getAccount(); |
||||||
|
Account targetAccount = target.getAccount(); |
||||||
|
|
||||||
|
UpdateVaultMsg uvmSource = new UpdateVaultMsg(sourceAccount); |
||||||
|
UpdateVaultMsg uvmTarget = new UpdateVaultMsg(targetAccount); |
||||||
|
|
||||||
|
dispatch = Dispatch.borrow(source, uvmSource); |
||||||
|
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); |
||||||
|
|
||||||
|
dispatch = Dispatch.borrow(target, uvmTarget); |
||||||
|
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); |
||||||
|
|
||||||
|
source.charItemManager.setVaultOpen(false); |
||||||
|
toTradeWith.setVaultOpen(false); |
||||||
|
source.charItemManager.setBankOpen(false); |
||||||
|
toTradeWith.setBankOpen(false); |
||||||
|
|
||||||
|
OpenTradeWindowMsg otwm = new OpenTradeWindowMsg(msg.getUnknown01(), source, target); |
||||||
|
|
||||||
|
// Only start trade if both players aren't already trading with
|
||||||
|
// someone
|
||||||
|
|
||||||
|
if (source.charItemManager.tradingWith != null || toTradeWith.getTradingWith() != null) |
||||||
|
return false; |
||||||
|
|
||||||
|
source.charItemManager.initializeTrade(); |
||||||
|
toTradeWith.initializeTrade(); |
||||||
|
source.charItemManager.setTradingWith(targetConn); |
||||||
|
toTradeWith.setTradingWith(sourceConn); |
||||||
|
source.charItemManager.tradeID = msg.getUnknown01(); |
||||||
|
toTradeWith.tradeID = msg.getUnknown01(); |
||||||
|
|
||||||
|
dispatch = Dispatch.borrow(source, otwm); |
||||||
|
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); |
||||||
|
|
||||||
|
dispatch = Dispatch.borrow(target, otwm); |
||||||
|
DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.PRIMARY); |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue