Получение списка игроков через сокет

Статус
В этой теме нельзя размещать новые ответы.

AngelOfFear

Пользователь
Регистрация
11 Июн 2017
Сообщения
197
Симпатии
16
Пол
Мужской
Нужен простенький скрипт для получения списка игроков через сокет.
В инете есть такие говнокоды, которые не работают, что не удивительно.
PHP:
<?php
if(isset($_GET['players']))
{
    $timeout = 10;
    $myserver = substr(getenv('REQUEST_URI'),
            strlen($_SERVER['SCRIPT_NAME']) + 1,
            strlen(getenv('REQUEST_URI')));
    $myserver = htmlspecialchars($myserver, ENT_QUOTES);

    list($myserver,) = explode("&", $myserver);
    list($ip, $port) = explode(":", $myserver);

    //--------------------------/----------------------------//
    if(@$fp = @fsockopen("udp://$ip", $port, $errno, $errstr))
    {
        socket_set_timeout($fp,$timeout);
        $challenge_code = "\xFF\xFF\xFF\xFF\x57";
        fwrite($fp, $challenge_code);
        $buffer = fread($fp, 4096);
        $server_status = ($buffer ? 'ON' : 'OFF');
        $challenge_code = substr($buffer, 5, 4);
        $challenge = "\xFF\xFF\xFF\xFFU".$challenge_code;
        fwrite($fp, $challenge);
        $buffer = fread($fp, 4096);

        fclose($fp);
    }
    //--------------------------/----------------------------//
    if ($server_status == "ON"){

        $player_number = 0;
        //$player[0]['name'] = "<b>Имя:</b>";
        //$player[0]['score'] = "<b>Фраги:</b>";
        $position = 6;
        do
        {
            $player_number++;
            $player[$player_number]['id'] = ord($buffer[$position]);
            $position ++;

            while($buffer[$position] != "\x00" && $position < 4000)
            {
                $player[$player_number]['name'] .= $buffer[$position];
                $position ++;
            }
            $player[$player_number]['score'] = (ord($buffer[$position + 1]))
            + (ord($buffer[$position + 2]) * 256)
            + (ord($buffer[$position + 3]) * 65536)
            + (ord($buffer[$position + 4]) * 16777216);
            if ($player[$player_number]['score'] > 2147483648) { $player[$player_number]['score'] -= 4294967296; }
            $position += 9;
        }
        while ($position < strlen($buffer));
        echo "<table height=\"100%\" width=\"100%\"><tr><td valign=\"middle\" align=\"center\">";
        echo "<table width=\"100%\">\r\n";
        echo "<tr><td align=\"center\" style=\"border:2px dotted;margin:5px;
padding-left:5px;padding-right:5px;padding-top:5px;padding-bottom:5px;font-family:tahoma;font-size:11px;background:#E5EBF3;
color:#6E6E6E;line-height:12px;\"><b>Имя:</b></td><td style=\"border:2px dotted;margin:5px;padding-left:5px;padding-right:5px;
padding-top:5px;padding-bottom:5px;font-family:tahoma;font-size:11px;background:#E5EBF3;color:#6E6E6E;line-height:12px;\" align=\"center\"><b>Фраги:</b></td></tr>";
        if( empty($player[1]['name']) )
        {
            echo "<tr><td align=\"center\" style=\"border:1px solid #2c2c2c;margin:5px;padding-left:5px;
padding-right:5px;padding-top:5px;padding-bottom:5px;font-family:tahoma;font-size:11px;color:#6E6E6E;line-height:12px;\"> -- </td>
<td align=\"center\" style=\"border:1px solid;margin:5px;padding-left:5px;padding-right:5px;padding-top:5px;padding-bottom:5px;font-family:tahoma;font-size:11px;color:#6E6E6E;line-height:12px;\"> -- </td></tr>\r\n";
            echo "<tr><td align=\"center\" colspan=\"2\" style=\"font-family:tahoma;font-size:11px;color:#6E6E6E;line-height:12px;\">На сервере нет игроков</td></tr>";
        }
        else
        for($i=1; isset($player[$i]['name']); $i++)
        {
            echo "<tr><td align=\"center\" style=\"border:1px solid #2c2c2c;margin:5px;padding-left:5px;
padding-right:5px;padding-top:5px;padding-bottom:5px;font-family:tahoma;font-size:11px;color:#6E6E6E;line-height:12px;\">
".htmlspecialchars($player[$i]['name'], ENT_QUOTES)."</td><td align=\"center\" style=\"border:1px solid;
margin:5px;padding-left:5px;padding-right:5px;padding-top:5px;padding-bottom:5px;font-family:tahoma;font-size:11px;color:#6E6E6E;line-height:12px;\">
".$player[$i]['score']."</td></tr>\r\n";
            $i++;
            if(isset($player[$i]['name']))echo "<tr>
<td align=\"center\" style=\"border:1px solid #2c2c2c;margin:5px;padding-left:5px;padding-right:5px;padding-top:5px;
padding-bottom:5px;font-family:tahoma;font-size:11px; background:#EEE; color:#6E6E6E;line-height:12px;\">".htmlspecialchars($player[$i]['name'], ENT_QUOTES)."</td>
<td align=\"center\" style=\"border:1px solid #2c2c2c;margin:5px;padding-left:5px;padding-right:5px;padding-top:5px;
padding-bottom:5px;font-family:tahoma;font-size:11px;background:#EEE;color:#6E6E6E;line-height:12px;\">".$player[$i]['score']."</td></tr>\r\n";
        }
        echo "</table><a OnClick=\"window.close()\" href=\"#\" style=\"color:#666666;text-align:center;text-decoration:underline;font-size:13px;font-weight:bold;\">Закрыть</a></td></tr></table>\r\n";
    }
    exit();
}

