#1470: data retrieval updates, though untested

--HG--
branch : gsoc2012-achievements
This commit is contained in:
SirCotare 2012-06-18 19:49:40 +02:00
parent f39db9b0ca
commit 4e806d0956
6 changed files with 176 additions and 38 deletions

View file

@ -74,13 +74,19 @@
$DBc->sendSQL("DELETE FROM ach_player_valuecache WHERE NOT EXISTS (SELECT * FROM ach_monitor_character WHERE amc_character='apv_player')","NONE");
}
#MISSING: fetch candidates
// fetch candidates
if($MODE == "SINGLE") {
$chars = array();
$chars[] = array('amc_character',$CID);
}
else {
$chars = array();
#$chars = array();
$DBc->sendSQL("UPDATE ach_monitor_character SET amc_working='0' WHERE amc_last_import<'".(time()-60*60)."'"); // unlock if something went wrong
$DBc->sendSQL("UPDATE ach_monitor_character SET amc_working='".$RID."' WHERE amc_last_login>amc_last_import AND amc_working='0'","NONE");
$chars = $DBc->sendSQL("SELECT amc_character FROM ach_monitor_character WHERE amc_working='".$RID."'","ARRAY");
}
@ -105,13 +111,15 @@
$atom_list = array();
foreach($chars as $elem) {
$_DATA->freeData($elem['amc_character']);
#STEP 1: evaluate atoms
//get unfinished perks which have no parent or complete parent
$res = $DBc->sendSQL("SELECT ap_id FROM ach_perk WHERE (ap_parent IS NULL OR EXISTS (SELECT * FROM ach_player_perk WHERE app_player='".$elem['amc_character']."' AND app_perk=ap_parent)) AND (NOT EXISTS (SELECT * FROM ach_player_perk WHERE app_player='".$elem['amc_character']."' AND app_perk=ap_id))","ARRAY");
foreach($res as $perk) {
//get unfinished atoms belonging to unfinished objectives
$res = $DBc->sendSQL("","ARRAY");
$res = $DBc->sendSQL("SELECT ach_atom.* FROM ach_atom,ach_objective WHERE ao_perk='".$perk['ap_id']."' AND ao_id=atom_objective AND NOT EXISTS (SELECT * FROM ach_player_objective WHERE apo_player='".$elem['amc_character']."' AND apo_objective=ao_id)","ARRAY");
foreach($res2 as $atom) {
if(!isset($atom_list[$atom['atom_id']])) { // only load if not already cached
$atom_list[$atom['atom_id']] = new Atom($atom);
@ -121,6 +129,10 @@
}
}
$_DATA->freeData($elem['amc_character']);
$DBc->sendSQL("UPDATE ach_monitor_character SET amc_last_import='".time()."', amc_working='0' WHERE amc_character='".$elem['amc_character']."' AND amc_working='".$RID."'","NONE");
#STEP 2: detect obj/perk progression
//obj
$res = $DBc->sendSQL("SELECT ao_id FROM ach_objective WHERE ao_condition='all' AND NOT EXISTS (SELECT * FROM ach_atom WHERE atom_objective=ao_id AND NOT EXISTS (SELECT * FROM ach_player_atom WHERE apa_atom=atom_id AND apa_state!='GRANT' AND apa_player='".$elem['amc_character']."'))","ARRAY");
@ -166,10 +178,6 @@
sleep($CONF['sleep_time']);
}
if($logfile) {
$logfile->write();
}
//self call if cron mode is on
if($MODE == "CRON" && $CONF['enable_selfcall'] == true) {
$DBc->sendSQL("UPDATE ach_monitor_state SET ams_end='".time()."' WHERE ams_id='".$RID."'","NONE");
@ -188,5 +196,9 @@
}
}
if($logfile) {
$logfile->write();
}
exit(0);
?>

View file

