2009-04-06 21:14:51 +02:00
/*
2009-02-04 12:42:26 +01:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2009-02-04 12:04:12 +01:00
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05: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
2008-11-06 10:27:58 -06:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* 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, write to the Free Software
2008-11-06 10:27:58 -06:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
/** \file
\ingroup realmd
*/
#include "Common.h"
2009-06-04 21:41:07 +02:00
#include "RealmList.h"
2009-06-13 09:18:54 -05:00
#include "Policies/SingletonImp.h"
#include "Database/DatabaseEnv.h"
2009-09-19 00:51:47 -04:00
INSTANTIATE_SINGLETON_1 ( RealmList );
2009-07-31 11:29:28 +08:00
extern DatabaseType loginDatabase ;
2009-09-19 00:51:47 -04:00
RealmList :: RealmList () : m_UpdateInterval ( 0 ), m_NextUpdateTime ( time ( NULL ))
2008-10-02 16:23:55 -05:00
{
}
/// Load the realm list from the database
void RealmList :: Initialize ( uint32 updateInterval )
{
m_UpdateInterval = updateInterval ;
///- Get the content of the realmlist table in the database
UpdateRealms ( true );
}
2009-09-19 00:51:47 -04:00
void RealmList :: UpdateRealm ( uint32 ID , const std :: string & name , const std :: string & address , uint32 port , uint8 icon , uint8 color , uint8 timezone , AccountTypes allowedSecurityLevel , float popu , uint32 build )
2008-10-02 16:23:55 -05:00
{
///- Create new if not exist or update existed
Realm & realm = m_realms [ name ];
realm . m_ID = ID ;
2009-03-21 01:42:46 +01:00
realm . name = name ;
2008-10-02 16:23:55 -05:00
realm . icon = icon ;
realm . color = color ;
realm . timezone = timezone ;
realm . allowedSecurityLevel = allowedSecurityLevel ;
realm . populationLevel = popu ;
///- Append port to IP address.
std :: ostringstream ss ;
ss << address << ":" << port ;
realm . address = ss . str ();
2009-09-13 00:02:14 +02:00
realm . gamebuild = build ;
2008-10-02 16:23:55 -05:00
}
void RealmList :: UpdateIfNeed ()
{
// maybe disabled or updated recently
2009-09-19 00:51:47 -04:00
if ( ! m_UpdateInterval || m_NextUpdateTime > time ( NULL ))
2008-10-02 16:23:55 -05:00
return ;
m_NextUpdateTime = time ( NULL ) + m_UpdateInterval ;
// Clears Realm list
m_realms . clear ();
// Get the content of the realmlist table in the database
UpdateRealms ( false );
}
void RealmList :: UpdateRealms ( bool init )
{
sLog . outDetail ( "Updating Realm List..." );
2009-09-19 00:51:47 -04:00
QueryResult * result = loginDatabase . Query ( "SELECT id, name, address, port, icon, color, timezone, allowedSecurityLevel, population, gamebuild FROM realmlist WHERE color <> 3 ORDER BY name" );
2008-10-02 16:23:55 -05:00
///- Circle through results and add them to the realm map
2009-09-19 00:51:47 -04:00
if ( result )
2008-10-02 16:23:55 -05:00
{
do
{
Field * fields = result -> Fetch ();
uint8 allowedSecurityLevel = fields [ 7 ]. GetUInt8 ();
2009-09-19 00:51:47 -04:00
UpdateRealm ( fields [ 0 ]. GetUInt32 (), fields [ 1 ]. GetCppString (), fields [ 2 ]. GetCppString (), fields [ 3 ]. GetUInt32 (), fields [ 4 ]. GetUInt8 (), fields [ 5 ]. GetUInt8 (), fields [ 6 ]. GetUInt8 (), ( allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes ( allowedSecurityLevel ) : SEC_ADMINISTRATOR ), fields [ 8 ]. GetFloat (), fields [ 9 ]. GetUInt32 ());
if ( init )
2008-10-02 16:23:55 -05:00
sLog . outString ( "Added realm \" %s \" ." , fields [ 1 ]. GetString ());
2009-09-19 00:51:47 -04:00
} while ( result -> NextRow ());
2008-10-02 16:23:55 -05:00
delete result ;
}
}