Skip to content

Instantly share code, notes, and snippets.

@themasch
Created December 27, 2011 02:04
Show Gist options
  • Select an option

  • Save themasch/1522540 to your computer and use it in GitHub Desktop.

Select an option

Save themasch/1522540 to your computer and use it in GitHub Desktop.
simple swtor server status (incl. controller boilerplate)
<?php
class ServerStatus
{
private $cachePath = './status.cache';
private $cacheLifetime = 60; // in sekunden
private $statusUrl = 'http://www.swtor.com/de/server-status';
public function dispatch($url)
{
return $this->getStatus($url);
}
public function getStatus($serverName)
{
$serverName = str_replace('_', ' ', $serverName);
$status = $this->lookup($serverName);
return json_encode($status);
}
private function lookup($sn)
{
$nfo = stat($this->cachePath);
if($nfo['mtime']+$this->cacheLifetime < time()) {
// refresh cache
$data = $this->updateCache();
} else {
$data = $this->readCache();
}
return $data[$sn];
}
private function readCache()
{
return json_decode(file_get_contents($this->cachePath), true);
}
private function updateCache()
{
$content = file_get_contents($this->statusUrl);
$regExp = '/data-status="(?<status>UP|DOWN)" data-name="(?P<name>[^"]+)" data-population="(?P<population>\d+)" data-type="(?P<type>[PVvER-]+)" ((data-timezone="(?P<timezone>Ost|West)")|(data-language="(?P<language>[^"]+)"))/';
preg_match_all($regExp, $content, $groups, PREG_SET_ORDER);
$server = array();
foreach($groups as $group) {
$server[$group['name']] = array(
'status' => $group['status'],
'name' => $group['name'],
'population' => $group['population'],
'type' => $group['type'],
'timezone' => isset($group['timezone']) ? $group['timezone'] : '',
'language' => isset($group['language']) ? $group['language'] : ''
);
}
file_put_contents($this->cachePath, json_encode($server));
return $server;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment