From db87ce88a701c48d79de0f224567b907432d76da Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Fri, 17 Nov 2017 20:23:01 +0100 Subject: [PATCH] refactor: getUnit() can take multiple IDs at once #313 --- GameEngine/Automation.php | 13 ++++++++++++- GameEngine/Database.php | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 251d9a31..5a3634f7 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -570,6 +570,7 @@ class Automation { $database->query($q); } } + if(file_exists("GameEngine/Prevention/culturepoints.txt")) { unlink("GameEngine/Prevention/culturepoints.txt"); } @@ -4272,6 +4273,16 @@ class Automation { global $database,$hero_levels; $harray = $database->getHero(); if(!empty($harray)){ + // first of all, prepare all unit data at once for these heroes + $heroVillageIDs = []; + foreach($harray as $hdata) { + $heroVillageIDs[] = $hdata['wref']; + } + + // load data for those prepared IDs + $unitData = $database->getUnit($heroVillageIDs); + + // now do the math foreach($harray as $hdata){ if((time()-$hdata['lastupdate'])>=1){ if($hdata['health']<100 and $hdata['health']>0){ @@ -4300,7 +4311,7 @@ class Automation { } } } - $villunits = $database->getUnit($hdata['wref']); + $villunits = $unitData[$hdata['wref']]; if($villunits['hero'] == 0 && $hdata['trainingtime'] < time() && $hdata['inrevive'] == 1){ mysqli_query($GLOBALS['link'],"UPDATE " . TB_PREFIX . "units SET hero = 1 WHERE vref = ".(int) $hdata['wref'].""); mysqli_query($GLOBALS['link'],"UPDATE ".TB_PREFIX."hero SET `dead` = '0', `inrevive` = '0', `health` = '100', `lastupdate` = ".(int) $hdata['trainingtime']." WHERE `heroid` = ".(int) $hdata['heroid']); diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 971ac8ee..4357da1e 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -5056,7 +5056,7 @@ class MYSQLi_DB implements IDbConnection { } function getUnit($vid, $use_cache = true) { - list($vid) = $this->escape_input((int) $vid); + $vid = (int) $vid; // first of all, check if we should be using cache and whether the field // required is already cached @@ -5064,15 +5064,41 @@ class MYSQLi_DB implements IDbConnection { return $cachedValue; } - $q = "SELECT * from " . TB_PREFIX . "units where vref = $vid"; + if (!is_array($vid)) { + $vid = [$vid]; + } + + $q = "SELECT * from " . TB_PREFIX . "units where vref IN(".implode(', ', $vid).")"; $result = mysqli_query($this->dblink,$q); - if (!empty($result)) { - self::$unitsCache[$vid] = mysqli_fetch_assoc($result); + $returnArray = []; + $resCount = 0; + $vidCount = count($vid); + + if (!empty($result) && ($resCount = mysqli_num_rows($result)) && $resCount) { + while ($row = mysqli_fetch_assoc($result)) { + self::$unitsCache[$row['vref']] = $row; + $returnArray[$row['vref']] = $row; + } } else { - self::$unitsCache[$vid] = null; + // fill everything with nulls + foreach ($vid as $id) { + self::$unitsCache[$id] = null; + $returnArray[$id] = null; + } } - return self::$unitsCache[$vid]; + // check if we're not missing any return values + if ($vidCount != $resCount) { + // fill-in the gaps, as it would mean some of the IDs we got were not found + // (which is super-strange, but it's still a mathematical possibility) + foreach ($vid as $id) { + if (!isset($returnArray[$id])) { + $returnArray[$id] = null; + } + } + } + + return ($vidCount > 1 ? $returnArray : reset($returnArray)); } // no need to cache this method