コード例 #1
0
// Save schema if not present
if (!file_exists($version_schema)) {
    $schema = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/v2/?key={$apikey}&appid={$appid}");
    file_put_contents($version_schema, $schema);
}
foreach ($servers as $address => $data) {
    echo "Checking {$address}...\n";
    // Load values from array if set
    $port = isset($data['port']) ? $data['port'] : '';
    $rcon_password = isset($data['rcon_password']) ? $data['rcon_password'] : '';
    // Connect to game server
    $server = new \SteamCondenser\Servers\SourceServer($address, $port);
    $server->initialize();
    // Collect data from Steam Condenser
    $getPing = $server->getPing();
    $getPlayers = $server->getPlayers($rcon_password);
    $getRules = $server->getRules();
    $getServerInfo = $server->getServerInfo();
    // Parse version information into different formats for different tools
    $version = $getServerInfo["gameVersion"];
    $version_num = preg_replace("/[^0-9]/", "", $version);
    // Some variables to use in the display
    $playercount = $getServerInfo["numberOfPlayers"];
    $maxplayers = $getServerInfo["maxPlayers"];
    // Check for version number
    $version_check = $version_num == $required_version ? "OK" : "FAIL - Should be {$required_version}";
    // Display the status of this server
    echo "{$address}: \"{$getServerInfo["serverName"]}\": ({$playercount}/{$maxplayers}): {$version}: {$version_check}\n";
    //var_dump($getServerInfo);
    //$getRules,$getPing,$getPlayers);
}
コード例 #2
0
function GetServer($host, $port, $region = '', $cacheonly = 1, $forcerefresh = 0)
{
    global $paths, $columns, $server_file_maxage, $tag_values;
    $ip = gethostbyname($host);
    $server = array('region' => $region, 'info' => array('serverName' => "<b>[LOADING]</b>{$ip}:{$port}", 'mapName' => '', 'numberOfPlayers' => 0, 'maxPlayers' => 0, 'serverTags' => ''), 'tags' => array('g' => '', 'p' => '', 't' => '', 'pure' => ''), 'ipAddress' => $ip, 'port' => $port);
    /*
    	foreach ($columns as $column) {
    		if (!isset($server[$column])) {
    			$server[$column] = '';
    		}
    	}
    */
    $cache_file = "{$paths['servers']}/{$ip}_{$port}.json";
    // Should we use the cache file?
    if (file_exists($cache_file)) {
        $get_cache = filemtime($cache_file) > time() - $server_file_maxage && !$forcerefresh;
        if ($get_cache || $cacheonly) {
            $cache = json_decode(file_get_contents($cache_file), TRUE);
            $server = array_replace_recursive($server, $cache);
            //var_dump($cache,$server);
            //$server['updated'] = filemtime($cache_file);
            // If server has an error, attempt new fetch
            //$get_cache = (!isset($server['error']));
        }
    }
    // If the cached data is not available or has been invalidated, refresh
    if (!$cacheonly && (!$get_cache || $forcerefresh)) {
        try {
            $connection = new \SteamCondenser\Servers\SourceServer($ip, $port);
            $connection->initialize();
            // Collect data from Steam Condenser
            $server['ping'] = $connection->getPing();
            $server['players'] = $connection->getPlayers();
            $server['rules'] = $connection->getRules();
            $server['info'] = $connection->getServerInfo();
            $tags = array_filter(explode(',', $server['info']['serverTags']));
            foreach ($tags as $tag) {
                $bits = explode(':', $tag, 2);
                if (count($bits) == 2) {
                    $server['tags'][$bits[0]] = $tag_values[$bits[0]][$bits[1]] = $bits[1];
                } else {
                    $server['tags'][$tag] = $tag_values[$tag] = "__";
                }
            }
            $server['updated'] = time();
            file_put_contents($cache_file, prettyPrint(json_encode($server)), LOCK_EX);
        } catch (Exception $e) {
            //			echo 'Caught exception: ',  $e->getMessage(), "\n";
            $server['error'] = $e->getMessage();
            file_put_contents($cache_file, prettyPrint(json_encode($server)), LOCK_EX);
        }
    }
    // Skip processing for servers with errors
    if ($region) {
        $server['region'] = $region;
    }
    return $server;
}