1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | /** \file |
---|
22 | \ingroup realmd |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Common.h" |
---|
26 | #include "RealmList.h" |
---|
27 | #include "Policies/SingletonImp.h" |
---|
28 | #include "Database/DatabaseEnv.h" |
---|
29 | |
---|
30 | INSTANTIATE_SINGLETON_1( RealmList ); |
---|
31 | |
---|
32 | extern DatabaseType dbRealmServer; |
---|
33 | |
---|
34 | RealmList::RealmList( ) : m_UpdateInterval(0), m_NextUpdateTime(time(NULL)) |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | /// Load the realm list from the database |
---|
39 | void RealmList::Initialize(uint32 updateInterval) |
---|
40 | { |
---|
41 | m_UpdateInterval = updateInterval; |
---|
42 | |
---|
43 | ///- Get the content of the realmlist table in the database |
---|
44 | UpdateRealms(true); |
---|
45 | } |
---|
46 | |
---|
47 | void RealmList::UpdateRealm( uint32 ID, std::string name, std::string address, uint32 port, uint8 icon, uint8 color, uint8 timezone, AccountTypes allowedSecurityLevel, float popu) |
---|
48 | { |
---|
49 | ///- Create new if not exist or update existed |
---|
50 | Realm& realm = m_realms[name]; |
---|
51 | |
---|
52 | realm.m_ID = ID; |
---|
53 | realm.name = name; |
---|
54 | realm.icon = icon; |
---|
55 | realm.color = color; |
---|
56 | realm.timezone = timezone; |
---|
57 | realm.allowedSecurityLevel = allowedSecurityLevel; |
---|
58 | realm.populationLevel = popu; |
---|
59 | |
---|
60 | ///- Append port to IP address. |
---|
61 | std::ostringstream ss; |
---|
62 | ss << address << ":" << port; |
---|
63 | realm.address = ss.str(); |
---|
64 | } |
---|
65 | |
---|
66 | void RealmList::UpdateIfNeed() |
---|
67 | { |
---|
68 | // maybe disabled or updated recently |
---|
69 | if(!m_UpdateInterval || m_NextUpdateTime > time(NULL)) |
---|
70 | return; |
---|
71 | |
---|
72 | m_NextUpdateTime = time(NULL) + m_UpdateInterval; |
---|
73 | |
---|
74 | // Clears Realm list |
---|
75 | m_realms.clear(); |
---|
76 | |
---|
77 | // Get the content of the realmlist table in the database |
---|
78 | UpdateRealms(false); |
---|
79 | } |
---|
80 | |
---|
81 | void RealmList::UpdateRealms(bool init) |
---|
82 | { |
---|
83 | sLog.outDetail("Updating Realm List..."); |
---|
84 | |
---|
85 | QueryResult *result = dbRealmServer.Query( "SELECT id, name, address, port, icon, color, timezone, allowedSecurityLevel, population FROM realmlist WHERE color <> 3 ORDER BY name" ); |
---|
86 | |
---|
87 | ///- Circle through results and add them to the realm map |
---|
88 | if(result) |
---|
89 | { |
---|
90 | do |
---|
91 | { |
---|
92 | Field *fields = result->Fetch(); |
---|
93 | |
---|
94 | uint8 allowedSecurityLevel = fields[7].GetUInt8(); |
---|
95 | |
---|
96 | 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() ); |
---|
97 | if(init) |
---|
98 | sLog.outString("Added realm \"%s\".", fields[1].GetString()); |
---|
99 | } while( result->NextRow() ); |
---|
100 | delete result; |
---|
101 | } |
---|
102 | } |
---|