$timeout = $_GET['timeout'] > 100 ? 10 : $_GET['timeout'];
$my = substr(substr(getenv('REQUEST_URI'),strlen($_SERVER['SCRIPT_NAME'])),1);
list($my,) = explode('?', $my);
$my = str_replace("%20","",$my);
$servers = explode(',',$my);
$out = <<<EOF
var mystatusbar = document.getElementById("statusbar");
var out='';
var OldColors = new Array(15);
var OldColor;


function lightCell(cell)
{
    OldColor = cell.style.backgroundColor;
    cell.style.backgroundColor = CellBackground;
}

function darkCell(cell)
{
    cell.style.backgroundColor = OldColor;
}

EOF;

$out .= "\r\n\r\n";
$out .= "out += \"<div class='monitoring'><table align='center' class='monitoring'><tr>\";\r\n";

foreach ($servers as $server)
{
    list($ip, $port) = explode(":", $server);unset($server);

    if( $server = @fsockopen( "udp://".$ip, (int) $port, $errno, $errstr ) )
    {
           $write = "\xFF\xFF\xFF\xFFTSource Engine Query\x00";
           socket_set_timeout($server,$timeout);
           fwrite($server, $write);
           $server_response = fread($server,4096);
           fclose($server);
          $server_status = ($server_response ? 'ON' : 'OFF');
    }

    if($server_status == 'ON')
    {
        $server_response = substr($server_response, 5);
        $server_info = explode(chr(0x00), $server_response);
        $server_info['hostname'] = $server_info['1'];
        if(strlen($server_info['hostname']) >= ($kk=35))
            $server_info['hostname'] = str_replace(substr($server_info['hostname'], $kk, strlen($server_info['hostname'])),'...', $server_info['hostname']);
        $server_info['address'] = "$ip:$port";
        $server_info['map'] = $server_info['2'];
        if(strlen($server_info['map']) >= ($kk=11))
            $server_info['map'] = str_replace(substr($server_info['map'], $kk, strlen($server_info['map'])),'...', $server_info['map']);
        $server_info['players'] = ord(substr($server_info[5], 0, 1));
        $server_info['max'] = $server_info['players'] == 0 ? ord(substr($server_info[6], 0, 1)) : ord(substr($server_info[5], 1, 1));
        $server_status = "<b><font color='green'>($server_status)</font></b>";
    }

    if($server_status == 'OFF')
    {
        $server_info['hostname'] = "Выкл;)";
        $server_info['address'] = "$ip:$port";
        $server_info['map'] = "unknown";
        $server_info['players'] = "0";
        $server_info['max'] = "0";
        $server_status = "<b><font color='red'>($server_status)</font></b>";
    }

    if($server_status)
        if($server_info['players'])
            $out .= "out += \"<td align='center' class='monitoring' onmouseover='lightCell(this);' onmouseout='darkCell(this);'
onclick='window.open(\\\"http://monitoring.c-s.net.ua/statusbar.php?$ip:$port&players\\\", \\\"Players :: $ip:$port\\\", \\\"width=260, height=390, menubar=no, scrollbars=1,resizable=1\\\");' >
".$server_info['hostname']."<br />$ip:$port $server_status<br /><b>Map:</b> ".$server_info['map']."; <b>Players:</b> ".$server_info['players']."/".$server_info['max']."</td>\";\r\n";
        else
            $out .= "out += \"<td align='center' class='monitoring' onmouseover='lightCell(this);' onmouseout='darkCell(this);' onclick='my();' >
".$server_info['hostname']."<br />$ip:$port $server_status<br /><b>Map:</b> ".$server_info['map']."; <b>Players:</b> ".$server_info['players']."/".$server_info['max']."</td>\";\r\n";
    unset($server_status);
}

