added bank functionality, refactored itemMoveHandler

Co-authored-by: Cake Lancelot <CakeLancelot@users.noreply.github.com>
This commit is contained in:
kamilprzyb 2020-09-09 22:42:55 +02:00 committed by dongresource
parent 480cca82fa
commit de15e2004b
6 changed files with 90 additions and 35 deletions

View File

@ -405,6 +405,20 @@ void Database::updateInventory(Player player) {
db.insert(toAdd); db.insert(toAdd);
} }
} }
//insert bank
for (int i = 0; i < ABANK_COUNT; i++) {
if (player.Bank[i].iID != 0) {
sItemBase* next = &player.Bank[i];
Inventory toAdd = {};
toAdd.playerId = player.iID;
toAdd.slot = i + AEQUIP_COUNT + AINVEN_COUNT;
toAdd.id = next->iID;
toAdd.Opt = next->iOpt;
toAdd.Type = next->iType;
toAdd.TimeLimit = next->iTimeLimit;
db.insert(toAdd);
}
}
db.commit(); db.commit();
} }
void Database::updateNanos(Player player) { void Database::updateNanos(Player player) {
@ -441,10 +455,13 @@ void Database::getInventory(Player* player) {
toSet.iType = current.Type; toSet.iType = current.Type;
toSet.iOpt = current.Opt; toSet.iOpt = current.Opt;
toSet.iTimeLimit = current.TimeLimit; toSet.iTimeLimit = current.TimeLimit;
if (current.slot > AEQUIP_COUNT) //assign to proper arrays
if (current.slot <= AEQUIP_COUNT)
player->Equip[current.slot] = toSet;
else if (current.slot <= (AEQUIP_COUNT + AINVEN_COUNT))
player->Inven[current.slot - AEQUIP_COUNT] = toSet; player->Inven[current.slot - AEQUIP_COUNT] = toSet;
else else
player->Equip[current.slot] = toSet; player->Bank[current.slot - AEQUIP_COUNT - AINVEN_COUNT] = toSet;
} }
} }

View File

