2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "RealmList.h"
25 #include "Policies/SingletonImp.h"
26 #include "Database/DatabaseEnv.h"
28 INSTANTIATE_SINGLETON_1( RealmList
);
30 extern DatabaseType dbRealmServer
;
32 RealmList::RealmList( ) : m_UpdateInterval(0), m_NextUpdateTime(time(NULL
))
36 /// Load the realm list from the database
37 void RealmList::Initialize(uint32 updateInterval
)
39 m_UpdateInterval
= updateInterval
;
41 ///- Get the content of the realmlist table in the database
45 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
)
47 ///- Create new if not exist or update existed
48 Realm
& realm
= m_realms
[name
];
53 realm
.timezone
= timezone
;
54 realm
.allowedSecurityLevel
= allowedSecurityLevel
;
55 realm
.populationLevel
= popu
;
57 ///- Append port to IP address.
58 std::ostringstream ss
;
59 ss
<< address
<< ":" << port
;
60 realm
.address
= ss
.str();
63 void RealmList::UpdateIfNeed()
65 // maybe disabled or updated recently
66 if(!m_UpdateInterval
|| m_NextUpdateTime
> time(NULL
))
69 m_NextUpdateTime
= time(NULL
) + m_UpdateInterval
;
74 // Get the content of the realmlist table in the database
78 void RealmList::UpdateRealms(bool init
)
80 sLog
.outDetail("Updating Realm List...");
82 QueryResult
*result
= dbRealmServer
.Query( "SELECT id, name, address, port, icon, color, timezone, allowedSecurityLevel, population FROM realmlist WHERE color <> 3 ORDER BY name" );
84 ///- Circle through results and add them to the realm map
89 Field
*fields
= result
->Fetch();
91 uint8 allowedSecurityLevel
= fields
[7].GetUInt8();
93 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() );
95 sLog
.outString("Added realm \"%s\".", fields
[1].GetString());
96 } while( result
->NextRow() );