$out .= "out += \"</tr></table>\";\r\n";
$out .= "out += \"</div>\";\r\n";
$out .= "mystatusbar.innerHTML = out;\r\n";
$out .= "\r\n\r\n// c-s.net.ua © Lavrik";
echo $out;
?>
 

AngelOfFear

Пользователь
Регистрация
11 Июн 2017
Сообщения
197
Симпатии
16
Пол
Мужской
Тему можно закрыть или удалить. Нашел такой код, переделаю всё и доведу до ума. Главное, что работает.
PHP:
<?php
    list($ip, $port) = explode(":", $_GET["server"]);
    $fp = @fsockopen("udp://".$ip, $port, $errno, $errstr);
   
    $server_name = "unknown";
    $server_online = "<font style=\"color: red;\">Выкл.</font>";
    $server_player = "??";
    $server_maxplayer = "??";
    $server_mapname = "unknown";
    $mapimg = "nomap.png";
   
    if($fp)
    {
        stream_set_timeout($fp, 2);
       
        fwrite($fp, "\xFF\xFF\xFF\xFFTSource Engine Query\x00");
        $buffer = fread($fp, 8096);
        fclose($fp);
       
        if($buffer)
        {
            $tmp = explode("\x00", $buffer);
            $place = strlen($tmp[0].$tmp[1].$tmp[2].$tmp[3].$tmp[4]) + 5;
            $version = ord($buffer[4]);
            $server_online = "<font style=\"color: green;\">Вкл.</font>";
            $server_player = ord($buffer[$place]);
            $server_maxplayer = ord($buffer[$place + 1]);
            $mapimg = "nomap.png";
           
            if($version == 109)
            {
                $server_name = $tmp[1];
                $server_mapname = $tmp[2];
            }
            else
            {
                $server_name = substr($tmp[0], 6);
                $server_mapname = $tmp[1];
            }
           
            if(file_exists("maps/". $server_mapname .".jpg")) {$mapimg = "$server_mapname.jpg";}
        }
    }
   
    function playersInfo()
    {
        $player = array();
        global $ip, $port;
        $fp = @fsockopen("udp://".$ip, $port);
       
        if($fp)
        {
            stream_set_timeout($fp, 2);
           
            fwrite($fp, "\xFF\xFF\xFF\xFF\x55\xFF\xFF\xFF\xFF");
            $buffer = fread($fp, 4096);
           
            if(trim($buffer))
            {
                fwrite($fp, "\xFF\xFF\xFF\xFF\x55".substr($buffer, 5, 4));
                $buffer = fread($fp, 4096);
               
                if($buffer)
                {
                    $player_number = 0;
                    $position = 2;
                    do
                    {
                        $player_number++;
                        $player[$player_number]['name']= "";
                        $player[$player_number]['id'] = ord($buffer[$position]);
                        $position++;
                       
                        while($buffer[$position] != "\x00" && $position < 400000)
                        {
                            $player[$player_number]['name'] .= $buffer[$position];
                            $position++;
                        }
                       
                        $player[$player_number]['score'] = (ord($buffer[$position + 1]))
                        + (ord($buffer[$position + 2]) * 256)
                        + (ord($buffer[$position + 3]) * 65536)
                        + (ord($buffer[$position + 4]) * 16777216);
                       
                        if($player[$player_number]['score'] > 2147483648) $player[$player_number]['score'] -= 4294967296;
                        if($player[$player_number]['score'] > 100) $player[$player_number]['score'] = 0;
                        $time = substr($buffer, $position + 5, 4);              
                        if(strlen($time) < 4)
                            return FALSE;            
                        list(,$time) = unpack("f", $time);              
                        $time = mktime(0, 0, $time);                        
                        $player[$player_number]['time'] = date("H:i:s", $time);
                        $position += 9;
                    }
                    while($position < strlen($buffer));                  
                    return $player;
                }
            }
        }
    }
   
    header("Content-Type: text/javascript");
    $pl_score = "''";
    $pl_time = "''";
    $players_info = playersInfo();
    $i = 0;
    echo "var pl_name = new Array(''";
    foreach($players_info as $player)
    {
        if($player["name"])
        {
            $player["name"] = str_replace("\n", "", $player["name"]);
            $player["name"] = str_replace("\r", "", $player["name"]);
           
            $i++;
           
            $pl_score .= ", '$player[score]'";
            $pl_time .= ", '$player[time]'";
           
            echo ", '". htmlspecialchars($player["name"], ENT_QUOTES) ."'";
        }
    }
    echo ");\n";
    echo "var pl_score = new Array($pl_score);\n";
    echo "var pl_time = new Array($pl_time);\n";
    echo "var players = $i;\n";
    echo "var info = {";
    echo "'ip_port':'$ip:$port', 'hostname':'$server_name', 'map':'$server_mapname', 'mapimg':'<img src=\"../images/maps/$mapimg\" alt=\"\"/>', 'players':'$server_player', 'maxplayers':'$server_maxplayer', 'status':'$server_online'";
    echo "};";
 

