Browse Source

Handler created for AceeptTradeMsg

combat-2
MagicBot 8 months ago
parent
commit
d4e38c113b
  1. 16
      src/engine/gameManager/TradeManager.java
  2. 3
      src/engine/net/client/ClientMessagePump.java
  3. 2
      src/engine/net/client/Protocol.java
  4. 129
      src/engine/net/client/handlers/AcceptTradeRequestMsgHandler.java
  5. 91
      src/engine/objects/CharacterItemManager.java

16
src/engine/gameManager/TradeManager.java

@ -22,22 +22,6 @@ public enum TradeManager { @@ -22,22 +22,6 @@ public enum TradeManager {
TRADEMANAGER;
public static void acceptTradeRequest(AcceptTradeRequestMsg msg, ClientConnection origin) {
PlayerCharacter source = origin.getPlayerCharacter();
if (source == null)
return;
try {
source.charItemManager.acceptTradeRequest(msg);
} catch (Exception e) {
Logger.error(e);
// TODO Auto-generated catch block
}
}
public static void rejectTradeRequest(RejectTradeRequestMsg msg, ClientConnection origin) {
// TODO Do nothing? If so, delete this method & case above
}

3
src/engine/net/client/ClientMessagePump.java

@ -73,9 +73,6 @@ public class ClientMessagePump implements NetMsgHandler { @@ -73,9 +73,6 @@ public class ClientMessagePump implements NetMsgHandler {
break;
case OPENVAULT:
break;
case REQUESTTRADEOK:
TradeManager.acceptTradeRequest((AcceptTradeRequestMsg) msg, origin);
break;
case REQUESTTRADECANCEL:
TradeManager.rejectTradeRequest((RejectTradeRequestMsg) msg, origin);
break;

2
src/engine/net/client/Protocol.java

@ -179,7 +179,7 @@ public enum Protocol { @@ -179,7 +179,7 @@ public enum Protocol {
REQUESTTOOPENBANK(0xF26E453F, null, null), // RequestToOpenBankMsg
REQUESTTOTRADE(0x4D84259B, TradeRequestMsg.class, TradeRequestMsgHandler.class), // Trade Request
REQUESTTRADECANCEL(0xCB0C5735, RejectTradeRequestMsg.class, null), // Reject RequestToTrade
REQUESTTRADEOK(0xFFD29841, AcceptTradeRequestMsg.class, null), // Accept Trade Request
REQUESTTRADEOK(0xFFD29841, AcceptTradeRequestMsg.class, AcceptTradeRequestMsgHandler.class), // Accept Trade Request
RESETAFTERDEATH(0xFDCBB98F, RespawnMsg.class, RespawnMsgHandler.class), //Respawn Request/Response
ROTATEMSG(0x57F2088E, RotateObjectMsg.class, null),
SAFEMODE(0x9CF3922A, SafeModeMsg.class, null), //Tell client they're in safe mode

129
src/engine/net/client/handlers/AcceptTradeRequestMsgHandler.java

@ -0,0 +1,129 @@ @@ -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;
}
}

91
src/engine/objects/CharacterItemManager.java

@ -53,12 +53,12 @@ public class CharacterItemManager { @@ -53,12 +53,12 @@ public class CharacterItemManager {
private short inventoryWeight;
private short equipWeight;
private short vaultWeight;
private ClientConnection tradingWith;
public ClientConnection tradingWith;
private byte tradeCommitted;
private boolean tradeSuccess;
private HashSet<Integer> trading;
private int goldTradingAmount;
private int tradeID = 0;
public int tradeID = 0;
public CharacterItemManager(AbstractCharacter ac) {
super();
@ -444,93 +444,6 @@ public class CharacterItemManager { @@ -444,93 +444,6 @@ public class CharacterItemManager {
return true;
}
public synchronized boolean acceptTradeRequest(AcceptTradeRequestMsg msg) {
PlayerCharacter source = (PlayerCharacter) this.getOwner();
PlayerCharacter target = PlayerCharacter.getFromCache(msg.getTargetID());
Dispatch dispatch;
if (source == null || !source.isAlive())
return false;
if (target == null || !target.isAlive())
return false;
if (this.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 = source.getClientConnection();
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);
this.setVaultOpen(false);
toTradeWith.setVaultOpen(false);
this.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 (this.getTradingWith() != null || toTradeWith.getTradingWith() != null)
return false;
this.initializeTrade();
toTradeWith.initializeTrade();
this.setTradingWith(targetConn);
toTradeWith.setTradingWith(sourceConn);
this.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;
}
public synchronized boolean addItemToTradeWindow(AddItemToTradeWindowMsg msg) {
PlayerCharacter source = (PlayerCharacter) this.getOwner();
Dispatch dispatch;

Loading…
Cancel
Save