aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Globals/CharacterTemplateDataStore.cpp
blob: 54e56f0f0e8c66743bdf592cd1e0060f2e9e3cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "CharacterTemplateDataStore.h"
#include "DatabaseEnv.h"
#include "DB2Stores.h"
#include "Log.h"
#include "Timer.h"

namespace
{
    CharacterTemplateContainer _characterTemplateStore;
}

void CharacterTemplateDataStore::LoadCharacterTemplates()
{
    uint32 oldMSTime = getMSTime();
    _characterTemplateStore.clear();

    std::unordered_map<uint32, std::vector<CharacterTemplateClass>> characterTemplateClasses;

    if (QueryResult classesResult = WorldDatabase.Query("SELECT TemplateId, FactionGroup, Class FROM character_template_class"))
    {
        do
        {
            Field* fields = classesResult->Fetch();

            uint32 templateId = fields[0].GetUInt32();
            uint8 factionGroup = fields[1].GetUInt8();
            uint8 classID = fields[2].GetUInt8();

            if (!((factionGroup & (FACTION_MASK_PLAYER | FACTION_MASK_ALLIANCE)) == (FACTION_MASK_PLAYER | FACTION_MASK_ALLIANCE)) &&
                !((factionGroup & (FACTION_MASK_PLAYER | FACTION_MASK_HORDE)) == (FACTION_MASK_PLAYER | FACTION_MASK_HORDE)))
            {
                TC_LOG_ERROR("sql.sql", "Faction group {} defined for character template {} in `character_template_class` is invalid. Skipped.", factionGroup, templateId);
                continue;
            }

            ChrClassesEntry const* classEntry = sChrClassesStore.LookupEntry(classID);
            if (!classEntry)
            {
                TC_LOG_ERROR("sql.sql", "Class {} defined for character template {} in `character_template_class` does not exists, skipped.", classID, templateId);
                continue;
            }

            characterTemplateClasses[templateId].emplace_back(factionGroup, classID);
        }
        while (classesResult->NextRow());
    }
    else
    {
        TC_LOG_INFO("server.loading", ">> Loaded 0 character template classes. DB table `character_template_class` is empty.");
    }

    QueryResult templates = WorldDatabase.Query("SELECT Id, Name, Description, Level FROM character_template");
    if (!templates)
    {
        TC_LOG_INFO("server.loading", ">> Loaded 0 character templates. DB table `character_template` is empty.");
        return;
    }

    do
    {
        Field* fields = templates->Fetch();

        CharacterTemplate templ;
        templ.TemplateSetId = fields[0].GetUInt32();
        templ.Name = fields[1].GetString();
        templ.Description = fields[2].GetString();
        templ.Level = fields[3].GetUInt8();
        templ.Classes = std::move(characterTemplateClasses[templ.TemplateSetId]);

        if (templ.Classes.empty())
        {
            TC_LOG_ERROR("sql.sql", "Character template {} does not have any classes defined in `character_template_class`. Skipped.", templ.TemplateSetId);
            continue;
        }

        _characterTemplateStore[templ.TemplateSetId] = templ;
    }
    while (templates->NextRow());

    TC_LOG_INFO("server.loading", ">> Loaded {} character templates in {} ms.", _characterTemplateStore.size(), GetMSTimeDiffToNow(oldMSTime));
}

CharacterTemplateContainer const& CharacterTemplateDataStore::GetCharacterTemplates() const
{
    return _characterTemplateStore;
}

CharacterTemplate const* CharacterTemplateDataStore::GetCharacterTemplate(uint32 templateId) const
{
    auto itr = _characterTemplateStore.find(templateId);
    if (itr != _characterTemplateStore.end())
        return &itr->second;

    return nullptr;
}

CharacterTemplateDataStore* CharacterTemplateDataStore::Instance()
{
    static CharacterTemplateDataStore instance;
    return &instance;
}