neygomon

Его Темнейшество :j
Заблокированные
Пользователь
Регистрация
8 Июн 2017
Сообщения
804
Симпатии
382
Пол
Мужской
- глаза
 

neygomon

Его Темнейшество :j
Заблокированные
Пользователь
Регистрация
8 Июн 2017
Сообщения
804
Симпатии
382
Пол
Мужской

AngelOfFear

Пользователь
Регистрация
11 Июн 2017
Сообщения
197
Симпатии
16
Пол
Мужской
Да ну нафиг всё это дерьмо. Я лучше так: :D
PHP:
<?php
$fp = @fsockopen('udp://IP', 27015);
if($fp) {
    stream_set_timeout($fp, 2);
    fwrite($fp, "\xFF\xFF\xFF\xFF\x55\xFF\xFF\xFF\xFF");
    $buffer = fread($fp, 9);
    fwrite($fp, "\xFF\xFF\xFF\xFF\x55".substr($buffer, 5, 4));
    $buffer = fread($fp, 4096);
    fclose($fp);
    $buffer = substr($buffer, 6);
    $i = 1;
    $position = 1;
    while($position < strlen($buffer)) {
        $players[$i]['name'] = '';
        while($buffer[$position] != "\x00" && $position < 400000) {
            $players[$i]['name'] .= $buffer[$position];
            $position++;
        }
        $players[$i]['name'] = str_replace("\n", '', $players[$i]['name']);
        $players[$i]['name'] = str_replace("\r", '', $players[$i]['name']);
        $players[$i]['name'] = htmlspecialchars($players[$i]['name'], ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML5);
        $players[$i]['score'] = (ord($buffer[$position+1])) + (ord($buffer[$position+2])*256) + (ord($buffer[$position+3])*65536) + (ord($buffer[$position+4])*16777216);
        if($players[$i]['score'] > 2147483648) $players[$i]['score'] -= 4294967296;
        if($players[$i]['score'] > 100) $players[$i]['score'] = 0;
        $time = substr($buffer, $position + 5, 4);
        list(,$time) = unpack('f', $time);
        $time = mktime(0, 0, $time);                     
        $players[$i]['time'] = date('H:i:s', $time);
        $i++;
        $position += 10;
    }
echo '<table><tr><td>Ник<td>Убийств<td>Длительность';
foreach($players as $player)
    echo '<tr><td>',$player['name'],'<td>',$player['score'],'<td>',$player['time'];
echo '</table>';}?>
Результат:
 

Вложения

Последнее редактирование:

breakt

Заблокированные
Регистрация
8 Июн 2017
Сообщения
109
Симпатии
33
@AngelOfFear,есть еще steam-condenser годная штука
 
Статус
В этой теме нельзя размещать новые ответы.
Сверху Снизу