@ -11,6 +11,8 @@ void ItemManager::init() {
REGISTER_SHARD_PACKET(P_CL2FE_REQ_ITEM_MOVE, itemMoveHandler); REGISTER_SHARD_PACKET(P_CL2FE_REQ_ITEM_MOVE, itemMoveHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_ITEM_DELETE, itemDeleteHandler); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_ITEM_DELETE, itemDeleteHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_GIVE_ITEM, itemGMGiveHandler); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_GIVE_ITEM, itemGMGiveHandler);
//Bank
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_BANK_OPEN, itemBankOpenHandler);
//Trade handlers //Trade handlers
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TRADE_OFFER, itemTradeOfferHandler); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TRADE_OFFER, itemTradeOfferHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TRADE_OFFER_ACCEPT, itemTradeOfferAcceptHandler); REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TRADE_OFFER_ACCEPT, itemTradeOfferAcceptHandler);
@ -33,6 +35,7 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) {
PlayerView& plr = PlayerManager::players[sock]; PlayerView& plr = PlayerManager::players[sock];
//sanity check
if (plr.plr->Equip[itemmove->iFromSlotNum].iType != 0 && itemmove->eFrom == 0 && itemmove->eTo == 0) { if (plr.plr->Equip[itemmove->iFromSlotNum].iType != 0 && itemmove->eFrom == 0 && itemmove->eTo == 0) {
// this packet should never happen unless it is a weapon, tell the client to do nothing and do nothing ourself // this packet should never happen unless it is a weapon, tell the client to do nothing and do nothing ourself
resp.eTo = itemmove->eFrom; resp.eTo = itemmove->eFrom;
@ -46,46 +49,59 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) {
return; return;
} }
if (itemmove->iToSlotNum > AINVEN_COUNT || itemmove->iToSlotNum < 0) // sanity check
return; // sanity checks if (itemmove->iToSlotNum > ABANK_COUNT || itemmove->iToSlotNum < 0)
return;
sItemBase fromItem; //get the fromItem
sItemBase toItem; sItemBase *fromItem;
switch (itemmove->eFrom) {
// eFrom 0 means from equip case (int)slot::equip:
if (itemmove->eFrom == 0) { fromItem = &plr.plr->Equip[itemmove->iFromSlotNum];
// unequiping an item break;
fromItem = plr.plr->Equip[itemmove->iFromSlotNum]; case (int)slot::inventory:
} else { fromItem = &plr.plr->Inven[itemmove->iFromSlotNum];
fromItem = plr.plr->Inven[itemmove->iFromSlotNum]; break;
case (int)slot::bank:
fromItem = &plr.plr->Bank[itemmove->iFromSlotNum];
break;
} }
// eTo 0 means to equip //get the toItem
if (itemmove->eTo == 0) { sItemBase* toItem;
// equiping an item switch (itemmove->eTo) {
toItem = plr.plr->Equip[itemmove->iToSlotNum]; case (int)slot::equip:
plr.plr->Equip[itemmove->iToSlotNum] = fromItem; toItem = &plr.plr->Equip[itemmove->iToSlotNum];
} else { break;
toItem = plr.plr->Inven[itemmove->iToSlotNum]; case (int)slot::inventory:
plr.plr->Inven[itemmove->iToSlotNum] = fromItem; toItem = &plr.plr->Inven[itemmove->iToSlotNum];
break;
case (int)slot::bank:
toItem = &plr.plr->Bank[itemmove->iToSlotNum];
break;
} }
if (itemmove->eFrom == 0) { //save items to response
plr.plr->Equip[itemmove->iFromSlotNum] = toItem; resp.ToSlotItem = *toItem;
} else { resp.FromSlotItem = *fromItem;
plr.plr->Inven[itemmove->iFromSlotNum] = toItem;
}
if (itemmove->eFrom == 0 || itemmove->eTo == 0) { //swap items in session
sItemBase temp = *toItem;
*toItem = *fromItem;
*fromItem = temp;
//send equip change to viewable players
if (itemmove->eFrom == (int)slot::equip || itemmove->eTo == (int)slot::equip) {
INITSTRUCT(sP_FE2CL_PC_EQUIP_CHANGE, equipChange); INITSTRUCT(sP_FE2CL_PC_EQUIP_CHANGE, equipChange);
equipChange.iPC_ID = plr.plr->iID; equipChange.iPC_ID = plr.plr->iID;
if (itemmove->eFrom == 0) { if (itemmove->eFrom == (int)slot::equip) {
equipChange.iEquipSlotNum = itemmove->iFromSlotNum; equipChange.iEquipSlotNum = itemmove->iFromSlotNum;
equipChange.EquipSlotItem = toItem; equipChange.EquipSlotItem = resp.ToSlotItem;
} else { }
else {
equipChange.iEquipSlotNum = itemmove->iToSlotNum; equipChange.iEquipSlotNum = itemmove->iToSlotNum;
equipChange.EquipSlotItem = fromItem; equipChange.EquipSlotItem = resp.FromSlotItem;
} }
// unequip vehicle if equip slot 8 is 0 // unequip vehicle if equip slot 8 is 0
@ -98,13 +114,12 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) {
} }
} }
//send response
resp.eTo = itemmove->eFrom; resp.eTo = itemmove->eFrom;
resp.iToSlotNum = itemmove->iFromSlotNum; resp.iToSlotNum = itemmove->iFromSlotNum;
resp.ToSlotItem = toItem;
resp.eFrom = itemmove->eTo; resp.eFrom = itemmove->eTo;
resp.iFromSlotNum = itemmove->iToSlotNum; resp.iFromSlotNum = itemmove->iToSlotNum;
resp.FromSlotItem = fromItem;
sock->sendPacket((void*)&resp, P_FE2CL_PC_ITEM_MOVE_SUCC, sizeof(sP_FE2CL_PC_ITEM_MOVE_SUCC)); sock->sendPacket((void*)&resp, P_FE2CL_PC_ITEM_MOVE_SUCC, sizeof(sP_FE2CL_PC_ITEM_MOVE_SUCC));
} }
@ -158,6 +173,19 @@ void ItemManager::itemGMGiveHandler(CNSocket* sock, CNPacketData* data) {
} }
} }
void ItemManager::itemBankOpenHandler(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2FE_REQ_PC_BANK_OPEN))
return; // ignore the malformed packet
//just send bank inventory
INITSTRUCT(sP_FE2CL_REP_PC_BANK_OPEN_SUCC, resp);
for (int i = 0; i < ABANK_COUNT; i++) {
resp.aBank[i] = PlayerManager::players[sock].plr->Bank[i];
}
resp.iExtraBank = 1;
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_BANK_OPEN_SUCC, sizeof(sP_FE2CL_REP_PC_BANK_OPEN_SUCC));
}
void ItemManager::itemTradeOfferHandler(CNSocket* sock, CNPacketData* data) { void ItemManager::itemTradeOfferHandler(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2FE_REQ_PC_TRADE_OFFER)) if (data->size != sizeof(sP_CL2FE_REQ_PC_TRADE_OFFER))
return; // ignore the malformed packet return; // ignore the malformed packet

