TrinityCore
Loading...
Searching...
No Matches
CharacterDatabaseCleaner.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 "Common.h"
20#include "DatabaseEnv.h"
21#include "DBCStores.h"
22#include "Log.h"
23#include "SpellMgr.h"
24#include "World.h"
25
27{
28 // config to disable
29 if (!sWorld->getBoolConfig(CONFIG_CLEAN_CHARACTER_DB))
30 return;
31
32 TC_LOG_INFO("misc", "Cleaning character database...");
33
34 uint32 oldMSTime = getMSTime();
35
36 // check flags which clean ups are necessary
37 QueryResult result = CharacterDatabase.PQuery("SELECT value FROM worldstates WHERE entry = {}", WS_CLEANING_FLAGS);
38 if (!result)
39 return;
40
41 uint32 flags = (*result)[0].GetUInt32();
42
43 // clean up
46
49
52
55
58
59 // NOTE: In order to have persistentFlags be set in worldstates for the next cleanup,
60 // you need to define them at least once in worldstates.
62 CharacterDatabase.DirectPExecute("UPDATE worldstates SET value = {} WHERE entry = {}", flags, WS_CLEANING_FLAGS);
63
64 sWorld->SetCleaningFlags(flags);
65
66 TC_LOG_INFO("server.loading", ">> Cleaned character database in {} ms", GetMSTimeDiffToNow(oldMSTime));
67}
68
69void CharacterDatabaseCleaner::CheckUnique(char const* column, char const* table, bool (*check)(uint32))
70{
71 QueryResult result = CharacterDatabase.PQuery("SELECT DISTINCT {} FROM {}", column, table);
72 if (!result)
73 {
74 TC_LOG_INFO("misc", "Table {} is empty.", table);
75 return;
76 }
77
78 bool found = false;
79 std::ostringstream ss;
80 do
81 {
82 Field* fields = result->Fetch();
83
84 uint32 id = fields[0].GetUInt32();
85
86 if (!check(id))
87 {
88 if (!found)
89 {
90 ss << "DELETE FROM " << table << " WHERE " << column << " IN (";
91 found = true;
92 }
93 else
94 ss << ',';
95
96 ss << id;
97 }
98 }
99 while (result->NextRow());
100
101 if (found)
102 {
103 ss << ')';
104 CharacterDatabase.Execute(ss.str().c_str());
105 }
106}
107
109{
110 return sAchievementCriteriaStore.LookupEntry(criteria) != nullptr;
111}
112
114{
115 CheckUnique("criteria", "character_achievement_progress", &AchievementProgressCheck);
116}
117
119{
120 return sSkillLineStore.LookupEntry(skill) != nullptr;
121}
122
124{
125 CheckUnique("skill", "character_skills", &SkillCheck);
126}
127
129{
130 return sSpellMgr->GetSpellInfo(spell_id) && !GetTalentSpellPos(spell_id);
131}
132
134{
135 CheckUnique("spell", "character_spell", &SpellCheck);
136}
137
139{
140 TalentEntry const* talentInfo = sTalentStore.LookupEntry(talent_id);
141 if (!talentInfo)
142 return false;
143
144 return sTalentTabStore.LookupEntry(talentInfo->TabID) != nullptr;
145}
146
148{
149 CharacterDatabase.DirectPExecute("DELETE FROM character_talent WHERE talentGroup > {}", MAX_TALENT_GROUPS);
150 CheckUnique("spell", "character_talent", &TalentCheck);
151}
152
154{
155 CharacterDatabase.DirectExecute("DELETE FROM character_queststatus WHERE status = 0");
156}
DBCStorage< AchievementCriteriaEntry > sAchievementCriteriaStore(AchievementCriteriafmt)
DBCStorage< TalentTabEntry > sTalentTabStore(TalentTabEntryfmt)
TalentSpellPos const * GetTalentSpellPos(uint32 spellId)
DBCStorage< SkillLineEntry > sSkillLineStore(SkillLinefmt)
DBCStorage< TalentEntry > sTalentStore(TalentEntryfmt)
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
uint32_t uint32
Definition Define.h:133
uint16 flags
#define TC_LOG_INFO(filterType__,...)
Definition Log.h:159
#define MAX_TALENT_GROUPS
@ WS_CLEANING_FLAGS
#define sSpellMgr
Definition SpellMgr.h:738
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
uint32 GetUInt32() const
Definition Field.cpp:61
#define sWorld
Definition World.h:900
@ CONFIG_PERSISTENT_CHARACTER_CLEAN_FLAGS
Definition World.h:354
@ CONFIG_CLEAN_CHARACTER_DB
Definition World.h:93
TC_GAME_API bool SpellCheck(uint32 spell_id)
TC_GAME_API bool SkillCheck(uint32 skill)
TC_GAME_API void CheckUnique(char const *column, char const *table, bool(*check)(uint32))
TC_GAME_API bool TalentCheck(uint32 talent_id)
TC_GAME_API void CleanCharacterAchievementProgress()
TC_GAME_API bool AchievementProgressCheck(uint32 criteria)