diff options
author | ForesterDev <11771800+ForesterDev@users.noreply.github.com> | 2020-05-29 19:26:53 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-29 18:26:53 +0200 |
commit | f7ac6296b6d5ee10f4248f64ee090950499cd12d (patch) | |
tree | ad9c1cd818c1e494be1ec2600afb74eaa020e75c | |
parent | f0060496f9c8c4a21c28552a95928f2d05e031a9 (diff) |
Core/PacketIO: Create BankHandler & update bank opcodes to new packet class (#24695)
* Core/PacketIO: Create BankHandler & update bank opcodes to new packet class
(cherry picked from commit 478e86c074cc9c6a19f6c83d2a265eb1ccd705fc)
* fix nopch build
* Update SMSG_BUY_BANK_SLOT_RESULT
* fix naming
* fix copypaste error
Co-authored-by: Luzifix <luzifix19@gmail.com>
-rw-r--r-- | src/server/game/Handlers/BankHandler.cpp | 189 | ||||
-rw-r--r-- | src/server/game/Handlers/ItemHandler.cpp | 151 | ||||
-rw-r--r-- | src/server/game/Handlers/NPCHandler.cpp | 30 | ||||
-rw-r--r-- | src/server/game/Server/Packets/AllPackets.h | 1 | ||||
-rw-r--r-- | src/server/game/Server/Packets/BankPackets.cpp | 49 | ||||
-rw-r--r-- | src/server/game/Server/Packets/BankPackets.h | 81 | ||||
-rw-r--r-- | src/server/game/Server/WorldSession.h | 17 |
7 files changed, 333 insertions, 185 deletions
diff --git a/src/server/game/Handlers/BankHandler.cpp b/src/server/game/Handlers/BankHandler.cpp new file mode 100644 index 00000000000..f1710f8131a --- /dev/null +++ b/src/server/game/Handlers/BankHandler.cpp @@ -0,0 +1,189 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "BankPackets.h" +#include "Item.h" +#include "DBCStores.h" +#include "Log.h" +#include "NPCPackets.h" +#include "Opcodes.h" +#include "Player.h" +#include "WorldPacket.h" +#include "WorldSession.h" + +bool WorldSession::CanUseBank(ObjectGuid bankerGUID) const +{ + // bankerGUID parameter is optional, set to 0 by default. + if (!bankerGUID) + bankerGUID = m_currentBankerGUID; + + bool isUsingBankCommand = (bankerGUID == GetPlayer()->GetGUID() && bankerGUID == m_currentBankerGUID); + + if (!isUsingBankCommand) + { + Creature* creature = GetPlayer()->GetNPCIfCanInteractWith(bankerGUID, UNIT_NPC_FLAG_BANKER); + if (!creature) + return false; + } + + return true; +} + +void WorldSession::HandleBankerActivateOpcode(WorldPackets::NPC::Hello& packet) +{ + Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(packet.Unit, UNIT_NPC_FLAG_BANKER); + if (!unit) + { + TC_LOG_DEBUG("network", "WORLD: HandleBankerActivateOpcode - %s not found or you can not interact with him.", packet.Unit.ToString().c_str()); + return; + } + + // remove fake death + if (GetPlayer()->HasUnitState(UNIT_STATE_DIED)) + GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH); + + SendShowBank(packet.Unit); +} + +void WorldSession::HandleAutoBankItemOpcode(WorldPackets::Bank::AutoBankItem& packet) +{ + TC_LOG_DEBUG("network", "STORAGE: receive bag = %u, slot = %u", packet.Bag, packet.Slot); + + if (!CanUseBank()) + { + TC_LOG_DEBUG("network", "WORLD: HandleAutoBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str()); + return; + } + + Item* item = _player->GetItemByPos(packet.Bag, packet.Slot); + if (!item) + return; + + ItemPosCountVec dest; + InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false); + if (msg != EQUIP_ERR_OK) + { + _player->SendEquipError(msg, item, nullptr); + return; + } + + if (dest.size() == 1 && dest[0].pos == item->GetPos()) + { + _player->SendEquipError(EQUIP_ERR_NONE, item, nullptr); + return; + } + + _player->RemoveItem(packet.Bag, packet.Slot, true); + _player->ItemRemovedQuestCheck(item->GetEntry(), item->GetCount()); + _player->BankItem(dest, item, true); +} + +void WorldSession::HandleAutoStoreBankItemOpcode(WorldPackets::Bank::AutoStoreBankItem& packet) +{ + TC_LOG_DEBUG("network", "STORAGE: receive bag = %u, slot = %u", packet.Bag, packet.Slot); + + if (!CanUseBank()) + { + TC_LOG_DEBUG("network", "WORLD: HandleAutoStoreBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str()); + return; + } + + Item* item = _player->GetItemByPos(packet.Bag, packet.Slot); + if (!item) + return; + + if (_player->IsBankPos(packet.Bag, packet.Slot)) // moving from bank to inventory + { + ItemPosCountVec dest; + InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, item, false); + if (msg != EQUIP_ERR_OK) + { + _player->SendEquipError(msg, item, nullptr); + return; + } + + _player->RemoveItem(packet.Bag, packet.Slot, true); + if (Item const* storedItem = _player->StoreItem(dest, item, true)) + _player->ItemAddedQuestCheck(storedItem->GetEntry(), storedItem->GetCount()); + } + else // moving from inventory to bank + { + ItemPosCountVec dest; + InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, item, false); + if (msg != EQUIP_ERR_OK) + { + _player->SendEquipError(msg, item, nullptr); + return; + } + + _player->RemoveItem(packet.Bag, packet.Slot, true); + _player->BankItem(dest, item, true); + } +} + +void WorldSession::HandleBuyBankSlotOpcode(WorldPackets::Bank::BuyBankSlot& buyBankSlot) +{ + WorldPackets::Bank::BuyBankSlotResult packet; + if (!CanUseBank(buyBankSlot.Banker)) + { + packet.Result = ERR_BANKSLOT_NOTBANKER; + SendPacket(packet.Write()); + TC_LOG_DEBUG("network", "WORLD: HandleBuyBankSlotOpcode - %s not found or you can't interact with him.", buyBankSlot.Banker.ToString().c_str()); + return; + } + + uint32 slot = _player->GetBankBagSlotCount(); + + // next slot + ++slot; + + TC_LOG_INFO("network", "PLAYER: Buy bank bag slot, slot number = %u", slot); + + BankBagSlotPricesEntry const* slotEntry = sBankBagSlotPricesStore.LookupEntry(slot); + + if (!slotEntry) + { + packet.Result = ERR_BANKSLOT_FAILED_TOO_MANY; + SendPacket(packet.Write()); + return; + } + + uint32 price = slotEntry->price; + + if (!_player->HasEnoughMoney(price)) + { + packet.Result = ERR_BANKSLOT_INSUFFICIENT_FUNDS; + SendPacket(packet.Write()); + return; + } + + _player->SetBankBagSlotCount(slot); + _player->ModifyMoney(-int32(price)); + + packet.Result = ERR_BANKSLOT_OK; + SendPacket(packet.Write()); + + _player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BUY_BANK_SLOT); +} + +void WorldSession::SendShowBank(ObjectGuid guid) +{ + m_currentBankerGUID = guid; + WorldPackets::Bank::ShowBank packet; + packet.Banker = guid; + SendPacket(packet.Write()); +} diff --git a/src/server/game/Handlers/ItemHandler.cpp b/src/server/game/Handlers/ItemHandler.cpp index f4558493589..f0b2c072a53 100644 --- a/src/server/game/Handlers/ItemHandler.cpp +++ b/src/server/game/Handlers/ItemHandler.cpp @@ -748,139 +748,6 @@ void WorldSession::HandleAutoStoreBagItemOpcode(WorldPacket& recvData) _player->StoreItem(dest, pItem, true); } -void WorldSession::HandleBuyBankSlotOpcode(WorldPacket& recvPacket) -{ - TC_LOG_DEBUG("network", "WORLD: CMSG_BUY_BANK_SLOT"); - - ObjectGuid guid; - recvPacket >> guid; - - if (!CanUseBank(guid)) - { - TC_LOG_DEBUG("network", "WORLD: HandleBuyBankSlotOpcode - %s not found or you can't interact with him.", guid.ToString().c_str()); - return; - } - - uint32 slot = _player->GetBankBagSlotCount(); - - // next slot - ++slot; - - TC_LOG_INFO("network", "PLAYER: Buy bank bag slot, slot number = %u", slot); - - BankBagSlotPricesEntry const* slotEntry = sBankBagSlotPricesStore.LookupEntry(slot); - - WorldPacket data(SMSG_BUY_BANK_SLOT_RESULT, 4); - - if (!slotEntry) - { - data << uint32(ERR_BANKSLOT_FAILED_TOO_MANY); - SendPacket(&data); - return; - } - - uint32 price = slotEntry->price; - - if (!_player->HasEnoughMoney(price)) - { - data << uint32(ERR_BANKSLOT_INSUFFICIENT_FUNDS); - SendPacket(&data); - return; - } - - _player->SetBankBagSlotCount(slot); - _player->ModifyMoney(-int32(price)); - - data << uint32(ERR_BANKSLOT_OK); - SendPacket(&data); - - _player->UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_BUY_BANK_SLOT); -} - -void WorldSession::HandleAutoBankItemOpcode(WorldPacket& recvPacket) -{ - TC_LOG_DEBUG("network", "WORLD: CMSG_AUTOBANK_ITEM"); - uint8 srcbag, srcslot; - - recvPacket >> srcbag >> srcslot; - TC_LOG_DEBUG("network", "STORAGE: receive srcbag = %u, srcslot = %u", srcbag, srcslot); - - if (!CanUseBank()) - { - TC_LOG_DEBUG("network", "WORLD: HandleAutoBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str()); - return; - } - - Item* pItem = _player->GetItemByPos(srcbag, srcslot); - if (!pItem) - return; - - ItemPosCountVec dest; - InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, pItem, false); - if (msg != EQUIP_ERR_OK) - { - _player->SendEquipError(msg, pItem, nullptr); - return; - } - - if (dest.size() == 1 && dest[0].pos == pItem->GetPos()) - { - _player->SendEquipError(EQUIP_ERR_NONE, pItem, nullptr); - return; - } - - _player->RemoveItem(srcbag, srcslot, true); - _player->ItemRemovedQuestCheck(pItem->GetEntry(), pItem->GetCount()); - _player->BankItem(dest, pItem, true); -} - -void WorldSession::HandleAutoStoreBankItemOpcode(WorldPacket& recvPacket) -{ - TC_LOG_DEBUG("network", "WORLD: CMSG_AUTOSTORE_BANK_ITEM"); - uint8 srcbag, srcslot; - - recvPacket >> srcbag >> srcslot; - TC_LOG_DEBUG("network", "STORAGE: receive srcbag = %u, srcslot = %u", srcbag, srcslot); - - if (!CanUseBank()) - { - TC_LOG_DEBUG("network", "WORLD: HandleAutoStoreBankItemOpcode - Unit (%s) not found or you can't interact with him.", m_currentBankerGUID.ToString().c_str()); - return; - } - - Item* pItem = _player->GetItemByPos(srcbag, srcslot); - if (!pItem) - return; - - if (_player->IsBankPos(srcbag, srcslot)) // moving from bank to inventory - { - ItemPosCountVec dest; - InventoryResult msg = _player->CanStoreItem(NULL_BAG, NULL_SLOT, dest, pItem, false); - if (msg != EQUIP_ERR_OK) - { - _player->SendEquipError(msg, pItem, nullptr); - return; - } - - _player->RemoveItem(srcbag, srcslot, true); - if (Item const* storedItem = _player->StoreItem(dest, pItem, true)) - _player->ItemAddedQuestCheck(storedItem->GetEntry(), storedItem->GetCount()); - } - else // moving from inventory to bank - { - ItemPosCountVec dest; - InventoryResult msg = _player->CanBankItem(NULL_BAG, NULL_SLOT, dest, pItem, false); - if (msg != EQUIP_ERR_OK) - { - _player->SendEquipError(msg, pItem, nullptr); - return; - } - - _player->RemoveItem(srcbag, srcslot, true); - _player->BankItem(dest, pItem, true); - } -} - void WorldSession::HandleSetAmmoOpcode(WorldPacket& recvData) { if (!_player->IsAlive()) @@ -1350,21 +1217,3 @@ void WorldSession::HandleItemTextQuery(WorldPacket& recvData ) SendPacket(&data); } - -bool WorldSession::CanUseBank(ObjectGuid bankerGUID) const -{ - // bankerGUID parameter is optional, set to 0 by default. - if (!bankerGUID) - bankerGUID = m_currentBankerGUID; - - bool isUsingBankCommand = (bankerGUID == GetPlayer()->GetGUID() && bankerGUID == m_currentBankerGUID); - - if (!isUsingBankCommand) - { - Creature* creature = GetPlayer()->GetNPCIfCanInteractWith(bankerGUID, UNIT_NPC_FLAG_BANKER); - if (!creature) - return false; - } - - return true; -} diff --git a/src/server/game/Handlers/NPCHandler.cpp b/src/server/game/Handlers/NPCHandler.cpp index 89baa638a81..1078de17111 100644 --- a/src/server/game/Handlers/NPCHandler.cpp +++ b/src/server/game/Handlers/NPCHandler.cpp @@ -78,36 +78,6 @@ void WorldSession::SendTabardVendorActivate(ObjectGuid guid) SendPacket(&data); } -void WorldSession::HandleBankerActivateOpcode(WorldPacket& recvData) -{ - ObjectGuid guid; - - TC_LOG_DEBUG("network", "WORLD: Received CMSG_BANKER_ACTIVATE"); - - recvData >> guid; - - Creature* unit = GetPlayer()->GetNPCIfCanInteractWith(guid, UNIT_NPC_FLAG_BANKER); - if (!unit) - { - TC_LOG_DEBUG("network", "WORLD: HandleBankerActivateOpcode - %s not found or you can not interact with him.", guid.ToString().c_str()); - return; - } - - // remove fake death - if (GetPlayer()->HasUnitState(UNIT_STATE_DIED)) - GetPlayer()->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH); - - SendShowBank(guid); -} - -void WorldSession::SendShowBank(ObjectGuid guid) -{ - WorldPacket data(SMSG_SHOW_BANK, 8); - data << guid; - m_currentBankerGUID = guid; - SendPacket(&data); -} - void WorldSession::SendShowMailBox(ObjectGuid guid) { WorldPacket data(SMSG_SHOW_MAILBOX, 8); diff --git a/src/server/game/Server/Packets/AllPackets.h b/src/server/game/Server/Packets/AllPackets.h index 3fdad4e577f..5fb2c5b9e82 100644 --- a/src/server/game/Server/Packets/AllPackets.h +++ b/src/server/game/Server/Packets/AllPackets.h @@ -18,6 +18,7 @@ #ifndef AllPackets_h__ #define AllPackets_h__ +#include "BankPackets.h" #include "CharacterPackets.h" #include "ChatPackets.h" #include "CombatLogPackets.h" diff --git a/src/server/game/Server/Packets/BankPackets.cpp b/src/server/game/Server/Packets/BankPackets.cpp new file mode 100644 index 00000000000..3356d293ed7 --- /dev/null +++ b/src/server/game/Server/Packets/BankPackets.cpp @@ -0,0 +1,49 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "BankPackets.h" + +void WorldPackets::Bank::AutoBankItem::Read() +{ + _worldPacket >> Bag; + _worldPacket >> Slot; +} + +void WorldPackets::Bank::AutoStoreBankItem::Read() +{ + _worldPacket >> Bag; + _worldPacket >> Slot; +} + +void WorldPackets::Bank::BuyBankSlot::Read() +{ + _worldPacket >> Banker; +} + +WorldPacket const* WorldPackets::Bank::BuyBankSlotResult::Write() +{ + _worldPacket << uint32(Result); + + return &_worldPacket; +} + +WorldPacket const* WorldPackets::Bank::ShowBank::Write() +{ + _worldPacket << Banker; + + return &_worldPacket; +} diff --git a/src/server/game/Server/Packets/BankPackets.h b/src/server/game/Server/Packets/BankPackets.h new file mode 100644 index 00000000000..8cff53f4912 --- /dev/null +++ b/src/server/game/Server/Packets/BankPackets.h @@ -0,0 +1,81 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef BankPackets_h__ +#define BankPackets_h__ + +#include "Packet.h" +#include "ObjectGuid.h" + +namespace WorldPackets +{ + namespace Bank + { + class AutoBankItem final : public ClientPacket + { + public: + AutoBankItem(WorldPacket&& packet) : ClientPacket(CMSG_AUTOBANK_ITEM, std::move(packet)) { } + + void Read() override; + + uint8 Bag = 0; + uint8 Slot = 0; + }; + + class AutoStoreBankItem final : public ClientPacket + { + public: + AutoStoreBankItem(WorldPacket&& packet) : ClientPacket(CMSG_AUTOSTORE_BANK_ITEM, std::move(packet)) { } + + void Read() override; + + uint8 Bag = 0; + uint8 Slot = 0; + }; + + class BuyBankSlot final : public ClientPacket + { + public: + BuyBankSlot(WorldPacket&& packet) : ClientPacket(CMSG_BUY_BANK_SLOT, std::move(packet)) { } + + void Read() override; + + ObjectGuid Banker; + }; + + class BuyBankSlotResult final : public ServerPacket + { + public: + BuyBankSlotResult() : ServerPacket(SMSG_BUY_BANK_SLOT_RESULT, 4) { } + + WorldPacket const* Write() override; + + uint32 Result; + }; + + class ShowBank final : public ServerPacket + { + public: + ShowBank() : ServerPacket(SMSG_SHOW_BANK, 8) { } + + WorldPacket const* Write() override; + + ObjectGuid Banker; + }; + } +} +#endif // BankPackets_h__ diff --git a/src/server/game/Server/WorldSession.h b/src/server/game/Server/WorldSession.h index cdc1c77111e..57794ac81bf 100644 --- a/src/server/game/Server/WorldSession.h +++ b/src/server/game/Server/WorldSession.h @@ -78,6 +78,13 @@ class RBACData; namespace WorldPackets { + namespace Bank + { + class AutoBankItem; + class AutoStoreBankItem; + class BuyBankSlot; + } + namespace Character { class LogoutCancel; @@ -755,8 +762,6 @@ class TC_GAME_API WorldSession void SendActivateTaxiReply(ActivateTaxiReply reply); void HandleTabardVendorActivateOpcode(WorldPacket& recvPacket); - void HandleBankerActivateOpcode(WorldPacket& recvPacket); - void HandleBuyBankSlotOpcode(WorldPacket& recvPacket); void HandleTrainerListOpcode(WorldPackets::NPC::Hello& packet); void HandleTrainerBuySpellOpcode(WorldPackets::NPC::TrainerBuySpell& packet); void HandlePetitionShowListOpcode(WorldPacket& recvPacket); @@ -798,6 +803,12 @@ class TC_GAME_API WorldSession void HandleAuctionPlaceBid(WorldPacket& recvData); void HandleAuctionListPendingSales(WorldPacket& recvData); + // Bank + void HandleBankerActivateOpcode(WorldPackets::NPC::Hello& packet); + void HandleAutoBankItemOpcode(WorldPackets::Bank::AutoBankItem& packet); + void HandleAutoStoreBankItemOpcode(WorldPackets::Bank::AutoStoreBankItem& packet); + void HandleBuyBankSlotOpcode(WorldPackets::Bank::BuyBankSlot& buyBankSlot); + void HandleGetMailList(WorldPacket& recvData); void HandleSendMail(WorldPacket& recvData); void HandleMailTakeMoney(WorldPacket& recvData); @@ -823,8 +834,6 @@ class TC_GAME_API WorldSession void HandleAutoEquipItemSlotOpcode(WorldPacket& recvPacket); void HandleSwapItem(WorldPacket& recvPacket); void HandleBuybackItem(WorldPacket& recvPacket); - void HandleAutoBankItemOpcode(WorldPacket& recvPacket); - void HandleAutoStoreBankItemOpcode(WorldPacket& recvPacket); void HandleWrapItemOpcode(WorldPacket& recvPacket); void HandleAttackSwingOpcode(WorldPackets::Combat::AttackSwing& packet); |