- Offizieller Beitrag
1. Composer installieren (frisches Ubuntu)
sudo apt update
sudo apt install composer -y
2. Projektordner erstellen
mkdir -p /var/www/html/serverview
cd /var/www/html/serverview
3. xPaw installieren
composer require xpaw/php-source-query-class
4.Template-Ordner erstellen
mkdir -p /var/www/html/serverview/template
5. Template-Datei erstell
nano /var/www/html/serverview/template/cs2.html.php
<?php
$SQ_TIMEOUT = 5;
$Query = new \xPaw\SourceQuery\SourceQuery();
$Info = [];
$Players = [];
$Bots = 0;
try
{
$Query->Connect($SQ_SERVER_ADDR, $SQ_SERVER_PORT, $SQ_TIMEOUT, $SQ_ENGINE);
$Info = $Query->GetInfo();
$Players = $Query->GetPlayers();
}
catch( Exception $e )
{
return "CS2 Server derzeit nicht erreichbar";
}
finally
{
$Query->Disconnect();
}
if( empty($Info) )
{
return "CS2 Server derzeit nicht erreichbar";
}
$Bots = array_key_exists('Bots', $Info) ? (int)$Info['Bots'] : 0;
$mapWithoutPath = explode("/", $Info["Map"]);
$map = $mapWithoutPath[count($mapWithoutPath) - 1];
$imageFile = $map . ".png";
$relMapUrl = $REL_URL_IMAGES . $imageFile;
$absMapPath = $ABS_PATH_IMAGES . $imageFile;
$timestamp = date("Y-m-d H:i:s");
if (is_array($Players)) {
foreach ($Players as &$player) {
if (!isset($player["TimeF"])) {
$seconds = isset($player["Time"]) ? (int)$player["Time"] : 0;
$hours = floor($seconds / 3600);
$minutes = floor(($seconds % 3600) / 60);
$secs = $seconds % 60;
$player["TimeF"] = sprintf('%02d:%02d:%02d', $hours, $minutes, $secs);
}
}
unset($player);
}
ob_start();
?>
<a href="http://store.steampowered.com/app/730/" target="_blank">
<img height="16" width="16" src="/images/icons/cs2.png"/>
</a>
<a href="steam://connect/<?php echo $SQ_CONNECT_ADDR; ?>" target="_top">
<?php echo htmlspecialchars($Info["HostName"]); ?>
</a><br/>
<?php if(file_exists($absMapPath)): ?>
<div style="text-align:center;">
<img src="<?php echo $relMapUrl; ?>" width="100%">
</div>
<?php endif; ?>
<table style="width:100%;">
<tr>
<td>Map</td>
<td style="text-align:right;"><?php echo htmlspecialchars($map); ?></td>
</tr>
<tr>
<td>Spieler</td>
<td style="text-align:right;">
<?php echo $Info["Players"]; ?> (<?php echo $Bots; ?> Bots) / <?php echo $Info["MaxPlayers"]; ?>
</td>
</tr>
</table>
<?php if(!empty($Players)): ?>
<table style="width:100%;">
<tr><th>Name</th><th>Kills</th><th>Zeit</th></tr>
<?php foreach($Players as $p): ?>
<tr>
<td><?php echo htmlspecialchars($p["Name"]); ?></td>
<td><?php echo (int)$p["Frags"]; ?></td>
<td><?php echo htmlspecialchars($p["TimeF"]); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<?php
return ob_get_clean();
Alles anzeigen
6. Loader erstellen
nano /var/www/html/serverview/server_view_load_HL2.php
Inhalt
<?php
$ABS_PATH_IMAGES = "/var/www/html/images/maps_cs2/";
$REL_URL_IMAGES = "/images/maps_cs2/";
$templateFile = __DIR__ . "/template/cs2.html.php";
$outputDir = "/var/www/html/cache/serverview/";
mkdir -p($outputDir);
require_once __DIR__ . '/vendor/autoload.php';
$SQ_SERVER_ADDR = "127.0.0.1";
$SQ_ENGINE = \xPaw\SourceQuery\SourceQuery::SOURCE;
/* SERVER 1 */
$SQ_SERVER_PORT = 27015;
$SQ_CONNECT_ADDR = "84.118.93.219:27015";
$html = include $templateFile;
file_put_contents($outputDir . "cs2.html", $html);
/* SERVER 2 */
$SQ_SERVER_PORT = 27016;
$SQ_CONNECT_ADDR = "84.118.93.219:27016";
$html = include $templateFile;
file_put_contents($outputDir . "cs2--2.html", $html);
Alles anzeigen
7. Cache-Ordner erstellen
mkdir -p /var/www/html/cache/serverview
chmod -R 775 /var/www/html/cache
8. Script einmal ausführen
php /var/www/html/serverview/server_view_load_HL2.php
9. Im Browser öffnen
https://deine-domain/cache/serverview/cs2.html
10. Cronjob (automatisch alle 3 Minuten)
crontab -e
*/3 * * * * /usr/bin/php /var/www/html/serverview/server_view_load_HL2.php
Fertig
Du hast jetzt:
✔ Composer installiert
✔ xPaw installiert
✔ Template-System
✔ mehrere Server
✔ Cache-System
✔ automatische Updates