TrinityCore
CharacterTemplateDataStore.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
19#include "DatabaseEnv.h"
20#include "DB2Stores.h"
21#include "Log.h"
22#include "Timer.h"
23
24namespace
25{
26 CharacterTemplateContainer _characterTemplateStore;
27}
28
30{
31 uint32 oldMSTime = getMSTime();
32 _characterTemplateStore.clear();
33
34 std::unordered_map<uint32, std::vector<CharacterTemplateClass>> characterTemplateClasses;
35
36 if (QueryResult classesResult = WorldDatabase.Query("SELECT TemplateId, FactionGroup, Class FROM character_template_class"))
37 {
38 do
39 {
40 Field* fields = classesResult->Fetch();
41
42 uint32 templateId = fields[0].GetUInt32();
43 uint8 factionGroup = fields[1].GetUInt8();
44 uint8 classID = fields[2].GetUInt8();
45
48 {
49 TC_LOG_ERROR("sql.sql", "Faction group {} defined for character template {} in `character_template_class` is invalid. Skipped.", factionGroup, templateId);
50 continue;
51 }
52
53 ChrClassesEntry const* classEntry = sChrClassesStore.LookupEntry(classID);
54 if (!classEntry)
55 {
56 TC_LOG_ERROR("sql.sql", "Class {} defined for character template {} in `character_template_class` does not exists, skipped.", classID, templateId);
57 continue;
58 }
59
60 characterTemplateClasses[templateId].emplace_back(factionGroup, classID);
61 }
62 while (classesResult->NextRow());
63 }
64 else
65 {
66 TC_LOG_INFO("server.loading", ">> Loaded 0 character template classes. DB table `character_template_class` is empty.");
67 }
68
69 QueryResult templates = WorldDatabase.Query("SELECT Id, Name, Description, Level FROM character_template");
70 if (!templates)
71 {
72 TC_LOG_INFO("server.loading", ">> Loaded 0 character templates. DB table `character_template` is empty.");
73 return;
74 }
75
76 do
77 {
78 Field* fields = templates->Fetch();
79
81 templ.TemplateSetId = fields[0].GetUInt32();
82 templ.Name = fields[1].GetString();
83 templ.Description = fields[2].GetString();
84 templ.Level = fields[3].GetUInt8();
85 templ.Classes = std::move(characterTemplateClasses[templ.TemplateSetId]);
86
87 if (templ.Classes.empty())
88 {
89 TC_LOG_ERROR("sql.sql", "Character template {} does not have any classes defined in `character_template_class`. Skipped.", templ.TemplateSetId);
90 continue;
91 }
92
93 _characterTemplateStore[templ.TemplateSetId] = templ;
94 }
95 while (templates->NextRow());
96
97 TC_LOG_INFO("server.loading", ">> Loaded {} character templates in {} ms.", _characterTemplateStore.size(), GetMSTimeDiffToNow(oldMSTime));
98}
99
101{
102 return _characterTemplateStore;
103}
104
106{
107 auto itr = _characterTemplateStore.find(templateId);
108 if (itr != _characterTemplateStore.end())
109 return &itr->second;
110
111 return nullptr;
112}
113
115{
116 static CharacterTemplateDataStore instance;
117 return &instance;
118}
std::unordered_map< uint32, CharacterTemplate > CharacterTemplateContainer
DB2Storage< ChrClassesEntry > sChrClassesStore("ChrClasses.db2", &ChrClassesLoadInfo::Instance)
@ FACTION_MASK_ALLIANCE
Definition: DBCEnums.h:950
@ FACTION_MASK_HORDE
Definition: DBCEnums.h:951
@ FACTION_MASK_PLAYER
Definition: DBCEnums.h:949
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
uint8_t uint8
Definition: Define.h:144
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
static CharacterTemplateDataStore * Instance()
CharacterTemplateContainer const & GetCharacterTemplates() const
CharacterTemplate const * GetCharacterTemplate(uint32 templateId) const
Class used to access individual fields of database query result.
Definition: Field.h:90
uint8 GetUInt8() const
Definition: Field.cpp:30
std::string GetString() const
Definition: Field.cpp:118
uint32 GetUInt32() const
Definition: Field.cpp:62
std::vector< CharacterTemplateClass > Classes