TrinityCore
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 "Player.h"
23#include "Timer.h"
24#include "WorldSession.h"
25#include <unordered_map>
26
27namespace
28{
29 std::unordered_map<ObjectGuid, Petition> _petitionStore;
30}
31
33{
34 static PetitionMgr instance;
35 return &instance;
36}
37
39{
40 uint32 oldMSTime = getMSTime();
41 _petitionStore.clear();
42
43 QueryResult result = CharacterDatabase.Query("SELECT petitionguid, ownerguid, name FROM petition");
44 if (!result)
45 {
46 TC_LOG_INFO("server.loading", ">> Loaded 0 petitions.");
47 return;
48 }
49
50 uint32 count = 0;
51 do
52 {
53 Field* fields = result->Fetch();
54 AddPetition(ObjectGuid::Create<HighGuid::Item>(fields[0].GetUInt64()), ObjectGuid::Create<HighGuid::Player>(fields[1].GetUInt64()), fields[2].GetString(), true);
55 ++count;
56 } while (result->NextRow());
57
58 TC_LOG_INFO("server.loading", ">> Loaded {} petitions in: {} ms.", count, GetMSTimeDiffToNow(oldMSTime));
59}
60
62{
63 uint32 oldMSTime = getMSTime();
64
65 QueryResult result = CharacterDatabase.Query("SELECT petitionguid, player_account, playerguid FROM petition_sign");
66 if (!result)
67 {
68 TC_LOG_INFO("server.loading", ">> Loaded 0 Petition signs!");
69 return;
70 }
71
72 uint32 count = 0;
73 do
74 {
75 Field* fields = result->Fetch();
76
77 Petition* petition = GetPetition(ObjectGuid::Create<HighGuid::Item>(fields[0].GetUInt64()));
78 if (!petition)
79 continue;
80
81 petition->AddSignature(fields[1].GetUInt32(), ObjectGuid::Create<HighGuid::Player>(fields[2].GetUInt64()), true);
82 ++count;
83 } while (result->NextRow());
84
85 TC_LOG_INFO("server.loading", ">> Loaded {} Petition signs in {} ms.", count, GetMSTimeDiffToNow(oldMSTime));
86}
87
88void PetitionMgr::AddPetition(ObjectGuid petitionGuid, ObjectGuid ownerGuid, std::string const& name, bool isLoading)
89{
90 Petition& p = _petitionStore[petitionGuid];
91 p.PetitionGuid = petitionGuid;
92 p.OwnerGuid = ownerGuid;
93 p.PetitionName = name;
94 p.Signatures.clear();
95
96 if (isLoading)
97 return;
98
100 stmt->setUInt64(0, ownerGuid.GetCounter());
101 stmt->setUInt64(1, petitionGuid.GetCounter());
102 stmt->setString(2, name);
103 CharacterDatabase.Execute(stmt);
104}
105
107{
108 _petitionStore.erase(petitionGuid);
109
110 // Delete From DB
111 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
112
114 stmt->setUInt64(0, petitionGuid.GetCounter());
115 trans->Append(stmt);
116
117 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_SIGNATURE_BY_GUID);
118 stmt->setUInt64(0, petitionGuid.GetCounter());
119 trans->Append(stmt);
120
121 CharacterDatabase.CommitTransaction(trans);
122}
123
125{
126 auto itr = _petitionStore.find(petitionGuid);
127 if (itr != _petitionStore.end())
128 return &itr->second;
129
130 return nullptr;
131}
132
134{
135 for (auto& petitionPair : _petitionStore)
136 if (petitionPair.second.OwnerGuid == ownerGuid)
137 return &petitionPair.second;
138
139 return nullptr;
140}
141
143{
144 for (auto itr = _petitionStore.begin(); itr != _petitionStore.end();)
145 {
146 if (itr->second.OwnerGuid == ownerGuid)
147 {
148 _petitionStore.erase(itr);
149 break;
150 }
151 else
152 ++itr;
153 }
154
156 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
157 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_BY_OWNER);
158 stmt->setUInt32(0, ownerGuid.GetCounter());
159 trans->Append(stmt);
160
161 stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_PETITION_SIGNATURE_BY_OWNER);
162 stmt->setUInt32(0, ownerGuid.GetCounter());
163 trans->Append(stmt);
164 CharacterDatabase.CommitTransaction(trans);
165}
166
168{
169 for (auto& petitionPair : _petitionStore)
170 petitionPair.second.RemoveSignatureBySigner(signerGuid);
171
173 stmt->setUInt32(0, signerGuid.GetCounter());
174 CharacterDatabase.Execute(stmt);
175}
176
178{
179 for (Signature const& signature : Signatures)
180 if (signature.first == accountId)
181 return true;
182
183 return false;
184}
185
186void Petition::AddSignature(uint32 accountId, ObjectGuid playerGuid, bool isLoading)
187{
188 Signatures.emplace_back(accountId, playerGuid);
189
190 if (isLoading)
191 return;
192
194
195 stmt->setUInt64(0, OwnerGuid.GetCounter());
197 stmt->setUInt64(2, playerGuid.GetCounter());
198 stmt->setUInt32(3, accountId);
199
200 CharacterDatabase.Execute(stmt);
201}
202
203void Petition::UpdateName(std::string const& newName)
204{
205 PetitionName = newName;
206
208 stmt->setString(0, newName);
210 CharacterDatabase.Execute(stmt);
211}
212
214{
215 for (auto itr = Signatures.begin(); itr != Signatures.end(); ++itr)
216 {
217 if (itr->second == playerGuid)
218 {
219 Signatures.erase(itr);
220
221 // notify owner
223 owner->GetSession()->SendPetitionQueryOpcode(PetitionGuid);
224
225 break;
226 }
227 }
228}
@ CHAR_INS_PETITION_SIGNATURE
@ CHAR_DEL_PETITION_BY_GUID
@ CHAR_DEL_PETITION_SIGNATURE_BY_OWNER
@ CHAR_DEL_ALL_PETITION_SIGNATURES
@ CHAR_UPD_PETITION_NAME
@ CHAR_INS_PETITION
@ CHAR_DEL_PETITION_SIGNATURE_BY_GUID
@ CHAR_DEL_PETITION_BY_OWNER
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
std::pair< uint32, ObjectGuid > Signature
Definition: PetitionMgr.h:50
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:90
LowType GetCounter() const
Definition: ObjectGuid.h:293
Petition * GetPetition(ObjectGuid petitionGuid)
Petition * GetPetitionByOwner(ObjectGuid ownerGuid)
static PetitionMgr * instance()
Definition: PetitionMgr.cpp:32
void RemoveSignaturesBySigner(ObjectGuid signerGuid)
void LoadPetitions()
Definition: PetitionMgr.cpp:38
void RemovePetitionsByOwner(ObjectGuid ownerGuid)
void AddPetition(ObjectGuid petitionGuid, ObjectGuid ownerGuid, std::string const &name, bool isLoading)
Definition: PetitionMgr.cpp:88
void LoadSignatures()
Definition: PetitionMgr.cpp:61
void RemovePetition(ObjectGuid petitionGuid)
void setUInt32(const uint8 index, const uint32 value)
void setString(const uint8 index, const std::string &value)
void setUInt64(const uint8 index, const uint64 value)
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)
void UpdateName(std::string const &newName)
void RemoveSignatureBySigner(ObjectGuid playerGuid)
ObjectGuid OwnerGuid
Definition: PetitionMgr.h:56
SignaturesVector Signatures
Definition: PetitionMgr.h:58
bool IsPetitionSignedByAccount(uint32 accountId) const
ObjectGuid PetitionGuid
Definition: PetitionMgr.h:55
void AddSignature(uint32 accountId, ObjectGuid playerGuid, bool isLoading)
std::string PetitionName
Definition: PetitionMgr.h:57