From 89c0071156325cf3acefcf380deecd1331594c32 Mon Sep 17 00:00:00 2001 From: SirCotare Date: Mon, 18 Jun 2012 19:49:40 +0200 Subject: [PATCH] #1470: data retrieval updates, though untested --- .../_AchWebParser/AchWebParser.php | 26 ++++-- .../class/DataSourceHandler_class.php | 60 ++++++++----- .../class/DataSource_abstract.php | 26 +++++- .../_AchWebParser/class/DataTable_class.php | 88 +++++++++++++++++++ .../source/ValueCache/ValueCache_class.php | 12 +-- .../_AchWebParser/source/ValueCache/conf.php | 2 +- 6 files changed, 176 insertions(+), 38 deletions(-) create mode 100644 code/web/app/app_achievements/_AchWebParser/class/DataTable_class.php diff --git a/code/web/app/app_achievements/_AchWebParser/AchWebParser.php b/code/web/app/app_achievements/_AchWebParser/AchWebParser.php index 68cb415b5..ced23e986 100644 --- a/code/web/app/app_achievements/_AchWebParser/AchWebParser.php +++ b/code/web/app/app_achievements/_AchWebParser/AchWebParser.php @@ -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); ?> \ No newline at end of file diff --git a/code/web/app/app_achievements/_AchWebParser/class/DataSourceHandler_class.php b/code/web/app/app_achievements/_AchWebParser/class/DataSourceHandler_class.php index 8c446d25f..3eab46a95 100644 --- a/code/web/app/app_achievements/_AchWebParser/class/DataSourceHandler_class.php +++ b/code/web/app/app_achievements/_AchWebParser/class/DataSourceHandler_class.php @@ -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]]; } } ?> \ No newline at end of file diff --git a/code/web/app/app_achievements/_AchWebParser/class/DataSource_abstract.php b/code/web/app/app_achievements/_AchWebParser/class/DataSource_abstract.php index 02453f06e..bfda4f00e 100644 --- a/code/web/app/app_achievements/_AchWebParser/class/DataSource_abstract.php +++ b/code/web/app/app_achievements/_AchWebParser/class/DataSource_abstract.php @@ -1,5 +1,6 @@ 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); } ?> \ No newline at end of file diff --git a/code/web/app/app_achievements/_AchWebParser/class/DataTable_class.php b/code/web/app/app_achievements/_AchWebParser/class/DataTable_class.php new file mode 100644 index 000000000..fcc7ba8b5 --- /dev/null +++ b/code/web/app/app_achievements/_AchWebParser/class/DataTable_class.php @@ -0,0 +1,88 @@ +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; + } + } +?> \ No newline at end of file diff --git a/code/web/app/app_achievements/_AchWebParser/source/ValueCache/ValueCache_class.php b/code/web/app/app_achievements/_AchWebParser/source/ValueCache/ValueCache_class.php index 7918e27e5..7d92d4996 100644 --- a/code/web/app/app_achievements/_AchWebParser/source/ValueCache/ValueCache_class.php +++ b/code/web/app/app_achievements/_AchWebParser/source/ValueCache/ValueCache_class.php @@ -3,17 +3,17 @@ function ValueCache() { 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"); - return array($res[0]['apv_value'],$res[0]['apv_date']); + 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"); + + $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"); } } ?> \ No newline at end of file diff --git a/code/web/app/app_achievements/_AchWebParser/source/ValueCache/conf.php b/code/web/app/app_achievements/_AchWebParser/source/ValueCache/conf.php index 117d56f0a..69f6c5cf8 100644 --- a/code/web/app/app_achievements/_AchWebParser/source/ValueCache/conf.php +++ b/code/web/app/app_achievements/_AchWebParser/source/ValueCache/conf.php @@ -1,7 +1,7 @@ \ No newline at end of file