1 | <?php
|
---|
2 |
|
---|
3 | function ErrorHandler()
|
---|
4 | {
|
---|
5 | }
|
---|
6 |
|
---|
7 | class Server extends Module
|
---|
8 | {
|
---|
9 | var $Id;
|
---|
10 | var $Server;
|
---|
11 | var $WorlddBaseNetworkPort = 8085;
|
---|
12 | var $RealmdBaseNetworkPort = 3724;
|
---|
13 |
|
---|
14 | function __construct($Database, $Id)
|
---|
15 | {
|
---|
16 | $this->Database = $Database;
|
---|
17 | $this->Id = $Id;
|
---|
18 | $DbResult = $this->Database->query('SELECT * FROM `Server` WHERE `Id`='.$Id);
|
---|
19 | $this->Server = $DbResult->fetch_assoc();
|
---|
20 | $DbResult = $this->Database->query('SELECT * FROM `Database` WHERE `Id`='.$this->Server['Database']);
|
---|
21 | if($DbResult->num_rows > 0) $this->Server['Database'] = $DbResult->fetch_assoc();
|
---|
22 | else $this->Server['Database'] = array('Emulator' => 0);
|
---|
23 | $DbResult = $this->Database->query('SELECT * FROM `Emulator` WHERE `Id`='.$this->Server['Database']['Emulator']);
|
---|
24 | if($DbResult->num_rows > 0) $this->Server['Database']['Emulator'] = $DbResult->fetch_assoc();
|
---|
25 | else $this->Server['Database']['Emulator'] = array('ClientVersion' => 0);
|
---|
26 | $DbResult = $this->Database->query('SELECT * FROM `ClientVersion` WHERE `Id`='.$this->Server['Database']['Emulator']['ClientVersion']);
|
---|
27 | if($DbResult->num_rows > 0) $this->Server['Database']['Emulator']['ClientVersion'] = $DbResult->fetch_assoc();
|
---|
28 | else $this->Server['Database']['Emulator']['ClientVersion'] = array();
|
---|
29 | }
|
---|
30 |
|
---|
31 | function AddTask($Title, $Task)
|
---|
32 | {
|
---|
33 | global $System;
|
---|
34 |
|
---|
35 | $this->Database->insert('Task', array('User' => $System->Modules['User']->User['Id'], 'Title' => $Title, 'Time' => 'NOW()', 'CommandList' => implode("\n", $Task)));
|
---|
36 | }
|
---|
37 |
|
---|
38 | function CreateDatabase()
|
---|
39 | {
|
---|
40 | $this->Database->query('CREATE DATABASE `server'.$this->Id.'_realmd`');
|
---|
41 | $this->Database->query('CREATE DATABASE `server'.$this->Id.'_mangos`');
|
---|
42 | $this->Database->query('CREATE DATABASE `server'.$this->Id.'_characters`');
|
---|
43 | $this->Database->query('CREATE DATABASE `server'.$this->Id.'_scriptdev2`');
|
---|
44 | $this->Database->query('CREATE USER "server'.$this->Id.'"@"localhost" IDENTIFIED BY "server'.$this->Id.'"');
|
---|
45 | $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_realmd` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
|
---|
46 | $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_mangos` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
|
---|
47 | $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_characters` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
|
---|
48 | $this->Database->query('GRANT ALL PRIVILEGES ON `server'.$this->Id.'\_scriptdev2` . * TO "server'.$this->Id.'"@"localhost" WITH GRANT OPTION');
|
---|
49 | }
|
---|
50 |
|
---|
51 | function DeleteDatabase()
|
---|
52 | {
|
---|
53 | $this->Database->query('DROP DATABASE `server'.$this->Id.'_realmd`');
|
---|
54 | $this->Database->query('DROP DATABASE `server'.$this->Id.'_mangos`');
|
---|
55 | $this->Database->query('DROP DATABASE `server'.$this->Id.'_characters`');
|
---|
56 | $this->Database->query('DROP DATABASE `server'.$this->Id.'_scriptdev2`');
|
---|
57 | $this->Database->query('DROP USER "server'.$this->Id.'"@"localhost"');
|
---|
58 | }
|
---|
59 |
|
---|
60 | function ImportDatabase($Delete = false)
|
---|
61 | {
|
---|
62 | $CommandList = array(
|
---|
63 | 'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < database/'.$this->Server['Database']['Id'].'/'.$this->Server['Database']['SourceFileName'],
|
---|
64 | 'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emulator/'.$this->Server['Database']['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_create_structure.sql',
|
---|
65 | 'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_scriptdev2 < emulator/'.$this->Server['Database']['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/scriptdev2_script_full.sql',
|
---|
66 | 'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_mangos < emulator/'.$this->Server['Database']['Emulator']['Id'].'/source/src/bindings/ScriptDev2/sql/mangos_scriptname_full.sql',
|
---|
67 | 'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_realmd < emulator/'.$this->Server['Database']['Emulator']['Id'].'/share/mangos/sql/realmd.sql',
|
---|
68 | 'mysql --user=server'.$this->Id.' --password=server'.$this->Id.' server'.$this->Id.'_characters < emulator/'.$this->Server['Database']['Emulator']['Id'].'/share/mangos/sql/characters.sql',
|
---|
69 | );
|
---|
70 | if($Delete == true)
|
---|
71 | {
|
---|
72 | array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_mangos -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_mangos');
|
---|
73 | array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_characters -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_characters');
|
---|
74 | array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_realmd -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_realmd');
|
---|
75 | array_unshift($CommandList, 'mysql --silent --skip-column-names -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_scriptdev2 -e "show tables" | gawk \'{print "drop table " $1 ";"}\' | mysql -u server'.$this->Id.' -pserver'.$this->Id.' server'.$this->Id.'_scriptdev2');
|
---|
76 | }
|
---|
77 | $this->AddTask('Inicializace databáze', $CommandList);
|
---|
78 | }
|
---|
79 |
|
---|
80 | function AddServer($Version)
|
---|
81 | {
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | function Start()
|
---|
86 | {
|
---|
87 | $this->AddTask('Start emulátoru', array(
|
---|
88 | 'screen -A -m -d -S server'.$this->Id.'-realmd "emulator/'.$this->Server['Database']['Emulator']['Id'].'/bin/mangos-realmd -c server/'.$this->Id.'/etc/realmd.conf"',
|
---|
89 | 'screen -A -m -d -S server'.$this->Id.'-worldd "emulator/'.$this->Server['Database']['Emulator']['Id'].'/bin/mangos-worldd -c server/'.$this->Id.'/etc/mangosd.conf"',
|
---|
90 | ));
|
---|
91 | return('Požadavek na start serveru zařazen.');
|
---|
92 | }
|
---|
93 |
|
---|
94 | function Stop()
|
---|
95 | {
|
---|
96 | $this->AddTask('Zastavení emulátoru', array(
|
---|
97 | 'ps -ef | grep \'SCREEN -A -m -d -S server'.$this->Id.'-realmd\' | grep -v grep | awk \'{print $2}\' | xargs -i kill {}',
|
---|
98 | 'ps -ef | grep \'SCREEN -A -m -d -S server'.$this->Id.'-worldd\' | grep -v grep | awk \'{print $2}\' | xargs -i kill {}',
|
---|
99 | ));
|
---|
100 | return('Požadavek na zastavení serveru zařazen.');
|
---|
101 | }
|
---|
102 |
|
---|
103 | function NetworkPortState($Address, $Port, $Timeout = 1)
|
---|
104 | {
|
---|
105 | set_error_handler('ErrorHandler');
|
---|
106 | if($Socket = @fsockopen($Address, $Port, $ERROR_NO, $ERROR_STR, (float)$Timeout))
|
---|
107 | {
|
---|
108 | fclose($Socket);
|
---|
109 | $Result = true;
|
---|
110 | } else $Result = false;
|
---|
111 | restore_error_handler();
|
---|
112 | return($Result);
|
---|
113 | }
|
---|
114 |
|
---|
115 | function GetState()
|
---|
116 | {
|
---|
117 | $State = array();
|
---|
118 | $State['RealmdPortState'] = $this->NetworkPortState('localhost', $this->RealmdBaseNetworkPort + $this->Server['Id']);
|
---|
119 | $State['WorlddPortState'] = $this->NetworkPortState('localhost', $this->WorlddBaseNetworkPort + $this->Server['Id']);
|
---|
120 | $State['Online'] = $State['RealmdPortState'] and $State['WorlddPortState'];
|
---|
121 | $DbResult = $this->Database->query('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = "server'.$this->Id.'_characters"');
|
---|
122 | $DbRow = $DbResult->fetch_row();
|
---|
123 | if($DbRow[0] > 0)
|
---|
124 | {
|
---|
125 | $DbResult = $this->Database->query('SELECT COUNT(*) FROM server'.$this->Id.'_characters.characters AS T WHERE T.online = 1');
|
---|
126 | $DbRow = $DbResult->fetch_row();
|
---|
127 | $State['OnlinePlayerCount'] = $DbRow[0];
|
---|
128 | $DbResult = $this->Database->query('SELECT COUNT(*) FROM server'.$this->Id.'_characters.characters AS T');
|
---|
129 | $DbRow = $DbResult->fetch_row();
|
---|
130 | $State['CharacterCount'] = $DbRow[0];
|
---|
131 | $DbResult = $this->Database->query('SELECT COUNT(*) FROM server'.$this->Id.'_realmd.account AS T');
|
---|
132 | $DbRow = $DbResult->fetch_row();
|
---|
133 | $State['AccountCount'] = $DbRow[0];
|
---|
134 | } else
|
---|
135 | {
|
---|
136 | $State['OnlinePlayerCount'] = 0;
|
---|
137 | $State['CharacterCount'] = 0;
|
---|
138 | $State['AccountCount'] = 0;
|
---|
139 | }
|
---|
140 | return($State);
|
---|
141 | }
|
---|
142 |
|
---|
143 | function UpdateRealmlist()
|
---|
144 | {
|
---|
145 | global $Config;
|
---|
146 |
|
---|
147 | $this->Database->query('UPDATE server'.$this->Id.'_realmd.realmlist SET name="'.addslashes($this->Server['Name']).'", address="'.$Config['Web']['Host'].'", port='.($this->WorlddBaseNetworkPort + $this->Id));
|
---|
148 | }
|
---|
149 |
|
---|
150 | function SaveConfiguration()
|
---|
151 | {
|
---|
152 | $this->SetupConfigurationFiles();
|
---|
153 | $this->UpdateRealmlist();
|
---|
154 | }
|
---|
155 |
|
---|
156 | function SetupConfigurationFiles()
|
---|
157 | {
|
---|
158 | $EmulatorEtcDir = '../emulator/'.$this->Server['Database']['Emulator']['Id'].'/etc/';
|
---|
159 | $ServerEtcDir = '../server/'.$this->Id.'/etc/';
|
---|
160 | $ServerLogDir = '../server/'.$this->Id.'/log/';
|
---|
161 | if(!file_exists($ServerEtcDir)) mkdir($ServerEtcDir, 0777, true);
|
---|
162 | if(!file_exists($ServerLogDir)) mkdir($ServerLogDir, 0777, true);
|
---|
163 |
|
---|
164 | // mangosd.conf
|
---|
165 | $Config = new MangosConfigurationFile($this->Database);
|
---|
166 | $Config->Load($EmulatorEtcDir.'mangosd.conf.dist');
|
---|
167 | $Config->ParameterList['LoginDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_realmd';
|
---|
168 | $Config->ParameterList['WorldDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_mangos';
|
---|
169 | $Config->ParameterList['CharacterDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_characters';
|
---|
170 | $Config->ParameterList['ScriptDev2DatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_scriptdev2';
|
---|
171 | $Config->ParameterList['WorldServerPort'] = $this->WorlddBaseNetworkPort + $this->Id;
|
---|
172 | $Config->ParameterList['Motd'] = $this->Server['Motd'];
|
---|
173 | $Config->ParameterList['DataDir'] = 'wowclient/'.$this->Server['Database']['Emulator']['ClientVersion']['Version'];
|
---|
174 | $Config->ParameterList['LogsDir'] = 'server/'.$this->Id.'/log';
|
---|
175 | $ServerTypes = array(0 => 0, 1 => 1, 2 => 6, 3 => 8);
|
---|
176 | $Config->ParameterList['GameType'] = $ServerTypes[$this->Server['Type']];
|
---|
177 | $Config->Save($ServerEtcDir.'mangosd.conf');
|
---|
178 |
|
---|
179 | // realmd.conf
|
---|
180 | $Config = new MangosConfigurationFile($this->Database);
|
---|
181 | $Config->Load($EmulatorEtcDir.'realmd.conf.dist');
|
---|
182 | $Config->ParameterList['LoginDatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_realmd';
|
---|
183 | $Config->ParameterList['RealmServerPort'] = $this->RealmdBaseNetworkPort + $this->Id;
|
---|
184 | $Config->ParameterList['LogsDir'] = 'server/'.$this->Id.'/log';
|
---|
185 | $Config->Save($ServerEtcDir.'realmd.conf');
|
---|
186 |
|
---|
187 | // scriptdev2.conf
|
---|
188 | $Config = new MangosConfigurationFile($this->Database);
|
---|
189 | $Config->Load($EmulatorEtcDir.'scriptdev2.conf');
|
---|
190 | $Config->ParameterList['ScriptDev2DatabaseInfo'] = 'localhost;3306;server'.$this->Id.';server'.$this->Id.';server'.$this->Id.'_scriptdev2';
|
---|
191 | $Config->Save($ServerEtcDir.'scriptdev2.conf');
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | ?>
|
---|