@ -17,39 +17,55 @@
}
}
function getData($ident,$field) {
$type = false;
$tmp = $this->getDataSource($field,$type);
if($tmp == false) {
return false;
function freeData($ident) {
foreach($source as $elem) {
$elem->freeData($ident);
}
return $tmp->getData($field,$ident,$type);
}
function writeData($ident,$field,$data) {
$type = false;
$tmp = $this->getDataSource($field,$type);
if($tmp == false) {
function getData($ident,$query) { // SELECT ? FROM c_items WHERE q>='300'
$matches = array();
preg_match("#SELECT (\?|\*) FROM ([^ ]+) WHERE ([.]*)#", $query, $matches);
$mode = $matches[1];
$type = $matches[2];
$cond = $matches[3];
$tmp = $this->getDataSource($type);
if($tmp == false) { // no datasource available for this ident
return false;
}
return $tmp->writeData($field,$ident,$data,$type);
return $tmp->getData($ident,$type,$mode,$cond);
}
function writeData($ident,$query) { // INSERT INTO c_cache () VALUES ()
$matches = array();
preg_match("#INSERT INTO ([^ ]+) \(([^\)]*)\) VALUES \(([^\)]*)\)#", $query, $matches);
$type = $matches[1];
$keys = $matches[2];
$data = $matches[3];
$tmp = $this->getDataSource($type);
if($tmp == false) { // no datasource available for this ident
return false;
}
if(!$tmp->isWriteable()) { // can't write here
return false;
}
return $tmp->writeData($ident,$type,$keys,$data);
}
private function getDataSource(&$field,&$type) {
$type = $field;
//allow wildcard datafields
$tmp = explode(":",$field);
if(sizeof($tmp) > 1) {
$field = $tmp[1];
$type = $tmp[0]."*";
}
if(!$this->alloc[$type]) {
private function getDataSource(&$ident) {
if(!$this->alloc[$ident]) {
return false; //unknown type
}
return $this->source[$this->alloc[$type]];
return $this->source[$this->alloc[$ident]];
}
}
?>

View file

@ -1,5 +1,6 @@
<?php
abstract class DataSource {
private $data;
private $types = array();
private $write = false;
@ -8,6 +9,11 @@
$this->types = $CONF["types"];
$this->write = $CONF["write"];
$this->data = array();
}
function freeData($ident) {
unset $this->data[$ident];
}
function getTypes() {
@ -18,8 +24,24 @@
return $this->write;
}
abstract function getData($ident,$field,$type);
function getData($ident,$type,$mode,$cond) {
if(!isset($this->data[$ident])) {
$this->data[$ident] = array();
}
abstract function writeData($ident,$field,$data,$type);
if(!isset($this->data[$ident][$type])) {
$this->loadData($ident,$type);
}
if($mode == "*") {
return $this->data[$ident][$type]->getRows($cond);
}
else {
return $this->data[$ident][$type]->countRows($cond);
}
}
abstract function loadData($ident,$type);
abstract function writeData($ident,$type,$keys,$data);
}
?>

View file

@ -0,0 +1,88 @@
<?php
class DataTable {
private $table;
function DataTable(&$res) {
$this->table = $res;
}
function countRows(&$cond) {
$rules = $this->parseCond($cond);
$res = 0;
foreach($this->table as $elem) {
$m = true;
foreach($rules as $r) {
$tmp = '
if($elem[$r[0]] '.$r[1].') { }
else {
$m = false;
}
';
try {
eval($tmp);
}
catch(Exception $e) {
return $e->getMessage();
}
}
if($m == true) {
$res++;
}
}
return $res;
}
function getRows(&$cond) {
$rules = $this->parseCond($cond);
$res = array();
foreach($this->table as $elem) {
$m = true;
foreach($rules as $r) {
$tmp = '
if($elem[$r[0]] '.$r[1].') { }
else {
$m = false;
}
';
try {
eval($tmp);
}
catch(Exception $e) {
return $e->getMessage();
}
}
if($m == true) {
$res[] = $elem;
}
}
return $res;
}
private function parseCond(&$cond) {
$c = array();
$tmp = explode("and",strtolower($cond));
foreach($tmp as $elem) {
$matches = array();
preg_match("#([a-zA-Z0-9_]+) ?([.]*)#", trim($elem), $matches);
$c[] = array($matches[1],$matches[2]);
}
return $c;
}
}
?>

View file

@ -4,16 +4,16 @@
parent::__construct();
}
function getData($ident,$field,$type) {
$res = $DBc->sendSQL("SELECT apv_value,apv_date FROM ach_player_valuecache WHERE apv_name='".$DBc->mre($field)."' AND apv_player='".$DBc->mre($ident)."'","ARRAY");
function loadData($ident,$type) {
$res = $DBc->sendSQL("SELECT apv_value,apv_date,apv_name FROM ach_player_valuecache WHERE apv_player='".$DBc->mre($ident)."'","ARRAY");
return array($res[0]['apv_value'],$res[0]['apv_date']);
$this->data[$ident][$type] = new DataTable($res);
}
function writeData($ident,$field,$data,$type) {
function writeData($ident,$type,$keys,$data) {
global $DBc;
$DBc->sendSQL("INSERT INTO ach_player_valuecache (apv_name,apv_player,apv_value,apv_date) VALUES ('".$DBc->mre($field)."','".$DBc->mre($ident)."','".$DBc->mre($data)."','".time()."') ON DUPLICATE KEY UPDATE apv_value='".$DBc->mre($data)."', apv_date='".time()."'","NONE");
$DBc->sendSQL("INSERT INTO ach_player_valuecache (apv_name,apv_player,apv_value,apv_date) VALUES ('".$DBc->mre($keys[0])."','".$DBc->mre($ident)."','".$DBc->mre($data[0])."','".time()."') ON DUPLICATE KEY UPDATE apv_value='".$DBc->mre($data)."', apv_date='".time()."'","NONE");
}
}
?>

View file

@ -1,7 +1,7 @@
<?php
$CONF = array();
$CONF['types'] = array("c_cache*");
$CONF['types'] = array("c_cache");
$CONF['write'] = true;
?>