From a87802296b2892c38a8bd84bb27ce831ab08b600 Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Sun, 19 Nov 2017 16:24:33 +0100 Subject: [PATCH] refactor: getBuildingByType2() takes multiple IDs #313 --- GameEngine/Database.php | 32 ++++++++++-- Templates/Build/avaliable.tpl | 92 ++++++++++++++++++++--------------- 2 files changed, 81 insertions(+), 43 deletions(-) diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 26b06b67..faad4c9a 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -4662,11 +4662,35 @@ class MYSQLi_DB implements IDbConnection { } function getBuildingByType2($wid,$type) { - list($wid,$type) = $this->escape_input((int) $wid,(int) $type); + $wid = (int) $wid; - $q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "bdata where wid = $wid and type = $type and master = 0"; - $result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC); - return $result['Total']; + if (!is_array($type)) { + $type = [(int) $type]; + } else { + foreach ($type as $index => $typeValue) { + $type[$index] = (int) $typeValue; + } + } + + $q = "SELECT CONCAT(type, \"=\", Count(type)) FROM " . TB_PREFIX . "bdata where wid = $wid and type IN(".implode(', ', $type).") and master = 0"; + $result = mysqli_query($this->dblink, $q); + $newresult = []; + + if (mysqli_num_rows($result)) { + while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) { + if ($row[0]) { + $val = explode( '=', $row[0] ); + $newresult[ $val[0] ] = $val[1]; + } + } + + $result = $newresult; + + } else { + $result = []; + } + + return $result; } // no need to cache this method diff --git a/Templates/Build/avaliable.tpl b/Templates/Build/avaliable.tpl index 00be5fb5..4670eaec 100644 --- a/Templates/Build/avaliable.tpl +++ b/Templates/Build/avaliable.tpl @@ -43,46 +43,60 @@ $greatwarehouse = $building->getTypeLevel(38); $greatgranary = $building->getTypeLevel(39); $greatworkshop = $building->getTypeLevel(42); -$mainbuilding1 = $database->getBuildingByType2($village->wid,15); -$cranny1 = $database->getBuildingByType2($village->wid,23); -$granary1 = $database->getBuildingByType2($village->wid,11); -$warehouse1 = $database->getBuildingByType2($village->wid,10); -$embassy1 = $database->getBuildingByType2($village->wid,18); +$typesArray = []; +for ($i = 1; $i <= 42; $i++) { + $typesArray[] = $i; +} + +global $typeCounts; +$typeCounts = $database->getBuildingByType2($village->wid, $typesArray); + +function getTypeCount($id) { + global $typeCounts; + + return (isset($typeCounts[$id]) ? $typeCounts[$id] : 0); +} + +$mainbuilding1 = getTypeCount(15); +$cranny1 = getTypeCount(23); +$granary1 = getTypeCount(11); +$warehouse1 = getTypeCount(10); +$embassy1 = getTypeCount(18); $wall1 = $database->getBuildingByField2($village->wid,40); -$rallypoint1 = $database->getBuildingByType2($village->wid,16); -$hero1 = $database->getBuildingByType2($village->wid,37); -$market1 = $database->getBuildingByType2($village->wid,17); -$barrack1 = $database->getBuildingByType2($village->wid,19); -$cropland1 = $database->getBuildingByType2($village->wid,4); -$grainmill1 = $database->getBuildingByType2($village->wid,8); -$residence1 = $database->getBuildingByType2($village->wid,25); -$academy1 = $database->getBuildingByType2($village->wid,22); -$armoury1 = $database->getBuildingByType2($village->wid,13); -$woodcutter1 = $database->getBuildingByType2($village->wid,1); -$palace1 = $database->getBuildingByType2($village->wid,26); -$claypit1 = $database->getBuildingByType2($village->wid,2); -$ironmine1 = $database->getBuildingByType2($village->wid,3); -$blacksmith1 = $database->getBuildingByType2($village->wid,12); -$stable1 = $database->getBuildingByType2($village->wid,20); -$trapper1 = $database->getBuildingByType2($village->wid,36); -$treasury1 = $database->getBuildingByType2($village->wid,27); -$sawmill1 = $database->getBuildingByType2($village->wid,5); -$brickyard1 = $database->getBuildingByType2($village->wid,6); -$ironfoundry1 = $database->getBuildingByType2($village->wid,7); -$workshop1 = $database->getBuildingByType2($village->wid,21); -$stonemasonslodge1 = $database->getBuildingByType2($village->wid,34); -$townhall1 = $database->getBuildingByType2($village->wid,24); -$tournamentsquare1 = $database->getBuildingByType2($village->wid,14); -$bakery1 = $database->getBuildingByType2($village->wid,9); -$tradeoffice1 = $database->getBuildingByType2($village->wid,28); -$greatbarracks1 = $database->getBuildingByType2($village->wid,29); -$greatstable1 = $database->getBuildingByType2($village->wid,30); -$brewery1 = $database->getBuildingByType2($village->wid,35); -$horsedrinkingtrough1 = $database->getBuildingByType2($village->wid,41); -$herosmansion1 = $database->getBuildingByType2($village->wid,37); -$greatwarehouse1 = $database->getBuildingByType2($village->wid,38); -$greatgranary1 = $database->getBuildingByType2($village->wid,39); -$greatworkshop1 = $database->getBuildingByType2($village->wid,42); +$rallypoint1 = getTypeCount(16); +$hero1 = getTypeCount(37); +$market1 = getTypeCount(17); +$barrack1 = getTypeCount(19); +$cropland1 = getTypeCount(4); +$grainmill1 = getTypeCount(8); +$residence1 = getTypeCount(25); +$academy1 = getTypeCount(22); +$armoury1 = getTypeCount(13); +$woodcutter1 = getTypeCount(1); +$palace1 = getTypeCount(26); +$claypit1 = getTypeCount(2); +$ironmine1 = getTypeCount(3); +$blacksmith1 = getTypeCount(12); +$stable1 = getTypeCount(20); +$trapper1 = getTypeCount(36); +$treasury1 = getTypeCount(27); +$sawmill1 = getTypeCount(5); +$brickyard1 = getTypeCount(6); +$ironfoundry1 = getTypeCount(7); +$workshop1 = getTypeCount(21); +$stonemasonslodge1 = getTypeCount(34); +$townhall1 = getTypeCount(24); +$tournamentsquare1 = getTypeCount(14); +$bakery1 = getTypeCount(9); +$tradeoffice1 = getTypeCount(28); +$greatbarracks1 = getTypeCount(29); +$greatstable1 = getTypeCount(30); +$brewery1 = getTypeCount(35); +$horsedrinkingtrough1 = getTypeCount(41); +$herosmansion1 = getTypeCount(37); +$greatwarehouse1 = getTypeCount(38); +$greatgranary1 = getTypeCount(39); +$greatworkshop1 = getTypeCount(42); ?>