ramirez
November 7, 2005, 5:03 pm
Here's a simple function I wrote to parse the REFRESH packet in PHP:
[code]<?php
function GetSoldatInfo(&$sock) {
if (!$sock) return false;
$info = array(
'gamemode' => 0,
'teammode' => false,
'pointmode' => false,
'players' => 0,
'spectators' => 0,
'map' => '',
'timelimit' => 0,
'currenttime' => 0,
'timeleft' => 0,
'limit' => 0,
'player' => array(),
'spectator' => array(),
'team' => array()
);
// Temporary array for players
$players = array();
for ($i = 0; $i < 32; $i++) {
$players[$i] = array(
'name' => '',
'ip' => '',
'id' => 0,
'kills' => 0,
'deaths' => 0,
'team' => 0,
'ping' => 0
);
}
// Get player names
for ($i = 0; $i < 32; $i++) {
$data = fread($sock, 25);
$len = ord($data[0]);
$players[$i]['name'] = substr($data, 1, $len);
}
// Get player teams
for ($i = 0; $i < 32; $i++) {
$data = fread($sock, 1);
$players[$i]['team'] = ord($data);
}
// Get player kills
for ($i = 0; $i < 32; $i++) {
$data = unpack("v", fread($sock, 2));
$kills = $data[1];
$players[$i]['kills'] = $kills;
}
// Get player deaths
for ($i = 0; $i < 32; $i++) {
$data = unpack("v", fread($sock, 2));
$deaths = $data[1];
$players[$i]['deaths'] = $deaths;
}
// Get player pings
for ($i = 0; $i < 32; $i++) {
$data = fread($sock, 1);
$players[$i]['ping'] = ord($data);
}
// Get player IDs
for ($i = 0; $i < 32; $i++) {
$data = fread($sock, 1);
$players[$i]['id'] = ord($data);
}
// Get player IPs
for ($i = 0; $i < 32; $i++) {
$data = unpack("N", fread($sock, 4));
$players[$i]['ip'] = long2ip($data[1]);
}
// Get team scores
for ($i = 1; $i < 5; $i++) {
$data = unpack("v", fread($sock, 2));
$score = $data[1];
$info['team'][$i] = $score;
}
// Get map name
$data = fread($sock, 17);
$len = ord($data[0]);
$info['map'] = substr($data, 1, $len);
// Get time limit & current time, and form timeleft
$data = unpack("V", fread($sock, 4));
$timelimit = $data[1];
$info['timelimit'] = $timelimit;
$data = unpack("V", fread($sock, 4));
$currenttime = $data[1];
$info['currenttime'] = $timelimit - $currenttime;
$timeleft = $currenttime;
$temp = (floor($timeleft / 60) % 60);
$info['timeleft'] = floor($timeleft / 3600) . ':' . ($temp < 10 ? '0' : '') . $temp;
// Get kill limit
$data = unpack("v", fread($sock, 2));
$limit = $data[1];
$info['limit'] = $limit;
// Get gamestyle
$data = fread($sock, 1);
$gamestyle = ord($data);
$info['gamemode'] = $gamestyle;
if ($gamestyle == 2 || $gamestyle == 3 || $gamestyle == 5 || $gamestyle == 6) {
$info['teammode'] = true;
if ($gamestyle != 2) { $info['pointmode'] = true; }
}
if ($gamestyle != 2) {
if ($gamestyle != 3 && $gamestyle != 5) {
unset($info['team'][1]);
unset($info['team'][2]);
}
unset($info['team'][3]);
unset($info['team'][4]);
}
foreach ($players as $player) {
if ($player['team'] < 5) {
$info['players']++;
$info['player'][] = $player;
}
else if ($player['team'] == 5) {
$info['spectators']++;
$info['spectator'][] = $player;
}
}
return $info;
}
?>[/code]
Example usage:
[code]<?php
$s = @fsockopen("localhost", 23073);
if ($s) {
$info = array();
fputs($s, "password\n);
fputs($s, "REFRESH\n");
while(1) {
$data = trim(fgets($s, 1024));
if (!$data) break;
else if ($data == "REFRESH") {
$info = GetSoldatInfo($s);
break;
}
}
fclose($s);
// Make sure the REFRESH packet was received
if ($info) {
// You can now use the $info array to check different stuff from server, check the function for the array's elements
// Remember that the gamemode value is a number, so to get the string do something like:
$gamemodes = array(
0 => 'Deathmatch',
1 => 'Pointmatch',
2 => 'Teammatch',
3 => 'Capture The Flag',
4 => 'Rambomatch',
5 => 'Infiltration',
6 => 'Hold The Flag'
);
$gamemode_str = $gamemodes[$info['gamemode']];
}
}
?>[/code]
And another note, the $info['timeleft'] is actually a string, in form of mins:secs, because I only needed to use it in this form. If you need an integral value in seconds, or ticks, it's easy enough to edit to have it in that form.
mar77a
November 8, 2005, 1:07 am
Awesome! Thanks to ramirez, you can now see who's playing, get map limit, etc, etc. Thx a bunch.
Vijchtidoodah
November 8, 2005, 1:18 am
"Here's a simple function I wrote to parse the REFRESH packet in PHP:"
Hahahaha, if that's simple, I don't want to see your complex codes.
By the way, good job mar77a!
ramirez
November 8, 2005, 10:07 am
It is relatively simple. :P
Anyway, glad to see that I could be of help.
Nice app you have made.
Good job. :)
mar77a
November 8, 2005, 11:30 am
Yeah, ill try and make a stable 1.0 with a bit more style today.
i am ahab
November 8, 2005, 12:14 pm
ah ty ram, i was wanting to parse the refresh, when i get time to start on my app again. that'll help me a lot.
cheers dude!
rabidhamster
November 8, 2005, 10:37 pm
this is very secksy, it'll be very helpful once i get my 24/7 server up and running.
well done, kudos to ramirez.
mar77a
November 8, 2005, 11:44 pm
Hmn, looks like i have found a safe way to listen to the server so expect version 1.1 out soon. :)
Leo
December 19, 2005, 4:04 pm
mar77a, is it possible to include the status in a web page, but just show the general info ? Like the following image:
[IMAGE]
*Of course no IP's should be be visible.
mar77a
December 19, 2005, 5:00 pm
Yeah, it is, but i'll tell you on MSN if you want too, not here.
Leo
December 19, 2005, 7:25 pm
quote:Originally posted by mar77aYeah, it is, but i'll tell you on MSN if you want too, not here.
ยจ
Alright, talk to you there :)
Deleted User
April 12, 2006, 1:07 am
check out solperl (http://solperl.phpgamedesign.org)
for a perl version of this (refresh.pl)
it converts the packet into XML format :P
kills and deaths are buggy, but hopefully you can help fix that!