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 /src/server/game/Handlers/BankHandler.cpp | |
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>
Diffstat (limited to 'src/server/game/Handlers/BankHandler.cpp')
-rw-r--r-- | src/server/game/Handlers/BankHandler.cpp | 189 |
1 files changed, 189 insertions, 0 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()); +} |