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);
}
}
//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();
}
void Database::updateNanos(Player player) {
@ -441,10 +455,13 @@ void Database::getInventory(Player* player) {
toSet.iType = current.Type;
toSet.iOpt = current.Opt;
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;
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_PC_ITEM_DELETE, itemDeleteHandler);
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_GIVE_ITEM, itemGMGiveHandler);
//Bank
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_BANK_OPEN, itemBankOpenHandler);
//Trade handlers
REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_TRADE_OFFER, itemTradeOfferHandler);
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];
//sanity check
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
resp.eTo = itemmove->eFrom;
@ -46,46 +49,59 @@ void ItemManager::itemMoveHandler(CNSocket* sock, CNPacketData* data) {
return;
}
if (itemmove->iToSlotNum > AINVEN_COUNT || itemmove->iToSlotNum < 0)
return; // sanity checks
// sanity check
if (itemmove->iToSlotNum > ABANK_COUNT || itemmove->iToSlotNum < 0)
return;
sItemBase fromItem;
sItemBase toItem;
// eFrom 0 means from equip
if (itemmove->eFrom == 0) {
// unequiping an item
fromItem = plr.plr->Equip[itemmove->iFromSlotNum];
} else {
fromItem = plr.plr->Inven[itemmove->iFromSlotNum];
//get the fromItem
sItemBase *fromItem;
switch (itemmove->eFrom) {
case (int)slot::equip:
fromItem = &plr.plr->Equip[itemmove->iFromSlotNum];
break;
case (int)slot::inventory:
fromItem = &plr.plr->Inven[itemmove->iFromSlotNum];
break;
case (int)slot::bank:
fromItem = &plr.plr->Bank[itemmove->iFromSlotNum];
break;
}
// eTo 0 means to equip
if (itemmove->eTo == 0) {
// equiping an item
toItem = plr.plr->Equip[itemmove->iToSlotNum];
plr.plr->Equip[itemmove->iToSlotNum] = fromItem;
} else {
toItem = plr.plr->Inven[itemmove->iToSlotNum];
plr.plr->Inven[itemmove->iToSlotNum] = fromItem;
//get the toItem
sItemBase* toItem;
switch (itemmove->eTo) {
case (int)slot::equip:
toItem = &plr.plr->Equip[itemmove->iToSlotNum];
break;
case (int)slot::inventory:
toItem = &plr.plr->Inven[itemmove->iToSlotNum];
break;
case (int)slot::bank:
toItem = &plr.plr->Bank[itemmove->iToSlotNum];
break;
}
if (itemmove->eFrom == 0) {
plr.plr->Equip[itemmove->iFromSlotNum] = toItem;
} else {
plr.plr->Inven[itemmove->iFromSlotNum] = toItem;
}
//save items to response
resp.ToSlotItem = *toItem;
resp.FromSlotItem = *fromItem;
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);
equipChange.iPC_ID = plr.plr->iID;
if (itemmove->eFrom == 0) {
if (itemmove->eFrom == (int)slot::equip) {
equipChange.iEquipSlotNum = itemmove->iFromSlotNum;
equipChange.EquipSlotItem = toItem;
} else {
equipChange.EquipSlotItem = resp.ToSlotItem;
}
else {
equipChange.iEquipSlotNum = itemmove->iToSlotNum;
equipChange.EquipSlotItem = fromItem;
equipChange.EquipSlotItem = resp.FromSlotItem;
}
// 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.iToSlotNum = itemmove->iFromSlotNum;
resp.ToSlotItem = toItem;
resp.iToSlotNum = itemmove->iFromSlotNum;
resp.eFrom = itemmove->eTo;
resp.iFromSlotNum = itemmove->iToSlotNum;
resp.FromSlotItem = fromItem;
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) {
if (data->size != sizeof(sP_CL2FE_REQ_PC_TRADE_OFFER))
return; // ignore the malformed packet

View File

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

View File

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

View File

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

View File

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