diff options
author | MitchesD <majklprofik@seznam.cz> | 2015-09-14 11:29:57 +0200 |
---|---|---|
committer | MitchesD <majklprofik@seznam.cz> | 2015-09-14 11:29:16 +0200 |
commit | 5a895f77596923b55b81d02dc924c7e2ce5ef804 (patch) | |
tree | 79c87d71add23c535df9422c4d7ace009dbe80a1 /src | |
parent | 04de58c8d3beb393233071650c65d50967b45266 (diff) |
Core/Player: moved player collections to separate class
* the idea is to have all collections (toybox, heirlooms, mount and later even battle pets) in one class to make it easier and don't increase size of already big Player and WorldSession class
Diffstat (limited to 'src')
-rw-r--r-- | src/server/game/Entities/Player/CollectionMgr.cpp | 78 | ||||
-rw-r--r-- | src/server/game/Entities/Player/CollectionMgr.h | 52 | ||||
-rw-r--r-- | src/server/game/Entities/Player/Player.cpp | 23 | ||||
-rw-r--r-- | src/server/game/Entities/Player/Player.h | 3 | ||||
-rw-r--r-- | src/server/game/Handlers/ToyHandler.cpp | 8 | ||||
-rw-r--r-- | src/server/game/Server/Packets/ToyPackets.h | 1 | ||||
-rw-r--r-- | src/server/game/Server/WorldSession.cpp | 40 | ||||
-rw-r--r-- | src/server/game/Server/WorldSession.h | 14 |
8 files changed, 145 insertions, 74 deletions
diff --git a/src/server/game/Entities/Player/CollectionMgr.cpp b/src/server/game/Entities/Player/CollectionMgr.cpp new file mode 100644 index 00000000000..34e19fabc1a --- /dev/null +++ b/src/server/game/Entities/Player/CollectionMgr.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * + * 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 "CollectionMgr.h" +#include "Player.h" + +void CollectionMgr::LoadToys() +{ + for (auto const& t : _toys) + _owner->GetPlayer()->AddDynamicValue(PLAYER_DYNAMIC_FIELD_TOYS, t.first); +} + +bool CollectionMgr::AddToy(uint32 itemId, bool isFavourite /*= false*/) +{ + if (UpdateAccountToys(itemId, isFavourite)) + { + _owner->GetPlayer()->AddDynamicValue(PLAYER_DYNAMIC_FIELD_TOYS, itemId); + return true; + } + + return false; +} + +void CollectionMgr::LoadAccountToys(PreparedQueryResult result) +{ + if (!result) + return; + + do + { + Field* fields = result->Fetch(); + uint32 itemId = fields[0].GetUInt32(); + bool isFavourite = fields[1].GetBool(); + + _toys[itemId] = isFavourite; + } while (result->NextRow()); +} + +void CollectionMgr::SaveAccountToys(SQLTransaction& trans) +{ + PreparedStatement* stmt = NULL; + for (ToyBoxContainer::const_iterator itr = _toys.begin(); itr != _toys.end(); ++itr) + { + stmt = LoginDatabase.GetPreparedStatement(LOGIN_REP_ACCOUNT_TOYS); + stmt->setUInt32(0, _owner->GetBattlenetAccountId()); + stmt->setUInt32(1, itr->first); + stmt->setBool(2, itr->second); + trans->Append(stmt); + } +} + +bool CollectionMgr::UpdateAccountToys(uint32 itemId, bool isFavourite /*= false*/) +{ + return _toys.insert(ToyBoxContainer::value_type(itemId, isFavourite)).second; +} + +void CollectionMgr::ToySetFavorite(uint32 itemId, bool favorite) +{ + ToyBoxContainer::iterator itr = _toys.find(itemId); + if (itr == _toys.end()) + return; + + itr->second = favorite; +} diff --git a/src/server/game/Entities/Player/CollectionMgr.h b/src/server/game/Entities/Player/CollectionMgr.h new file mode 100644 index 00000000000..1920447be9c --- /dev/null +++ b/src/server/game/Entities/Player/CollectionMgr.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * + * 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 CollectionMgr_h__ +#define CollectionMgr_h__ + +#include "WorldSession.h" + +typedef std::map<uint32, bool> ToyBoxContainer; + +class CollectionMgr +{ +public: + explicit CollectionMgr(WorldSession* owner) : _owner(owner) { } + + WorldSession* GetOwner() const { return _owner; } + + // Account-wide toys + void LoadToys(); + void LoadAccountToys(PreparedQueryResult result); + void SaveAccountToys(SQLTransaction& trans); + void ToySetFavorite(uint32 itemId, bool favorite); + + bool AddToy(uint32 itemId, bool isFavourite /*= false*/); + bool UpdateAccountToys(uint32 itemId, bool isFavourite /*= false*/); + + ToyBoxContainer const& GetAccountToys() const { return _toys; } + + // Account-wide heirlooms + // Account-wide mounts + +private: + WorldSession* _owner; + + ToyBoxContainer _toys; +}; + +#endif // CollectionMgr_h__ diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index 9689144ebc9..33f3b3cab16 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -17233,7 +17233,7 @@ bool Player::LoadFromDB(ObjectGuid guid, SQLQueryHolder *holder) _LoadTalents(holder->GetPreparedResult(PLAYER_LOGIN_QUERY_LOAD_TALENTS)); _LoadSpells(holder->GetPreparedResult(PLAYER_LOGIN_QUERY_LOAD_SPELLS)); - _LoadToys(GetSession()->GetAccountToys()); + GetSession()->GetCollectionMgr()->LoadToys(); LearnSpecializationSpells(); @@ -18374,23 +18374,6 @@ void Player::_LoadSpells(PreparedQueryResult result) } } -void Player::_LoadToys(ToyBoxContainer const& toys) -{ - for (auto const& t : toys) - AddDynamicValue(PLAYER_DYNAMIC_FIELD_TOYS, t.first); -} - -bool Player::AddToy(uint32 itemId, bool isFavourite /*= false*/) -{ - if (GetSession()->UpdateAccountToys(itemId, isFavourite)) - { - AddDynamicValue(PLAYER_DYNAMIC_FIELD_TOYS, itemId); - return true; - } - - return false; -} - void Player::_LoadGroup(PreparedQueryResult result) { //QueryResult* result = CharacterDatabase.PQuery("SELECT guid FROM group_member WHERE memberGuid=%u", GetGUIDLow()); @@ -19193,7 +19176,7 @@ void Player::SaveToDB(bool create /*=false*/) // TODO: Move this out trans = LoginDatabase.BeginTransaction(); - GetSession()->SaveAccountToys(trans); + GetSession()->GetCollectionMgr()->SaveAccountToys(trans); GetSession()->GetBattlePetMgr()->SaveToDB(trans); LoginDatabase.CommitTransaction(trans); @@ -22645,7 +22628,7 @@ void Player::SendInitialPacketsBeforeAddToMap() // SMSG_ACCOUNT_TOYS_UPDATE WorldPackets::Toy::AccountToysUpdate toysUpdate; toysUpdate.IsFullUpdate = true; - toysUpdate.Toys = &GetSession()->GetAccountToys(); + toysUpdate.Toys = &GetSession()->GetCollectionMgr()->GetAccountToys(); SendDirectMessage(toysUpdate.Write()); WorldPackets::Character::InitialSetup initialSetup; diff --git a/src/server/game/Entities/Player/Player.h b/src/server/game/Entities/Player/Player.h index 559f1f28570..20f6e201f0f 100644 --- a/src/server/game/Entities/Player/Player.h +++ b/src/server/game/Entities/Player/Player.h @@ -2632,8 +2632,6 @@ class Player : public Unit, public GridObject<Player> VoidStorageItem* GetVoidStorageItem(uint8 slot) const; VoidStorageItem* GetVoidStorageItem(uint64 id, uint8& slot) const; - bool AddToy(uint32 itemId, bool isFavourite /*= false*/); - void OnCombatExit(); void CreateGarrison(uint32 garrSiteId); @@ -2711,7 +2709,6 @@ class Player : public Unit, public GridObject<Player> void _LoadGroup(PreparedQueryResult result); void _LoadSkills(PreparedQueryResult result); void _LoadSpells(PreparedQueryResult result); - void _LoadToys(ToyBoxContainer const& toys); void _LoadFriendList(PreparedQueryResult result); bool _LoadHomeBind(PreparedQueryResult result); void _LoadDeclinedNames(PreparedQueryResult result); diff --git a/src/server/game/Handlers/ToyHandler.cpp b/src/server/game/Handlers/ToyHandler.cpp index 89ed9426c0f..a68c2ca5064 100644 --- a/src/server/game/Handlers/ToyHandler.cpp +++ b/src/server/game/Handlers/ToyHandler.cpp @@ -41,7 +41,7 @@ void WorldSession::HandleAddToy(WorldPackets::Toy::AddToy& packet) return; } - if (_player->AddToy(item->GetEntry(), false)) + if (_collectionMgr->AddToy(item->GetEntry(), false)) _player->DestroyItem(item->GetBagSlot(), item->GetSlot(), true); } @@ -74,9 +74,5 @@ void WorldSession::HandleUseToy(WorldPackets::Toy::UseToy& packet) void WorldSession::HandleToySetFavorite(WorldPackets::Toy::ToySetFavorite& packet) { - ToyBoxContainer::iterator itr = _toys.find(packet.ItemID); - if (itr == _toys.end()) - return; - - itr->second = packet.Favorite; + _collectionMgr->ToySetFavorite(packet.ItemID, packet.Favorite); } diff --git a/src/server/game/Server/Packets/ToyPackets.h b/src/server/game/Server/Packets/ToyPackets.h index 11b0f47ddda..1fb0b7ddaf0 100644 --- a/src/server/game/Server/Packets/ToyPackets.h +++ b/src/server/game/Server/Packets/ToyPackets.h @@ -21,6 +21,7 @@ #include "Packet.h" #include "ObjectGuid.h" #include "SpellPackets.h" +#include "CollectionMgr.h" namespace WorldPackets { diff --git a/src/server/game/Server/WorldSession.cpp b/src/server/game/Server/WorldSession.cpp index 4e60c366a66..79d1eb4ea12 100644 --- a/src/server/game/Server/WorldSession.cpp +++ b/src/server/game/Server/WorldSession.cpp @@ -50,6 +50,7 @@ #include "ChatPackets.h" #include "BattlePetMgr.h" #include "PacketUtilities.h" +#include "CollectionMgr.h" #include <zlib.h> @@ -132,7 +133,8 @@ WorldSession::WorldSession(uint32 id, std::string&& name, uint32 battlenetAccoun expireTime(60000), // 1 min after socket loss, session is deleted forceExit(false), m_currentBankerGUID(), - _battlePetMgr(Trinity::make_unique<BattlePetMgr>(this)) + _battlePetMgr(Trinity::make_unique<BattlePetMgr>(this)), + _collectionMgr(Trinity::make_unique<CollectionMgr>(this)) { memset(_tutorials, 0, sizeof(_tutorials)); @@ -750,40 +752,6 @@ void WorldSession::LoadAccountData(PreparedQueryResult result, uint32 mask) while (result->NextRow()); } -void WorldSession::LoadAccountToys(PreparedQueryResult result) -{ - if (!result) - return; - - do - { - Field* fields = result->Fetch(); - uint32 itemId = fields[0].GetUInt32(); - bool isFavourite = fields[1].GetBool(); - - _toys[itemId] = isFavourite; - } - while (result->NextRow()); -} - -void WorldSession::SaveAccountToys(SQLTransaction& trans) -{ - PreparedStatement* stmt = NULL; - for (ToyBoxContainer::const_iterator itr = _toys.begin(); itr != _toys.end(); ++itr) - { - stmt = LoginDatabase.GetPreparedStatement(LOGIN_REP_ACCOUNT_TOYS); - stmt->setUInt32(0, GetBattlenetAccountId()); - stmt->setUInt32(1, itr->first); - stmt->setBool(2, itr->second); - trans->Append(stmt); - } -} - -bool WorldSession::UpdateAccountToys(uint32 itemId, bool isFavourite /*= false*/) -{ - return _toys.insert(ToyBoxContainer::value_type(itemId, isFavourite)).second; -} - void WorldSession::SetAccountData(AccountDataType type, uint32 time, std::string const& data) { if ((1 << type) & GLOBAL_CACHE_MASK) @@ -1248,7 +1216,7 @@ void WorldSession::InitializeSessionCallback(SQLQueryHolder* realmHolder, SQLQue { LoadAccountData(realmHolder->GetPreparedResult(AccountInfoQueryHolderPerRealm::GLOBAL_ACCOUNT_DATA), GLOBAL_CACHE_MASK); LoadTutorialsData(realmHolder->GetPreparedResult(AccountInfoQueryHolderPerRealm::TUTORIALS)); - LoadAccountToys(holder->GetPreparedResult(AccountInfoQueryHolder::GLOBAL_ACCOUNT_TOYS)); + _collectionMgr->LoadAccountToys(holder->GetPreparedResult(AccountInfoQueryHolder::GLOBAL_ACCOUNT_TOYS)); if (!m_inQueue) SendAuthResponse(AUTH_OK, false); diff --git a/src/server/game/Server/WorldSession.h b/src/server/game/Server/WorldSession.h index 54c794ad0ca..715baff20ac 100644 --- a/src/server/game/Server/WorldSession.h +++ b/src/server/game/Server/WorldSession.h @@ -35,6 +35,7 @@ class BattlePetMgr; class Channel; +class CollectionMgr; class Creature; class GameObject; class InstanceSave; @@ -683,8 +684,6 @@ enum Tutorials #define MAX_ACCOUNT_TUTORIAL_VALUES 8 -typedef std::map<uint32, bool> ToyBoxContainer; - struct AccountData { time_t Time = 0; @@ -908,12 +907,6 @@ class WorldSession void SetAccountData(AccountDataType type, uint32 time, std::string const& data); void LoadAccountData(PreparedQueryResult result, uint32 mask); - // Account Toys - void LoadAccountToys(PreparedQueryResult result); - void SaveAccountToys(SQLTransaction& trans); - bool UpdateAccountToys(uint32 itemId, bool isFavourite /*= false*/); - ToyBoxContainer const& GetAccountToys() const { return _toys; } - void LoadTutorialsData(PreparedQueryResult result); void SendTutorialsData(); void SaveTutorialsData(SQLTransaction& trans); @@ -1003,6 +996,8 @@ class WorldSession // Battle Pets BattlePetMgr* GetBattlePetMgr() const { return _battlePetMgr.get(); } + CollectionMgr* GetCollectionMgr() const { return _collectionMgr.get(); } + public: // opcodes handlers void Handle_NULL(WorldPackets::Null& null); // not used @@ -1718,7 +1713,8 @@ class WorldSession uint32 expireTime; bool forceExit; ObjectGuid m_currentBankerGUID; - ToyBoxContainer _toys; + + std::unique_ptr<CollectionMgr> _collectionMgr; std::unique_ptr<BattlePetMgr> _battlePetMgr; |