Skip to content

Instantly share code, notes, and snippets.

@beheh
Created December 20, 2014 22:47
Show Gist options
  • Save beheh/b2eaf757d1c9bfd79a03 to your computer and use it in GitHub Desktop.
Save beheh/b2eaf757d1c9bfd79a03 to your computer and use it in GitHub Desktop.
CMC Gameparser
<?php
$list_count = 3;
$debug = false;
if($debug) {
header('Content-Type: text/plain; charset=utf-8');
} else {
header('Content-Type: text/css; charset=utf-8');
}
if(!$debug) {
if(filemtime('cache.css') > time() - 60) {
echo file_get_contents('cache.css');
exit();
}
if(file_exists('cache.css.lock')) {
if(filemtime('cache.css.lock') < time() - 5) {
unlink('cache.css.lock');
} else {
sleep(1);
}
if(!file_exists('cache.css.lock')) {
echo file_get_contents('cache.css');
}
exit();
}
}
file_put_contents('cache.css.lock', true);
require('ParseINI.php');
require('C4Network.php');
//get references
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'league.clonkspot.org');
curl_setopt($curl, CURLOPT_USERAGENT, 'CMCPublicGameparser/1.0');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
$raw = curl_exec($curl);
curl_close($curl);
//check for faults
if($raw === false) {
$result = '/* No connection to league.clonkspot.org at ' . date('d-m-Y H:i:s') . ' */';
file_put_contents('cache.css', $result);
echo $result;
unlink('cache.css.lock');
exit();
}
//parse references
$lines = explode("\n", $raw);
$header = true;
$reference = 0;
$references = array();
foreach($lines as $line) {
$line = trim($line);
if(empty($line))
continue;
if($line == '[Reference]') {
if($header) {
$header = false;
} else {
$reference++;
}
$references[$reference] = "";
}
if($header)
continue;
$references[$reference] .= $line . PHP_EOL;
}
$scenarios = new SimpleXMLElement(file_get_contents('scenarios/scenarios.xml'));
$parsed_references = array();
$global_playercount = 0;
foreach($references as $reference) {
//prefiltering
$resource_list = ParseINI::parseValuesByCategory('Filename', 'Resource', $reference);
$own = false;
foreach($resource_list as $resource) {
if($resource == 'ModernCombat.c4d') {
$own = true;
break;
}
}
if(!$own)
continue;
$valid = false;
//validate other data
$parsed_reference = array();
$filename = ParseINI::parseValue('Filename', $reference);
$parsed_reference['filename'] = str_replace('\\', '/', $filename);
$parsed_reference['filesize'] = ParseINI::parseValue('FileSize', $reference);
$parsed_reference['filecrc'] = ParseINI::parseValue('FileCRC', $reference);
$parsed_reference['contentscrc'] = ParseINI::parseValue('ContentsCRC', $reference);
foreach($scenarios as $scenario) {
if($parsed_reference['filename'] != $scenario->filename)
continue;
if($parsed_reference['filesize'] != $scenario->filesize)
continue;
if($parsed_reference['filecrc'] != $scenario->filecrc)
continue;
if($parsed_reference['contentscrc'] != $scenario->contentscrc)
continue;
$parsed_reference['image'] = $scenario->image;
$parsed_reference['title'] = $scenario->title;
$valid = true;
break;
}
//known scenario?
if(!$valid)
continue;
//get details
$time = ParseINI::parseValue('Time', $reference);
$starttime = ParseINI::parseValue('StartTime', $reference);
$state = ParseINI::parseValue('State', $reference);
$state_string = '';
switch($state) {
case 'Lobby':
$state_string = 'In der Lobby';
if($time) {
$state_string .= ' (seit ' . gmdate('G:i:s', (time() + 88) - $starttime) . ')';
}
$parsed_reference['priority'] = 3;
break;
case 'Running':
$state_string = 'Im Spiel';
if($time)
$state_string .= ' (seit ' . gmdate('G:i:s', $time) . ')';
$parsed_reference['priority'] = 2;
break;
case 'Paused':
$state_string = 'Pausiert';
if($time) {
$state_string .= ' (im Spiel seit ' . gmdate('G:i:s', $time) . ')';
}
$parsed_reference['priority'] = 2;
break;
default:
$state_string = 'Unbekannt';
$parsed_reference['priority'] = 1;
break;
}
$parsed_reference['state'] = $state_string;
$client_names = ParseINI::parseValuesByCategory('Name', 'Client', $reference);
$parsed_reference['host'] = $client_names[0];
//filter out download server
if($parsed_reference['host'] == "Download Server (Vanes)")
continue;
$player_list = ParseINI::parseValuesByCategory('Name', 'Player', $reference);
$players = '';
$playercount = 0;
foreach($player_list as $player) {
$playercount++;
if(!empty($players))
$players .= ', ';
$players .= trim(C4Network::cleanString($player));
}
//empty servers are boring
if(!$playercount)
continue;
$global_playercount += $playercount;
$parsed_reference['playercount'] = $playercount;
$parsed_reference['players'] = $players;
$parsed_reference['password'] = ParseINI::parseValue('PasswordNeeded', $reference) ? true : false;
//Add the reference
$parsed_references[] = $parsed_reference;
}
//sort
$order = array();
foreach($parsed_references as $key => $row) {
$order[$key] = $row['priority'];
}
array_multisort($parsed_references, $order);
if($debug) {
print_r($parsed_references);
print_r($references);
} else {
$result = '';
//hide placeholders
$result .= '#cmc-dynamic-game-no-connection {' . PHP_EOL;
$result .= ' display: none;' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
$result .= '.cmc-dynamic-game {' . PHP_EOL;
$result .= ' height: 180px;' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
$result .= '.cmc-dynamic-game-image {' . PHP_EOL;
$result .= ' display: block;' . PHP_EOL;
$result .= ' float: left;' . PHP_EOL;
$result .= ' background-color: black;' . PHP_EOL;
$result .= ' border: solid 1px white;' . PHP_EOL;
$result .= ' width: 200px;' . PHP_EOL;
$result .= ' height: 150px;' . PHP_EOL;
$result .= ' margin-right: 10px;' . PHP_EOL;
$result .= ' background-size: 200px 150px;' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
//data
$games = 0;
for($i = 1; $i <= $list_count; $i++) {
if($i > 1)
$result .= PHP_EOL . PHP_EOL;
if(isset($parsed_references[$i - 1])) {
$reference = $parsed_references[$i - 1];
$result .= '#cmc-dynamic-game' . $i . '-image {' . PHP_EOL;
$result .= ' background-image: url(\'http://clonk.beheh.de/cmc/scenarios/' . $reference['image'] . '\');' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
$result .= '#cmc-dynamic-game' . $i . '-title:after {' . PHP_EOL;
$result .= ' content: \'' . C4Network::cleanString($reference['title']) . '\';' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
$result .= '#cmc-dynamic-game' . $i . '-host:after {' . PHP_EOL;
$result .= ' content: \'auf ' . C4Network::cleanString($reference['host']) . '\';' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
$result .= '#cmc-dynamic-game' . $i . '-status:after {' . PHP_EOL;
$result .= ' content: \'' . $reference['state'] . '\';' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
$result .= '#cmc-dynamic-game' . $i . '-playercount:after {' . PHP_EOL;
$result .= ' content: \'' . $reference['playercount'] . '\';' . PHP_EOL;
$result .= '}' . PHP_EOL . PHP_EOL;
$result .= '#cmc-dynamic-game' . $i . '-players:after {' . PHP_EOL;
$result .= ' content: \'' . $reference['players'] . '\';' . PHP_EOL;
$result .= '}';
;
$games++;
} else {
$result .= '#cmc-dynamic-game' . $i . ' {' . PHP_EOL;
$result .= ' display: none;' . PHP_EOL;
$result .= '}';
}
}
$result .= PHP_EOL . PHP_EOL;
$result .= '#cmc-dynamic-game-status:after {' . PHP_EOL;
$result .= ' content: \'';
switch($games) {
case 0:
$result .= 'Keine Runden verfügbar. Hoste jetzt ein Spiel, um hier zu erscheinen.';
break;
case 1:
$result .= 'Folgende Runde ist jetzt verfügbar:';
break;
default:
$result .= 'Aktuell sind folgende Runden verfügbar (insgesamt ' . $global_playercount . ' Spieler):';
break;
}
$result .= '\';' . PHP_EOL;
$result .= '}';
$result .= PHP_EOL . PHP_EOL;
$result .= '/* Cached ' . date('Y-m-d H:i:s', filemtime('cache.css')) . ' */';
file_put_contents('cache.css', $result);
echo $result;
}
unlink('cache.css.lock');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment