AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
PetitionMgr Class Reference

#include "PetitionMgr.h"

Public Member Functions

void LoadPetitions ()
 
void LoadSignatures ()
 
void AddPetition (ObjectGuid petitionGUID, ObjectGuid ownerGuid, std::string const &name, uint8 type, uint32 petitionId)
 
void RemovePetition (ObjectGuid petitionGUID)
 
void RemovePetitionByOwnerAndType (ObjectGuid ownerGuid, uint8 type)
 
Petition const * GetPetition (ObjectGuid petitionGUID) const
 
Petition const * GetPetitionById (uint32 petitionId) const
 
Petition const * GetPetitionByOwnerWithType (ObjectGuid ownerGuid, uint8 type) const
 
PetitionContainer * GetPetitionStore ()
 
void AddSignature (ObjectGuid petitionGUID, uint32 accountId, ObjectGuid playerGuid)
 
void RemoveSignaturesByPlayer (ObjectGuid playerGuid)
 
void RemoveSignaturesByPlayerAndType (ObjectGuid playerGuid, uint8 type)
 
Signatures const * GetSignature (ObjectGuid petitionGUID) const
 
SignatureContainer * GetSignatureStore ()
 
uint32 GeneratePetitionId ()
 
uint32 GetPetitionIdByItemGuid (ObjectGuid petitionItemGuid) const
 
ObjectGuid GetItemGuidByPetitionId (uint32 petitionId) const
 

Static Public Member Functions

static PetitionMgr * instance ()
 

Protected Attributes

PetitionContainer PetitionStore
 
SignatureContainer SignatureStore
 
std::unordered_map< uint32, ObjectGuid > PetitionIdToItemGuid
 
uint32 _nextPetitionId = 1
 

Private Member Functions

 PetitionMgr ()
 
 ~PetitionMgr ()
 

Detailed Description

Constructor & Destructor Documentation

◆ PetitionMgr()

PetitionMgr::PetitionMgr ( )
private
27{
28}

◆ ~PetitionMgr()

PetitionMgr::~PetitionMgr ( )
private
31{
32}

Member Function Documentation

◆ AddPetition()

void PetitionMgr::AddPetition ( ObjectGuid  petitionGUID,
ObjectGuid  ownerGuid,
std::string const &  name,
uint8  type,
uint32  petitionId 
)
107{
108 Petition& p = PetitionStore[petitionGUID];
109 p.petitionGuid = petitionGUID;
110 p.petitionId = petitionId;
111 p.ownerGuid = ownerGuid;
112 p.petitionName = name;
113 p.petitionType = type;
114
115 Signatures& s = SignatureStore[petitionGUID];
116 s.petitionGuid = petitionGUID;
117 s.signatureMap.clear();
118}
PetitionContainer PetitionStore
Definition PetitionMgr.h:96
SignatureContainer SignatureStore
Definition PetitionMgr.h:97
Definition PetitionMgr.h:40
ObjectGuid petitionGuid
Definition PetitionMgr.h:42
ObjectGuid ownerGuid
Definition PetitionMgr.h:46
std::string petitionName
Definition PetitionMgr.h:50
uint8 petitionType
Definition PetitionMgr.h:48
uint32 petitionId
Definition PetitionMgr.h:44
Definition PetitionMgr.h:54
SignatureMap signatureMap
Definition PetitionMgr.h:57
ObjectGuid petitionGuid
Definition PetitionMgr.h:56

References Petition::ownerGuid, Petition::petitionGuid, Signatures::petitionGuid, Petition::petitionId, Petition::petitionName, PetitionStore, Petition::petitionType, Signatures::signatureMap, and SignatureStore.

Referenced by LoadPetitions().

◆ AddSignature()

void PetitionMgr::AddSignature ( ObjectGuid  petitionGUID,
uint32  accountId,
ObjectGuid  playerGuid 
)
187{
188 Signatures& s = SignatureStore[petitionGUID];
189 s.signatureMap[playerGuid] = accountId;
190}