View File

@ -4,11 +4,18 @@
#include "Player.hpp" #include "Player.hpp"
namespace ItemManager { namespace ItemManager {
enum class slot {
equip = 0,
inventory = 1,
bank = 3
};
void init(); void init();
void itemMoveHandler(CNSocket* sock, CNPacketData* data); void itemMoveHandler(CNSocket* sock, CNPacketData* data);
void itemDeleteHandler(CNSocket* sock, CNPacketData* data); void itemDeleteHandler(CNSocket* sock, CNPacketData* data);
void itemGMGiveHandler(CNSocket* sock, CNPacketData* data); void itemGMGiveHandler(CNSocket* sock, CNPacketData* data);
//Bank
void itemBankOpenHandler(CNSocket* sock, CNPacketData* data);
void itemTradeOfferHandler(CNSocket* sock, CNPacketData* data); void itemTradeOfferHandler(CNSocket* sock, CNPacketData* data);
//void itemTradeOfferCancel(CNSocket* sock, CNPacketData* data); //void itemTradeOfferCancel(CNSocket* sock, CNPacketData* data);
void itemTradeOfferAcceptHandler(CNSocket* sock, CNPacketData* data); void itemTradeOfferAcceptHandler(CNSocket* sock, CNPacketData* data);

View File

@ -31,6 +31,7 @@ struct Player {
int x, y, z, angle; int x, y, z, angle;
sItemBase Equip[AEQUIP_COUNT]; sItemBase Equip[AEQUIP_COUNT];
sItemBase Inven[AINVEN_COUNT]; sItemBase Inven[AINVEN_COUNT];
sItemBase Bank[ABANK_COUNT];
sItemTrade Trade[12]; sItemTrade Trade[12];
int32_t moneyInTrade; int32_t moneyInTrade;
bool isTrading; bool isTrading;

View File

@ -3,6 +3,7 @@
#define AEQUIP_COUNT 9 #define AEQUIP_COUNT 9
#define AINVEN_COUNT 50 #define AINVEN_COUNT 50
#define AQINVEN_COUNT 50 #define AQINVEN_COUNT 50
#define ABANK_COUNT 119
#pragma pack(push) #pragma pack(push)

View File

@ -3,6 +3,7 @@
#define AEQUIP_COUNT 12 #define AEQUIP_COUNT 12
#define AINVEN_COUNT 50 #define AINVEN_COUNT 50
#define AQINVEN_COUNT 50 #define AQINVEN_COUNT 50
#define ABANK_COUNT 200
#pragma pack(push) #pragma pack(push)