OregonCore  revision 3611e8a-git
Your Favourite TBC server
RealmList.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "Common.h"
19 #include "RealmList.h"
20 #include "AuthCodes.h"
21 #include "Util.h" // for Tokens typedef
22 #include "Database/DatabaseEnv.h"
23 
25 
26 // will only support WoW 1.12.1/1.12.2 , WoW:TBC 2.4.3 and official release for WoW:WotLK and later, client builds 10505, 8606, 6005, 5875
27 // if you need more from old build then add it in cases in realmd sources code
28 // list sorted from high to low build and first build used as low bound for accepted by default range (any > it will accepted by realmd at least)
29 
30 static RealmBuildInfo ExpectedRealmdClientBuilds[] =
31 {
32  {12340, 3, 3, 5, 'a'}, // highest supported build, also auto accept all above for simplify future supported builds testing
33  {11723, 3, 3, 3, 'a'},
34  {11403, 3, 3, 2, ' '},
35  {11159, 3, 3, 0, 'a'},
36  {10505, 3, 2, 2, 'a'},
37  {8606, 2, 4, 3, ' '},
38  {6005, 1, 12, 2, ' '},
39  {5875, 1, 12, 1, ' '},
40  {0, 0, 0, 0, ' '} // terminator
41 };
42 
44 {
45  // first build is low bound of always accepted range
46  if (_build >= ExpectedRealmdClientBuilds[0].build)
47  return &ExpectedRealmdClientBuilds[0];
48 
49  // continue from 1 with explicit equal check
50  for (int i = 1; ExpectedRealmdClientBuilds[i].build; ++i)
51  if (_build == ExpectedRealmdClientBuilds[i].build)
52  return &ExpectedRealmdClientBuilds[i];
53 
54  // none appropriate build
55  return NULL;
56 }
57 
58 RealmList::RealmList() : m_UpdateInterval(0), m_NextUpdateTime(time(NULL))
59 {
60 }
61 
62 // Load the realm list from the database
63 void RealmList::Initialize(uint32 updateInterval)
64 {
65  m_UpdateInterval = updateInterval;
66 
67  // Get the content of the realmlist table in the database
68  UpdateRealms(true);
69 }
70 
71 void RealmList::UpdateRealm( uint32 ID, const std::string& name, const std::string& address, uint32 port, uint8 icon, RealmFlags realmflags, uint8 timezone, AccountTypes allowedSecurityLevel, float popu, const char* builds)
72 {
73  // Create new if not exist or update existed
74  Realm& realm = m_realms[name];
75 
76  realm.m_ID = ID;
77  realm.icon = icon;
78  realm.realmflags = realmflags;
79  realm.timezone = timezone;
80  realm.allowedSecurityLevel = allowedSecurityLevel;
81  realm.populationLevel = popu;
82 
83  Tokens tokens = StrSplit(builds, " ");
84  Tokens::iterator iter;
85 
86  for (iter = tokens.begin(); iter != tokens.end(); ++iter)
87  {
88  uint32 build = atol((*iter).c_str());
89  realm.realmbuilds.insert(build);
90  }
91 
92  uint16 first_build = !realm.realmbuilds.empty() ? *realm.realmbuilds.begin() : 0;
93 
94  realm.realmBuildInfo.build = first_build;
95  realm.realmBuildInfo.major_version = 0;
96  realm.realmBuildInfo.minor_version = 0;
98  realm.realmBuildInfo.hotfix_version = ' ';
99 
100  if (first_build)
101  if (RealmBuildInfo const* bInfo = FindBuildInfo(first_build))
102  if (bInfo->build == first_build)
103  realm.realmBuildInfo = *bInfo;
104 
105  // Append port to IP address.
106  std::ostringstream ss;
107  ss << address << ":" << port;
108  realm.address = ss.str();
109 }
110 
112 {
113  // maybe disabled or updated recently
114  if (!m_UpdateInterval || m_NextUpdateTime > time(NULL))
115  return;
116 
117  m_NextUpdateTime = time(NULL) + m_UpdateInterval;
118 
119  // Clears Realm list
120  m_realms.clear();
121 
122  // Get the content of the realmlist table in the database
123  UpdateRealms(false);
124 }
125 
126 void RealmList::UpdateRealms(bool init)
127 {
128  sLog.outDetail("Updating Realm List...");
129 
130  // 0 1 2 3 4 5 6 7 8 9
131  QueryResult_AutoPtr result = LoginDatabase.Query( "SELECT id, name, address, port, icon, realmflags, timezone, allowedSecurityLevel, population, realmbuilds FROM realmlist WHERE (realmflags & 1) = 0 ORDER BY name" );
132 
133  // Circle through results and add them to the realm map
134  if (result)
135  {
136  do
137  {
138  Field* fields = result->Fetch();
139 
140  uint8 allowedSecurityLevel = fields[7].GetUInt8();
141 
142  uint8 realmflags = fields[5].GetUInt8();
143 
145  {
146  sLog.outError("Realm allowed have only OFFLINE Mask 0x2), or NEWPLAYERS (mask 0x20), or RECOMENDED (mask 0x40), or SPECIFICBUILD (mask 0x04) flags in DB");
148  }
149 
150  UpdateRealm(
151  fields[0].GetUInt32(), fields[1].GetCppString(), fields[2].GetCppString(), fields[3].GetUInt32(),
152  fields[4].GetUInt8(), RealmFlags(realmflags), fields[6].GetUInt8(),
153  (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR),
154  fields[8].GetFloat(), fields[9].GetString());
155 
156  if (init)
157  sLog.outString("Added realm \"%s\"", fields[1].GetString());
158  }
159  while ( result->NextRow() );
160  }
161 }
162 
std::vector< std::string > Tokens
Definition: Util.h:26
std::string address
Definition: RealmList.h:41
AccountTypes
Definition: Common.h:188
RealmBuildInfo const * FindBuildInfo(uint16 _build)
Definition: RealmList.cpp:43
Definition: Field.h:24
RealmBuildInfo realmBuildInfo
Definition: RealmList.h:49
uint32 m_ID
Definition: RealmList.h:45
#define sLog
Log class singleton.
Definition: Log.h:187
int hotfix_version
Definition: RealmList.h:31
uint32 m_UpdateInterval
Definition: RealmList.h:88
Tokens StrSplit(const std::string &src, const std::string &sep)
Definition: Util.cpp:97
QueryResult_AutoPtr Query(const char *sql)
Definition: Database.cpp:383
void UpdateIfNeed()
Definition: RealmList.cpp:111
int minor_version
Definition: RealmList.h:29
ACE_UINT8 uint8
Definition: Define.h:73
uint8 icon
Definition: RealmList.h:42
DatabaseType LoginDatabase
Accessor to the realm/login database.
Definition: Main.cpp:55
RealmBuilds realmbuilds
Definition: RealmList.h:48
RealmMap m_realms
Internal map of realms.
Definition: RealmList.h:87
time_t m_NextUpdateTime
Definition: RealmList.h:89
int major_version
Definition: RealmList.h:28
RealmFlags realmflags
Definition: RealmList.h:43
uint8 timezone
Definition: RealmList.h:44
void Initialize(uint32 updateInterval)
Definition: RealmList.cpp:63
ACE_Refcounted_Auto_Ptr< QueryResult, ACE_Null_Mutex > QueryResult_AutoPtr
Definition: QueryResult.h:113
int bugfix_version
Definition: RealmList.h:30
AccountTypes allowedSecurityLevel
Definition: RealmList.h:46
void UpdateRealms(bool init)
Definition: RealmList.cpp:126
ACE_UINT16 uint16
Definition: Define.h:72
float populationLevel
Definition: RealmList.h:47
ACE_UINT32 uint32
Definition: Define.h:71
RealmFlags
Definition: Common.h:198
void UpdateRealm(uint32 ID, const std::string &name, const std::string &address, uint32 port, uint8 icon, RealmFlags realmflags, uint8 timezone, AccountTypes allowedSecurityLevel, float popu, const char *builds)
Definition: RealmList.cpp:71
#define atol(a)
Definition: Common.h:162