This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax_refresh.php
252 lines (245 loc) · 7.96 KB
/
ajax_refresh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* ThreadSpace: Hyperbol Server List
*
* Authors: Nickolas Simard
* Vek (for the Python part)
* Original Idea: Nephyrin
* Websites: http://www.hyperbol.com/
* http://www.nickolabs.com/
* Demo: http://www.nickolabs.com/hyperbol/
* Last updated: December 11th, 2009
* Files required: function.php (Contains a simple Array "Dynamic Multisort" function).
* xmlLib.php (A "XML <--> Array" Library)
*
* The goal of this script was to generates a web accessible server list, to prevent users from having to log
* into the game to know if people are playing.
*
* This PHP file can called via AJAX. It generates a Table with the data from the XML, which is generated by
* the Python script. It calls the script using an "exec()" method, which is usually blocked by web host. You
* can put the Python script into a cronjob to workaround this issue.
*
* The Python script is used to communicate with the Master Server, grab the list of the servers, then connect
* to each of them to get the information.
* It then generates the XML saving it locally, and optionally, uploading it to a remote FTP server.
*
* As stated earlier, this Python script could be put in a cronjob, but it could also be running locally on a
* machine. There are commented lines at the end of the script in order to get it to run every 5 minutes. You
* might want to set the FTP server parameters to upload the generated file afterwards.
*
* This script is provided as is and the author cannot be held responsible for any damage or misbehaviour.
*
* Extra notes:
* The author is bilingual, and code in two languages in some place... bear with it! :P
* This script is based on the structure of the "ThreadSpace: Hyperbol" Master server and will most likely
* not suite your server configuration.
* Is was mostly a learning process for me to get to know more about socket and TCP communication
* between server.
*/
require( 'includes/xmlLib.php' );
require( 'includes/function.php' );
$xmlurl = "http://www.nickolabs.com/hyperbol/servers.xml"; // Treated as remote
/**
* Load XML function
* Using the $xmlurl variable set above, it creates the basic array used for parsing.
*/
function load_xml()
{
global $xmlurl, $xml, $array_xml, $arr_attributes, $arr_attributes_master, $updated, $players_online, $xml_age;
$xml = new XMLToArray( $xmlurl, array(), array( 'server' => '_array_' ), TRUE, FALSE);
$array_xml = $xml->getArray();
$arr_attributes = $xml->getAttributes();
// Récupère les attrib de "masterquery" (update timestamp et Players)
$arr_attributes_master = $arr_attributes[0];
$arr_attributes_master = $arr_attributes_master['serverlist'];
$arr_attributes_master = $arr_attributes_master[0];
// Récupère les attrib de chaque server pour le merge... + simplifie (root du array)
$arr_attributes = $arr_attributes[1];
$arr_attributes = $arr_attributes['server'];
$updated = $arr_attributes_master['mktime'];
$players_online = $arr_attributes_master['players'];
$xml_age = mktime() - $updated;
}
//
// Load the XML and check if the data are fresh... if not, refresh them. Age is in Seconds
//
load_xml();
if ($xml_age >= 60)
{
// Force Refresh the data
$output = exec('python querymaster.py');
// Load it again
load_xml();
}
//
// Manually merge the two arrays (infos & attributes)
//
$x = 0;
$array_result = array();
foreach ($arr_attributes as $server)
{
// This server is not responding, so structure will differ
if ($server['responding'] == 0)
{
// Add important parsing field...
$server['version'] = '';
$server['servername'] = '';
}
$array_result[] = $server;
$x++;
}
// Vide les variables qui ne seront plus utilisé.
unset($server, $x, $arr_attributes, $array_xml, $arr_attributes_master, $xml, $xml_age);
// Array sorting... Default : Offline/Online, Name
// Note: Après qq test, les Offline était top list... ajout de "result" dans le sort.
// Le "vrai" Sorting est par nom... mais version et result passe quand même avant, puisque ce ne seront jamais des columns "sort_by"
$array_result = dynamic_multisort($array_result, array('version','responding','servername'), array('SORT_DESC','SORT_DESC','SORT_ASC'));
?>
<p>Players online: <?=$players_online; ?><br /> </p>
<div id="background"><!-- Was to display an image behind the data. --></div>
<table class="server_list" width="960" cellpadding="8" cellspacing="0">
<tr>
<th>Status</th>
<th>Name</th>
<th>Map</th>
<th>Gametype</th>
<th>Players</th>
<?php /*?>
<th style="border-right:#FFFFFF solid 2px; border-bottom:#FFFFFF solid 2px;">Flavor</th><?php */?>
<th style="border-right:0;">Skill</th>
</tr>
<?php
foreach ($array_result as $server)
{
// Generate the "5/16" player output
$output_players = "";
if ($server['playercount'] < 1)
{
$currentplayers = 0;
}
else
{
$currentplayers = $server['playercount'];
}
$output_players = $currentplayers . "/" . $server['maxplayercount'];
// 0 = default - not official server
// 1 = IMF home server (objective mode)
// 2 = REP home server (objective mode)
// 3 = SYN home server (objective mode)
// 4 = Arena (Elimination)
// 5 = Team Arena (Team Elimination)
// 6 = Bounty Arena (First to 500 bounty wins)
// 7 = Training Arena (New players allowed in only)
switch ($server['flavor'])
{
case 1:
$flavor = "IMF home server";
break;
case 2:
$flavor = "REP home server";
break;
case 3:
$flavor = "SYN home server";
break;
case 4:
$flavor = "Arena";
break;
case 5:
$flavor = "Team Arena";
break;
case 6:
$flavor = "Bounty Arena";
break;
case 7:
$flavor = "Training Arena";
break;
default:
$flavor = "Unofficial";
break;
}
// Détecte la version du serveur
// Current : 1.08b
if ($server['version'] <> '1.08b' && $server['version'] <> '')
{
$server['responding'] = "version"; // Pour le switch du Status...
}
// Localhosted game
else if ($server['version'] == '')
{
$server['servername'] = 'Locally hosted server (' . $flavor . ')';
$server['responding'] = 'localhost';
}
// 0 - anyone can join
// 1 - Training Servers - only very new players can join
// 2 - Beginners Only
// 3 - Novices Only
// 4 - Anything above Novice (pro)
// Mettre un code de couleur à barre...
switch ($server['skill'])
{
case 1:
$skill = "Training";
break;
case 2:
$skill = "Beginner";
break;
case 3:
$skill = "Novice";
break;
case 4:
$skill = "Pro";
break;
default:
$skill = "Free";
break;
}
// Status
// Image Led and Output details
$img_alt_text = "";
switch ($server['responding'])
{
case 1:
case "success":
$led_icon = "led_green.gif";
$img_alt_text = "Online";
break;
case 0:
case "timeout":
$led_icon = "led_red.gif";
$img_alt_text = "Timed Out";
$output_players = "";
$skill = "";
$server['name'] = "<em>Server is not responding.</em>";
break;
case "version":
$led_icon = "led_yellow.gif";
$img_alt_text = "Server is running a different version of Hyperbol (" . $server['version'] . ").";
break;
case 2:
case "localhost":
$led_icon = "led_grey.gif";
$img_alt_text = "This game is locally hosted and cannot be pinged.";
break;
default:
$led_icon = "led_grey.gif";
$img_alt_text = "N/A";
$output_players = "";
$skill = "";
break;
}
// Map download Link
$map = strtolower($server['mapname']);
if (file_exists("maps/" . $map . ".hbc"))
{
$map = $map . " (<a href='maps/" . $map . ".hbc'>download</a>)";
}
// ID de TSHB: -applaunch 2720
// steam://run/2720+connect:" . $server['addr']
// Ca boot le jeu, mais ca connecte pas, puisqu'il faut se connecter sur Starport une fois ingame... too bad.
echo "<tr align='center'><td><img src='image/" . $led_icon . "' alt='" . $img_alt_text . "' title='" . $img_alt_text . "' /></td><td>" . $server['servername'] . "</td><td>" . $map . "</td><td>" . $server['gametype'] . "</td><td>" . $output_players . "</td><td>" . $skill . "</td>"; //<td>" . $flavor . "</td>
echo "</tr>";
} // End foreach
unset($server);
?>
</table>
<p class="last_update">List was last updated : <?=date("D F j, Y H:i:s", $updated); ?></p>