TrinityCore
Loading...
Searching...
No Matches
PetitionMgr.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "PetitionMgr.h"
19#include "DatabaseEnv.h"
20#include "Log.h"
21#include "ObjectAccessor.h"
22#include "ObjectMgr.h"
23#include "Player.h"
24#include "Timer.h"
25#include "WorldSession.h"
26#include <unordered_map>
27
28namespace
29{
30 std::unordered_map<ObjectGuid, Petition> _petitionStore;
31}
32
38
40{
41 uint32 oldMSTime = getMSTime();
42 _petitionStore.clear();
43
44 QueryResult result = CharacterDatabase.Query("SELECT petitionguid, ownerguid, name, type FROM petition");
45 if (!result)
46 {
47 TC_LOG_INFO("server.loading", ">> Loaded 0 petitions.");
48 return;
49 }
50
51 uint32 count = 0;
52 do
53 {
54 Field* fields = result->Fetch();
55 AddPetition(ObjectGuid::Create<HighGuid::Item>(fields[0].GetUInt32()), ObjectGuid::Create<HighGuid::Player>(fields[1].GetUInt32()), fields[2].GetString(), static_cast<CharterTypes>(fields[3].GetUInt8()), true);
56 ++count;
57 } while (result->NextRow());
58
59 TC_LOG_INFO("server.loading", ">> Loaded {} petitions in: {} ms.", count, GetMSTimeDiffToNow(oldMSTime));
60}
61
63{
64 uint32 oldMSTime = getMSTime();
65
66 QueryResult result = CharacterDatabase.Query("SELECT petitionguid, player_account, playerguid FROM petition_sign");
67 if (!result)
68 {
69 TC_LOG_INFO("server.loading", ">> Loaded 0 Petition signs!");
70 return;
71 }
72
73 uint32 count = 0;
74 do
75 {
76 Field* fields = result->Fetch();
77
78 Petition* petition = GetPetition(ObjectGuid::Create<HighGuid::Item>(fields[0].GetUInt32()));
79 if (!petition)
80 continue;
81
82 petition->AddSignature(fields[1].GetUInt32(), ObjectGuid::Create<HighGuid::Player>(fields[2].GetUInt32()), true);
83 ++count;
84 } while (result->NextRow());
85
86 TC_LOG_INFO("server.loading", ">> Loaded {} Petition signs in {} ms.", count, GetMSTimeDiffToNow(oldMSTime));
87}
88
89void PetitionMgr::AddPetition(ObjectGuid petitionGuid, ObjectGuid ownerGuid, std::string const& name, CharterTypes type, bool isLoading)
90{
91 Petition& p = _petitionStore[petitionGuid];
92 p.PetitionGuid = petitionGuid;
93 p.OwnerGuid = ownerGuid;
94 p.PetitionName = name;
95 p.PetitionType = type;
96 p.Signatures.clear();
97
98 if (isLoading)
99 return;
100
102 stmt->setUInt32(0, ownerGuid.GetCounter());
103 stmt->setUInt32(1, petitionGuid.GetCounter());
104 stmt->setString(2, name);
105 stmt->setUInt8(3, uint8(type));
106 CharacterDatabase.Execute(stmt);
107}
108
110{
111 _petitionStore.erase(petitionGuid);
112
113 // Delete From DB
114 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
115
117 stmt->setUInt32(0, petitionGuid.GetCounter());
118 trans->Append(stmt);
119
120 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_SIGNATURE_BY_GUID);
121 stmt->setUInt32(0, petitionGuid.GetCounter());
122 trans->Append(stmt);
123
124 CharacterDatabase.CommitTransaction(trans);
125}
126
128{
129 auto itr = _petitionStore.find(petitionGuid);
130 if (itr != _petitionStore.end())
131 return &itr->second;
132
133 return nullptr;
134}
135
137{
138 for (auto& petitionPair : _petitionStore)
139 if (petitionPair.second.OwnerGuid == ownerGuid && petitionPair.second.PetitionType == type)
140 return &petitionPair.second;
141
142 return nullptr;
143}
144
146{
147 for (auto itr = _petitionStore.begin(); itr != _petitionStore.end();)
148 {
149 if (itr->second.OwnerGuid == ownerGuid)
150 {
151 if (type == CHARTER_TYPE_ANY)
152 itr = _petitionStore.erase(itr);
153 else if (type == itr->second.PetitionType)
154 {
155 itr = _petitionStore.erase(itr);
156 break;
157 }
158 else
159 ++itr;
160 }
161 else
162 ++itr;
163 }
164
166 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
167 if (type == CHARTER_TYPE_ANY)
168 {
169 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_BY_OWNER);
170 stmt->setUInt32(0, ownerGuid.GetCounter());
171 trans->Append(stmt);
172
173 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_SIGNATURE_BY_OWNER);
174 stmt->setUInt32(0, ownerGuid.GetCounter());
175 trans->Append(stmt);
176 }
177 else
178 {
179 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_BY_OWNER_AND_TYPE);
180 stmt->setUInt32(0, ownerGuid.GetCounter());
181 stmt->setUInt8(1, uint8(type));
182 trans->Append(stmt);
183
185 stmt->setUInt32(0, ownerGuid.GetCounter());
186 stmt->setUInt8(1, uint8(type));
187 trans->Append(stmt);
188 }
189 CharacterDatabase.CommitTransaction(trans);
190}
191
193{
194 for (auto& petitionPair : _petitionStore)
195 {
196 if (petitionPair.second.PetitionType == CHARTER_TYPE_ANY || petitionPair.second.PetitionType == type)
197 petitionPair.second.RemoveSignatureBySigner(signerGuid);
198 }
199
201 if (type == CHARTER_TYPE_ANY)
202 {
203 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_ALL_PETITION_SIGNATURES);
204 stmt->setUInt32(0, signerGuid.GetCounter());
205 }
206 else
207 {
208 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_SIGNATURE);
209 stmt->setUInt32(0, signerGuid.GetCounter());
210 stmt->setUInt8(1, uint8(type));
211 }
212 CharacterDatabase.Execute(stmt);
213}
214
216{
217 for (Signature const& signature : Signatures)
218 if (signature.first == accountId)
219 return true;
220
221 return false;
222}
223
224void Petition::AddSignature(uint32 accountId, ObjectGuid playerGuid, bool isLoading)
225{
226 Signatures.emplace_back(accountId, playerGuid);
227
228 if (isLoading)
229 return;
230
232
233 stmt->setUInt32(0, OwnerGuid.GetCounter());
235 stmt->setUInt32(2, playerGuid.GetCounter());
236 stmt->setUInt32(3, accountId);
237
238 CharacterDatabase.Execute(stmt);
239}
240
241void Petition::UpdateName(std::string const& newName)
242{
243 PetitionName = newName;
244
246 stmt->setString(0, newName);
248 CharacterDatabase.Execute(stmt);
249}
250
252{
253 for (auto itr = Signatures.begin(); itr != Signatures.end(); ++itr)
254 {
255 if (itr->second == playerGuid)
256 {
257 Signatures.erase(itr);
258
259 // notify owner
261 owner->GetSession()->SendPetitionQueryOpcode(PetitionGuid);
262
263 break;
264 }
265 }
266}
@ CHAR_INS_PETITION_SIGNATURE
@ CHAR_DEL_PETITION_BY_GUID
@ CHAR_DEL_PETITION_SIGNATURE
@ CHAR_DEL_PETITION_SIGNATURE_BY_OWNER
@ CHAR_DEL_ALL_PETITION_SIGNATURES
@ CHAR_UPD_PETITION_NAME
@ CHAR_DEL_PETITION_SIGNATURE_BY_OWNER_AND_TYPE
@ CHAR_INS_PETITION
@ CHAR_DEL_PETITION_SIGNATURE_BY_GUID
@ CHAR_DEL_PETITION_BY_OWNER
@ CHAR_DEL_PETITION_BY_OWNER_AND_TYPE
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
#define TC_LOG_INFO(filterType__,...)
Definition Log.h:159
std::pair< uint32, ObjectGuid > Signature
Definition PetitionMgr.h:48
CharterTypes
@ CHARTER_TYPE_ANY
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:57
uint32 getMSTime()
Definition Timer.h:33
Class used to access individual fields of database query result.
Definition Field.h:92
LowType GetCounter() const
Definition ObjectGuid.h:156
void AddPetition(ObjectGuid petitionGuid, ObjectGuid ownerGuid, std::string const &name, CharterTypes type, bool isLoading)
Petition * GetPetition(ObjectGuid petitionGuid)
static PetitionMgr * instance()
void LoadPetitions()
void RemoveSignaturesBySignerAndType(ObjectGuid signerGuid, CharterTypes type)
Petition * GetPetitionByOwnerWithType(ObjectGuid ownerGuid, CharterTypes type)
void LoadSignatures()
void RemovePetition(ObjectGuid petitionGuid)
void RemovePetitionsByOwnerAndType(ObjectGuid ownerGuid, CharterTypes type)
void setUInt32(uint8 index, uint32 value)
void setUInt8(uint8 index, uint8 value)
void setString(uint8 index, std::string const &value)
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)
void UpdateName(std::string const &newName)
void RemoveSignatureBySigner(ObjectGuid playerGuid)
ObjectGuid OwnerGuid
Definition PetitionMgr.h:54
SignaturesVector Signatures
Definition PetitionMgr.h:57
CharterTypes PetitionType
Definition PetitionMgr.h:55
bool IsPetitionSignedByAccount(uint32 accountId) const
ObjectGuid PetitionGuid
Definition PetitionMgr.h:53
void AddSignature(uint32 accountId, ObjectGuid playerGuid, bool isLoading)
std::string PetitionName
Definition PetitionMgr.h:56