root/trunk/src/trinityrealm/RealmList.cpp @ 3

Revision 2, 3.2 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19/** \file
20    \ingroup realmd
21*/
22
23#include "Common.h"
24#include "RealmList.h"
25#include "Policies/SingletonImp.h"
26#include "Database/DatabaseEnv.h"
27
28INSTANTIATE_SINGLETON_1( RealmList );
29
30extern DatabaseType dbRealmServer;
31
32RealmList::RealmList( ) : m_UpdateInterval(0), m_NextUpdateTime(time(NULL))
33{
34}
35
36/// Load the realm list from the database
37void RealmList::Initialize(uint32 updateInterval)
38{
39    m_UpdateInterval = updateInterval;
40
41    ///- Get the content of the realmlist table in the database
42    UpdateRealms(true);
43}
44
45void RealmList::UpdateRealm( uint32 ID, std::string name, std::string address, uint32 port, uint8 icon, uint8 color, uint8 timezone, AccountTypes allowedSecurityLevel, float popu)
46{
47    ///- Create new if not exist or update existed
48    Realm& realm = m_realms[name];
49
50    realm.m_ID      = ID;
51    realm.name      = name;
52    realm.icon      = icon;
53    realm.color     = color;
54    realm.timezone  = timezone;
55    realm.allowedSecurityLevel = allowedSecurityLevel;
56    realm.populationLevel        = popu;
57
58    ///- Append port to IP address.
59    std::ostringstream ss;
60    ss << address << ":" << port;
61    realm.address   = ss.str();
62}
63
64void RealmList::UpdateIfNeed()
65{
66    // maybe disabled or updated recently
67    if(!m_UpdateInterval || m_NextUpdateTime > time(NULL))
68        return;
69
70    m_NextUpdateTime = time(NULL) + m_UpdateInterval;
71
72    // Clears Realm list
73    m_realms.clear();
74
75    // Get the content of the realmlist table in the database
76    UpdateRealms(false);
77}
78
79void RealmList::UpdateRealms(bool init)
80{
81    sLog.outDetail("Updating Realm List...");
82
83    QueryResult *result = dbRealmServer.Query( "SELECT id, name, address, port, icon, color, timezone, allowedSecurityLevel, population FROM realmlist WHERE color <> 3 ORDER BY name" );
84
85    ///- Circle through results and add them to the realm map
86    if(result)
87    {
88        do
89        {
90            Field *fields = result->Fetch();
91
92            uint8 allowedSecurityLevel = fields[7].GetUInt8();
93
94            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            if(init)
96                sLog.outString("Added realm \"%s\".", fields[1].GetString());
97        } while( result->NextRow() );
98        delete result;
99    }
100}
Note: See TracBrowser for help on using the browser.