From 24789507a74696ce3af237502c488285dfd911ee Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Sun, 19 Nov 2017 15:57:52 +0100 Subject: [PATCH] refactor: getProfileVillages() takes multiple IDs #313 --- Admin/Templates/alliance.tpl | 8 ++++++ Admin/Templates/results_player.tpl | 25 +++++++++++++------ GameEngine/Automation.php | 9 +++++++ GameEngine/Database.php | 39 +++++++++++++++++++++++++----- Templates/Alliance/overview.tpl | 9 +++++++ 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/Admin/Templates/alliance.tpl b/Admin/Templates/alliance.tpl index 06b73701..e2959060 100644 --- a/Admin/Templates/alliance.tpl +++ b/Admin/Templates/alliance.tpl @@ -181,6 +181,14 @@ if($_GET['aid']) getProfileVillages($userIDs); + + // display the page foreach($aliusers as $user) { $rank = $rank + 1; diff --git a/Admin/Templates/results_player.tpl b/Admin/Templates/results_player.tpl index 1fd44486..0b33d266 100644 --- a/Admin/Templates/results_player.tpl +++ b/Admin/Templates/results_player.tpl @@ -29,12 +29,22 @@ $result = $admin->search_player($_POST['s']); getProfileVillages($result[$i]["id"]); -$totalpop = 0; -foreach($varray as $vil) { - $totalpop += $vil['pop']; -} + + // preload villages data + $userIDs = []; + for ($i = 0; $i <= count($result)-1; $i++) { + $userIDs[] = $result[$i]["id"]; + } + $database->getProfileVillages($userIDs); + + // display + for ($i = 0; $i <= count($result)-1; $i++) { + $varray = $database->getProfileVillages($result[$i]["id"]); + $totalpop = 0; + + foreach($varray as $vil) { + $totalpop += $vil['pop']; + } echo ' '.$result[$i]["id"].' @@ -43,7 +53,8 @@ echo ' '.$totalpop.' '; -}} + } +} else{ echo ' diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 94fa095e..346a8702 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -4644,6 +4644,15 @@ class Automation { global $database; $q = "SELECT id, invited FROM ".TB_PREFIX."users WHERE invited > 0"; $array = $database->query_return($q); + + // preload villages data + $userIDs = []; + foreach($array as $user) { + $userIDs[] = $user['id']; + } + $database->getProfileVillages($userIDs); + + // continue... foreach($array as $user) { $numusers = mysqli_fetch_array(mysqli_query($GLOBALS['link'],"SELECT Count(*) as Total FROM ".TB_PREFIX."users WHERE id = ".(int) $user['invited']), MYSQLI_ASSOC); if($numusers['Total'] > 0){ diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 3cd8f0e5..26b06b67 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -1561,19 +1561,46 @@ class MYSQLi_DB implements IDbConnection { } function getProfileVillages($uid, $use_cache = true) { - list($uid) = $this->escape_input((int) $uid); + $arrayPassed = is_array($uid); + + if (!$arrayPassed) { + $uid = [(int) $uid]; + } else { + foreach ($uid as $index => $uidValue) { + $uid[$index] = (int) $uidValue; + } + } + + if (!count($uid)) { + return []; + } // first of all, check if we should be using cache and whether the field // required is already cached - if ($use_cache && ($cachedValue = self::returnCachedContent(self::$profileVillagesCache, $uid)) && !is_null($cachedValue)) { + if ($use_cache && !$arrayPassed && ($cachedValue = self::returnCachedContent(self::$profileVillagesCache, $uid[0])) && !is_null($cachedValue)) { return $cachedValue; } - $q = "SELECT capital,wref,name,pop,created from " . TB_PREFIX . "vdata where owner = $uid order by pop desc"; - $result = mysqli_query($this->dblink,$q); + $q = "SELECT capital,wref,name,pop,created,owner from " . TB_PREFIX . "vdata where owner IN(".implode(', ', $uid).") order by pop desc"; + $result = mysqli_query($this->dblink,$q); - self::$profileVillagesCache[$uid] = $this->mysqli_fetch_all($result); - return self::$profileVillagesCache[$uid]; + if (!$arrayPassed) { + $result = $this->mysqli_fetch_all($result); + self::$profileVillagesCache[ $uid[0] ] = $result; + } else { + // we're preloading, cache all the data individually + if (mysqli_num_rows($result)) { + while ( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) ) { + if ( ! isset( self::$profileVillagesCache[ $row['owner'] ] ) ) { + self::$profileVillagesCache[ $row['owner'] ] = []; + } + + self::$profileVillagesCache[ $row['owner'] ][] = $row; + } + } + } + + return $result; } // no need to refactor this method diff --git a/Templates/Alliance/overview.tpl b/Templates/Alliance/overview.tpl index 6e621884..3bedaeb8 100644 --- a/Templates/Alliance/overview.tpl +++ b/Templates/Alliance/overview.tpl @@ -126,6 +126,15 @@ if($aid == $session->alliance){ getProfileVillages($userIDs); + +// continue... foreach($memberlist as $member) { $rank = $rank+1;