Skip to content

Instantly share code, notes, and snippets.

@pedrofaria
Created September 9, 2010 16:47
Show Gist options
  • Save pedrofaria/572153 to your computer and use it in GitHub Desktop.
Save pedrofaria/572153 to your computer and use it in GitHub Desktop.
<?php
class Awproxy_Player {
public static function load($pid) {
$proxy = Awproxy::instance();
$data = $proxy->get('/0/Player/Profile.php?id='.$pid);
$proxy->check_session($data);
$xpbase = $data->body->table[1]->tr->td->table->tr;
$player = new StdClass;
$xir = NULL;
// has IR
if ($xpbase->td->count() == 2) {
$xplayer = $xpbase->td[0]->table->tr->td->table;
$xir = $xpbase->td[1]->table->tr->td->table;
}
else {
$xplayer = $xpbase->td->table;
}
if ($xplayer->tr->count() == 1) {
throw new Awproxy_Ex_PlayerNotFound;
}
$player->id = $pid;
$player->name = trim((string) $xplayer->tr[0]->td->b);
$player->alliance = preg_replace('/.*tag=(.*?)$/', '$1', $xplayer->tr[0]->td->b->a['href']);
$player->local_time = (string) $xplayer->tr[1]->td[1];
$player->logins = (string) $xplayer->tr[2]->td[1];
$player->idle = (string) $xplayer->tr[3]->td[1];
$player->level = (string) $xplayer->tr[4]->td[1];
$player->science = (string) $xplayer->tr[5]->td[1];
$player->culture = (string) $xplayer->tr[6]->td[1];
$player->rank = preg_replace('/^#(\d+) .*$/', '$1', (string) $xplayer->tr[7]->td[1]);
$player->points = preg_replace('/^.*\((\d+)\)$/', '$1', (string) $xplayer->tr[7]->td[1]);
$player->from = preg_replace('/^.*\/([^.]+)\.php$/', '$1', (string) $xplayer->tr[8]->td[1]->a['href']);
$player->origin = array(
'x' => preg_replace('/^.*\((-?\d+)\/-?\d+\)$/', '$1', (string) $xplayer->tr[9]->td[1]->a),
'y' => preg_replace('/^.*\(-?\d+\/(-?\d+)\)$/', '$1', (string) $xplayer->tr[9]->td[1]->a),
);
$player->joined = trim((string) $xplayer->tr[10]->td[1]);
$player->status = self::get_status($xplayer->tr[11]->td[1]['bgcolor']);
$player->ir = NULL;
// Intelligence Report
if ($xir) {
$ir = new StdClass;
$ir->biology = (int) $xir->tr[1]->td[1];
$ir->economy = (int) $xir->tr[2]->td[1];
$ir->energy = (int) $xir->tr[3]->td[1];
$ir->mathematics = (int) $xir->tr[4]->td[1];
$ir->physics = (int) $xir->tr[5]->td[1];
$ir->social = (int) $xir->tr[6]->td[1];
$ir->trade = (string) $xir->tr[7]->td[1];
$ir->race = array(
'growth' => preg_replace('/^([^%]+%).*$/', '$1', (string) $xir->tr[9]->td->ul->li[0]),
'science' => preg_replace('/^([^%]+%).*$/', '$1', (string) $xir->tr[9]->td->ul->li[1]),
'culture' => preg_replace('/^([^%]+%).*$/', '$1', (string) $xir->tr[9]->td->ul->li[2]),
'production' => preg_replace('/^([^%]+%).*$/', '$1', (string) $xir->tr[9]->td->ul->li[3]),
'speed' => preg_replace('/^([^h]+h).*$/', '$1', (string) $xir->tr[9]->td->ul->li[4]),
'attack' => preg_replace('/^([^%]+%).*$/', '$1', (string) $xir->tr[9]->td->ul->li[5]),
'defense' => preg_replace('/^([^%]+%).*$/', '$1', (string) $xir->tr[9]->td->ul->li[6]),
);
$player->ir = $ir;
}
return $player;
}
public static function get_status ($color) {
switch ($color) {
case '#206020': return 'OK';
default: return 'unknown '.$color;
}
}
}
class Awproxy_Ex_PlayerNotFound extends Kohana_Exception {
public function __construct() {
parent::__construct('Player not found!', NULL, 400);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment