Created
December 27, 2011 02:04
-
-
Save themasch/1522540 to your computer and use it in GitHub Desktop.
simple swtor server status (incl. controller boilerplate)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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