References Signatures::signatureMap, and SignatureStore.

Referenced by LoadSignatures().

◆ GeneratePetitionId()

uint32 PetitionMgr::GeneratePetitionId ( )
225{
226 // ensure 31-bit range and avoid collisions with already loaded petitions
227 if (_nextPetitionId == 0 || _nextPetitionId >= 0x7FFFFFFF)
228 _nextPetitionId = 1;
229
230 // find first free id
232 {
234 if (_nextPetitionId >= 0x7FFFFFFF)
235 _nextPetitionId = 1;
236 }
237
238 uint32 id = _nextPetitionId++;
239 if (_nextPetitionId >= 0x7FFFFFFF)
240 _nextPetitionId = 1;
241 return id;
242}
std::uint32_t uint32
Definition Define.h:107
std::unordered_map< uint32, ObjectGuid > PetitionIdToItemGuid
Definition PetitionMgr.h:99
uint32 _nextPetitionId
Definition PetitionMgr.h:101

References _nextPetitionId, and PetitionIdToItemGuid.

◆ GetItemGuidByPetitionId()

ObjectGuid PetitionMgr::GetItemGuidByPetitionId ( uint32  petitionId) const
251{
252 auto it = PetitionIdToItemGuid.find(petitionId);
253 if (it == PetitionIdToItemGuid.end())
254 return ObjectGuid::Empty;
255 return it->second;
256}
static ObjectGuid const Empty
Definition ObjectGuid.h:123

References ObjectGuid::Empty, and PetitionIdToItemGuid.

◆ GetPetition()

Petition const * PetitionMgr::GetPetition ( ObjectGuid  petitionGUID) const
161{
162 PetitionContainer::const_iterator itr = PetitionStore.find(petitionGUID);
163 if (itr != PetitionStore.end())
164 return &itr->second;
165 return nullptr;
166}

References PetitionStore.

Referenced by GetPetitionById(), and GetPetitionIdByItemGuid().

◆ GetPetitionById()

Petition const * PetitionMgr::GetPetitionById ( uint32  petitionId) const
169{
170 auto it = PetitionIdToItemGuid.find(petitionId);
171 if (it == PetitionIdToItemGuid.end())
172 return nullptr;
173
174 return GetPetition(it->second);
175}
Petition const * GetPetition(ObjectGuid petitionGUID) const
Definition PetitionMgr.cpp:160

References GetPetition(), and PetitionIdToItemGuid.

◆ GetPetitionByOwnerWithType()

Petition const * PetitionMgr::GetPetitionByOwnerWithType ( ObjectGuid  ownerGuid,
uint8  type 
) const
178{
179 for (PetitionContainer::const_iterator itr = PetitionStore.begin(); itr != PetitionStore.end(); ++itr)
180 if (itr->second.ownerGuid == ownerGuid && itr->second.petitionType == type)
181 return &itr->second;
182
183 return nullptr;
184}

References PetitionStore.

◆ GetPetitionIdByItemGuid()

uint32 PetitionMgr::GetPetitionIdByItemGuid ( ObjectGuid  petitionItemGuid) const
245{
246 Petition const* p = GetPetition(petitionItemGuid);
247 return p ? p->petitionId : 0;
248}

References GetPetition(), and Petition::petitionId.

◆ GetPetitionStore()

PetitionContainer * PetitionMgr::GetPetitionStore ( )
inline
82{ return &PetitionStore; }

References PetitionStore.

◆ GetSignature()

Signatures const * PetitionMgr::GetSignature ( ObjectGuid  petitionGUID) const
193{
194 SignatureContainer::const_iterator itr = SignatureStore.find(petitionGUID);
195 if (itr != SignatureStore.end())
196 return &itr->second;
197 return nullptr;
198}

References SignatureStore.

◆ GetSignatureStore()

SignatureContainer * PetitionMgr::GetSignatureStore ( )
inline
89{ return &SignatureStore; }

