From 80160d683e61425c1cfc69d40183762db656e003 Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Tue, 21 Nov 2017 00:59:06 +0100 Subject: [PATCH] refactor: s1_wdata caching implemented #313 --- GameEngine/Automation.php | 23 ++++++++++++++- GameEngine/Database.php | 60 +++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 3e6e3e07..b2630e1b 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -924,6 +924,15 @@ class Automation { $time = time(); $q = "SELECT `from`, wood, clay, iron, crop, wid, deliveries, id FROM ".TB_PREFIX."route where timestamp < $time"; $dataarray = $database->query_return($q); + + $vilIDs = []; + foreach($dataarray as $data) { + $vilIDs[$data['to']] = true; + $vilIDs[$data['from']] = true; + } + $vilIDs = array_keys($vilIDs); + $database->getVillageByWorldID($vilIDs); + foreach($dataarray as $data) { $targettribe = $database->getUserField($database->getVillageField($data['from'],"owner"),"tribe",0); $this->sendResource2($data['wood'],$data['clay'],$data['iron'],$data['crop'],$data['from'],$data['wid'],$targettribe,$data['deliveries']); @@ -972,6 +981,15 @@ class Automation { $q1 = "SELECT send, moveid, `to`, wood, clay, iron, crop, `from` FROM ".TB_PREFIX."movement WHERE proc = 0 and sort_type = 2 and endtime < $time"; $dataarray1 = $database->query_return($q1); + + $vilIDs = []; + foreach($dataarray1 as $data1) { + $vilIDs[$data1['to']] = true; + $vilIDs[$data1['from']] = true; + } + $vilIDs = array_keys($vilIDs); + $database->getVillageByWorldID($vilIDs); + foreach($dataarray1 as $data1) { $database->setMovementProc($data1['moveid']); if($data1['send'] > 1){ @@ -3396,6 +3414,7 @@ class Automation { $database->getProfileVillages($vilIDs, 5); $database->getUnit($vilIDs); $database->getEnforce(array_keys($tos), array_keys($froms)); + $database->getVillageByWorldID($vilIDs); // calculate reinforcements data $movementProcIDs = []; @@ -3713,7 +3732,9 @@ class Automation { $vilIDs[$data['from']] = true; $vilIDs[$data['to']] = true; } - $database->getProfileVillages(array_keys($vilIDs), 5); + $vilIDs = array_keys($vilIDs); + $database->getProfileVillages($vilIDs, 5); + $database->getVillageByWorldID($vilIDs); foreach($dataarray as $data) { $ownerID = $database->getUserField($database->getVillageField($data['from'],"owner"),"id",0); diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 0975f2a7..6de0024a 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -1861,19 +1861,67 @@ class MYSQLi_DB implements IDbConnection { } function getVillageByWorldID($vid, $use_cache = true) { - $vid = (int) $vid; + $array_passed = is_array($vid); + + if (!$array_passed) { + $vid = [(int) $vid]; + } else { + foreach ($vid as $index => $ivdValue) { + $vid[$index] = (int) $ivdValue; + } + } + + if (!count($vid)) { + 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::$villageFieldsCacheByWorldID, $vid)) && !is_null($cachedValue)) { + if ($use_cache && !$array_passed && isset(self::$villageFieldsCacheByWorldID[$vid[0]]) && is_array(self::$villageFieldsCacheByWorldID[$vid[0]]) && !count(self::$villageFieldsCacheByWorldID[$vid[0]])) { + return self::$villageFieldsCacheByWorldID[$vid[0]]; + } else if ($use_cache && $array_passed) { + // check what we can return from cache + $newVIDs = []; + foreach ($vid as $key) { + if (!isset(self::$villageFieldsCacheByWorldID[$key])) { + $newVIDs [] = $key; + } + } + + // everything's cached, just return the cache + if (!count($newVIDs)) { + return self::$villageFieldsCacheByWorldID; + } else { + // update remaining IDs to select and cache + $vid = $newVIDs; + } + } else if ($use_cache && !$array_passed && ($cachedValue = self::returnCachedContent(self::$villageFieldsCacheByWorldID, $vid[0])) && !is_null($cachedValue)) { return $cachedValue; } - $q = "SELECT * FROM " . TB_PREFIX . "wdata where id = $vid LIMIT 1"; - $result = mysqli_query($this->dblink,$q); + $q = "SELECT * FROM " . TB_PREFIX . "wdata where id IN(".implode(', ', $vid).")"; + $result = $this->mysqli_fetch_all(mysqli_query($this->dblink,$q)); - self::$villageFieldsCacheByWorldID[$vid] = mysqli_fetch_array($result, MYSQLI_ASSOC); - return self::$villageFieldsCacheByWorldID[$vid]; + // return a single value + if (!$array_passed) { + self::$villageFieldsCacheByWorldID[$vid[0]] = $result[0]; + } else { + if ($result && count($result)) { + foreach ( $result as $record ) { + self::$villageFieldsCacheByWorldID[$record['id']] = $record; + } + } + + // check for any missing IDs and fill them in with blanks, + // since no reinforcements were found for these villages + foreach ($vid as $key) { + if (!isset(self::$villageFieldsCacheByWorldID[$key])) { + self::$villageFieldsCacheByWorldID[$key] = []; + } + } + } + + return ($array_passed ? self::$villageFieldsCacheByWorldID : self::$villageFieldsCacheByWorldID[$vid[0]]); } public function getVillageBattleData($vid, $use_cache = true) {