#1470: data retrieval updates, though untested

This commit is contained in:
SirCotare 2012-06-18 19:49:40 +02:00
parent 3f00c7e0b0
commit b8f236515e
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"); $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") { if($MODE == "SINGLE") {
$chars = array(); $chars = array();
$chars[] = array('amc_character',$CID); $chars[] = array('amc_character',$CID);
} }
else { 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(); $atom_list = array();
foreach($chars as $elem) { foreach($chars as $elem) {
$_DATA->freeData($elem['amc_character']);
#STEP 1: evaluate atoms #STEP 1: evaluate atoms
//get unfinished perks which have no parent or complete parent //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"); $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) { foreach($res as $perk) {
//get unfinished atoms belonging to unfinished objectives //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) { foreach($res2 as $atom) {
if(!isset($atom_list[$atom['atom_id']])) { // only load if not already cached if(!isset($atom_list[$atom['atom_id']])) { // only load if not already cached
$atom_list[$atom['atom_id']] = new Atom($atom); $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 #STEP 2: detect obj/perk progression
//obj //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"); $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']); sleep($CONF['sleep_time']);
} }
if($logfile) {
$logfile->write();
}
//self call if cron mode is on //self call if cron mode is on
if($MODE == "CRON" && $CONF['enable_selfcall'] == true) { if($MODE == "CRON" && $CONF['enable_selfcall'] == true) {
$DBc->sendSQL("UPDATE ach_monitor_state SET ams_end='".time()."' WHERE ams_id='".$RID."'","NONE"); $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); exit(0);
?> ?>

View file

@ -17,39 +17,55 @@
} }
} }
function getData($ident,$field) { function freeData($ident) {
$type = false; foreach($source as $elem) {
$tmp = $this->getDataSource($field,$type); $elem->freeData($ident);
if($tmp == false) {
return false;
} }
return $tmp->getData($field,$ident,$type);
} }
function writeData($ident,$field,$data) { function getData($ident,$query) { // SELECT ? FROM c_items WHERE q>='300'
$type = false; $matches = array();
$tmp = $this->getDataSource($field,$type); preg_match("#SELECT (\?|\*) FROM ([^ ]+) WHERE ([.]*)#", $query, $matches);
if($tmp == false) {
$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 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) { private function getDataSource(&$ident) {
$type = $field; if(!$this->alloc[$ident]) {
//allow wildcard datafields
$tmp = explode(":",$field);
if(sizeof($tmp) > 1) {
$field = $tmp[1];
$type = $tmp[0]."*";
}
if(!$this->alloc[$type]) {
return false; //unknown type return false; //unknown type
} }
return $this->source[$this->alloc[$type]]; return $this->source[$this->alloc[$ident]];
} }
} }
?> ?>

View file

@ -1,5 +1,6 @@
<?php <?php
abstract class DataSource { abstract class DataSource {
private $data;
private $types = array(); private $types = array();
private $write = false; private $write = false;
@ -8,6 +9,11 @@
$this->types = $CONF["types"]; $this->types = $CONF["types"];
$this->write = $CONF["write"]; $this->write = $CONF["write"];
$this->data = array();
}
function freeData($ident) {
unset $this->data[$ident];
} }
function getTypes() { function getTypes() {
@ -18,8 +24,24 @@
return $this->write; 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(); parent::__construct();
} }
function getData($ident,$field,$type) { function loadData($ident,$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"); $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; 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 <?php
$CONF = array(); $CONF = array();
$CONF['types'] = array("c_cache*"); $CONF['types'] = array("c_cache");
$CONF['write'] = true; $CONF['write'] = true;
?> ?>