TrinityCore
ArenaTeamMgr.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 "ArenaTeamMgr.h"
19#include "DatabaseEnv.h"
20#include "Define.h"
21#include "Log.h"
22#include "Util.h"
23#include "World.h"
24
26{
28}
29
31{
32 for (ArenaTeamContainer::iterator itr = ArenaTeamStore.begin(); itr != ArenaTeamStore.end(); ++itr)
33 delete itr->second;
34}
35
37{
39 return &instance;
40}
41
42// Arena teams collection
44{
45 ArenaTeamContainer::const_iterator itr = ArenaTeamStore.find(arenaTeamId);
46 if (itr != ArenaTeamStore.end())
47 return itr->second;
48 return nullptr;
49}
50
51ArenaTeam* ArenaTeamMgr::GetArenaTeamByName(std::string_view arenaTeamName) const
52{
53 for (auto [teamId, team] : ArenaTeamStore)
54 if (StringEqualI(arenaTeamName, team->GetName()))
55 return team;
56 return nullptr;
57}
58
60{
61 for (auto [teamId, team] : ArenaTeamStore)
62 if (team->GetCaptain() == guid)
63 return team;
64 return nullptr;
65}
66
68{
69 ArenaTeam*& team = ArenaTeamStore[arenaTeam->GetId()];
70 ASSERT((team == nullptr) || (team == arenaTeam), "Duplicate arena team with ID %u", arenaTeam->GetId());
71 team = arenaTeam;
72}
73
75{
76 ArenaTeamStore.erase(arenaTeamId);
77}
78
80{
81 if (NextArenaTeamId >= 0xFFFFFFFE)
82 {
83 TC_LOG_ERROR("bg.battleground", "Arena team ids overflow!! Can't continue, shutting down server. ");
85 }
86 return NextArenaTeamId++;
87}
88
90{
91 uint32 oldMSTime = getMSTime();
92
93 // Clean out the trash before loading anything
94 CharacterDatabase.DirectExecute("DELETE FROM arena_team_member WHERE arenaTeamId NOT IN (SELECT arenaTeamId FROM arena_team)"); // One-time query
95
96 // 0 1 2 3 4 5 6 7 8
97 QueryResult result = CharacterDatabase.Query("SELECT arenaTeamId, name, captainGuid, type, backgroundColor, emblemStyle, emblemColor, borderStyle, borderColor, "
98 // 9 10 11 12 13 14
99 "rating, weekGames, weekWins, seasonGames, seasonWins, `rank` FROM arena_team ORDER BY arenaTeamId ASC");
100
101 if (!result)
102 {
103 TC_LOG_INFO("server.loading", ">> Loaded 0 arena teams. DB table `arena_team` is empty!");
104 return;
105 }
106
107 QueryResult result2 = CharacterDatabase.Query(
108 // 0 1 2 3 4 5 6 7 8 9
109 "SELECT arenaTeamId, atm.guid, atm.weekGames, atm.weekWins, atm.seasonGames, atm.seasonWins, c.name, class, personalRating, matchMakerRating FROM arena_team_member atm"
110 " INNER JOIN arena_team ate USING (arenaTeamId)"
111 " LEFT JOIN characters AS c ON atm.guid = c.guid"
112 " LEFT JOIN character_arena_stats AS cas ON c.guid = cas.guid AND (cas.slot = 0 AND ate.type = 2 OR cas.slot = 1 AND ate.type = 3 OR cas.slot = 2 AND ate.type = 5)"
113 " ORDER BY atm.arenateamid ASC");
114
115 uint32 count = 0;
116 do
117 {
118 ArenaTeam* newArenaTeam = new ArenaTeam;
119
120 if (!newArenaTeam->LoadArenaTeamFromDB(result) || !newArenaTeam->LoadMembersFromDB(result2))
121 {
122 newArenaTeam->Disband(nullptr);
123 delete newArenaTeam;
124 continue;
125 }
126
127 AddArenaTeam(newArenaTeam);
128
129 ++count;
130 }
131 while (result->NextRow());
132
133 TC_LOG_INFO("server.loading", ">> Loaded {} arena teams in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
134}
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 ASSERT
Definition: Errors.h:68
#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
bool StringEqualI(std::string_view a, std::string_view b)
Definition: Util.cpp:891
void LoadArenaTeams()
uint32 GenerateArenaTeamId()
ArenaTeam * GetArenaTeamByCaptain(ObjectGuid guid) const
uint32 NextArenaTeamId
Definition: ArenaTeamMgr.h:49
ArenaTeam * GetArenaTeamById(uint32 arenaTeamId) const
void AddArenaTeam(ArenaTeam *arenaTeam)
ArenaTeamContainer ArenaTeamStore
Definition: ArenaTeamMgr.h:50
ArenaTeam * GetArenaTeamByName(std::string_view arenaTeamName) const
void RemoveArenaTeam(uint32 Id)
static ArenaTeamMgr * instance()
void Disband(WorldSession *session)
Definition: ArenaTeam.cpp:345
bool LoadArenaTeamFromDB(QueryResult arenaTeamDataResult)
Definition: ArenaTeam.cpp:185
bool LoadMembersFromDB(QueryResult arenaTeamMembersResult)
Definition: ArenaTeam.cpp:211
uint32 GetId() const
Definition: ArenaTeam.h:123
static void StopNow(uint8 exitcode)
Definition: World.h:670
@ ERROR_EXIT_CODE
Definition: World.h:75