Skip to content

Instantly share code, notes, and snippets.

@ranisalt
Created May 9, 2014 05:14
Show Gist options
  • Save ranisalt/512f3715a2296e55b182 to your computer and use it in GitHub Desktop.
Save ranisalt/512f3715a2296e55b182 to your computer and use it in GitHub Desktop.
<?php
if (!defined('IN_IPB')) {
print '<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.';
exit();
}
class Otinfo {
//class that works
}
class task_item {
protected $class;
protected $task = array();
protected $registry;
protected $DB;
protected $table = 'ccs_custom_database_3';
protected $fields = array(
'host' => 'field_15',
'port' => 'field_16',
'client' => 'field_21',
'online_players' => 'field_18',
'record_players' => 'field_19',
'max_players' => 'field_20',
'last_check' => 'field_26',
'uptime' => 'field_27',
);
protected $cutoff = 604800;
public function __construct(ipsRegistry $registry, $class, $task) {
IPSDebug::addLogMessage('__construct');
$this->registry = registry;
$this->DB = $registry->DB();
$this->class = $class;
$this->task = $task;
}
public function runTask() {
$this->DB->build(array(
'select' => '`primary_id_field`,'.implode(',', $this->fields),
'from' => $this->table,
));
IPSDebug::addLogMessage('construiu a query');
$this->DB->execute();
IPSDebug::addLogMessage('executou a query');
while ($tbl = $this->DB->fetch()) {
IPSDebug::addLogMessage('fetchou a query');
$server = new Otinfo($tbl[$this->fields['host']], $tbl[$this->fields['port']]);
$pk = "`primary_id_field`={$tbl['primary_id_field']}";
IPSDebug::addLogMessage('construiu o otinfo');
if ($server->execute()) {
IPSDebug::addLogMessage('executou otinfo');
$uptime_hours = (int)($server->serverinfo['uptime'] / 3600);
$uptime_minutes = ($server->serverinfo['uptime'] - $uptime_hours * 60) % 60;
$uptime = "{$uptime_hours}h {$uptime_minutes}min";
$query_values = array(
$tbl[$this->fields['host']],
$tbl[$this->fields['port']],
filter_var($server->serverinfo['client'], FILTER_SANITIZE_NUMBER_INT),
filter_var($server->players['online'], FILTER_SANITIZE_NUMBER_INT),
filter_var($server->players['peak'], FILTER_SANITIZE_NUMBER_INT),
filter_var($server->players['max'], FILTER_SANITIZE_NUMBER_INT),
time(),
$uptime,
);
$query_fv = array_combine(array_values($this->fields), $query_values);
$query_fv['record_approved'] = 1;
IPSDebug::addLogMessage('construiu a query update');
$registry->DB()->update($this->table, $query_fv, $pk);
IPSDebug::addLogMessage('executou update');
} else {
IPSDebug::addLogMessage('otinfo n deu');
$registry->DB()->update($this->table, array('record_approved' => -1), $pk);
IPSDebug::addLogMessage('updateou o erro');
}
}
IPSDebug::addLogMessage('deletar');
$this->DB()->delete($this->table, $this->fields['last_check'].'<'.time() - $this->cutoff);
IPSDebug::addLogMessage('deletou');
$this->class->appendTaskLog($this->task, 'lista_de_servidores_update');
$this->class->unlockTask($this->task);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment