From 21d68743257cf9d4af56d1e2ed6c2dfa92585b79 Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Wed, 8 Nov 2017 18:12:54 +0100 Subject: [PATCH] refactor: marketComplete() to use less queries and caching --- GameEngine/Automation.php | 35 +++++++++++++++++++++++++++++------ GameEngine/Database.php | 21 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 95973fa0..24b6a162 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -932,8 +932,31 @@ class Automation { $time = microtime(true); $q = "SELECT s.wood, s.clay, s.iron, s.crop, `to`, `from`, endtime, merchant, send, moveid FROM ".TB_PREFIX."movement m, ".TB_PREFIX."send s WHERE m.ref = s.id AND m.proc = 0 AND sort_type = 0 AND endtime < $time"; $dataarray = $database->query_return($q); + + // holds village owners of the given village IDs, as we can have + // multiple market routes from the same village going to other villages + $villageOwners = []; + // cache of alliances for the village owners + $alliances = []; + // cache for tribes for the village owners + $tribes = []; + foreach($dataarray as $data) { - + // cache village owners + if (!isset($villageOwners[$data['from']])) { + $villageOwners[$data['from']] = $database->getVillageField($data['from'],"owner"); + $userData = $database->getUserFields($villageOwners[$data['from']],"alliance, tribe",0); + $alliances[$data['from']] = $userData["alliance"]; + $tribes[$data['from']] = $userData["tribe"]; + } + + if (!isset($villageOwners[$data['to']])) { + $villageOwners[$data['to']] = $database->getVillageField($data['to'],"owner"); + $userData = $database->getUserFields($villageOwners[$data['to']],"alliance, tribe",0); + $alliances[$data['to']] = $userData["alliance"]; + $tribes[$data['to']] = $userData["tribe"]; + } + if($data['wood'] >= $data['clay'] && $data['wood'] >= $data['iron'] && $data['wood'] >= $data['crop']){ $sort_type = "10"; } elseif($data['clay'] >= $data['wood'] && $data['clay'] >= $data['iron'] && $data['clay'] >= $data['crop']){ $sort_type = "11"; } elseif($data['iron'] >= $data['wood'] && $data['iron'] >= $data['clay'] && $data['iron'] >= $data['crop']){ $sort_type = "12"; } @@ -941,9 +964,9 @@ class Automation { $to = $database->getMInfo($data['to']); $from = $database->getMInfo($data['from']); - - $ownally = $database->getUserField($database->getVillageField($data['from'],"owner"),"alliance",0); - $targetally = $database->getUserField($database->getVillageField($data['to'],"owner"),"alliance",0); + + $ownally = $alliances[$data['to']]; + $targetally = $alliances[$data['to']]; $database->addNotice($to['owner'],$to['wref'],$targetally,$sort_type,''.addslashes($from['name']).' send resources to '.addslashes($to['name']).'',''.$from['owner'].','.$from['wref'].','.$data['wood'].','.$data['clay'].','.$data['iron'].','.$data['crop'].'',$data['endtime']); if($from['owner'] != $to['owner']) { @@ -952,7 +975,7 @@ class Automation { $database->modifyResource($data['to'],$data['wood'],$data['clay'],$data['iron'],$data['crop'],1); $tocoor = $database->getCoor($data['from']); $fromcoor = $database->getCoor($data['to']); - $targettribe = $database->getUserField($database->getVillageField($data['from'],"owner"),"tribe",0); + $targettribe = $tribes[$data['from']]; $endtime = $this->procDistanceTime($tocoor,$fromcoor,$targettribe,0) + $data['endtime']; $database->addMovement(2,$data['to'],$data['from'],$data['merchant'],time(),$endtime,$data['send'],$data['wood'],$data['clay'],$data['iron'],$data['crop']); $database->setMovementProc($data['moveid']); @@ -962,7 +985,7 @@ class Automation { foreach($dataarray1 as $data1) { $database->setMovementProc($data1['moveid']); if($data1['send'] > 1){ - $targettribe1 = $database->getUserField($database->getVillageField($data1['to'],"owner"),"tribe",0); + $targettribe1 = $tribes[$data1['to']]; $send = $data1['send']-1; $this->sendResource2($data1['wood'],$data1['clay'],$data1['iron'],$data1['crop'],$data1['to'],$data1['from'],$targettribe1,$send); } diff --git a/GameEngine/Database.php b/GameEngine/Database.php index b4e7ed8f..69412c33 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -478,6 +478,27 @@ class MYSQLi_DB implements IDbConnection { }else return 0; } + function getUserFields($ref, $fields, $mode) { + list($ref, $fields, $mode) = $this->escape_input($ref, $fields, $mode); + + // update for Multihunter's username and ID + if (($mode && $ref == '') || (!$mode && $ref == 0)) { + $ref = 'Multihunter'; + $mode = 1; + } + + if(!$mode) { + $q = "SELECT $fields FROM " . TB_PREFIX . "users where id = " . (int) $ref; + } else { + $q = "SELECT $fields FROM " . TB_PREFIX . "users where username = '$ref'"; + } + + $result = mysqli_query($this->dblink,$q) or die(mysqli_error($this->dblink)); + if($result) { + return mysqli_fetch_array($result, MYSQLI_ASSOC); + } else return 0; + } + function getInvitedUser($uid) { list($uid) = $this->escape_input((int) $uid);