From a3d40de2835107579c8885e8d935d0b40abece9a Mon Sep 17 00:00:00 2001 From: SirCotare Date: Mon, 25 Jun 2012 15:03:14 +0200 Subject: [PATCH] #1470 major update; these changes were necessary for the admin tool; currently untested - need new testserver with php 5.4 --HG-- branch : gsoc2012-achievements --- .../class/AchAchievement_class.php | 72 +++++++++---------- .../class/AchCategory_class.php | 27 ++++--- .../class/AchList_abstract.php | 31 ++++++-- .../class/AchMenuNode_class.php | 18 +++-- .../app_achievements/class/AchMenu_class.php | 8 +-- .../class/AchObjective_class.php | 32 ++++----- .../app_achievements/class/AchPerk_class.php | 45 ++++++------ .../class/AchSummary_class.php | 27 +++---- .../class/NodeIterator_class.php | 25 +++++++ .../app/app_achievements/class/Node_trait.php | 22 ++++++ .../class/Parentum_abstract.php | 65 ++++++++++++++++- .../class/RenderNodeIteraor_abstract.php | 21 ------ .../include/ach_render_ig.php | 47 +++++++----- .../include/ach_render_web.php | 61 ++++++++++------ code/web/app/app_achievements/index.php | 4 +- 15 files changed, 309 insertions(+), 196 deletions(-) create mode 100644 code/web/app/app_achievements/class/NodeIterator_class.php create mode 100644 code/web/app/app_achievements/class/Node_trait.php delete mode 100644 code/web/app/app_achievements/class/RenderNodeIteraor_abstract.php diff --git a/code/web/app/app_achievements/class/AchAchievement_class.php b/code/web/app/app_achievements/class/AchAchievement_class.php index 5d48d7b11..45c44ee80 100644 --- a/code/web/app/app_achievements/class/AchAchievement_class.php +++ b/code/web/app/app_achievements/class/AchAchievement_class.php @@ -1,21 +1,23 @@ id = $data['aa_id']; - $this->parent = $data['aa_parent']; + + $this->setParent($parent); + $this->setID($data['aa_id']); + $this->parent_id = $data['aa_parent']; $this->category = $data['aa_category']; $this->tie_race = $data['aa_tie_race']; $this->tie_civ = $data['aa_tie_civ']; @@ -25,44 +27,27 @@ $this->template = $data['aal_template']; $this->dev = $data['aa_dev']; - #echo $this->id; - $res = $DBc->sqlQuery("SELECT * FROM ach_perk LEFT JOIN (ach_perk_lang) ON (apl_lang='".$_USER->getLang()."' AND apl_perk=ap_id) LEFT JOIN (ach_player_perk) ON (app_perk=ap_id AND app_player='".$_USER->getID()."') WHERE ap_achievement='".$this->id."' AND ap_parent IS NULL"); #MISSING: or parent is done $sz = sizeof($res); for($i=0;$i<$sz;$i++) { - #echo "Z"; - $res[$i]['this'] = $this; $tmp = $this->makeChild($res[$i]); - #echo var_export($tmp,true); - - - if($tmp->isDone()) { - $this->child_done[] = sizeof($this->nodes); + $this->addDone($tmp); } else { - $this->child_open[] = sizeof($this->nodes); + $this->addOpen($tmp); } - $this->nodes[] = $tmp; - #MISSING: divide into groups -> open/done } - - #echo var_export($this->child_open,true); - #echo "X-".$this->hasOpen(); } - protected function makeChild(&$a) { - return new AchPerk($a); + protected function makeChild($a) { + return new AchPerk($a,$this); } - function getID() { - return $this->id; - } - - function getParent() { - return $this->parent; + function getParentID() { + return $this->parent_id; } function getTieRace() { @@ -87,14 +72,21 @@ function getValueDone() { $val = 0; - foreach($this->child_done as $elem) { - $val += $this->nodes[$elem]->getValue(); + $iter = $this->getDone(); + while($iter->hasNext()) { + $curr = $this->findNodeIdx($iter->getNext()); + $val += $curr->getValue(); } return $val; } function getValueOpen() { - return $this->nodes[$this->child_open[0]]->getValue(); + $iter = $this->getDone(); + if($iter->hasNext()) { + $curr = $this->findNodeIdx($iter->getNext()); + return $curr->getValue(); + } + return 0; } function getTemplate($insert = array()) { diff --git a/code/web/app/app_achievements/class/AchCategory_class.php b/code/web/app/app_achievements/class/AchCategory_class.php index 55ef42c0f..d31d6eb90 100644 --- a/code/web/app/app_achievements/class/AchCategory_class.php +++ b/code/web/app/app_achievements/class/AchCategory_class.php @@ -1,12 +1,12 @@ makeChild($res[$i]); - #echo var_export($tmp,true); + if($tmp->hasOpen()) { - $this->child_open[] = sizeof($this->nodes); + $this->addOpen($tmp); } if($tmp->hasDone()) { - $this->child_done[] = sizeof($this->nodes); + $this->addDone($tmp); } - - $this->nodes[] = $tmp; } $res = $DBc->sqlQuery("SELECT count(*) as anz FROM ach_achievement WHERE aa_tie_cult IS NOT NULL AND aa_category='".$this->id."' AND aa_dev='0'"); @@ -54,8 +51,8 @@ $this->ties_civ_dev = $res[0]['anz']; } - protected function makeChild(&$a) { - return new AchAchievement($a); + protected function makeChild($a) { + return new AchAchievement($a,$this); } function getID() { diff --git a/code/web/app/app_achievements/class/AchList_abstract.php b/code/web/app/app_achievements/class/AchList_abstract.php index 707aef425..41c2040e9 100644 --- a/code/web/app/app_achievements/class/AchList_abstract.php +++ b/code/web/app/app_achievements/class/AchList_abstract.php @@ -1,22 +1,39 @@ child_done; + final function getDone() { + return new NodeIterator($this->child_done); } - function getOpen() { - return $this->child_open; + final function getOpen() { + return new NodeIterator($this->child_open); } - function hasOpen() { + final function hasOpen() { return (sizeof($this->child_open) != 0); } - function hasDone() { + final function hasDone() { return (sizeof($this->child_done) != 0); } + + final function addOpen($n) { + $this->child_open[] = $this->addChild($n); + } + + final function addDone($n) { + $this->child_done[] = $this->addChild($n); + } + + function removeChild() { + $idx = $this->getIdx($id); + if($idx != null) { + unset($this->child_open[$idx]); + unset($this->child_done[$idx]); + parent::removeIdx($idx); + } + } } ?> \ No newline at end of file diff --git a/code/web/app/app_achievements/class/AchMenuNode_class.php b/code/web/app/app_achievements/class/AchMenuNode_class.php index 8d90632ae..a9112b855 100644 --- a/code/web/app/app_achievements/class/AchMenuNode_class.php +++ b/code/web/app/app_achievements/class/AchMenuNode_class.php @@ -1,6 +1,7 @@ id = $data['ac_id']; + $this->setParent($parent); + $this->setID($data['ac_id']); $this->parent_id = $data['ac_parent']; $this->name = $data['acl_name']; $this->image = $data['ac_image']; @@ -28,12 +30,8 @@ } } - protected function makeChild(&$a) { - return new AchMenuNode($a); - } - - function getID() { - return $this->id; + protected function makeChild($a) { + return new AchMenuNode($a,$this); } function getName() { diff --git a/code/web/app/app_achievements/class/AchMenu_class.php b/code/web/app/app_achievements/class/AchMenu_class.php index a28e807cc..cb54ce216 100644 --- a/code/web/app/app_achievements/class/AchMenu_class.php +++ b/code/web/app/app_achievements/class/AchMenu_class.php @@ -1,5 +1,5 @@ nodes[] = new AchMenuNode($tmp); + $this->nodes[] = new AchMenuNode($tmp,$this); $res = $DBc->sqlQuery("SELECT * FROM ach_category LEFT JOIN (ach_category_lang) ON (acl_lang='".$_USER->getLang()."' AND acl_category=ac_id) WHERE ac_parent IS NULL ORDER by ac_order ASC, acl_name ASC"); @@ -45,8 +45,8 @@ return 0; } - protected function makeChild(&$a) { - return new AchMenuNode($a); + protected function makeChild($a) { + return new AchMenuNode($a,$this); } } ?> \ No newline at end of file diff --git a/code/web/app/app_achievements/class/AchObjective_class.php b/code/web/app/app_achievements/class/AchObjective_class.php index 47c946381..4101fe727 100644 --- a/code/web/app/app_achievements/class/AchObjective_class.php +++ b/code/web/app/app_achievements/class/AchObjective_class.php @@ -1,19 +1,21 @@ id = $data['ao_id']; + + $this->setParent($parent); + $this->setID($data['ao_id']); $this->perk = $data['ao_perk']; $this->condition = $data['ao_condition']; $this->value = $data['ao_value']; @@ -30,7 +32,7 @@ } } - protected function makeChild(&$a) { + protected function makeChild($a) { return null; } @@ -38,10 +40,6 @@ return $this->meta_image; } - function getID() { - return $this->id; - } - function getPerk() { return $this->perk; } diff --git a/code/web/app/app_achievements/class/AchPerk_class.php b/code/web/app/app_achievements/class/AchPerk_class.php index 15c9547ec..a8c0713d2 100644 --- a/code/web/app/app_achievements/class/AchPerk_class.php +++ b/code/web/app/app_achievements/class/AchPerk_class.php @@ -1,18 +1,18 @@ id = $data['ap_id']; - $this->parent = $data['this']; + + $this->setParent($parent); + $this->setID($data['ap_id']); $this->achievement = $data['ap_achievement']; $this->value = $data['ap_value']; $this->name = $data['apl_name']; @@ -22,20 +22,12 @@ $res = $DBc->sqlQuery("SELECT * FROM ach_objective LEFT JOIN (ach_objective_lang) ON (aol_lang='".$_USER->getLang()."' AND aol_objective=ao_id) LEFT JOIN (ach_player_objective) ON (apo_objective=ao_id AND apo_player='".$_USER->getID()."') LEFT JOIN (ach_achievement) ON (aa_id=ao_metalink) WHERE ao_perk='".$this->id."'"); $sz = sizeof($res); for($i=0;$i<$sz;$i++) { - $this->nodes[] = $this->makeChild($res[$i]); + $this->addChild($this->makeChild($res[$i])); } } - protected function makeChild(&$a) { - return new AchObjective($a); - } - - function getID() { - return $this->id; - } - - function getParent() { - return $this->parent; + protected function makeChild($a) { + return new AchObjective($a,$this); } function getAchievement() { @@ -51,11 +43,14 @@ } function objDrawable() { - foreach($this->nodes as $elem) { - if($elem->getDisplay() != "hidden") { + $iter = $this->getIterator(); + while($iter->hasNext()) { + $curr = $iter->getNext(); + if($curr->getDisplay() != "hidden") { return true; } } + return false; } diff --git a/code/web/app/app_achievements/class/AchSummary_class.php b/code/web/app/app_achievements/class/AchSummary_class.php index db1d51978..fbd1642a6 100644 --- a/code/web/app/app_achievements/class/AchSummary_class.php +++ b/code/web/app/app_achievements/class/AchSummary_class.php @@ -15,15 +15,12 @@ $sz = sizeof($res); for($i=0;$i<$sz;$i++) { - $tmp = $this->makeChild($res[$i]); - - $this->child_done[] = sizeof($this->nodes); - $this->nodes[] = $tmp; + $this->addDone($this->makeChild($res[$i])); } } - protected function makeChild(&$a) { - return new AchAchievement($a); + protected function makeChild($a) { + return new AchAchievement($a,$this); } function getSummary() { @@ -32,13 +29,15 @@ //and also sum up how many have been completed $this->stats = array(); // [][name,done,total] - $tmp = $this->menu->getChildren(); - foreach($tmp as $elem) { - if($elem->getID() == 0 || $elem->inDev()) { + $iter = $this->menu->getIterator(); + while($iter->hasNext()) { + $curr = $iter->getNext(); + + if($curr->getID() == 0 || $curr->inDev()) { continue; // skip summary page } - $res = $this->sumStats($elem); - $this->stats[] = array($elem->getName(),$res[0],$res[1]); + $res = $this->sumStats($curr); + $this->stats[] = array($curr->getName(),$res[0],$res[1]); } } @@ -59,8 +58,10 @@ $res = $DBc->sqlQuery("SELECT count(ap_id) as anz FROM ach_perk,ach_achievement WHERE aa_category='".$node->getID()."' AND ap_achievement=aa_id AND aa_dev='0' AND ap_dev='0'"); $total += $res[0]["anz"]; - $tmp = $node->getChildren(); - foreach($tmp as $elem) { + $iter = $node->getIterator(); + while($iter->hasNext()) { + $curr = $iter->getNext(); + $res = $this->sumStats($elem); $done += $res[0]; $total += $res[1]; diff --git a/code/web/app/app_achievements/class/NodeIterator_class.php b/code/web/app/app_achievements/class/NodeIterator_class.php new file mode 100644 index 000000000..3bd4fd4b9 --- /dev/null +++ b/code/web/app/app_achievements/class/NodeIterator_class.php @@ -0,0 +1,25 @@ +nodes = $nodes; + $this->curr = 0; + } + + function hasNext() { + $tmp = array_keys($this->nodes); + return isset($this->nodes[$tmp[$this->curr]]); + } + + function getNext() { + $tmp = array_keys($this->nodes); + return $this->nodes[$tmp[$this->curr++]]; + } + + function first() { + $this->curr = 0; + } + } +?> \ No newline at end of file diff --git a/code/web/app/app_achievements/class/Node_trait.php b/code/web/app/app_achievements/class/Node_trait.php new file mode 100644 index 000000000..527f6caa7 --- /dev/null +++ b/code/web/app/app_achievements/class/Node_trait.php @@ -0,0 +1,22 @@ +id; + } + + final function getParent() { + return $this->parent; + } + + final protected function setID($id) { + $this->id = $id; + } + + final protected function setParent($p) { + $this->parent = $p; + } + } +?> \ No newline at end of file diff --git a/code/web/app/app_achievements/class/Parentum_abstract.php b/code/web/app/app_achievements/class/Parentum_abstract.php index 25b476535..0d725b9c4 100644 --- a/code/web/app/app_achievements/class/Parentum_abstract.php +++ b/code/web/app/app_achievements/class/Parentum_abstract.php @@ -1,5 +1,68 @@ nodes); + } + + final function isEmpty() { + return (sizeof($this->nodes) == 0); + } + + final function getIterator() { + return new NodeIterator($this->nodes); + } + + final function addChild($n) { + $tmp = sizeof($this->nodes); + $this->nodes[] = $n; + return $tmp; + } + + function removeChild($id) { + $this->removeIdx($this->getIdx($id)); + } + + function removeNode($n) { + $this->removeIdx($this->findNode($n)); + } + + final protected function removeIdx($idx) { + if($idx != null) { + unset($this->nodes[$idx]); + } + } + + final protected function findNode($n) { + foreach($this->nodes as $key=>$elem) { + if($this->nodes[$key] === $n) { + return $key; + } + } + + return null; + } + + final protected function findNodeIdx($idx) { + return $this->nodes[$idx]; + } + + final protected function getIdx($id) { + foreach($this->nodes as $key=>$elem) { + if($elem->getID() == $id) { + return $key; + } + } + + return null; + } } ?> \ No newline at end of file diff --git a/code/web/app/app_achievements/class/RenderNodeIteraor_abstract.php b/code/web/app/app_achievements/class/RenderNodeIteraor_abstract.php deleted file mode 100644 index 5fad955b8..000000000 --- a/code/web/app/app_achievements/class/RenderNodeIteraor_abstract.php +++ /dev/null @@ -1,21 +0,0 @@ -nodes); - } - - function getChild($i) { - return $this->nodes[$i]; - } - - function isEmpty() { - return (sizeof($this->nodes) == 0); - } - - function getChildren() { - return $this->nodes; - } - } -?> \ No newline at end of file diff --git a/code/web/app/app_achievements/include/ach_render_ig.php b/code/web/app/app_achievements/include/ach_render_ig.php index 8de237a6d..4b67407c1 100644 --- a/code/web/app/app_achievements/include/ach_render_ig.php +++ b/code/web/app/app_achievements/include/ach_render_ig.php @@ -132,9 +132,13 @@ if($sub == 0) { $html = ""; } - $sz = $menu->getSize(); - for($i=0;$i<$sz;$i++) { - $curr = $menu->getChild($i); + + $iter = $menu->getIterator(); + while($iter->hasNext()) { + $curr = $iter->getNext(); + #$sz = $menu->getSize(); + #for($i=0;$i<$sz;$i++) { + #$curr = $menu->getChild($i); if($curr->inDev()) { continue; } @@ -176,24 +180,28 @@ $html .= ach_render_tiebar($cat->getCurrentCult(),$cat->getCurrentCiv(),$cat); } - $tmp = $cat->getDone(); - $sz = sizeof($tmp); - for($i=0;$i<$sz;$i++) { + $iter = $cat->getDone(); + while($iter->hasNext()) { + $curr = $cat->findNodeIdx($iter->getNext()); + #$sz = sizeof($tmp); + #for($i=0;$i<$sz;$i++) { #echo "A"; - if($cat->getChild($tmp[$i])->inDev()) { + if($curr->inDev()) { continue; } - $html .= ach_render_achievement_done($cat->getChild($tmp[$i])); + $html .= ach_render_achievement_done($curr); } - $tmp = $cat->getOpen(); - $sz = sizeof($tmp); - for($i=0;$i<$sz;$i++) { + $iter = $cat->getOpen(); + while($iter->hasNext()) { + $curr = $cat->findNodeIdx($iter->getNext()); + #$sz = sizeof($tmp); + #for($i=0;$i<$sz;$i++) { #echo "B"; - if($cat->getChild($tmp[$i])->inDev()) { + if($curr->inDev()) { continue; } - $html .= ach_render_achievement_open($cat->getChild($tmp[$i])); + $html .= ach_render_achievement_open($curr)); } return $html; @@ -251,8 +259,7 @@ $html = ""; $perk_list = $ach->getOpen(); - - $perk = $ach->getChild($perk_list[0]); + $perk = $ach->findNodeIdx($perk_list->getNext()); if($perk->inDev()) { return $html; @@ -273,9 +280,11 @@ $html = ""; $perk_list = $ach->getDone(); + while($perk_list->hasNext()) { + $perk = $this->findNodeIdx($perk_list->getNext()); - foreach($perk_list as $elem) { - $perk = $ach->getChild($elem); + #foreach($perk_list as $elem) { + #$perk = $ach->getChild($elem); if($perk->inDev()) { continue; } @@ -291,7 +300,9 @@ $i = 0; $skip = false; - foreach($obj as $elem) { + while($obj->hasNext()) { + #foreach($obj as $elem) { + $elem = $obj->getNext(); if(($i%2) == 0) { $html .= ""; } diff --git a/code/web/app/app_achievements/include/ach_render_web.php b/code/web/app/app_achievements/include/ach_render_web.php index 6ae549a09..a12713180 100644 --- a/code/web/app/app_achievements/include/ach_render_web.php +++ b/code/web/app/app_achievements/include/ach_render_web.php @@ -134,8 +134,6 @@ } function ach_render_menu(&$menu,$sub = 0) { - global $_CONF; - $html = ""; - $sz = $menu->getSize(); - for($i=0;$i<$sz;$i++) { - $curr = $menu->getChild($i); + return $html.ach_render_mnode($menu,$sub); + } + + function ach_render_mnode(&$menu,$sub) { + global $_CONF; + + $iter = $menu->getIterator(); + while($iter->hasNext()) { + $curr = $iter->getNext(); + #$sz = $menu->getSize(); + #for($i=0;$i<$sz;$i++) { + # $curr = $menu->getChild($i); if($curr->inDev()) { continue; } @@ -172,7 +179,7 @@
"; if($curr->hasOpenCat() != 0) { - $html .= "
".ach_render_menu($curr,($sub+4))."
"; + $html .= "
".ach_render_mnode($curr,($sub+4))."
"; } } @@ -186,24 +193,28 @@ $html .= ach_render_tiebar($cat->getCurrentCult(),$cat->getCurrentCiv(),$cat); } - $tmp = $cat->getDone(); - $sz = sizeof($tmp); - for($i=0;$i<$sz;$i++) { + $iter = $cat->getDone(); + while($iter->hasNext()) { + $curr = $cat->findNodeIdx($iter->getNext()); + #$sz = sizeof($tmp); + #for($i=0;$i<$sz;$i++) { #echo "A"; - if($cat->getChild($tmp[$i])->inDev()) { + if($curr->inDev()) { continue; } - $html .= ach_render_achievement_done($cat->getChild($tmp[$i])); + $html .= ach_render_achievement_done($curr); } - $tmp = $cat->getOpen(); - $sz = sizeof($tmp); - for($i=0;$i<$sz;$i++) { + $iter = $cat->getOpen(); + while($iter->hasNext()) { + $curr = $cat->findNodeIdx($iter->getNext()); + #$sz = sizeof($tmp); + #for($i=0;$i<$sz;$i++) { #echo "B"; - if($cat->getChild($tmp[$i])->inDev()) { + if($curr->inDev()) { continue; } - $html .= ach_render_achievement_open($cat->getChild($tmp[$i])); + $html .= ach_render_achievement_open($curr)); } return $html; @@ -284,8 +295,9 @@ $html = ""; $perk_list = $ach->getOpen(); + $perk = $ach->findNodeIdx($perk_list->getNext()); - $perk = $ach->getChild($perk_list[0]); + #$perk = $ach->getChild($perk_list[0]); if($perk->inDev()) { return $html; @@ -295,7 +307,7 @@ $html .= "".$perk->getName().""; } if($perk->objDrawable()) { - $html .= ach_render_obj_list($perk->getChildren()); + $html .= ach_render_obj_list($perk->getIterator()); } return $html; @@ -306,9 +318,10 @@ $html = ""; $perk_list = $ach->getDone(); - - foreach($perk_list as $elem) { - $perk = $ach->getChild($elem); + while($perk_list->hasNext()) { + $perk = $this->findNodeIdx($perk_list->getNext()); + #foreach($perk_list as $elem) { + #$perk = $ach->getChild($elem); if($perk->inDev()) { continue; } @@ -323,8 +336,10 @@ $i = 0; $skip = false; - - foreach($obj as $elem) { + + while($obj->hasNext()) { + #foreach($obj as $elem) { + $elem = $obj->getNext(); if(($i%2) == 0) { $html .= ""; } diff --git a/code/web/app/app_achievements/index.php b/code/web/app/app_achievements/index.php index a315f70d9..7dd206c89 100644 --- a/code/web/app/app_achievements/index.php +++ b/code/web/app/app_achievements/index.php @@ -34,10 +34,10 @@ else { require_once("include/ach_render_common.php"); require_once("class/Parentum_abstract.php"); -require_once("class/RenderNodeIteraor_abstract.php"); require_once("class/AchList_abstract.php"); require_once("class/Tieable_inter.php"); - +require_once("class/NodeIterator_class.php"); +require_once("class/Node_trait.php"); require_once("class/AchMenu_class.php"); require_once("class/AchMenuNode_class.php");