refactor: s1_units caching implemented

#313
This commit is contained in:
Martin Ambrus
2017-11-20 16:17:26 +01:00
parent 64d7d158c7
commit ceeb6a3a9b
2 changed files with 49 additions and 11 deletions
+15 -3
View File
@@ -1219,7 +1219,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->getUnit($vilIDs);
// calculate battles
foreach($dataarray as $data) {
@@ -3376,7 +3378,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->getUnit($vilIDs);
// calculate reinforcements data
$movementProcIDs = [];
@@ -4263,12 +4267,15 @@ class Automation {
$vilIDs = array_keys($vilIDs);
$database->getProfileVillages($vilIDs, 5);
$database->cacheResourceLevels($vilIDs);
$database->getUnit($vilIDs);
// calculate training updates
foreach($trainlist as $train){
$timepast = $train['timestamp2'] - $time;
$pop = $train['pop'];
$valuesUpdated = false;
if($timepast <= 0 && $train['amt'] > 0) {
$valuesUpdated = true;
$timepast2 = $time - $train['timestamp2'];
$trained = 1;
while($timepast2 >= $train['eachtime']){
@@ -4285,11 +4292,16 @@ class Automation {
}
$database->updateTraining($train['id'],$trained,$trained*$train['eachtime']);
}
if ($valuesUpdated) {
call_user_func(get_class($database).'::clearUnitsCache');
}
if($train['amt'] == 0){
$database->trainUnit($train['id'],0,0,0,0,1,1);
}
$crop = $database->getCropProdstarv($train['vref']);
$unitarrays = $this->getAllUnits($train['vref'], false);
$unitarrays = $this->getAllUnits($train['vref']);
$village = $database->getVillage($train['vref'], 0);
$upkeep = $village['pop'] + $this->getUpkeep($unitarrays, 0);
$starv = $database->getVillageField($train['vref'],"starv");
+34 -8
View File
@@ -5472,22 +5472,44 @@ References: User ID/Message ID, Mode
}
function getUnit($vid, $use_cache = true) {
$vid = (int) $vid;
$array_passed = is_array($vid);
if (!$array_passed) {
$singleVillage = true;
$vid = [$vid];
} else {
foreach ($vid as $index => $vidValue) {
$vid[$index] = (int) $vidValue;
}
}
$returnArray = [];
// 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::$unitsCache, $vid)) && !is_null($cachedValue)) {
if ($use_cache && !$array_passed && ($cachedValue = self::returnCachedContent(self::$unitsCache, (int) $vid[0])) && !is_null($cachedValue)) {
return $cachedValue;
}
} else if ($use_cache && $array_passed) {
$newIDs = [];
foreach ($vid as $villageID) {
// don't cache what we don't need to cache
if (isset(self::$unitsCache[$villageID])) {
$returnArray[$villageID] = self::$unitsCache[$villageID];
} else {
// add the uncached ID, so we can select and cache it
$newIDs[] = $villageID;
}
}
$vid = $newIDs;
if (!is_array($vid)) {
$singleVillage = true;
$vid = [$vid];
// nothing to cache? return what we have
if (!count($vid)) {
return $returnArray;
}
}
$q = "SELECT * from " . TB_PREFIX . "units where vref IN(".implode(', ', $vid).")";
$result = mysqli_query($this->dblink,$q);
$returnArray = [];
$resCount = 0;
$vidCount = count($vid);
@@ -6048,7 +6070,11 @@ References: User ID/Message ID, Mode
self::$villageFromReinforcementsCache = [];
self::$oasisArrayReinforcementsCache = [];
self::$oasisReinforcementsCache = [];
self::$unitsCache = [];
self::clearUnitsCache();
}
public static function clearUnitsCache() {
self::$unitsCache = [];
}
// no need to cache this method