TrinityCore
Loading...
Searching...
No Matches
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 "Define.h"
19#include "ArenaTeamMgr.h"
20#include "World.h"
21#include "Log.h"
22#include "DatabaseEnv.h"
23#include "Language.h"
24#include "Player.h"
25#include "ObjectAccessor.h"
26
31
33{
34 for (ArenaTeamContainer::iterator itr = ArenaTeamStore.begin(); itr != ArenaTeamStore.end(); ++itr)
35 delete itr->second;
36}
37
43
44// Arena teams collection
46{
47 ArenaTeamContainer::const_iterator itr = ArenaTeamStore.find(arenaTeamId);
48 if (itr != ArenaTeamStore.end())
49 return itr->second;
50 return nullptr;
51}
52
53ArenaTeam* ArenaTeamMgr::GetArenaTeamByName(std::string_view arenaTeamName) const
54{
55 for (auto [teamId, team] : ArenaTeamStore)
56 if (StringEqualI(arenaTeamName, team->GetName()))
57 return team;
58 return nullptr;
59}
60
62{
63 for (auto [teamId, team] : ArenaTeamStore)
64 if (team->GetCaptain() == guid)
65 return team;
66 return nullptr;
67}
68
70{
71 ArenaTeam*& team = ArenaTeamStore[arenaTeam->GetId()];
72 ASSERT((team == nullptr) || (team == arenaTeam), "Duplicate arena team with ID %u", arenaTeam->GetId());
73 team = arenaTeam;
74}
75
77{
78 ArenaTeamStore.erase(arenaTeamId);
79}
80
82{
83 if (NextArenaTeamId >= 0xFFFFFFFE)
84 {
85 TC_LOG_ERROR("bg.battleground", "Arena team ids overflow!! Can't continue, shutting down server. ");
87 }
88 return NextArenaTeamId++;
89}
90
92{
93 uint32 oldMSTime = getMSTime();
94
95 // Clean out the trash before loading anything
96 CharacterDatabase.DirectExecute("DELETE FROM arena_team_member WHERE arenaTeamId NOT IN (SELECT arenaTeamId FROM arena_team)"); // One-time query
97
98 // 0 1 2 3 4 5 6 7 8
99 QueryResult result = CharacterDatabase.Query("SELECT arenaTeamId, name, captainGuid, type, backgroundColor, emblemStyle, emblemColor, borderStyle, borderColor, "
100 // 9 10 11 12 13 14
101 "rating, weekGames, weekWins, seasonGames, seasonWins, `rank` FROM arena_team ORDER BY arenaTeamId ASC");
102
103 if (!result)
104 {
105 TC_LOG_INFO("server.loading", ">> Loaded 0 arena teams. DB table `arena_team` is empty!");
106 return;
107 }
108
109 QueryResult result2 = CharacterDatabase.Query(
110 // 0 1 2 3 4 5 6 7 8 9
111 "SELECT arenaTeamId, atm.guid, atm.weekGames, atm.weekWins, atm.seasonGames, atm.seasonWins, c.name, class, personalRating, matchMakerRating FROM arena_team_member atm"
112 " INNER JOIN arena_team ate USING (arenaTeamId)"
113 " LEFT JOIN characters AS c ON atm.guid = c.guid"
114 " 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)"
115 " ORDER BY atm.arenateamid ASC");
116
117 uint32 count = 0;
118 do
119 {
120 ArenaTeam* newArenaTeam = new ArenaTeam;
121
122 if (!newArenaTeam->LoadArenaTeamFromDB(result) || !newArenaTeam->LoadMembersFromDB(result2))
123 {
124 newArenaTeam->Disband(nullptr);
125 delete newArenaTeam;
126 continue;
127 }
128
129 AddArenaTeam(newArenaTeam);
130
131 ++count;
132 }
133 while (result->NextRow());
134
135 TC_LOG_INFO("server.loading", ">> Loaded {} arena teams in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
136}
137
139{
140 // Used to distribute arena points based on last week's stats
141 sWorld->SendWorldText(LANG_DIST_ARENA_POINTS_START);
142
144
145 // Temporary structure for storing maximum points to add values for all players
146 std::map<ObjectGuid, uint32> PlayerPoints;
147
148 // At first update all points for all team members
149 for (auto [teamId, team] : ArenaTeamStore)
150 team->UpdateArenaPointsHelper(PlayerPoints);
151
152 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
153
155
156 // Cycle that gives points to all players
157 for (std::map<ObjectGuid, uint32>::iterator playerItr = PlayerPoints.begin(); playerItr != PlayerPoints.end(); ++playerItr)
158 {
159 // Add points to player if online
160 if (Player* player = ObjectAccessor::FindConnectedPlayer(playerItr->first))
161 player->ModifyArenaPoints(playerItr->second, trans);
162 else // Update database
163 {
164 stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ADD_CHAR_ARENA_POINTS);
165 stmt->setUInt32(0, playerItr->second);
166 stmt->setUInt32(1, playerItr->first.GetCounter());
167 trans->Append(stmt);
168 }
169 }
170
171 CharacterDatabase.CommitTransaction(trans);
172
173 PlayerPoints.clear();
174
176
178 for (auto [teamId, team] : ArenaTeamStore)
179 {
180 if (team->FinishWeek())
181 team->SaveToDB(true);
182
183 team->NotifyStatsChanged();
184 }
185
187
188 sWorld->SendWorldText(LANG_DIST_ARENA_POINTS_END);
189}
@ CHAR_UPD_ADD_CHAR_ARENA_POINTS
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
uint32_t uint32
Definition Define.h:133
#define ASSERT
Definition Errors.h:68
@ LANG_DIST_ARENA_POINTS_TEAM_START
Definition Language.h:737
@ LANG_DIST_ARENA_POINTS_ONLINE_START
Definition Language.h:735
@ LANG_DIST_ARENA_POINTS_START
Definition Language.h:734
@ LANG_DIST_ARENA_POINTS_TEAM_END
Definition Language.h:738
@ LANG_DIST_ARENA_POINTS_END
Definition Language.h:739
@ LANG_DIST_ARENA_POINTS_ONLINE_END
Definition Language.h:736
#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:706
void LoadArenaTeams()
void DistributeArenaPoints()
uint32 GenerateArenaTeamId()
ArenaTeam * GetArenaTeamByCaptain(ObjectGuid guid) const
uint32 NextArenaTeamId
ArenaTeam * GetArenaTeamById(uint32 arenaTeamId) const
void AddArenaTeam(ArenaTeam *arenaTeam)
ArenaTeamContainer ArenaTeamStore
ArenaTeam * GetArenaTeamByName(std::string_view arenaTeamName) const
void RemoveArenaTeam(uint32 Id)
static ArenaTeamMgr * instance()
void Disband(WorldSession *session)
bool LoadArenaTeamFromDB(QueryResult arenaTeamDataResult)
bool LoadMembersFromDB(QueryResult arenaTeamMembersResult)
uint32 GetId() const
Definition ArenaTeam.h:128
void setUInt32(uint8 index, uint32 value)
static void StopNow(uint8 exitcode)
Definition World.h:673
#define sWorld
Definition World.h:900
@ ERROR_EXIT_CODE
Definition World.h:64
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)