RealTime Server Statusをサイトに張り付ける

qstatを利用してゲームサーバーから情報を吸い出してPHPを使って表示させる方法

というか当暇つぶしサーバーのTOYBOX60に使用している生データー。

gamespyやgametrackerなどを使わずスタンドアローンで完結。

スタイルシートが混ざってるので適宜サイトの変数なりに置き換えてください。

phpが汚いとかexec使うなとか苦情は勘弁してください。管理者は独学なので細かい質問には答えられない場合があります。

訪問者が多いページ、下品な検索エンジンが来る場合はページキャッシュを調整してください。ロードアベレージが痛い事になります。

<?php

$pid = exec("ps aux | grep etded.x86 | grep server1 | grep -v \"sh\" | awk '{print $2}'");

if (empty($pid)) {
	echo "<table width=\"100%\" class=\"status-table\">";
	echo "<tr>\n";
	echo "<td>\n";
	include('statusET.inc');
	echo "<br />\n";
	echo "<font color='ff0000'>Suspended</font><br /><br />\n";
	echo "</td>\n</tr>\n</table>\n";
    } else {
    $etstarttime = exec("ps u $pid | awk '{print $9}'");
    $etruntime = exec("ps -p $pid -o etime");
    $null = "START";
    exec("/usr/local/bin/qstat -raw , -rws 127.0.0.1:27960 -R", $qserver, $emp);
	

    $ets = explode(",", $qserver[0]);
	$ets1 = explode(",", $qserver[1]);
    $pb = preg_replace("/punkbuster=/", "", $ets1[9]);
    $team = preg_replace("/balancedteams=/", "", $ets1[13]);
    $weap = preg_replace("/weaprestrict=/", "", $ets1[12]);
    $lag = preg_replace("/g_antilag=/", "", $ets1[11]);
    $pass = preg_replace("/needpass=/", "", $ets1[8]);
    $live = preg_replace("/maxlives=/", "", $ets1[7]);
    $ff = preg_replace("/friendlyFire=/", "", $ets1[6]);
    $team2 = preg_replace("/P=/", "", $ets1[15]);
    $team3 = preg_replace("/-/", "", $team2);

    exec("/usr/local/bin/qstat -rws 127.0.0.1:27960 -P | grep 'frags'", $lines ,$emp);
    $CRow = "Textback";
    $gpass = file_get_contents("/var/www/html/toybox_pass.inc");

    echo "<table width=\"100%\" class=\"status-table\">\n";
    echo "<tr>\n<td width=\"50%\"><img alt='$ets[3]' title='$ets[3]' src=\"./images/et/$ets[3].jpg\"><br />$ets[3]</td>\n";
    echo "<td width=\"50%\">Socket:$ets[5]/$ets[4]\n"; 
    echo "<br />Password:$gpass<br />";

    if ($pb == 1) {
        echo "<img alt='Punkbuster Enable' title='Punkbuster Enable' src=\"./images/pb.png\" width=\"27\" height=\"27\">";
        } else {
        echo "";
    }

    if ($ff == 1) {
        echo "<img alt='Friendly Fire' title='Friendly Fire' 
        src=\"./images/ff.png\" width=\"27\" height=\"27\">";
    } else {
        echo "";
    }

    if ($team == 1) {
        echo "<img alt='Force Team Balance' title='Force Team BalanceForce' src=\"./images/balance.png\" width=\"27\" height=\"27\">";
    } else {
        echo "";
    }

    if ($weap < 100) {
        echo "<img alt='Heavy Weapons Restriction' title='Heavy Weapons Restriction' src=\"./images/weap.png\" width=\"27\" height=\"27\">";
    } else {
        echo "";
    }

    if ($lag == 1) {
        echo "<img alt='AntiLag Enable' title='AntiLag Enable' src=\"./images/antilag.png\" width=\"27\" height=\"27\">";
    } else {
        echo "";
    }

    if ($pass == 1) {
        echo "<img alt='Private Server' title='Private Server' src=\"./images/pass.png\" width=\"27\" height=\"27\">";
    } else {
        echo "";
    }

    if ($live == 1) {
        echo "<img alt='MaxLives Enable' title='MaxLives Enable' src=\"./images/live.png\" width=\"27\" height=\"27\">";
    } else {
        echo "";
    }

    echo "</td>\n</tr>\n</table>\n";
    echo "<table width='100%' class='status-table'>\n<tr>\n";
    echo "<td class='$CRow'>No.</td>\n";
    echo "<td class='$CRow'>Player Name</td>\n";
    echo "<td class='$CRow'>Team</td>\n";
    echo "<td class='$CRow'><div style='text-align:right; font-size:100%'>Score</div></td>\n";
    echo "<td class='$CRow'><div style='text-align:right; font-size:100%'>ping</div></td>\n";
    echo "</tr>\n";

    foreach ($lines as $line_num => $line) {
        if ($CRow == "Textback"){
            $CRow = "Text";
        } else {
            $CRow = "Textback";
        }
        $byte = strlen($line);
        $ping2 = strpos($line, 'ms');
        $ping = substr($line, $ping2 - 4, 6);
	$ping = trim($ping, " \t\n\r\0\x0B");
        $name2 = substr($line, $ping2 + 4, $byte - $ping2 + 4);
        $frags2 = strpos($line, 'fr');
        $frags = substr($line, 0, $frags2);
	$frags = trim($frags, " \t\n\r\0\x0B");
        $number = ($line_num + 1);
        $name = htmlspecialchars ($name2);
        $team4 = substr($team3, $line_num, 1);

        if ($team4 == 0) {
            $team = "Waiting";
        } else {
            if ($team4 == 1) {
                $team = "Axis";
            } else {
                if ($team4 == 2) {
                    $team = "Allies";
                } else {
                    $team = "Spect";
                }
            }
        }

    $name = mb_strimwidth($name, 0, 17, "...");
    echo "<tr>\n";
    echo "<td class='$CRow'>$number</td>\n";
    echo "<td class='$CRow'>$name</td>\n";
    echo "<td class='$CRow'>$team</td>\n";
    echo "<td class='$CRow'><div style='text-align:right; font-size:100%'>$frags</div></td>\n";
    echo "<td class='$CRow'><div style='text-align:right; font-size:100%'>$ping</div></td>\n";

    }

echo "<tr>\n<td colspan='5'>boot at $etstarttime. $etruntime times running.</td>\n";
echo "</tr>\n</table>\n";

}

?>

多分コピペでは動かないとおもいます。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です