refactor: multiple calls to getVSumField() replaced by a single one

#313
This commit is contained in:
Martin Ambrus
2017-11-25 20:29:12 +01:00
parent 446e9b5f1d
commit 8d8c96014b
6 changed files with 138 additions and 54 deletions
+11 -4
View File
@@ -16,10 +16,17 @@ if($_GET['aid'])
$aliusers = $database->getAllMember($_GET['aid']); $aliusers = $database->getAllMember($_GET['aid']);
if($alidata and $aliusers) if($alidata and $aliusers)
{ {
foreach($aliusers as $member) $memberIDs = [];
{ foreach($aliusers as $member) {
$totalpop += $database->getVSumField($member['id'],"pop"); $memberIDs[] = $member['id'];
} ?> }
$data = $database->getVSumField($memberIDs,"pop");
if (count($data)) {
foreach ($data as $row) {
$totalpop += $row['Total'];
}
} ?>
<br> <br>
<table id="profile" cellpadding="1" cellspacing="1"> <table id="profile" cellpadding="1" cellspacing="1">
+11 -1
View File
@@ -5250,9 +5250,19 @@ class Automation {
$ally = $database->getAlliance($aid); $ally = $database->getAlliance($aid);
$memberlist = $database->getAllMember($ally['id']); $memberlist = $database->getAllMember($ally['id']);
$oldrank = 0; $oldrank = 0;
$memberIDs = [];
foreach($memberlist as $member) { foreach($memberlist as $member) {
$oldrank += $database->getVSumField($member['id'],"pop"); $memberIDs[] = $member['id'];
} }
$data = $database->getVSumField($memberIDs,"pop");
if (count($data)) {
foreach ($data as $row) {
$oldrank += $row['Total'];
}
}
if($ally['oldrank'] != $oldrank){ if($ally['oldrank'] != $oldrank){
if($ally['oldrank'] < $oldrank) { if($ally['oldrank'] < $oldrank) {
$totalpoints = $oldrank - $ally['oldrank']; $totalpoints = $oldrank - $ally['oldrank'];
+49 -10
View File
@@ -1770,6 +1770,11 @@ class MYSQLi_DB implements IDbConnection {
$uid = $newIDs; $uid = $newIDs;
} }
// nothing left to cache, return the full cache
if (!count($uid)) {
return self::$userVillagesCache;
}
switch ($mode) { switch ($mode) {
// by owner ID // by owner ID
case 0: $q = "SELECT * FROM " . TB_PREFIX . "vdata WHERE owner IN(".implode(', ', $uid).") ORDER BY capital DESC,pop DESC"; case 0: $q = "SELECT * FROM " . TB_PREFIX . "vdata WHERE owner IN(".implode(', ', $uid).") ORDER BY capital DESC,pop DESC";
@@ -2910,9 +2915,20 @@ class MYSQLi_DB implements IDbConnection {
$ally = $this->getAlliance($aid); $ally = $this->getAlliance($aid);
$memberlist = $this->getAllMember($ally['id']); $memberlist = $this->getAllMember($ally['id']);
$oldrank = 0; $oldrank = 0;
foreach($memberlist as $member) { $memberIDs = [];
$oldrank += $this->getVSumField($member['id'],"pop");
} foreach($memberlist as $member) {
$memberIDs[] = $member['id'];
}
$data = $this->getVSumField($memberIDs,"pop");
if (count($data)) {
foreach ($data as $row) {
$oldrank += $row['Total'];
}
}
if($ally['oldrank'] != $oldrank){ if($ally['oldrank'] != $oldrank){
if($ally['oldrank'] < $oldrank) { if($ally['oldrank'] < $oldrank) {
$totalpoints = $oldrank - $ally['oldrank']; $totalpoints = $oldrank - $ally['oldrank'];
@@ -3824,7 +3840,7 @@ class MYSQLi_DB implements IDbConnection {
lastupdate < (UNIX_TIMESTAMP() - 600) lastupdate < (UNIX_TIMESTAMP() - 600)
"; // recount every 10 minutes "; // recount every 10 minutes
mysqli_query($this->dblink, $q) OR DIE ($q); mysqli_query($this->dblink, $q);
} }
function getVSumField($uid, $field, $use_cache = true) { function getVSumField($uid, $field, $use_cache = true) {
@@ -7038,15 +7054,38 @@ References: User ID/Message ID, Mode
// first of all, check if we should be using cache and whether the field // first of all, check if we should be using cache and whether the field
// required is already cached // required is already cached
if ($use_cache && ($cachedValue = self::returnCachedContent(self::$foolArtefactCache, $type.$vid.$uid)) && !is_null($cachedValue)) { if ($use_cache && isset(self::$foolArtefactCache[$vid]) && is_array(self::$foolArtefactCache[$vid]) && !count(self::$foolArtefactCache[$vid])) {
return $cachedValue; return [];
} else if ($use_cache && ($cachedValue = self::returnCachedContent(self::$foolArtefactCache, $vid)) && !is_null($cachedValue)) {
$data = [];
// prepare the data as requested
if (isset($cachedValue[$type.$uid])) {
foreach ($cachedValue[$type.$uid] as $row) {
if ($row['type'] == 8 && $row['kind'] == $type && $row['owner'] == $uid && $row['size'] > 1 && $row['active'] > 1) {
$data[] = $row;
}
}
}
return $data;
} }
$q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vid AND ((type = 8 AND kind = $type) OR (owner = $uid AND size > 1 AND active = 1 AND type = 8 AND kind = $type))"; $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE vref = $vid"; //" AND ((type = 8 AND kind = $type) OR (owner = $uid AND size > 1 AND active = 1 AND type = 8 AND kind = $type))";
$result = mysqli_query($this->dblink,$q); $result = $this->mysqli_fetch_all(mysqli_query($this->dblink,$q));
self::$foolArtefactCache[$type.$vid.$uid] = $this->mysqli_fetch_all($result); // cache all types and return the requested one
return self::$foolArtefactCache[$type.$vid.$uid]; if (count($result)) {
foreach ($result as $arteInfo) {
if (!isset(self::$foolArtefactCache[$vid][$arteInfo['type'].$arteInfo['owner']])) {
self::$foolArtefactCache[$vid][$arteInfo['type'].$arteInfo['owner']] = [];
}
self::$foolArtefactCache[$vid][$arteInfo['type'].$arteInfo['owner']][] = $arteInfo;
}
} else {
self::$foolArtefactCache[$vid] = [];
}
return (isset(self::$foolArtefactCache[$vid][$type.$uid]) ? self::$foolArtefactCache[$vid][$type.$uid] : []);
} }
function claimArtefact($vref, $ovref, $id) { function claimArtefact($vref, $ovref, $id) {
+19 -9
View File
@@ -27,7 +27,7 @@
$myrank = 0; $myrank = 0;
if(count($ranking) > 0) { if(count($ranking) > 0) {
for($i=0;$i<($users3);$i++) { for($i=0;$i<($users3);$i++) {
if( isset( $ranking[$i]['userid'] ) ) { if( isset( $ranking[$i]['userid'] ) ) {
if($ranking[$i]['userid'] == $id && $ranking[$i] != "pad") { if($ranking[$i]['userid'] == $id && $ranking[$i] != "pad") {
$myrank = $i; $myrank = $i;
} }
@@ -204,8 +204,8 @@
return $key; return $key;
break; break;
} }
} }
} }
if(!next($this->rankarray)) { if(!next($this->rankarray)) {
if($field != "userid"){ if($field != "userid"){
return $name; return $name;
@@ -215,7 +215,7 @@
break; break;
} }
} }
} }
} }
@@ -268,7 +268,7 @@
WHERE " . TB_PREFIX . "users.access < " . (INCLUDE_ADMIN ? "10" : "8") . " WHERE " . TB_PREFIX . "users.access < " . (INCLUDE_ADMIN ? "10" : "8") . "
AND " . TB_PREFIX . "users.tribe <= 3 AND " . TB_PREFIX . "users.tribe <= 3
AND " . TB_PREFIX . "users.id > 5 AND " . TB_PREFIX . "users.id > 5
ORDER BY totalpop DESC, totalvillages DESC, userid DESC"; ORDER BY totalpop DESC, totalvillages DESC, userid DESC";
} }
$result = (mysqli_query($GLOBALS['link'],$q)); $result = (mysqli_query($GLOBALS['link'],$q));
@@ -474,16 +474,26 @@
} }
public function procARankArray() { public function procARankArray() {
global $multisort; global $multisort, $database;
$array = $GLOBALS['db']->getARanking(); $array = $GLOBALS['db']->getARanking();
$holder = array(); $holder = array();
foreach($array as $value) { foreach($array as $value) {
$memberlist = $GLOBALS['db']->getAllMember($value['id']); $memberlist = $GLOBALS['db']->getAllMember($value['id']);
$totalpop = 0; $totalpop = 0;
foreach($memberlist as $member) {
$totalpop += $GLOBALS['db']->getVSumField($member['id'], "pop"); $memberIDs = [];
} foreach($memberlist as $member) {
$memberIDs[] = $member['id'];
}
$data = $database->getVSumField($memberIDs,"pop");
if (count($data)) {
foreach ($data as $row) {
$totalpop += $row['Total'];
}
}
$value['players'] = count($memberlist); $value['players'] = count($memberlist);
$value['totalpop'] = $totalpop; $value['totalpop'] = $totalpop;
if(!isset($value['avg'])) { if(!isset($value['avg'])) {
+17 -8
View File
@@ -1,17 +1,26 @@
<?php <?php
if(isset($aid)) { if(isset($aid)) {
$aid = $aid; $aid = $aid;
} }
else { else {
$aid = $session->alliance; $aid = $session->alliance;
} }
$varmedal = $database->getProfileMedalAlly($aid); $varmedal = $database->getProfileMedalAlly($aid);
$allianceinfo = $database->getAlliance($aid); $allianceinfo = $database->getAlliance($aid);
$memberlist = $database->getAllMember($aid); $memberlist = $database->getAllMember($aid);
$totalpop = 0; $totalpop = 0;
$memberIDs = [];
foreach($memberlist as $member) { foreach($memberlist as $member) {
$totalpop += $database->getVSumField($member['id'],"pop"); $memberIDs[] = $member['id'];
}
$data = $database->getVSumField($memberIDs,"pop");
if (count($data)) {
foreach ($data as $row) {
$totalpop += $row['Total'];
}
} }
echo "<h1>".$allianceinfo['tag']." - ".$allianceinfo['name']."</h1>"; echo "<h1>".$allianceinfo['tag']." - ".$allianceinfo['name']."</h1>";
@@ -80,9 +89,9 @@ INDELING CATEGORIEEN:
== 7. in top 3 - verdediging == == 7. in top 3 - verdediging ==
== 8. in top 3 - klimmers == == 8. in top 3 - klimmers ==
== 9. in top 3 - overval == == 9. in top 3 - overval ==
******************************/ ******************************/
foreach($varmedal as $medal) { foreach($varmedal as $medal) {
$titel="Bonus"; $titel="Bonus";
switch ($medal['categorie']) { switch ($medal['categorie']) {
@@ -122,7 +131,7 @@ INDELING CATEGORIEEN:
case "12": case "12":
$titel="Top 10 of Rank Attackers of week ".$medal['points']." in a row"; $titel="Top 10 of Rank Attackers of week ".$medal['points']." in a row";
break; break;
} }
echo"<tr> echo"<tr>
<td> ".$titel."</td> <td> ".$titel."</td>
<td>".$medal['plaats']."</td> <td>".$medal['plaats']."</td>
+31 -22
View File
@@ -5,7 +5,7 @@ $aid = $_GET['aid'];
else { else {
$aid = $session->alliance; $aid = $session->alliance;
} }
$varmedal = $database->getProfileMedalAlly($aid); $varmedal = $database->getProfileMedalAlly($aid);
$allianceinfo = $database->getAlliance($aid); $allianceinfo = $database->getAlliance($aid);
$memberlist = $database->getAllMember($aid); $memberlist = $database->getAllMember($aid);
@@ -14,16 +14,25 @@ if($allianceinfo['tag']==""){
header("Location: allianz.php"); header("Location: allianz.php");
exit; exit;
} }
$memberIDs = [];
foreach($memberlist as $member) { foreach($memberlist as $member) {
$totalpop += $database->getVSumField($member['id'],"pop"); $memberIDs[] = $member['id'];
} }
$data = $database->getVSumField($memberIDs,"pop");
if (count($data)) {
foreach ($data as $row) {
$totalpop += $row['Total'];
}
}
echo "<h1>".$allianceinfo['tag']." - ".$allianceinfo['name']."</h1>"; echo "<h1>".$allianceinfo['tag']." - ".$allianceinfo['name']."</h1>";
$profiel="".$allianceinfo['notice']."".md5('skJkev3')."".$allianceinfo['desc'].""; $profiel="".$allianceinfo['notice']."".md5('skJkev3')."".$allianceinfo['desc']."";
require("medal.php"); require("medal.php");
$profiel=explode("".md5('skJkev3')."", $profiel); $profiel=explode("".md5('skJkev3')."", $profiel);
include("alli_menu.tpl"); include("alli_menu.tpl");
?> ?>
<table cellpadding="1" cellspacing="1" id="profile"> <table cellpadding="1" cellspacing="1" id="profile">
@@ -69,25 +78,25 @@ include("alli_menu.tpl");
</tr> </tr>
<?php <?php
foreach($memberlist as $member) { foreach($memberlist as $member) {
//rank name //rank name
$rank = $database->getAlliancePermission($member['id'],"rank",0); $rank = $database->getAlliancePermission($member['id'],"rank",0);
//username //username
$name = $database->getUserField($member['id'],"username",0); $name = $database->getUserField($member['id'],"username",0);
//if there is no rank defined, user will not be printed //if there is no rank defined, user will not be printed
if($rank == ''){ if($rank == ''){
echo ''; echo '';
} }
//if there is user rank defined, user will be printed //if there is user rank defined, user will be printed
else if($rank != ''){ else if($rank != ''){
echo "<tr>"; echo "<tr>";
echo "<th>".stripslashes($rank)."</th>"; echo "<th>".stripslashes($rank)."</th>";
echo "<td><a href='spieler.php?uid=".$member['id']."'>".$name."</td>"; echo "<td><a href='spieler.php?uid=".$member['id']."'>".$name."</td>";
echo "</tr>"; echo "</tr>";
} }
} }
if($allianceinfo['forumlink'] != '' && $allianceinfo['forumlink'] != '0'){ if($allianceinfo['forumlink'] != '' && $allianceinfo['forumlink'] != '0'){
echo "<tr>"; echo "<tr>";
@@ -115,7 +124,7 @@ include("alli_menu.tpl");
<th>Player</th> <th>Player</th>
<th>Population</th> <th>Population</th>
<th>Villages</th> <th>Villages</th>
<?php <?php
if($aid == $session->alliance){ if($aid == $session->alliance){
echo "<th>&nbsp;</th>"; echo "<th>&nbsp;</th>";
} }
@@ -139,31 +148,31 @@ foreach($memberlist as $member) {
$rank = $rank+1; $rank = $rank+1;
$TotalUserPop = $database->getVSumField($member['id'],"pop"); $TotalUserPop = $database->getVSumField($member['id'],"pop");
$TotalVillages = $database->getProfileVillages($member['id']); $TotalVillages = $database->getProfileVillages($member['id']);
echo " <tr>"; echo " <tr>";
echo " <td class=ra>".$rank.".</td>"; echo " <td class=ra>".$rank.".</td>";
echo " <td class=pla><a href=spieler.php?uid=".$member['id'].">".$member['username']."</a></td>"; echo " <td class=pla><a href=spieler.php?uid=".$member['id'].">".$member['username']."</a></td>";
echo " <td class=hab>".$TotalUserPop."</td>"; echo " <td class=hab>".$TotalUserPop."</td>";
echo " <td class=vil>".count($TotalVillages)."</td>"; echo " <td class=vil>".count($TotalVillages)."</td>";
if($aid == $session->alliance){ if($aid == $session->alliance){
if ((time()-600) < $member['timestamp']){ // 0 Min - 10 Min if ((time()-600) < $member['timestamp']){ // 0 Min - 10 Min
echo " <td class=on><img class=online1 src=img/x.gif title='Now online' alt='Now online' /></td>"; echo " <td class=on><img class=online1 src=img/x.gif title='Now online' alt='Now online' /></td>";
}elseif ((time()-86400) < $member['timestamp'] && (time()-600) > $member['timestamp']){ // 10 Min - 1 Days }elseif ((time()-86400) < $member['timestamp'] && (time()-600) > $member['timestamp']){ // 10 Min - 1 Days
echo " <td class=on><img class=online2 src=img/x.gif title='Offline' alt='Offline' /></td>"; echo " <td class=on><img class=online2 src=img/x.gif title='Offline' alt='Offline' /></td>";
}elseif ((time()-259200) < $member['timestamp'] && (time()-86400) > $member['timestamp']){ // 1-3 Days }elseif ((time()-259200) < $member['timestamp'] && (time()-86400) > $member['timestamp']){ // 1-3 Days
echo " <td class=on><img class=online3 src=img/x.gif title='Last 3 days' alt='Last 3 days' /></td>"; echo " <td class=on><img class=online3 src=img/x.gif title='Last 3 days' alt='Last 3 days' /></td>";
}elseif ((time()-604800) < $member['timestamp'] && (time()-259200) > $member['timestamp']){ }elseif ((time()-604800) < $member['timestamp'] && (time()-259200) > $member['timestamp']){
echo " <td class=on><img class=online4 src=img/x.gif title='Last 7 days' alt='Last 7 days' /></td>"; echo " <td class=on><img class=online4 src=img/x.gif title='Last 7 days' alt='Last 7 days' /></td>";
}else{ }else{
echo " <td class=on><img class=online5 src=img/x.gif title=inactive alt=inactive /></td>"; echo " <td class=on><img class=online5 src=img/x.gif title=inactive alt=inactive /></td>";
} }
} }
echo " </tr>"; echo " </tr>";
} }
?> ?>
</tbody> </tbody>
</table> </table>