References SignatureStore.

◆ instance()

PetitionMgr * PetitionMgr::instance ( )
static
35{
36 static PetitionMgr instance;
37 return &instance;
38}
Definition PetitionMgr.h:64
static PetitionMgr * instance()
Definition PetitionMgr.cpp:34

References instance().

Referenced by instance().

◆ LoadPetitions()

void PetitionMgr::LoadPetitions ( )
41{
42 uint32 oldMSTime = getMSTime();
43 PetitionStore.clear();
45
46 QueryResult result = CharacterDatabase.Query("SELECT ownerguid, petitionguid, name, type, petition_id FROM petition");
47 if (!result)
48 {
49 LOG_WARN("server.loading", ">> Loaded 0 Petitions!");
50 LOG_INFO("server.loading", " ");
51 return;
52 }
53
54 uint32 count = 0;
55 uint32 maxId = 0;
56 do
57 {
58 Field* fields = result->Fetch();
59 uint32 itemLow = fields[1].Get<uint32>();
60 uint32 petitionId = fields[4].Get<uint32>();
61 ObjectGuid itemGuid = ObjectGuid::Create<HighGuid::Item>(itemLow);
62 ObjectGuid ownerGuid = ObjectGuid::Create<HighGuid::Player>(fields[0].Get<uint32>());
63 AddPetition(itemGuid, ownerGuid, fields[2].Get<std::string>(), fields[3].Get<uint8>(), petitionId);
64 PetitionIdToItemGuid[petitionId] = itemGuid;
65 if (petitionId > maxId)
66 maxId = petitionId;
67 ++count;
68 } while (result->NextRow());
69
70 // initialize next id (keep within 31-bit safe range)
71 _nextPetitionId = std::min<uint32>(std::max<uint32>(maxId + 1, 1), 0x7FFFFFFFu);
72
73 LOG_INFO("server.loading", ">> Loaded {} Petitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
74 LOG_INFO("server.loading", " ");
75}
std::shared_ptr< ResultSet > QueryResult
Definition DatabaseEnvFwd.h:27
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition DatabaseEnv.cpp:21
#define LOG_INFO(filterType__,...)
Definition Log.h:153
#define LOG_WARN(filterType__,...)
Definition Log.h:149
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:131
uint32 getMSTime()
Definition Timer.h:103
Class used to access individual fields of database query result.
Definition Field.h:99
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition Field.h:113
Definition ObjectGuid.h:121
void AddPetition(ObjectGuid petitionGUID, ObjectGuid ownerGuid, std::string const &name, uint8 type, uint32 petitionId)
Definition PetitionMgr.cpp:106

References _nextPetitionId, AddPetition(), CharacterDatabase, Field::Get(), getMSTime(), GetMSTimeDiffToNow(), LOG_INFO, LOG_WARN, PetitionIdToItemGuid, and PetitionStore.

◆ LoadSignatures()

void PetitionMgr::LoadSignatures ( )
78{
79 uint32 oldMSTime = getMSTime();
80 SignatureStore.clear();
81
82 QueryResult result = CharacterDatabase.Query("SELECT petition_id, playerguid, player_account FROM petition_sign");
83 if (!result)
84 {
85 LOG_WARN("server.loading", ">> Loaded 0 Petition signs!");
86 LOG_INFO("server.loading", " ");
87 return;
88 }
89
90 uint32 count = 0;
91 do
92 {
93 Field* fields = result->Fetch();
94 uint32 petitionId = fields[0].Get<uint32>();
95 auto it = PetitionIdToItemGuid.find(petitionId);
96 if (it == PetitionIdToItemGuid.end())
97 continue; // orphan signature, skip
98 AddSignature(it->second, fields[2].Get<uint32>(), ObjectGuid::Create<HighGuid::Player>(fields[1].Get<uint32>()));
99 ++count;
100 } while (result->NextRow());
101
102 LOG_INFO("server.loading", ">> Loaded {} Petition signs in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
103 LOG_INFO("server.loading", " ");
104}
void AddSignature(ObjectGuid petitionGUID, uint32 accountId, ObjectGuid playerGuid)
Definition PetitionMgr.cpp:186

References AddSignature(), CharacterDatabase, Field::Get(), getMSTime(), GetMSTimeDiffToNow(), LOG_INFO, LOG_WARN, PetitionIdToItemGuid, and SignatureStore.

◆ RemovePetition()

void PetitionMgr::RemovePetition ( ObjectGuid  petitionGUID)
121{
122 auto it = PetitionStore.find(petitionGUID);
123 if (it != PetitionStore.end())
124 {
125 PetitionIdToItemGuid.erase(it->second.petitionId);
126 PetitionStore.erase(it);
127 }
128
129 // remove signatures
130 SignatureStore.erase(petitionGUID);
131}

References PetitionIdToItemGuid, PetitionStore, and SignatureStore.

◆ RemovePetitionByOwnerAndType()

void PetitionMgr::RemovePetitionByOwnerAndType ( ObjectGuid  ownerGuid,
uint8  type 
)
134{
135 for (PetitionContainer::iterator itr = PetitionStore.begin(); itr != PetitionStore.end();)
136 {
137 if (itr->second.ownerGuid == ownerGuid && (!type || type == itr->second.petitionType))
138 {
139 // Remove invalid charter item
140 if (type == itr->second.petitionType)
141 {
142 if (Player* owner = ObjectAccessor::FindConnectedPlayer(ownerGuid))
143 {
144 if (Item* item = owner->GetItemByGuid(itr->first))
145 {
146 owner->DestroyItem(item->GetBagSlot(), item->GetSlot(), true);
147 }
148 }
149 }
150
151 // remove signatures
152 SignatureStore.erase(itr->first);
153 PetitionStore.erase(itr++);
154 }
155 else
156 ++itr;
157 }
158}
Definition Item.h:220
Definition Player.h:1086
Player * FindConnectedPlayer(ObjectGuid const guid)
Definition ObjectAccessor.cpp:281

References ObjectAccessor::FindConnectedPlayer(), PetitionStore, and SignatureStore.

◆ RemoveSignaturesByPlayer()

void PetitionMgr::RemoveSignaturesByPlayer ( ObjectGuid  playerGuid)
201{
202 for (SignatureContainer::iterator itr = SignatureStore.begin(); itr != SignatureStore.end(); ++itr)
203 {
204 SignatureMap::iterator signItr = itr->second.signatureMap.find(playerGuid);
205 if (signItr != itr->second.signatureMap.end())
206 itr->second.signatureMap.erase(signItr);
207 }
208}

References Signatures::signatureMap, and SignatureStore.

◆ RemoveSignaturesByPlayerAndType()

void PetitionMgr::RemoveSignaturesByPlayerAndType ( ObjectGuid  playerGuid,
uint8  type 
)
211{
212 for (SignatureContainer::iterator itr = SignatureStore.begin(); itr != SignatureStore.end(); ++itr)
213 {
214 Petition const* petition = sPetitionMgr->GetPetition(itr->first);
215 if (!petition || petition->petitionType != type)
216 continue;
217
218 SignatureMap::iterator signItr = itr->second.signatureMap.find(playerGuid);
219 if (signItr != itr->second.signatureMap.end())
220 itr->second.signatureMap.erase(signItr);
221 }
222}
#define sPetitionMgr
Definition PetitionMgr.h:104

References Petition::petitionType, SignatureStore, and sPetitionMgr.

Member Data Documentation

◆ _nextPetitionId

uint32 PetitionMgr::_nextPetitionId = 1
protected

◆ PetitionIdToItemGuid

std::unordered_map<uint32, ObjectGuid> PetitionMgr::PetitionIdToItemGuid
protected

◆ PetitionStore

◆ SignatureStore


The documentation for this class was generated from the following files: