2011-05-03 15:01:10 +02:00
/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2011-05-03 15:01:10 +02:00
*
* 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 "ArenaTeamMgr.h"
2011-09-09 13:05:51 +02:00
#include "DatabaseEnv.h"
2022-02-13 00:49:10 +01:00
#include "Define.h"
#include "Log.h"
#include "Util.h"
#include "World.h"
2011-05-03 15:01:10 +02:00
ArenaTeamMgr :: ArenaTeamMgr ()
{
NextArenaTeamId = 1 ;
}
ArenaTeamMgr ::~ ArenaTeamMgr ()
{
for ( ArenaTeamContainer :: iterator itr = ArenaTeamStore . begin (); itr != ArenaTeamStore . end (); ++ itr )
delete itr -> second ;
}
2016-03-11 17:11:05 +01:00
ArenaTeamMgr * ArenaTeamMgr :: instance ()
{
static ArenaTeamMgr instance ;
return & instance ;
}
2011-05-03 15:01:10 +02:00
// Arena teams collection
ArenaTeam * ArenaTeamMgr :: GetArenaTeamById ( uint32 arenaTeamId ) const
{
ArenaTeamContainer :: const_iterator itr = ArenaTeamStore . find ( arenaTeamId );
if ( itr != ArenaTeamStore . end ())
return itr -> second ;
2020-08-14 17:06:03 +02:00
return nullptr ;
2011-05-03 15:01:10 +02:00
}
2020-09-06 23:36:23 +02:00
ArenaTeam * ArenaTeamMgr :: GetArenaTeamByName ( std :: string_view arenaTeamName ) const
2011-05-03 15:01:10 +02:00
{
2020-09-06 23:36:23 +02:00
for ( auto [ teamId , team ] : ArenaTeamStore )
if ( StringEqualI ( arenaTeamName , team -> GetName ()))
return team ;
2020-08-14 17:06:03 +02:00
return nullptr ;
2011-05-03 15:01:10 +02:00
}
2014-09-14 16:14:12 +02:00
ArenaTeam * ArenaTeamMgr :: GetArenaTeamByCaptain ( ObjectGuid guid ) const
2011-05-03 15:01:10 +02:00
{
2020-09-06 23:36:23 +02:00
for ( auto [ teamId , team ] : ArenaTeamStore )
if ( team -> GetCaptain () == guid )
return team ;
2020-08-14 17:06:03 +02:00
return nullptr ;
2011-05-03 15:01:10 +02:00
}
void ArenaTeamMgr :: AddArenaTeam ( ArenaTeam * arenaTeam )
{
2020-09-06 23:36:23 +02:00
ArenaTeam *& team = ArenaTeamStore [ arenaTeam -> GetId ()];
ASSERT (( team == nullptr ) || ( team == arenaTeam ), "Duplicate arena team with ID %u" , arenaTeam -> GetId ());
team = arenaTeam ;
2011-05-03 15:01:10 +02:00
}
void ArenaTeamMgr :: RemoveArenaTeam ( uint32 arenaTeamId )
{
ArenaTeamStore . erase ( arenaTeamId );
}
uint32 ArenaTeamMgr :: GenerateArenaTeamId ()
{
if ( NextArenaTeamId >= 0xFFFFFFFE )
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "bg.battleground" , "Arena team ids overflow!! Can't continue, shutting down server. " );
2011-05-03 15:01:10 +02:00
World :: StopNow ( ERROR_EXIT_CODE );
}
return NextArenaTeamId ++ ;
}
void ArenaTeamMgr :: LoadArenaTeams ()
{
uint32 oldMSTime = getMSTime ();
// Clean out the trash before loading anything
2016-07-16 20:29:21 -03:00
CharacterDatabase . DirectExecute ( "DELETE FROM arena_team_member WHERE arenaTeamId NOT IN (SELECT arenaTeamId FROM arena_team)" ); // One-time query
2011-05-03 15:01:10 +02:00
2012-05-30 08:01:02 +02:00
// 0 1 2 3 4 5 6 7 8
QueryResult result = CharacterDatabase . Query ( "SELECT arenaTeamId, name, captainGuid, type, backgroundColor, emblemStyle, emblemColor, borderStyle, borderColor, "
// 9 10 11 12 13 14
2019-11-01 16:20:15 +01:00
"rating, weekGames, weekWins, seasonGames, seasonWins, `rank` FROM arena_team ORDER BY arenaTeamId ASC" );
2011-05-03 15:01:10 +02:00
if ( ! result )
{
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "server.loading" , ">> Loaded 0 arena teams. DB table `arena_team` is empty!" );
2011-05-03 15:01:10 +02:00
return ;
}
QueryResult result2 = CharacterDatabase . Query (
// 0 1 2 3 4 5 6 7 8 9
"SELECT arenaTeamId, atm.guid, atm.weekGames, atm.weekWins, atm.seasonGames, atm.seasonWins, c.name, class, personalRating, matchMakerRating FROM arena_team_member atm"
" INNER JOIN arena_team ate USING (arenaTeamId)"
" LEFT JOIN characters AS c ON atm.guid = c.guid"
" 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)"
" ORDER BY atm.arenateamid ASC" );
uint32 count = 0 ;
do
{
ArenaTeam * newArenaTeam = new ArenaTeam ;
if ( ! newArenaTeam -> LoadArenaTeamFromDB ( result ) || ! newArenaTeam -> LoadMembersFromDB ( result2 ))
{
2020-08-14 17:06:03 +02:00
newArenaTeam -> Disband ( nullptr );
2011-05-03 15:01:10 +02:00
delete newArenaTeam ;
continue ;
}
AddArenaTeam ( newArenaTeam );
++ count ;
}
while ( result -> NextRow ());
2023-01-08 21:16:53 +01:00
TC_LOG_INFO ( "server.loading" , ">> Loaded {} arena teams in {} ms" , count , GetMSTimeDiffToNow ( oldMSTime ));
2011-05-03 15:01:10 +02:00
}