From ed0fc6be2cf88cda9f83963a97f08bdd1285f6bc Mon Sep 17 00:00:00 2001 From: Catalin Novgorodschi <1140613+Shadowss@users.noreply.github.com> Date: Tue, 12 May 2026 11:35:15 +0300 Subject: [PATCH] Incremental refactor Building and Ranking Incremental refactor Building and Ranking --- GameEngine/Building.php | 2762 +++++++++++++++++++++++++----------- GameEngine/Lang/en.php | 126 +- GameEngine/Ranking.php | 375 ++--- Templates/Ajax/plusmap.tpl | 187 --- ajax.php | 4 - 5 files changed, 2214 insertions(+), 1240 deletions(-) delete mode 100644 Templates/Ajax/plusmap.tpl diff --git a/GameEngine/Building.php b/GameEngine/Building.php index 85dbe3cc..40f4e658 100755 --- a/GameEngine/Building.php +++ b/GameEngine/Building.php @@ -3,905 +3,2025 @@ ################################################################################# ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ## ## --------------------------------------------------------------------------- ## -## Project: TravianZ ## -## Version: 22.06.2015 ## -## Filename Building.php ## -## Developed by: Mr.php , Advocaite , brainiacX , yi12345 , Shadow , ronix ## -## Fixed by: Shadow - STARVATION , HERO FIXED COMPL., TPLinux ## -## Fixed by: InCube - double troops ## -## License: TravianZ Project ## -## Copyright: TravianZ (c) 2010-2015. All rights reserved. ## -## URLs: http://travian.shadowss.ro ## -## Source code: https://github.com/Shadowss/TravianZ ## +## Project: TravianZ ## +## Version: 12.05.2026 ## +## Filename: Building.php ## +## Developed by: Dzoki & Dixie ## +## Refactored by: Shadow ## +## Fixed by: InCube - double troops ## +## Reworked/Fix: ronix ## +## Thanks to: Akakori, Elmar & Kirilloid ## +## License: TravianZ Project ## +## Copyright: TravianZ (c) 2010-2026. All rights reserved. ## +## ## +## URLs: http://travian.shadowss.ro ## +## https://github.com/Shadowss/TravianZ ## ## ## ################################################################################# class Building { - public $NewBuilding = false; - private $maxConcurrent; - private $allocated; - private $basic, $inner, $plus = 0; - public $buildArray = []; + public $NewBuilding = false; - public function __construct() { - global $session; - - $this->maxConcurrent = BASIC_MAX; - - if(ALLOW_ALL_TRIBE || $session->tribe == 1) $this->maxConcurrent += INNER_MAX; - if($session->plus) $this->maxConcurrent += PLUS_MAX; - - $this->LoadBuilding(); - foreach($this->buildArray as $build) { - if($build['master'] == 1) $this->maxConcurrent++; - } - } + private $maxConcurrent; + private $allocated; - /* - * Checks whether to allow building Wonder of the World - * above current level. This includes checks for WW upgrade - * currently in progress as well as current WW level - * and the right number of building plans in the alliance. - */ - - public function allowWwUpgrade() { - global $database, $village, $session; - static $cached = null; + private $basic = 0; + private $inner = 0; + private $plus = 0; - if($cached === null){ - $wwHighestLevelFound = $village->resarray['f99']; - $wwBuildingProgress = $database->getBuildingByType($village->wid, 99); + public $buildArray = array(); - if(count($wwBuildingProgress)){ - if($wwBuildingProgress[0]['level'] > $wwHighestLevelFound) { - $wwHighestLevelFound = $wwBuildingProgress[0]['level']; + public function __construct() { + + global $session; + + $maxConcurrent = BASIC_MAX; + + if (ALLOW_ALL_TRIBE || $session->tribe == 1) { + $maxConcurrent += INNER_MAX; + } + + if ($session->plus) { + $maxConcurrent += PLUS_MAX; + } + + $this->LoadBuilding(); + + if (!empty($this->buildArray)) { + + foreach ($this->buildArray as $build) { + + if ($build['master'] == 1) { + $maxConcurrent++; } } - - //Get our WW construction plans - $userHasWWConstructionPlans = $database->getWWConstructionPlans($session->uid); - - //Get ally WW construction plans - $allyHasWWConstructionPlans = $session->alliance > 0 ? $database->getWWConstructionPlans($session->uid, $session->alliance) : false; - - //Check if we should allow building the WW this high - if($wwHighestLevelFound < 50) $cached = $userHasWWConstructionPlans; - else $cached = $userHasWWConstructionPlans && $allyHasWWConstructionPlans; } - + + $this->maxConcurrent = $maxConcurrent; + } + + /***************************************** + Function to Allow WW Upgrade + *****************************************/ + + public function allowWwUpgrade() { + global $database, $village, $session; + + static $cached = null; + + if ($cached !== null) { return $cached; } - public function canProcess($id, $tid) { - //add fix by ronix - global $session, $database, $village; - - $levels = $database->getResourceLevel($village->wid); - - //Don't allow building WW to level 51 with a waiting loop - if (!(($tid != 99 || ($tid == 99 && $this->allowWwUpgrade())))) $this->redirect($tid); - - if ( - //Check that our ID actually exists within the buildings list - isset($village->resarray['f'.$tid.'t']) && - //Let's see if we should allow building what we want where we want to - //(Prevent building resource fields in the village - ( - ($tid >= 1 && $tid <= 18 && $id >= 1 && $id <= 4) || - ($tid >= 19 && $id > 4) - ) && - //Check that we're not trying to change a standing building type - ( - $levels['f'.$tid.'t'] == $id || - $levels['f'.$tid.'t'] == 0 - ) && - //Check that we're not trying to build in the walls id - ( - ($tid == 40 && in_array($id, [31, 32, 33])) || - $tid != 40 - ) - ) { - if (!isset($_GET['master']) && $this->checkResource($id, $tid) != 4){ - $this->redirect($tid); - } - - //Check if the building will be built with the master builder - if(isset($_GET['master'])){ - //If so, we have to check if it'll be built or upgraded - if($levels['f'.$tid.'t'] == 0) - { - //The building will be built, we have to check if we can build it - if(!$this->meetRequirement($id)) $this->redirect($tid); - }else{ - //The building will be upgraded, we have to check if we can upgrade it - if($this->isMax($id, $tid, $this->isLoop($tid) + $this->isCurrent($tid))){ - $this->redirect($tid); - } - } - } - }else{ - header('Location: '.$_SERVER['SCRIPT_NAME']); - exit; - } + $wwHighestLevelFound = (int)$village->resarray['f99']; + + // Evităm count() pe array doar pentru existență + $wwBuildingProgress = $database->getBuildingByType($village->wid, 99); + + if (!empty($wwBuildingProgress[0]['level'])) { + $queuedLevel = (int)$wwBuildingProgress[0]['level']; + + if ($queuedLevel > $wwHighestLevelFound) { + $wwHighestLevelFound = $queuedLevel; + } } - /** - * Redirects to dorf1.php if we're building/upgrading resource fields otherwise in dorf2.php - * - * @param int $tid the id where the building is built/upgraded - */ - - private function redirect($tid){ - if ($tid >= 19) header("Location: dorf2.php"); - else header("Location: dorf1.php"); + // Cache natural prin static + $userHasWWConstructionPlans = $database->getWWConstructionPlans($session->uid); + + // Nu mai facem query inutil dacă nu avem alianță + $allyHasWWConstructionPlans = false; + + if ($session->alliance > 0) { + $allyHasWWConstructionPlans = $database->getWWConstructionPlans( + $session->uid, + $session->alliance + ); + } + + // Reducem branch-urile + $cached = ( + $wwHighestLevelFound < 50 + ? $userHasWWConstructionPlans + : ($userHasWWConstructionPlans && $allyHasWWConstructionPlans) + ); + + return $cached; +} + + /***************************************** + Function to Process + *****************************************/ + + public function canProcess($id, $tid) { + global $session, $database, $village; + + // Un singur query + $levels = $database->getResourceLevel($village->wid); + + // WW restriction early exit + if ($tid == 99 && !$this->allowWwUpgrade()) { + $this->redirect($tid); + } + + $fieldTypeKey = 'f'.$tid.'t'; + + // Mic cache local + $currentFieldType = isset($levels[$fieldTypeKey]) ? (int)$levels[$fieldTypeKey] : 0; + + // Validare slot/building + $isValidSlot = + ( + ($tid >= 1 && $tid <= 18 && $id >= 1 && $id <= 4) || + ($tid >= 19 && $id > 4) + ); + + // Validare building existent + $isValidFieldType = + ($currentFieldType == $id || $currentFieldType == 0); + + // Validare zid + $isValidWall = + ($tid != 40 || in_array($id, array(31, 32, 33))); + + if ( + !isset($village->resarray[$fieldTypeKey]) || + !$isValidSlot || + !$isValidFieldType || + !$isValidWall + ) { + header('Location: '.$_SERVER['SCRIPT_NAME']); exit; } - - public function procBuild($get) { - global $session, $village, $database; - - if(isset($get['a']) && $get['c'] == $session->checker && !isset($get['id'])) { - if($get['a'] == 0) $this->removeBuilding($get['d']); - else - { - $session->changeChecker(); - $this->canProcess($village->resarray['f'.$get['a'].'t'],$get['a']); - $this->upgradeBuilding($get['a']); - } - }elseif(isset($get['master']) && isset($get['id']) && $session->gold >= 1 && $session->goldclub && $village->master == 0 && (isset($get['c']) && $get['c']== $session->checker)) { - $this->canProcess($get['master'], $get['id']); - $session->changeChecker(); - $level = $database->getResourceLevel($village->wid); - $time = $this->resourceRequired($get['id'], $get['master'])['time']; - $database->addBuilding($village->wid, $get['id'], $get['master'], 1, $time, 1, $level['f'.$get['id']] + 1 + count($database->getBuildingByField($village->wid, $get['id']))); - $this->redirect($get['id']); - }elseif(isset($get['a']) && $get['c'] == $session->checker && isset($get['id'])) { - if($get['id'] > 18 && ($get['id'] < 41 || $get['id'] == 99)){ - $session->changeChecker(); - $this->canProcess($get['a'], $get['id']); - $this->constructBuilding($get['id'], $get['a']); - } + + // Evităm apel dublu inutil + if (!isset($_GET['master'])) { + if ($this->checkResource($id, $tid) != 4) { + $this->redirect($tid); } - elseif(isset($get['buildingFinish']) && $session->gold >= 2 && $session->sit == 0) $this->finishAll(); + + return; } + // MASTER BUILDER + if ($currentFieldType == 0) { + // build nou + if (!$this->meetRequirement($id)) { + $this->redirect($tid); + } + } else { + // upgrade + $loopLevel = $this->isLoop($tid); + $currentLevel = $this->isCurrent($tid); + + if ($this->isMax($id, $tid, $loopLevel + $currentLevel)) { + $this->redirect($tid); + } + } +} + + /***************************************** + Function to redirect to dorf1 and dorf2 + *****************************************/ + + private function redirect($tid) { + header('Location: '.($tid >= 19 ? 'dorf2.php' : 'dorf1.php')); + exit; +} + + /***************************************** + Function to proc Building + *****************************************/ + + public function procBuild($get) { + global $session, $village, $database; + + // Cache checker comparison + $validChecker = ( + isset($get['c']) && + $get['c'] == $session->checker + ); + + // REMOVE / UPGRADE EXISTING + if (isset($get['a']) && !isset($get['id']) && $validChecker) { + + if ($get['a'] == 0) { + $this->removeBuilding($get['d']); + return; + } + + $session->changeChecker(); + + $fieldType = $village->resarray['f'.$get['a'].'t']; + + $this->canProcess($fieldType, $get['a']); + $this->upgradeBuilding($get['a']); + + return; + } + + // MASTER BUILDER + if ( + isset($get['master'], $get['id']) && + $validChecker && + $session->gold >= 1 && + $session->goldclub && + $village->master == 0 + ) { + + $this->canProcess($get['master'], $get['id']); + + $session->changeChecker(); + + // Un singur query + $level = $database->getResourceLevel($village->wid); + + // Un singur calcul + $required = $this->resourceRequired($get['id'], $get['master']); + + // Un singur query + $queuedBuildings = $database->getBuildingByField( + $village->wid, + $get['id'] + ); + + $database->addBuilding( + $village->wid, + $get['id'], + $get['master'], + 1, + $required['time'], + 1, + $level['f'.$get['id']] + 1 + count($queuedBuildings) + ); + + $this->redirect($get['id']); + } + + // CONSTRUCT NEW BUILDING + if ( + isset($get['a'], $get['id']) && + $validChecker + ) { + + if ($get['id'] > 18 && ($get['id'] < 41 || $get['id'] == 99)) { + + $session->changeChecker(); + + $this->canProcess($get['a'], $get['id']); + $this->constructBuilding($get['id'], $get['a']); + } + + return; + } + + // FINISH ALL + if ( + isset($get['buildingFinish']) && + $session->gold >= 2 && + $session->sit == 0 + ) { + $this->finishAll(); + } +} + public function canBuild($id, $tid) { - global $village, $session, $database; - - $demolition = $database->getDemolition($village->wid); - if((isset($demolition[0])) && $demolition[0]['buildnumber'] == $id) return 11; - - if($this->isMax($tid, $id)) return 1; - else if($this->isMax($tid, $id, 1) && ($this->isLoop($id) || $this->isCurrent($id))) return 10; - else if($this->isMax($tid, $id, 2) && $this->isLoop($id) && $this->isCurrent($id)) return 10; - else if($this->isMax($tid, $id, 3) && $this->isLoop($id) && $this->isCurrent($id) && count($database->getMasterJobs($village->wid)) > 0) { - return 10; - } - else { - if($this->allocated <= $this->maxConcurrent) { - $resRequired = $this->resourceRequired($id, $village->resarray['f'.$id.'t']); - $resRequiredPop = $resRequired['pop']; - if (empty($resRequiredPop)) { - $buildarray = $GLOBALS["bid".$tid]; - $resRequiredPop = $buildarray[1]['pop']; - } - $jobs = $database->getJobs($village->wid); - if ($jobs > 0) { - $soonPop = 0; - foreach ($jobs as $j) { - $buildarray = $GLOBALS["bid".$j['type']]; - $soonPop += $buildarray[$database->getFieldLevel($village->wid, $j['field']) + 1]['pop']; - } - } - if(($village->allcrop - $village->pop - $soonPop - $resRequiredPop) <= 1 && $village->resarray['f'.$id.'t'] <> 4) { - return 4; - } - else { - switch($this->checkResource($tid, $id)) { - case 1: return 5; - case 2: return 6; - case 3: return 7; - case 4: - if($id >= 19) { - if($session->tribe == 1 || ALLOW_ALL_TRIBE) { - if($this->inner == 0) return 8; - else - { - if($session->plus || $tid == 40) { - if($this->plus == 0) return 9; - else return 3; - } - else return 2; - } - } - else { - if($this->basic == 0) return 8; - else - { - if($session->plus || $tid == 40) { - if($this->plus == 0) return 9; - else return 3; - } - else return 2; - } - } - } - else { - if($this->basic == 1) { - if(($session->plus || $tid == 40) && $this->plus == 0) return 9; - else return 3; - } - else return 8; - } - } - } - } - else return 2; - } - } + global $village, $session, $database; + + // Query unic + $demolition = $database->getDemolition($village->wid); + + if ( + isset($demolition[0]['buildnumber']) && + $demolition[0]['buildnumber'] == $id + ) { + return 11; + } + + // Cache funcții repetitive + $loop = $this->isLoop($id); + $current = $this->isCurrent($id); + + if ($this->isMax($tid, $id)) { + return 1; + } + + if ( + $this->isMax($tid, $id, 1) && + ($loop || $current) + ) { + return 10; + } + + if ( + $this->isMax($tid, $id, 2) && + $loop && + $current + ) { + return 10; + } + + // Evităm query inutil getMasterJobs() + if ( + $loop && + $current && + $this->isMax($tid, $id, 3) + ) { + + $masterJobs = $database->getMasterJobs($village->wid); + + if (!empty($masterJobs)) { + return 10; + } + } + + if ($this->allocated > $this->maxConcurrent) { + return 2; + } + + // Un singur apel + $fieldType = $village->resarray['f'.$id.'t']; + + // Un singur apel + $resRequired = $this->resourceRequired($id, $fieldType); + + $resRequiredPop = isset($resRequired['pop']) + ? $resRequired['pop'] + : 0; + + // Fallback doar dacă trebuie + if (empty($resRequiredPop)) { + $buildarray = $GLOBALS['bid'.$tid]; + $resRequiredPop = $buildarray[1]['pop']; + } + + // Un singur query + $jobs = $database->getJobs($village->wid); + + $soonPop = 0; + + if (!empty($jobs)) { + + foreach ($jobs as $j) { + + $buildarray = $GLOBALS['bid'.$j['type']]; + + // Aici era query per job (!) + // Reducem drastic CPU + SQL + static $fieldLevelCache = array(); + + $cacheKey = $village->wid.'_'.$j['field']; + + if (!isset($fieldLevelCache[$cacheKey])) { + $fieldLevelCache[$cacheKey] = $database->getFieldLevel( + $village->wid, + $j['field'] + ); + } + + $soonPop += $buildarray[ + $fieldLevelCache[$cacheKey] + 1 + ]['pop']; + } + } + + if ( + ($village->allcrop - $village->pop - $soonPop - $resRequiredPop) <= 1 && + $fieldType != 4 + ) { + return 4; + } + + // Un singur apel + $resourceCheck = $this->checkResource($tid, $id); + + switch ($resourceCheck) { + + case 1: + return 5; + + case 2: + return 6; + + case 3: + return 7; + + case 4: + + if ($id >= 19) { + + $hasMainBuilding = + ( + $session->tribe == 1 || + ALLOW_ALL_TRIBE + ) + ? $this->inner + : $this->basic; + + if ($hasMainBuilding == 0) { + return 8; + } + + if ($session->plus || $tid == 40) { + return ($this->plus == 0 ? 9 : 3); + } + + return 2; + } + + if ($this->basic == 1) { + + if (($session->plus || $tid == 40) && $this->plus == 0) { + return 9; + } + + return 3; + } + + return 8; + } +} + + /***************************************** + Function to wall + *****************************************/ public function walling() { - global $session; - $wall = array(31,32,33); - foreach($this->buildArray as $job) { - if(in_array($job['type'],$wall)) return "3".$session->tribe; - } - return false; - } + + global $session; + + foreach ($this->buildArray as $job) { + + // verificăm STRICT tipul clădirii + if ( + $job['type'] == 31 || + $job['type'] == 32 || + $job['type'] == 33 + ) { + return $job['type']; + } + } + + return false; +} + + /***************************************** + Function to rally point + *****************************************/ public function rallying() { - foreach($this->buildArray as $job) { - if($job['type'] == 16) return true; - } - return false; - } - public static function procResType($ref) { - switch($ref) { - - case 1: return WOODCUTTER; - case 2: return CLAYPIT; - case 3: return IRONMINE; - case 4: return CROPLAND; - case 5: return SAWMILL; - case 6: return BRICKYARD; - case 7: return IRONFOUNDRY; - case 8: return GRAINMILL; - case 9: return BAKERY; - case 10: return WAREHOUSE; - case 11: return GRANARY; - case 12: return BLACKSMITH; - case 13: return ARMOURY; - case 14: return TOURNAMENTSQUARE; - case 15: return MAINBUILDING; - case 16: return RALLYPOINT; - case 17: return MARKETPLACE; - case 18: return EMBASSY; - case 19: return BARRACKS; - case 20: return STABLE; - case 21: return WORKSHOP; - case 22: return ACADEMY; - case 23: return CRANNY; - case 24: return TOWNHALL; - case 25: return RESIDENCE; - case 26: return PALACE; - case 27: return TREASURY; - case 28: return TRADEOFFICE; - case 29: return GREATBARRACKS; - case 30: return GREATSTABLE; - case 31: return CITYWALL; - case 32: return EARTHWALL; - case 33: return PALISADE; - case 34: return STONEMASON; - case 35: return BREWERY; - case 36: return TRAPPER; - case 37: return HEROSMANSION; - case 38: return GREATWAREHOUSE; - case 39: return GREATGRANARY; - case 40: return WONDER; - case 41: return HORSEDRINKING; - case 42: return GREATWORKSHOP; - default: return "Error"; - } - } + if (empty($this->buildArray)) { + return false; + } + + foreach ($this->buildArray as $job) { + + // comparație directă mai rapidă + if ((int)$job['type'] === 16) { + return true; + } + } + + return false; +} + + /***************************************** + Function to proc resource type + *****************************************/ + + public static function procResType($ref) { + + // Cache static -> switch-ul nu mai este evaluat constant + static $types = array( + 1 => WOODCUTTER, + 2 => CLAYPIT, + 3 => IRONMINE, + 4 => CROPLAND, + 5 => SAWMILL, + 6 => BRICKYARD, + 7 => IRONFOUNDRY, + 8 => GRAINMILL, + 9 => BAKERY, + 10 => WAREHOUSE, + 11 => GRANARY, + 12 => BLACKSMITH, + 13 => ARMOURY, + 14 => TOURNAMENTSQUARE, + 15 => MAINBUILDING, + 16 => RALLYPOINT, + 17 => MARKETPLACE, + 18 => EMBASSY, + 19 => BARRACKS, + 20 => STABLE, + 21 => WORKSHOP, + 22 => ACADEMY, + 23 => CRANNY, + 24 => TOWNHALL, + 25 => RESIDENCE, + 26 => PALACE, + 27 => TREASURY, + 28 => TRADEOFFICE, + 29 => GREATBARRACKS, + 30 => GREATSTABLE, + 31 => CITYWALL, + 32 => EARTHWALL, + 33 => PALISADE, + 34 => STONEMASON, + 35 => BREWERY, + 36 => TRAPPER, + 37 => HEROSMANSION, + 38 => GREATWAREHOUSE, + 39 => GREATGRANARY, + 40 => WONDER, + 41 => HORSEDRINKING, + 42 => GREATWORKSHOP + ); + + return isset($types[$ref]) ? $types[$ref] : 'Error'; +} + + /***************************************** + Function to load all building + *****************************************/ public function loadBuilding() { - global $database,$village,$session; - $this->basic = 0; - $this->inner = 0; - $this->plus = 0; - $this->buildArray = $database->getJobs($village->wid); - $this->allocated = count($this->buildArray); - if($this->allocated > 0) { - foreach($this->buildArray as $build) { - if($build['loopcon'] == 1) $this->plus = 1; - else - { - if($build['field'] <= 18) $this->basic++; - else - { - if($session->tribe == 1 || ALLOW_ALL_TRIBE) $this->inner++; - else $this->basic++; - } - } - } - $this->NewBuilding = true; - } - else $this->NewBuilding = false; - } + global $database, $village, $session; + + $this->basic = 0; + $this->inner = 0; + $this->plus = 0; + + // un singur query + $this->buildArray = $database->getJobs($village->wid); + + // count() o singură dată + $this->allocated = count($this->buildArray); + + if ($this->allocated <= 0) { + $this->NewBuilding = false; + return; + } + + $romanQueue = ($session->tribe == 1 || ALLOW_ALL_TRIBE); + + foreach ($this->buildArray as $build) { + + // master queue + if ((int)$build['loopcon'] === 1) { + $this->plus = 1; + continue; + } + + // resource fields + if ($build['field'] <= 18) { + $this->basic++; + continue; + } + + // village buildings + if ($romanQueue) { + $this->inner++; + } else { + $this->basic++; + } + } + + $this->NewBuilding = true; +} + + /***************************************** + Function to remove building + *****************************************/ private function removeBuilding($d) { - global $database, $village, $session; - foreach($this->buildArray as $jobs) { - if($jobs['id'] == $d) { - $uprequire = $this->resourceRequired($jobs['field'], $jobs['type']); - if($database->removeBuilding($d, $session->tribe, $village->wid, $village->resarray)) { - if($jobs['master'] == 0) $database->modifyResource($village->wid, $uprequire['wood'], $uprequire['clay'], $uprequire['iron'], $uprequire['crop'], 1); - $this->redirect($jobs['field']); - } - } - } - } + global $database, $village, $session; + + if (empty($this->buildArray)) { + return; + } + + foreach ($this->buildArray as $jobs) { + + // early continue + if ($jobs['id'] != $d) { + continue; + } + + // calcul doar când chiar trebuie + $uprequire = $this->resourceRequired( + $jobs['field'], + $jobs['type'] + ); + + if ( + $database->removeBuilding( + $d, + $session->tribe, + $village->wid, + $village->resarray + ) + ) { + + // refund doar pentru non-master + if ((int)$jobs['master'] === 0) { + + $database->modifyResource( + $village->wid, + $uprequire['wood'], + $uprequire['clay'], + $uprequire['iron'], + $uprequire['crop'], + 1 + ); + } + + $this->redirect($jobs['field']); + } + + // nu mai continuăm foreach inutil + break; + } +} + + /***************************************** + Function to upgrade building + *****************************************/ private function upgradeBuilding($id) { - global $database, $village, $session, $logging, ${'bid'.$village->resarray['f'.$id.'t']}; + global $database, $village, $session, $logging, ${'bid'.$village->resarray['f'.$id.'t']}; - if(!$database->getBuildLock($village->wid)) return; - try { - $this->loadBuilding(); + if (!$database->getBuildLock($village->wid)) { + return; + } - if($this->allocated < $this->maxConcurrent) { - $uprequire = $this->resourceRequired($id,$village->resarray['f'.$id.'t']); - $time = time() + $uprequire['time']; - $bindicate = $this->canBuild($id,$village->resarray['f'.$id.'t']); + try { - // don't allow building above max levels and don't allow building if it's in demolition - if (in_array($bindicate, [1, 2, 3, 10, 11])) { - $database->releaseBuildLock($village->wid); - header("Location: dorf2.php"); - exit; - } + $this->loadBuilding(); - $loop = ($bindicate == 9 ? 1 : 0); - $loopsame = 0; - if($loop == 1) { - foreach($this->buildArray as $build) { - if($build['field'] == $id) { - $loopsame++; - $uprequire = $this->resourceRequired($id, $village->resarray['f'.$id.'t'], ($loopsame > 0 ? 2 : 1)); - } - } - if($session->tribe == 1 || ALLOW_ALL_TRIBE) { - if($id >= 19) { - foreach($this->buildArray as $build) { - if($build['field'] >= 19) { - $time = $build['timestamp'] + $uprequire['time']; - } - } - } - else { - foreach($this->buildArray as $build) { - if($build['field'] <= 18) { - $time = $build['timestamp'] + $uprequire['time']; - } - } - } - } - else { - $time = $this->buildArray[0]['timestamp'] + $uprequire['time']; - } - } - $level = $database->getResourceLevel($village->wid); - - - if($database->addBuilding($village->wid, $id, $village->resarray['f'.$id.'t'], $loop, $time + ($loop == 1 ? ceil(60 / SPEED) : 0), 0, $level['f'.$id] + 1 + count($database->getBuildingByField($village->wid, $id)))) { - $database->modifyResource($village->wid, $uprequire['wood'], $uprequire['clay'], $uprequire['iron'], $uprequire['crop'], 0); - $logging->addBuildLog($village->wid, self::procResType($village->resarray['f'.$id.'t']), ($village->resarray['f'.$id] + ($loopsame > 0 ? 2 : 1)), 0); - $this->redirect($id); - } - } - } finally { - $database->releaseBuildLock($village->wid); - } - } - - private function downgradeBuilding($id) { - global $database, $village, $session, $logging; - - if(!$database->getBuildLock($village->wid)) return; - try { - $this->loadBuilding(); - - if($this->allocated < $this->maxConcurrent) { - $name = "bid".$village->resarray['f'.$id.'t']; - global $$name; - $dataarray = $$name; - $time = time() + round($dataarray[$village->resarray['f'.$id]-1]['time'] / 4); - $loop = 0; - if($this->inner == 1 || $this->basic == 1) { - if(($session->plus || $village->resarray['f'.$id.'t']==40)&& $this->plus == 0) { - $loop = 1; - } - } - if($loop == 1) { - if($session->tribe == 1 || ALLOW_ALL_TRIBE) { - if($id >= 19) { - foreach($this->buildArray as $build) { - if($build['field'] >= 19) { - $time = $build['timestamp'] + round($dataarray[$village->resarray['f'.$id]-1]['time'] / 4); - } - } - } - } - else { - $time = $this->buildArray[0]['timestamp'] + round($dataarray[$village->resarray['f'.$id]-1]['time'] / 4); - } - } - - $level = $database->getResourceLevel($village->wid); - if($database->addBuilding($village->wid, $id, $village->resarray['f'.$id.'t'], $loop, $time, 0, 0, $level['f'.$id] + 1 + count($database->getBuildingByField($village->wid, $id)))){ - $logging->addBuildLog($village->wid, self::procResType($village->resarray['f'.$id.'t']), ($village->resarray['f'.$id] - 1), 2); - header("Location: dorf2.php"); - exit(); - } - } - } finally { - $database->releaseBuildLock($village->wid); - } - } - - private function constructBuilding($id, $tid) { - global $database, $village, $session, $logging; - - if(!$database->getBuildLock($village->wid)) return; - try { - $this->loadBuilding(); - - if($this->allocated < $this->maxConcurrent) { - if($tid == 16) $id = 39; - elseif($tid == 31 || $tid == 32 || $tid == 33) $id = 40; - - $uprequire = $this->resourceRequired($id, $tid); - $time = time() + $uprequire['time']; - $bindicate = $this->canBuild($id, $tid); - $loop = ($bindicate == 9 ? 1 : 0); - - if($loop == 1) { - foreach( $this->buildArray as $build) { - if($build['field'] >= 19 || ($session->tribe <> 1 && !ALLOW_ALL_TRIBE)) { - $time = $build['timestamp'] + ceil(60 / SPEED) + $uprequire['time']; - } - } - } - - if($this->meetRequirement($tid)) { - $level = $database->getResourceLevel($village->wid); - if($database->addBuilding($village->wid, $id, $tid, $loop, $time, 0, $level['f' . $id] + 1 + count($database->getBuildingByField($village->wid, $id)))){ - $logging->addBuildLog($village->wid, self::procResType($tid), ($village->resarray['f' . $id] + 1), 1); - $database->modifyResource($village->wid, $uprequire['wood'], $uprequire['clay'], $uprequire['iron'], $uprequire['crop'], 0); - header("Location: dorf2.php"); - exit; - } - }else{ - header("location: dorf2.php"); - exit; - } - }else{ - header("Location: dorf2.php"); - exit; - } - } finally { - $database->releaseBuildLock($village->wid); - } - } - - /** - * Search through all user's villages if the castle is built or not - * - * @return boolean Returns true if the castle is already built in the whole account, otherwise returns false - */ - - public function isCastleBuilt(){ - global $database, $session; - - $villages = $database->getProfileVillages($session->uid); - foreach ($villages as $vil){ - if(in_array(26, $database->getResourceLevel($vil['wref']))) return true; - } - return false; - } - - private function meetRequirement($id) { - global $village,$session,$database; - - $isBuilt = $this->getTypeField($id); - - switch ($id) { - case 1: - case 2: - case 3: - case 4: return true; - - case 5: return $this->getTypeLevel(1) >= 10 && $this->getTypeLevel(15) >= 5 && !$isBuilt; - case 6: return $this->getTypeLevel(2) >= 10 && $this->getTypeLevel(15) >= 5 && !$isBuilt; - case 7: return $this->getTypeLevel(3) >= 10 && $this->getTypeLevel(15) >= 5 && !$isBuilt; - case 8: return $this->getTypeLevel(4) >= 5 && !$isBuilt; - case 9: return $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(4) >= 10 && $this->getTypeLevel(8) >= 5 && !$isBuilt; - - case 10: - case 11: return $this->getTypeLevel(15) >= 1 && (!$isBuilt || $this->getTypeLevel($id) == 20); - - case 12: return $this->getTypeLevel(22) >= 3 && $this->getTypeLevel(15) >= 3 && !$isBuilt; - case 13: return $this->getTypeLevel(15) >= 3 && $this->getTypeLevel(22) >= 1 && !$isBuilt; - case 14: return $this->getTypeLevel(16) >= 15 && !$isBuilt; - - case 15: - case 16: return !$isBuilt; - - case 17: return $this->getTypeLevel(15) >= 3 && $this->getTypeLevel(10) >= 1 && $this->getTypeLevel(11) >= 1 && !$isBuilt; - case 18: return $this->getTypeLevel(15) >= 1 && !$isBuilt; - case 19: return $this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1 && !$isBuilt; - case 20: return $this->getTypeLevel(12) >= 3 && $this->getTypeLevel(22) >= 5 && !$isBuilt; - case 21: return $this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 5 && !$isBuilt; - case 22: return $this->getTypeLevel(15) >= 3 && $this->getTypeLevel(19) >= 3 && !$isBuilt; - case 23: return !$isBuilt || $this->getTypeLevel($id) == 10; - case 24: return $this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 10 && !$isBuilt; - case 25: return $this->getTypeLevel(15) >= 5 && !$isBuilt && !$this->getTypeField(26); - case 26: return $this->getTypeLevel(18) >= 1 && $this->getTypeLevel(15) >= 5 && !$isBuilt && !$this->isCastleBuilt() && !$this->getTypeField(25); - case 27: return $this->getTypeLevel(15) >= 10 && !$isBuilt; - case 28: return $this->getTypeLevel(17) == 20 && $this->getTypeLevel(20) >= 10 && !$isBuilt; - case 29: return $this->getTypeLevel(19) == 20 && $village->capital == 0 && !$isBuilt; - case 30: return $this->getTypeLevel(20) == 20 && $village->capital == 0 && !$isBuilt; - case 31: return $session->tribe == 1; - case 32: return $session->tribe == 2; - case 33: return $session->tribe == 3; - case 34: return $this->getTypeLevel(26) >= 3 && $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(25) == 0 && $village->capital != 0 && !$isBuilt; - case 35: return $this->getTypeLevel(16) >= 10 && $this->getTypeLevel(11) == 20 && $session->tribe == 2 && $village->capital != 0 && !$isBuilt; - case 36: return $this->getTypeLevel(16) >= 1 && $session->tribe == 3 && (!$isBuilt || $this->getTypeLevel($id) == 20); - case 37: return $this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1 && !$isBuilt; - - // great warehouse can only be built with artefact or in Natar villages - case 38: return $this->getTypeLevel(15) >= 10 && (!$isBuilt || $this->getTypeLevel($id) == 20) && ($village->natar == 1 || count($database->getOwnUniqueArtefactInfo2($village->wid, 6, 1, 1)) || count($database->getOwnUniqueArtefactInfo2($session->uid, 6, 2, 0))); - - // great granary can only be built with artefact or in Natar villages - case 39: return $this->getTypeLevel(15) >= 10 && (!$isBuilt || $this->getTypeLevel($id) == 20) && ($village->natar == 1 || count($database->getOwnUniqueArtefactInfo2($village->wid, 6, 1, 1)) || count($database->getOwnUniqueArtefactInfo2($session->uid, 6, 2, 0))); - - case 40: return $this->allowWwUpgrade(); - case 41: return $this->getTypeLevel(16) >= 10 && $this->getTypeLevel(20) == 20 && $session->tribe == 1 && !$isBuilt; - case 42: return GREAT_WKS && $this->getTypeLevel(21) == 20 && $village->capital == 0 && !$isBuilt; - default: return false; + if ($this->allocated >= $this->maxConcurrent) { + return; } - } - private function checkResource($tid, $id) { - $name = "bid".$tid; - global $village,$$name,$database; - - $plus = 1; - foreach($this->buildArray as $job) { - if($job['type'] == $tid && $job['field'] == $id) { - $plus = 2; - break; - } - } - - $dataarray = $$name; - $wood = $dataarray[$village->resarray['f'.$id] + $plus]['wood']; - $clay = $dataarray[$village->resarray['f'.$id] + $plus]['clay']; - $iron = $dataarray[$village->resarray['f'.$id] + $plus]['iron']; - $crop = $dataarray[$village->resarray['f'.$id] + $plus]['crop']; - - if($wood > $village->maxstore || $clay > $village->maxstore || $iron > $village->maxstore) return 1; - else - { - if($crop > $village->maxcrop) return 2; - else - { - if($wood > $village->awood || $clay > $village->aclay || $iron > $village->airon || $crop > $village->acrop) { - return 3; - } - else { - if($village->awood >= $wood && $village->aclay >= $clay && $village->airon >= $iron && $village->acrop >= $crop){ - return 4; - } - else return 3; - } - } - } - } + $fieldType = $village->resarray['f'.$id.'t']; - public function isMax($id, $field, $loop = 0) { - $name = "bid".$id; - global $$name,$village,$session; - $dataarray = $$name; + // un singur apel + $uprequire = $this->resourceRequired($id, $fieldType); - // special case for Multihunter login which mathematically (because of the resarray length) - // allows for building resource fields above level 20 - if ($session->tribe == 0) return $village->resarray['f'.$field] == 20; + $time = time() + $uprequire['time']; - if($id <= 4) { - if($village->capital == 1) return ($village->resarray['f'.$field] == (count($dataarray) - 1 - $loop)); - else return ($village->resarray['f'.$field] == (count($dataarray) - 11 - $loop)); - } - else return ($village->resarray['f'.$field] == count($dataarray) - $loop); - } + // un singur apel + $bindicate = $this->canBuild($id, $fieldType); - public function getTypeLevel($tid, $vid = 0) { - global $village, $database, $session; + // early exit + if (in_array($bindicate, array(1, 2, 3, 10, 11))) { - // Support would not have a village, so this is irrelevant - if ($session->uid == 1) return 0; + header('Location: dorf2.php'); + exit; + } - $keyholder = []; + $loop = ($bindicate == 9 ? 1 : 0); + $loopsame = 0; - if($vid == 0) $resourcearray = $village->resarray; - else $resourcearray = $database->getResourceLevel($vid); + if ($loop == 1) { - foreach(array_keys($resourcearray, $tid) as $key) { - if(strpos($key,'t')) { - $key = preg_replace("/[^0-9]/", '', $key); - array_push($keyholder, $key); - } - } + foreach ($this->buildArray as $build) { - $element = count($keyholder); + if ($build['field'] == $id) { - // if we count more than 1 instance of the building (mostly resource fields) - if($element >= 2){ - // resource field - if($tid <= 4){ - $temparray = []; - - for($i = 0; $i <= $element - 1; $i++){ - // collect current field level - array_push($temparray, $resourcearray['f'.$keyholder[$i]]); - } - - // find out the maximum field level for this village - $maValue = max($temparray); - foreach($temparray as $key => $val){ - if($val == $maValue){ - $target = $key; - } - } - }else{ // village building - $target = 0; - - // find the highest level built for this building type - for($i = 1; $i <= $element - 1; $i++){ - if($resourcearray['f'.$keyholder[$i]] > $resourcearray['f'.$keyholder[$target]]){ - $target = $i; - } - } - } - } - - // if we count only a single building - else if($element == 1) $target = 0; - else return 0; // no building matching search criteria - - if(!empty($keyholder[$target])) return $resourcearray['f'.$keyholder[$target]]; - else return 0; - } + $loopsame++; + // păstrăm logica originală + $uprequire = $this->resourceRequired( + $id, + $fieldType, + ($loopsame > 0 ? 2 : 1) + ); + } + } - public function isCurrent($id) { - foreach($this->buildArray as $build) { - if($build['field'] == $id && $build['loopcon'] <> 1) { - return true; - } - } - } + $romanQueue = ($session->tribe == 1 || ALLOW_ALL_TRIBE); - public function isLoop($id=0) { - foreach($this->buildArray as $build) { - if(($build['field'] == $id && $build['loopcon']) || ($build['loopcon'] == 1 && $id == 0)) { - return true; - } - } - return false; - } + if ($romanQueue) { - public function finishAll($redirect_url = '') { - global $database, $session, $logging, $village, $bid18, $bid10, $bid11, $technology, $_SESSION; + if ($id >= 19) { - // will be true if we should decrease player's gold by 2 - // for the immediate completion action - $countPlus2Gold = false; - // will be true if we should decrease player's gold by 1 - // for master builder queue - $countMasterGold = false; - // number of jobs to finish - $buildcount = ($this->buildArray ? count($this->buildArray) : 0); - // will be true if the job was successfully finished - $jobFinishSuccess = false; - // IDs of successful jobs to delete - $deletIDs = []; - - foreach ($this->buildArray as $jobs) { - if ($jobs['wid'] == $village->wid) { - $wwvillage = $database->getResourceLevel($jobs['wid']); - if ($wwvillage['f99t'] != 40) { - $level = $jobs['level']; - if ($jobs['type'] != 25 && $jobs['type'] != 26 && $jobs['type'] != 40) { - $resource = $this->resourceRequired($jobs['field'],$jobs['type']); - // master builder involved - if ($jobs['master'] != 0) { - if ($this->meetRequirement($jobs['field'])) { - // don't allow master builder to build anything - // if we only have 2 gold, since that would take us to -1 gold - if ( $session->gold > 2 ) { - $villwood = $database->getVillageField( $jobs['wid'], 'wood' ); - $villclay = $database->getVillageField( $jobs['wid'], 'clay' ); - $villiron = $database->getVillageField( $jobs['wid'], 'iron' ); - $villcrop = $database->getVillageField( $jobs['wid'], 'crop' ); - $type = $jobs['type']; - $buildarray = $GLOBALS[ "bid" . $type ]; - $buildwood = $buildarray[ $level ]['wood']; - $buildclay = $buildarray[ $level ]['clay']; - $buildiron = $buildarray[ $level ]['iron']; - $buildcrop = $buildarray[ $level ]['crop']; - if ( $buildwood < $villwood && $buildclay < $villclay && $buildiron < $villiron && $buildcrop < $villcrop ) { - $jobFinishSuccess = true; - $countMasterGold = true; - $enought_res = 1; - // we need to subtract resources for this, if another 2 jobs are active, - // as we'd never subtract those otherwise - if ( $buildcount > 2 ) { - $database->setVillageField( $jobs['wid'], - ['wood', 'clay', 'iron', 'crop'], - [( $villwood - $buildwood ), ( $villclay - $buildclay ), ( $villiron - $buildiron ), ( $villcrop - $buildcrop )]); - } - } - } else { - // if we only have 2 gold, we need to cancel this job, as there will never - // be enough gold now in our account to finish this up - $exclude_master = true; - $deletIDs[] = (int) $jobs['id']; - } - } - } else { - // non-master builder build, we should count +2 gold for it - $countPlus2Gold = true; - $jobFinishSuccess = true; + foreach ($this->buildArray as $build) { + + if ($build['field'] >= 19) { + $time = $build['timestamp'] + $uprequire['time']; } - - // update build level in the database - if ($jobFinishSuccess) { - $q = "UPDATE ".TB_PREFIX."fdata set f".$jobs['field']." = ".$jobs['level'].", f".$jobs['field']."t = ".$jobs['type']." where vref = ".$jobs['wid']; - } - - if (!isset($exclude_master) && $database->query($q) && ($enought_res == 1 || $jobs['master'] == 0)) { - $database->modifyPop($jobs['wid'],$resource['pop'],0); - $deletIDs[] = (int) $jobs['id']; - if($jobs['type'] == 18) { - $owner = $database->getVillageField($jobs['wid'],"owner"); - $max = $bid18[$level]['attri']; - $q = "UPDATE ".TB_PREFIX."alidata set max = $max where leader = $owner"; - $database->query($q); - } - } - - if (($jobs['field'] >= 19 && ($session->tribe == 1 || ALLOW_ALL_TRIBE)) || (!ALLOW_ALL_TRIBE && $session->tribe != 1)) { - $innertimestamp = $jobs['timestamp']; + } + } else { + + foreach ($this->buildArray as $build) { + + if ($build['field'] <= 18) { + $time = $build['timestamp'] + $uprequire['time']; } } } - } - - // reset the flag for the next job - $jobFinishSuccess = false; - } - - if (count($deletIDs)) { - $database->query("DELETE FROM " . TB_PREFIX . "bdata WHERE id IN(" . implode(', ', $deletIDs) . ")"); - } - - $demolition = $database->finishDemolition($village->wid); - $tech = $technology->finishTech(); - if ($demolition > 0 || $tech > 0) { - $countPlus2Gold = true; - $logging->goldFinLog($village->wid); - } - - // deduct the right amount of gold - if ($countMasterGold || $countPlus2Gold) { - $newgold = $session->gold - (($countMasterGold && $countPlus2Gold) ? 3 : 2); - $database->updateUserField($session->uid, "gold", $newgold, 1); - } - - $stillbuildingarray = $database->getJobs($village->wid); - if (count($stillbuildingarray) == 1) { - if($stillbuildingarray[0]['loopcon'] == 1) { - //$q = "UPDATE ".TB_PREFIX."bdata SET loopcon=0,timestamp=".(time()+$stillbuildingarray[0]['timestamp']-$innertimestamp)." WHERE id=".$stillbuildingarray[0]['id']; - $q = "UPDATE ".TB_PREFIX."bdata SET loopcon=0 WHERE id=".(int) $stillbuildingarray[0]['id']; - $database->query($q); + } else { + + if (isset($this->buildArray[0]['timestamp'])) { + $time = $this->buildArray[0]['timestamp'] + $uprequire['time']; + } } } - - self::recountCP($database, $village->wid); - - header("Location: ".($redirect_url ? $redirect_url : $session->referrer)); - exit; + + // un singur query + $level = $database->getResourceLevel($village->wid); + + // un singur query + $queuedBuildings = $database->getBuildingByField( + $village->wid, + $id + ); + + $nextLevel = + $level['f'.$id] + + 1 + + count($queuedBuildings); + + if ( + $database->addBuilding( + $village->wid, + $id, + $fieldType, + $loop, + $time + ($loop == 1 ? ceil(60 / SPEED) : 0), + 0, + $nextLevel + ) + ) { + + $database->modifyResource( + $village->wid, + $uprequire['wood'], + $uprequire['clay'], + $uprequire['iron'], + $uprequire['crop'], + 0 + ); + + $logging->addBuildLog( + $village->wid, + self::procResType($fieldType), + ($village->resarray['f'.$id] + ($loopsame > 0 ? 2 : 1)), + 0 + ); + + $this->redirect($id); + } + + } finally { + + $database->releaseBuildLock($village->wid); + } +} + + /***************************************** + Function to downgrade building + *****************************************/ + + private function downgradeBuilding($id) { + global $database, $village, $session, $logging; + + if (!$database->getBuildLock($village->wid)) { + return; } - public static function recountCP($database, $vid){ - $vid = (int) $vid; - $fdata = $database->getResourceLevel($vid); - $cpTot = 0; + try { - for ($i = 1; $i <= 40; $i++) { - $lvl = $fdata["f".$i]; - $building = $fdata["f".$i."t"]; - if($building){ - $cpTot += self::buildingCP($building,$lvl); - } + $this->loadBuilding(); + + if ($this->allocated >= $this->maxConcurrent) { + return; } - $q = "UPDATE ".TB_PREFIX."vdata set cp = $cpTot where wref = $vid"; - mysqli_query($database->dblink,$q); - } - - public static function buildingCP($f, $lvl){ - $name = "bid".$f; + $fieldType = $village->resarray['f'.$id.'t']; + $currentLevel = $village->resarray['f'.$id]; + + $name = 'bid'.$fieldType; + global $$name; $dataarray = $$name; - return ((isset($dataarray[$lvl]) && isset($dataarray[$lvl]['cp'])) ? $dataarray[$lvl]['cp'] : 0); - } - - public function resourceRequired($id, $tid, $plus = 1) { - $name = "bid".$tid; - global $$name, $village, $bid15, $database; - - $dataarray = $$name; + // calcul o singură dată + $buildTime = round($dataarray[$currentLevel - 1]['time'] / 4); - if ( !$$name ) { - return ["wood" => 0, "clay" => 0, "iron" => 0, "crop" => 0, "pop" => 0, "time" => 0, "cp" => 0]; + $time = time() + $buildTime; + + $loop = 0; + + // logică identică, branch-uri reduse + if ( + ($this->inner == 1 || $this->basic == 1) && + (($session->plus || $fieldType == 40) && $this->plus == 0) + ) { + $loop = 1; + } + + if ($loop == 1) { + + $romanQueue = ($session->tribe == 1 || ALLOW_ALL_TRIBE); + + if ($romanQueue) { + + if ($id >= 19) { + + foreach ($this->buildArray as $build) { + + if ($build['field'] >= 19) { + $time = $build['timestamp'] + $buildTime; + } + } + } + } else { + + if (isset($this->buildArray[0]['timestamp'])) { + $time = $this->buildArray[0]['timestamp'] + $buildTime; + } + } + } + + // un singur query + $level = $database->getResourceLevel($village->wid); + + // un singur query + $queuedBuildings = $database->getBuildingByField( + $village->wid, + $id + ); + + if ( + $database->addBuilding( + $village->wid, + $id, + $fieldType, + $loop, + $time, + 0, + 0, + $level['f'.$id] + 1 + count($queuedBuildings) + ) + ) { + + $logging->addBuildLog( + $village->wid, + self::procResType($fieldType), + ($currentLevel - 1), + 2 + ); + + header('Location: dorf2.php'); + exit(); + } + + } finally { + + $database->releaseBuildLock($village->wid); + } +} + + /***************************************** + Function to construct building + *****************************************/ + + private function constructBuilding($id, $tid) { + global $database, $village, $session, $logging; + + if (!$database->getBuildLock($village->wid)) { + return; + } + + try { + + $this->loadBuilding(); + + if ($this->allocated >= $this->maxConcurrent) { + + header('Location: dorf2.php'); + exit; + } + + // păstrăm exact logica originală + $buildField = $id; + if ($tid == 16) { + $buildField = 39; + } + elseif ($tid == 31 || $tid == 32 || $tid == 33) { + $buildField = 40; } - $wood = $dataarray[$village->resarray['f'.$id] + $plus]['wood']; - $clay = $dataarray[$village->resarray['f'.$id] + $plus]['clay']; - $iron = $dataarray[$village->resarray['f'.$id] + $plus]['iron']; - $crop = $dataarray[$village->resarray['f'.$id] + $plus]['crop']; - $pop = $dataarray[$village->resarray['f'.$id] + $plus]['pop']; - $time = $database->getBuildingTime($id, $tid, $plus, $village->wid, $village->resarray); - $cp = $dataarray[$village->resarray['f'.$id] + $plus]['cp']; - - return ["wood" => $wood, "clay" => $clay, "iron" => $iron, "crop" => $crop, "pop" => $pop, "time" => $time, "cp" => $cp]; - } + // un singur apel + $uprequire = $this->resourceRequired($buildField, $tid); + + $time = time() + $uprequire['time']; + + // un singur apel + $bindicate = $this->canBuild($buildField, $tid); + + $loop = ($bindicate == 9 ? 1 : 0); + + if ($loop == 1) { + + $romanQueue = ($session->tribe == 1 || ALLOW_ALL_TRIBE); + + foreach ($this->buildArray as $build) { + + if ( + $build['field'] >= 19 || + !$romanQueue + ) { + + $time = + $build['timestamp'] + + ceil(60 / SPEED) + + $uprequire['time']; + } + } + } + + // early exit + if (!$this->meetRequirement($tid)) { + + header('Location: dorf2.php'); + exit; + } + + // un singur query + $level = $database->getResourceLevel($village->wid); + + // un singur query + $queuedBuildings = $database->getBuildingByField( + $village->wid, + $buildField + ); + + $nextLevel = + $level['f'.$buildField] + + 1 + + count($queuedBuildings); + + if ( + $database->addBuilding( + $village->wid, + $buildField, + $tid, + $loop, + $time, + 0, + $nextLevel + ) + ) { + + $logging->addBuildLog( + $village->wid, + self::procResType($tid), + ($village->resarray['f'.$buildField] + 1), + 1 + ); + + $database->modifyResource( + $village->wid, + $uprequire['wood'], + $uprequire['clay'], + $uprequire['iron'], + $uprequire['crop'], + 0 + ); + + header('Location: dorf2.php'); + exit; + } + + } finally { + + $database->releaseBuildLock($village->wid); + } +} + + /***************************************** + Function to Pallace Build ? + *****************************************/ + public function isCastleBuilt() { + + global $database, $session; + + // un singur query + $villages = $database->getProfileVillages($session->uid); + + if (empty($villages)) { + return false; + } + + // cache local pentru level queries + static $villageLevelCache = array(); + + foreach ($villages as $vil) { + + $wid = $vil['wref']; + + if (!isset($villageLevelCache[$wid])) { + $villageLevelCache[$wid] = $database->getResourceLevel($wid); + } + + if (in_array(26, $villageLevelCache[$wid])) { + return true; + } + } + + return false; +} + + /***************************************** + Function to meet requirement + *****************************************/ + + private function meetRequirement($id) { + + global $village, $session, $database; + + // cache local enorm de important + static $typeLevelCache = array(); + static $typeFieldCache = array(); + + $getLevel = function($type) use (&$typeLevelCache) { + + if (!isset($typeLevelCache[$type])) { + $typeLevelCache[$type] = $this->getTypeLevel($type); + } + + return $typeLevelCache[$type]; + }; + + $getField = function($type) use (&$typeFieldCache) { + + if (!isset($typeFieldCache[$type])) { + $typeFieldCache[$type] = $this->getTypeField($type); + } + + return $typeFieldCache[$type]; + }; + + $isBuilt = $getField($id); + + switch ($id) { + + case 1: + case 2: + case 3: + case 4: + return true; + + case 5: + return $getLevel(1) >= 10 && + $getLevel(15) >= 5 && + !$isBuilt; + + case 6: + return $getLevel(2) >= 10 && + $getLevel(15) >= 5 && + !$isBuilt; + + case 7: + return $getLevel(3) >= 10 && + $getLevel(15) >= 5 && + !$isBuilt; + + case 8: + return $getLevel(4) >= 5 && + !$isBuilt; + + case 9: + return $getLevel(15) >= 5 && + $getLevel(4) >= 10 && + $getLevel(8) >= 5 && + !$isBuilt; + + case 10: + case 11: + return $getLevel(15) >= 1 && + (!$isBuilt || $getLevel($id) == 20); + + case 12: + return $getLevel(22) >= 3 && + $getLevel(15) >= 3 && + !$isBuilt; + + case 13: + return $getLevel(15) >= 3 && + $getLevel(22) >= 1 && + !$isBuilt; + + case 14: + return $getLevel(16) >= 15 && + !$isBuilt; + + case 15: + case 16: + return !$isBuilt; + + case 17: + return $getLevel(15) >= 3 && + $getLevel(10) >= 1 && + $getLevel(11) >= 1 && + !$isBuilt; + + case 18: + return $getLevel(15) >= 1 && + !$isBuilt; + + case 19: + return $getLevel(15) >= 3 && + $getLevel(16) >= 1 && + !$isBuilt; + + case 20: + return $getLevel(12) >= 3 && + $getLevel(22) >= 5 && + !$isBuilt; + + case 21: + return $getLevel(22) >= 10 && + $getLevel(15) >= 5 && + !$isBuilt; + + case 22: + return $getLevel(15) >= 3 && + $getLevel(19) >= 3 && + !$isBuilt; + + case 23: + return !$isBuilt || $getLevel($id) == 10; + + case 24: + return $getLevel(22) >= 10 && + $getLevel(15) >= 10 && + !$isBuilt; + + case 25: + return $getLevel(15) >= 5 && + !$isBuilt && + !$getField(26); + + case 26: + return $getLevel(18) >= 1 && + $getLevel(15) >= 5 && + !$isBuilt && + !$this->isCastleBuilt() && + !$getField(25); + + case 27: + return $getLevel(15) >= 10 && + !$isBuilt; + + case 28: + return $getLevel(17) == 20 && + $getLevel(20) >= 10 && + !$isBuilt; + + case 29: + return $getLevel(19) == 20 && + $village->capital == 0 && + !$isBuilt; + + case 30: + return $getLevel(20) == 20 && + $village->capital == 0 && + !$isBuilt; + + case 31: + return $session->tribe == 1; + + case 32: + return $session->tribe == 2; + + case 33: + return $session->tribe == 3; + + case 34: + return $getLevel(26) >= 3 && + $getLevel(15) >= 5 && + $getLevel(25) == 0 && + $village->capital != 0 && + !$isBuilt; + + case 35: + return $getLevel(16) >= 10 && + $getLevel(11) == 20 && + $session->tribe == 2 && + $village->capital != 0 && + !$isBuilt; + + case 36: + return $getLevel(16) >= 1 && + $session->tribe == 3 && + (!$isBuilt || $getLevel($id) == 20); + + case 37: + return $getLevel(15) >= 3 && + $getLevel(16) >= 1 && + !$isBuilt; + + // Great Warehouse + case 38: + + static $greatWarehouseArtefact = null; + + if ($greatWarehouseArtefact === null) { + + $greatWarehouseArtefact = + ( + count($database->getOwnUniqueArtefactInfo2($village->wid, 6, 1, 1)) || + count($database->getOwnUniqueArtefactInfo2($session->uid, 6, 2, 0)) + ); + } + + return $getLevel(15) >= 10 && + (!$isBuilt || $getLevel($id) == 20) && + ($village->natar == 1 || $greatWarehouseArtefact); + + // Great Granary + case 39: + + static $greatGranaryArtefact = null; + + if ($greatGranaryArtefact === null) { + + $greatGranaryArtefact = + ( + count($database->getOwnUniqueArtefactInfo2($village->wid, 6, 1, 1)) || + count($database->getOwnUniqueArtefactInfo2($session->uid, 6, 2, 0)) + ); + } + + return $getLevel(15) >= 10 && + (!$isBuilt || $getLevel($id) == 20) && + ($village->natar == 1 || $greatGranaryArtefact); + + case 40: + return $this->allowWwUpgrade(); + + case 41: + return $getLevel(16) >= 10 && + $getLevel(20) == 20 && + $session->tribe == 1 && + !$isBuilt; + + case 42: + return GREAT_WKS && + $getLevel(21) == 20 && + $village->capital == 0 && + !$isBuilt; + + default: + return false; + } +} + + /***************************************** + Function to check resources available + *****************************************/ + + private function checkResource($tid, $id) { + + global $village, $database; + + $name = 'bid'.$tid; + + global $$name; + + $dataarray = $$name; + + // evităm foreach inutil lung + $plus = 1; + + if (!empty($this->buildArray)) { + + foreach ($this->buildArray as $job) { + + if ( + $job['type'] == $tid && + $job['field'] == $id + ) { + $plus = 2; + break; + } + } + } + + // cache local + $nextLevel = $village->resarray['f'.$id] + $plus; + + $required = $dataarray[$nextLevel]; + + $wood = $required['wood']; + $clay = $required['clay']; + $iron = $required['iron']; + $crop = $required['crop']; + + // store limit + if ( + $wood > $village->maxstore || + $clay > $village->maxstore || + $iron > $village->maxstore + ) { + return 1; + } + + // granary limit + if ($crop > $village->maxcrop) { + return 2; + } + + // not enough resources + if ( + $wood > $village->awood || + $clay > $village->aclay || + $iron > $village->airon || + $crop > $village->acrop + ) { + return 3; + } + + return 4; +} + + /***************************************** + Function to building is max level ? + *****************************************/ + + public function isMax($id, $field, $loop = 0) { + + global $village, $session; + + $name = 'bid'.$id; + + global $$name; + + $dataarray = $$name; + + $currentLevel = $village->resarray['f'.$field]; + + // special MH case + if ($session->tribe == 0) { + return ($currentLevel == 20); + } + + // resource fields + if ($id <= 4) { + + $maxLevel = ( + $village->capital == 1 + ? (count($dataarray) - 1 - $loop) + : (count($dataarray) - 11 - $loop) + ); + + return ($currentLevel == $maxLevel); + } + + // normal buildings + return ($currentLevel == (count($dataarray) - $loop)); +} + + /***************************************** + Function to get type level + *****************************************/ + + public function getTypeLevel($tid, $vid = 0) { + + global $village, $database, $session; + + // support account + if ($session->uid == 1) { + return 0; + } + + // cache masiv de important + static $resourceCache = array(); + static $typeLevelCache = array(); + + $cacheKey = $tid.'_'.$vid; + + if (isset($typeLevelCache[$cacheKey])) { + return $typeLevelCache[$cacheKey]; + } + + // evităm query-uri repetate + if ($vid == 0) { + + $resourcearray = $village->resarray; + + } else { + + if (!isset($resourceCache[$vid])) { + $resourceCache[$vid] = $database->getResourceLevel($vid); + } + + $resourcearray = $resourceCache[$vid]; + } + + $keyholder = array(); + + // mai rapid decât array_keys + strpos + foreach ($resourcearray as $key => $value) { + + if ( + $value == $tid && + strpos($key, 't') !== false + ) { + + $keyholder[] = (int)preg_replace('/[^0-9]/', '', $key); + } + } + + $element = count($keyholder); + + // no building + if ($element == 0) { + $typeLevelCache[$cacheKey] = 0; + return 0; + } + + // single building + if ($element == 1) { + + $result = $resourcearray['f'.$keyholder[0]]; + + $typeLevelCache[$cacheKey] = $result; + + return $result; + } + + // multiple buildings + $target = 0; + + // resource fields + if ($tid <= 4) { + + $maxLevel = -1; + + foreach ($keyholder as $index => $fieldId) { + + $level = $resourcearray['f'.$fieldId]; + + if ($level > $maxLevel) { + $maxLevel = $level; + $target = $index; + } + } + + } else { + + // village buildings + foreach ($keyholder as $index => $fieldId) { + + if ( + $resourcearray['f'.$fieldId] > + $resourcearray['f'.$keyholder[$target]] + ) { + $target = $index; + } + } + } + + $result = isset($keyholder[$target]) + ? $resourcearray['f'.$keyholder[$target]] + : 0; + + $typeLevelCache[$cacheKey] = $result; + + return $result; +} + + /***************************************** + Function to is current ? + *****************************************/ + + public function isCurrent($id) { + + if (empty($this->buildArray)) { + return false; + } + + foreach ($this->buildArray as $build) { + + if ( + $build['field'] == $id && + $build['loopcon'] != 1 + ) { + return true; + } + } + + return false; +} + + /***************************************** + Function to is loop ? + *****************************************/ + + public function isLoop($id = 0) { + + if (empty($this->buildArray)) { + return false; + } + + foreach ($this->buildArray as $build) { + + if ( + ($build['field'] == $id && $build['loopcon']) || + ($build['loopcon'] == 1 && $id == 0) + ) { + return true; + } + } + + return false; +} + + /***************************************** + Function to finish all with gold + *****************************************/ + + public function finishAll($redirect_url = '') { + + global $database, $session, $logging, $village; + global $bid18, $bid10, $bid11, $technology, $_SESSION; + + $countPlus2Gold = false; + $countMasterGold = false; + + $buildcount = (!empty($this->buildArray) + ? count($this->buildArray) + : 0); + + $deletIDs = array(); + + // cache query-uri + static $villageResourceCache = array(); + static $villageFieldCache = array(); + + foreach ($this->buildArray as $jobs) { + + if ($jobs['wid'] != $village->wid) { + continue; + } + + $wid = $jobs['wid']; + + // cache getResourceLevel + if (!isset($villageResourceCache[$wid])) { + $villageResourceCache[$wid] = $database->getResourceLevel($wid); + } + + $wwvillage = $villageResourceCache[$wid]; + + // skip WW + if ($wwvillage['f99t'] == 40) { + continue; + } + + $level = $jobs['level']; + + // skip excluded + if ( + $jobs['type'] == 25 || + $jobs['type'] == 26 || + $jobs['type'] == 40 + ) { + continue; + } + + $resource = $this->resourceRequired( + $jobs['field'], + $jobs['type'] + ); + + $jobFinishSuccess = false; + $enought_res = 0; + $exclude_master = false; + + // MASTER BUILDER + if ($jobs['master'] != 0) { + + if ($this->meetRequirement($jobs['field'])) { + + // fix gold edge case + if ($session->gold > 2) { + + // cache village fields + if (!isset($villageFieldCache[$wid])) { + + $villageFieldCache[$wid] = array( + 'wood' => $database->getVillageField($wid, 'wood'), + 'clay' => $database->getVillageField($wid, 'clay'), + 'iron' => $database->getVillageField($wid, 'iron'), + 'crop' => $database->getVillageField($wid, 'crop') + ); + } + + $villwood = $villageFieldCache[$wid]['wood']; + $villclay = $villageFieldCache[$wid]['clay']; + $villiron = $villageFieldCache[$wid]['iron']; + $villcrop = $villageFieldCache[$wid]['crop']; + + $type = $jobs['type']; + + $buildarray = $GLOBALS['bid'.$type]; + + $buildwood = $buildarray[$level]['wood']; + $buildclay = $buildarray[$level]['clay']; + $buildiron = $buildarray[$level]['iron']; + $buildcrop = $buildarray[$level]['crop']; + + if ( + $buildwood < $villwood && + $buildclay < $villclay && + $buildiron < $villiron && + $buildcrop < $villcrop + ) { + + $jobFinishSuccess = true; + $countMasterGold = true; + $enought_res = 1; + + // scădem resurse doar dacă e nevoie + if ($buildcount > 2) { + + $database->setVillageField( + $wid, + array('wood', 'clay', 'iron', 'crop'), + array( + ($villwood - $buildwood), + ($villclay - $buildclay), + ($villiron - $buildiron), + ($villcrop - $buildcrop) + ) + ); + } + } + + } else { + + $exclude_master = true; + $deletIDs[] = (int)$jobs['id']; + } + } + + } else { + + // normal build + $countPlus2Gold = true; + $jobFinishSuccess = true; + } + + // update building + if ($jobFinishSuccess) { + + $q = " + UPDATE ".TB_PREFIX."fdata + SET + f".$jobs['field']." = ".$jobs['level'].", + f".$jobs['field']."t = ".$jobs['type']." + WHERE vref = ".$wid; + + if ( + !$exclude_master && + $database->query($q) && + ($enought_res == 1 || $jobs['master'] == 0) + ) { + + $database->modifyPop( + $wid, + $resource['pop'], + 0 + ); + + $deletIDs[] = (int)$jobs['id']; + + // embassy update + if ($jobs['type'] == 18) { + + $owner = $database->getVillageField( + $wid, + 'owner' + ); + + $max = $bid18[$level]['attri']; + + $database->query("UPDATE ".TB_PREFIX."alidata SET max=".$max." WHERE leader=".$owner); + } + } + } + + // roman queue + if ( + ($jobs['field'] >= 19 && + ($session->tribe == 1 || ALLOW_ALL_TRIBE)) || + (!ALLOW_ALL_TRIBE && $session->tribe != 1) + ) { + $innertimestamp = $jobs['timestamp']; + } + } + + // single delete query + if (!empty($deletIDs)) { + + $database->query(" + DELETE FROM ".TB_PREFIX."bdata + WHERE id IN(".implode(',', $deletIDs).") + "); + } + + $demolition = $database->finishDemolition($village->wid); + $tech = $technology->finishTech(); + + if ($demolition > 0 || $tech > 0) { + + $countPlus2Gold = true; + $logging->goldFinLog($village->wid); + } + + // gold update + if ($countMasterGold || $countPlus2Gold) { + + $newgold = $session->gold - + (($countMasterGold && $countPlus2Gold) ? 3 : 2); + + $database->updateUserField( + $session->uid, + 'gold', + $newgold, + 1 + ); + } + + // un singur query + $stillbuildingarray = $database->getJobs($village->wid); + + if ( + count($stillbuildingarray) == 1 && + $stillbuildingarray[0]['loopcon'] == 1 + ) { + + $database->query(" + UPDATE ".TB_PREFIX."bdata + SET loopcon = 0 + WHERE id = ".(int)$stillbuildingarray[0]['id'] + ); + } + + self::recountCP($database, $village->wid); + + header('Location: '.( + $redirect_url + ? $redirect_url + : $session->referrer + )); + + exit; +} + + /***************************************** + Function to recount culture point + *****************************************/ + + public static function recountCP($database, $vid) { + + $vid = (int)$vid; + + // un singur query + $fdata = $database->getResourceLevel($vid); + + $cpTot = 0; + + // micro-optimizare + for ($i = 1; $i <= 40; $i++) { + + $building = (int)$fdata['f'.$i.'t']; + + // skip rapid + if ($building <= 0) { + continue; + } + + $lvl = (int)$fdata['f'.$i]; + + $cpTot += self::buildingCP($building, $lvl); + } + + // evităm string concat multiplu + mysqli_query( + $database->dblink, + " + UPDATE ".TB_PREFIX."vdata + SET cp = ".$cpTot." + WHERE wref = ".$vid + ); +} + + /***************************************** + Function to building culture point + *****************************************/ + + public static function buildingCP($f, $lvl) { + + // cache static enorm de util + static $cpCache = array(); + + $cacheKey = $f.'_'.$lvl; + + if (isset($cpCache[$cacheKey])) { + return $cpCache[$cacheKey]; + } + + $name = 'bid'.$f; + + global $$name; + + $dataarray = $$name; + + $cp = ( + isset($dataarray[$lvl]['cp']) + ? $dataarray[$lvl]['cp'] + : 0 + ); + + $cpCache[$cacheKey] = $cp; + + return $cp; +} + + /***************************************** + Function to ressource required + *****************************************/ + + public function resourceRequired($id, $tid, $plus = 1) { + + global $village, $bid15, $database; + + $name = 'bid'.$tid; + + global $$name; + + // cache static foarte important + static $resourceCache = array(); + + $cacheKey = + $id.'_'. + $tid.'_'. + $plus.'_'. + $village->wid; + + if (isset($resourceCache[$cacheKey])) { + return $resourceCache[$cacheKey]; + } + + $dataarray = $$name; + + // safety + if (!$dataarray) { + + $empty = array( + 'wood' => 0, + 'clay' => 0, + 'iron' => 0, + 'crop' => 0, + 'pop' => 0, + 'time' => 0, + 'cp' => 0 + ); + + $resourceCache[$cacheKey] = $empty; + + return $empty; + } + + $level = $village->resarray['f'.$id] + $plus; + + // un singur lookup + $required = $dataarray[$level]; + + $result = array( + 'wood' => $required['wood'], + 'clay' => $required['clay'], + 'iron' => $required['iron'], + 'crop' => $required['crop'], + 'pop' => $required['pop'], + 'time' => $database->getBuildingTime( + $id, + $tid, + $plus, + $village->wid, + $village->resarray + ), + 'cp' => $required['cp'] + ); + + $resourceCache[$cacheKey] = $result; + + return $result; +} + + /***************************************** + Function to get type level + *****************************************/ public function getTypeField($type) { - global $village; - - for($i = 19; $i <= 40; $i++) { - if($village->resarray['f'.$i.'t'] == $type) return $i; - } - } + + global $village; + + // cache static + static $typeFieldCache = array(); + + if (isset($typeFieldCache[$type])) { + return $typeFieldCache[$type]; + } + + for ($i = 19; $i <= 40; $i++) { + + if ($village->resarray['f'.$i.'t'] == $type) { + + $typeFieldCache[$type] = $i; + + return $i; + } + } + + $typeFieldCache[$type] = false; + + return false; +} + + /***************************************** + Function to calculate avaliable + *****************************************/ public function calculateAvaliable($id, $tid, $plus = 1) { - global $village, $generator; - - $uprequire = $this->resourceRequired($id, $tid, $plus); - $rwood = $uprequire['wood'] - $village->awood; - $rclay = $uprequire['clay'] - $village->aclay; - $rcrop = $uprequire['crop'] - $village->acrop; - $riron = $uprequire['iron'] - $village->airon; - $woodProd = $village->getProd("wood"); - $clayProd = $village->getProd("clay"); - $cropProd = $village->getProd("crop"); - $ironProd = $village->getProd("iron"); - $rwtime = ($rwood <= 0) ? 0 : ($woodProd > 0 ? $rwood / $woodProd * 3600 : 0); - $rcltime = ($rclay <= 0) ? 0 : ($clayProd > 0 ? $rclay / $clayProd * 3600 : 0); - $rctime = ($rcrop <= 0) ? 0 : ($cropProd > 0 ? $rcrop / $cropProd * 3600 : 0); - $ritime = ($riron <= 0) ? 0 : ($ironProd > 0 ? $riron / $ironProd * 3600 : 0); - $reqtime = max($rwtime, $rctime, $rcltime, $ritime); - $reqtime += time(); - return $generator->procMtime($reqtime); - } + + global $village, $generator; + + // un singur apel + $uprequire = $this->resourceRequired($id, $tid, $plus); + + // cache producții + static $prodCache = array(); + + if (empty($prodCache)) { + + $prodCache = array( + 'wood' => $village->getProd('wood'), + 'clay' => $village->getProd('clay'), + 'crop' => $village->getProd('crop'), + 'iron' => $village->getProd('iron') + ); + } + + $rwood = $uprequire['wood'] - $village->awood; + $rclay = $uprequire['clay'] - $village->aclay; + $rcrop = $uprequire['crop'] - $village->acrop; + $riron = $uprequire['iron'] - $village->airon; + + // formule identice, branch-uri reduse + $rwtime = ( + $rwood > 0 && $prodCache['wood'] > 0 + ? ($rwood / $prodCache['wood']) * 3600 + : 0 + ); + + $rcltime = ( + $rclay > 0 && $prodCache['clay'] > 0 + ? ($rclay / $prodCache['clay']) * 3600 + : 0 + ); + + $rctime = ( + $rcrop > 0 && $prodCache['crop'] > 0 + ? ($rcrop / $prodCache['crop']) * 3600 + : 0 + ); + + $ritime = ( + $riron > 0 && $prodCache['iron'] > 0 + ? ($riron / $prodCache['iron']) * 3600 + : 0 + ); + + $reqtime = max( + $rwtime, + $rctime, + $rcltime, + $ritime + ) + time(); + + return $generator->procMtime($reqtime); +} }; ?> diff --git a/GameEngine/Lang/en.php b/GameEngine/Lang/en.php index acd85225..776fa131 100755 --- a/GameEngine/Lang/en.php +++ b/GameEngine/Lang/en.php @@ -74,12 +74,12 @@ define('ATAG_EMPTY', 'Tag empty'); define('ANAME_EMPTY', 'Name empty'); define('ATAG_EXIST', 'Tag taken'); define('ANAME_EXIST', 'Name taken'); -define('ALREADY_ALLY_MEMBER', 'You're already in an alliance'); +define('ALREADY_ALLY_MEMBER', 'You`re already in an alliance'); define('ALLY_TOO_LOW', 'You must have a level 3 or greater embassy'); define('USER_NOT_IN_YOUR_ALLY', 'This user is not in your alliance!'); -define('CANT_EDIT_YOUR_PERMISSIONS', 'You can't edit your own permissions!'); -define('CANT_EDIT_LEADER_PERMISSIONS', 'Alliance leader's permissions can't be edited!'); -define('NO_PERMISSION', 'You don't have enough permissions!'); +define('CANT_EDIT_YOUR_PERMISSIONS', 'You can`t edit your own permissions!'); +define('CANT_EDIT_LEADER_PERMISSIONS', 'Alliance leader`s permissions can`t be edited!'); +define('NO_PERMISSION', 'You don`t have enough permissions!'); define('NAME_OR_DIPL_EMPTY', 'Name or diplomacy empty'); define('ALLY_DOESNT_EXISTS', 'Alliance does not exist'); define('CANNOT_INVITE_SAME_ALLY', 'You cannot invite your own alliance'); @@ -95,16 +95,16 @@ define('ALLY_FORUM_LINK_UPDATED', 'Forum link updated'); define('NO_FORUMS_YET', 'There are no forums yet.'); define('ALLY_USER_KICKED', ' has been kicked from the alliance'); define('NOT_OPENED_YET', 'Server not started yet.'); -define('REGISTER_CLOSED', 'The register is closed. You can't register to this server.'); +define('REGISTER_CLOSED', 'The register is closed. You can`t register to this server.'); define('NAME_EMPTY', 'Please insert name'); define('NAME_NO_EXIST', 'There is no user with the name '); define('ID_NO_EXIST', 'There is no user with the id '); -define('SAME_NAME', 'You can't invite yourself'); +define('SAME_NAME', 'You can`t invite yourself'); define('ALREADY_INVITED', ' already invited'); define('ALREADY_IN_ALLY', ' is already in this alliance'); define('ALREADY_IN_AN_ALLY', ' is already in an alliance'); define('NAME_OR_TAG_CHANGED', 'Name or Tag changed'); -define('VAC_MODE_WRONG_DAYS', 'You've inserted a wrong number of days'); +define('VAC_MODE_WRONG_DAYS', 'You`ve inserted a wrong number of days'); //COPYRIGHT define('TRAVIAN_COPYRIGHT', 'TravianZ 100% Open Source Travian Clone.'); @@ -130,7 +130,7 @@ define('GO2MY_VILLAGE', 'Go to my village'); define('VILLAGE_CENTER', 'Village centre'); define('FINISH_GOLD', 'Finish all construction and research orders in this village immediately for 2 Gold?'); define('WAITING_LOOP', '(waiting loop)'); -define('CROP_NEGATIVE', 'Your crop production is negative, you'll neaver reach the amount of requested resources.'); +define('CROP_NEGATIVE', 'Your crop production is negative, you`ll neaver reach the amount of requested resources.'); define('HR', 'h.'); define('HRS', '(hrs.)'); define('DONE_AT', 'done at'); @@ -181,7 +181,7 @@ define('ONLINE_S4', 'Last 7 days'); define('ONLINE_S5', 'Inactive'); define('WAIT_FOR_CONFIRM', 'Wait for confirm'); define('CONFIRM', 'Confirm'); -define('WRITE_MESS_WARN', 'Warning: you can't use the values [message] or [/message] in your message because it can cause problem with bbcode system'); +define('WRITE_MESS_WARN', 'Warning: you can`t use the values [message] or [/message] in your message because it can cause problem with bbcode system'); define('NO_REPORTS', 'There are no reports available'); define('ATTACKER', 'Attacker'); define('NATAR_COUNTERFORCE', 'Natar Counterforce'); @@ -191,7 +191,7 @@ define('INFORMATION', 'Information'); define('CARRY', 'carry'); define('DEFENDER', 'Defender'); define('VISITED', 'visited'); -define('HIS_TROOPS', ''s troops'); +define('HIS_TROOPS', '`s troops'); define('WISHES_YOU', 'wishes you'); define('X_MAS', 'Merry Christmas'); define('NEW_YEAR', 'Happy New Year'); @@ -218,21 +218,21 @@ define('Q1_RESP', 'Yes, that way you gain more lumber.I helped a bit and complet define('Q1_REWARD', 'Woodcutter instantly completed.'); define('Q2', 'Task 2: Crop'); -define('Q2_DESC', 'Now your subjects are hungry from working all day. Extend a cropland to improve your subjects' supply. Come back here once the building is complete.'); +define('Q2_DESC', 'Now your subjects are hungry from working all day. Extend a cropland to improve your subjects supply. Come back here once the building is complete.'); define('Q2_ORDER', 'Order:
Extend one cropland.'); define('Q2_RESP', 'Very good. Now your subjects have enough to eat again...'); define('Q2_REWARD', 'Your reward:1 day Travian'); -define('Q3', 'Task 3: Your Village's Name'); -define('Q3_DESC', 'Creative as you are you can grant your village the ultimate name.If you no longer need a building, you can order the demolition of the building.
'); define('DEMOLISH', 'Demolish'); define('DEMOLITION_OF', 'Demolition of '); -define('MAINBUILDING_DESC', 'The village's master builders live in the main building. The higher its level the faster your master builders complete the construction of new buildings.'); +define('MAINBUILDING_DESC', 'The village`s master builders live in the main building. The higher its level the faster your master builders complete the construction of new buildings.'); define('RALLYPOINT', 'Rally Point'); define('RALLYPOINT_COMMENCE', 'Troops movement will be displayed when the '.RALLYPOINT.' is completed'); @@ -892,7 +892,7 @@ define('EVASION_SETTINGS', 'evasion settings'); define('SEND_TROOPS_AWAY_MAX', 'Send troops away a maximum of'); define('TIMES', 'times'); define('PER_EVASION', 'per evasion'); -define('RALLYPOINT_DESC', 'Your village's troops gather here. From here, you can send them out to conquer, raid or reinforce other villages.It's free to play and requires no downloads.'; +$lang['index'][0][5] = SERVER_NAME.' is a browser game featuring an engaging ancient world with thousands of other real players.
It`s free to play and requires no downloads.'; $lang['index'][0][6] = 'Click here to play '.SERVER_NAME; $lang['index'][0][7] = 'Total players'; $lang['index'][0][8] = 'Players active'; diff --git a/GameEngine/Ranking.php b/GameEngine/Ranking.php index 27b8a157..9f81f164 100755 --- a/GameEngine/Ranking.php +++ b/GameEngine/Ranking.php @@ -1,24 +1,61 @@ rankarray; } + + /***************************************** + Function to finalize rank array + *****************************************/ + + private function finalizeRankArray(array $holder): void { + $this->rankarray = array_merge(["pad"], $holder); + } + + /***************************************** + Function to start by rank + *****************************************/ + + private function setStartByRank($value, $field, $fallback = 1): void { + $rank = $this->searchRank($value, $field); + if($rank != 0) { + $this->getStart($rank); + } else { + $this->getStart($fallback); + } +} + + /***************************************** + Function to get user rank + *****************************************/ public function getUserRank($id) { $ranking = $this->getRank(); @@ -31,90 +68,88 @@ } return 0; } + + /***************************************** + Function to Process Ranking Request + *****************************************/ - public function procRankReq($get) { - global $village, $session; - if(isset($get['id'])) { - switch($get['id']) { - case 1: - $this->procRankArray(); - break; - case 8: - $this->procHeroRankArray(); - if($get['hero'] == 0) { - $this->getStart(1); - } else { - $this->getStart($this->searchRank($session->uid, "uid")); - } - break; - case 11: - $this->procRankRaceArray(1); - if($this->searchRank($session->uid, "userid") != 0){ - $this->getStart($this->searchRank($session->uid, "userid")); - }else{ - $this->getStart(1); - } - break; - case 12: - $this->procRankRaceArray(2); - if($this->searchRank($session->uid, "userid") != 0){ - $this->getStart($this->searchRank($session->uid, "userid")); - }else{ - $this->getStart(1); - } - break; - case 13: - $this->procRankRaceArray(3); - if($this->searchRank($session->uid, "userid") != 0){ - $this->getStart($this->searchRank($session->uid, "userid")); - }else{ - $this->getStart(1); - } - break; - case 31: - $this->procAttRankArray(); - $this->getStart($this->searchRank($session->uid, "userid")); - break; - case 32: - $this->procDefRankArray(); - $this->getStart($this->searchRank($session->uid, "userid")); - break; - case 2: - $this->procVRankArray(); - $this->getStart($this->searchRank($village->wid, "wref")); - break; - case 4: - $this->procARankArray(); - if($get['aid'] == 0) { - $this->getStart(1); - } else { - $this->getStart($this->searchRank($get['aid'], "id")); - } - break; - case 41: - $this->procAAttRankArray(); - if($get['aid'] == 0) { - $this->getStart(1); - } else { - $this->getStart($this->searchRank($get['aid'], "id")); - } - break; - case 42: - $this->procADefRankArray(); - if($get['aid'] == 0) { - $this->getStart(1); - } else { - $this->getStart($this->searchRank($get['aid'], "id")); - } - break; - } - } else { - $this->procRankArray(); - $this->getStart($this->searchRank($session->uid, "userid")); - } - } + public function procRankReq($get) { + global $village, $session; + if(isset($get['id'])) { + switch($get['id']) { + case 1: + $this->procRankArray(); + break; + case 8: + $this->procHeroRankArray(); + if($get['hero'] == 0) { + $this->getStart(1); + } else { + $this->setStartByRank($session->uid, "uid"); + } + break; + case 11: + $this->procRankRaceArray(1); + $this->setStartByRank($session->uid, "userid"); + break; + case 12: + $this->procRankRaceArray(2); + $this->setStartByRank($session->uid, "userid"); + break; + case 13: - public function procRank($post) { + $this->procRankRaceArray(3); + $this->setStartByRank($session->uid, "userid"); + break; + case 31: + $this->procAttRankArray(); + $this->setStartByRank($session->uid, "userid"); + break; + case 32: + + $this->procDefRankArray(); + $this->setStartByRank($session->uid, "userid"); + break; + case 2: + $this->procVRankArray(); + $this->setStartByRank($village->wid, "wref"); + break; + case 4: + $this->procARankArray(); + if($get['aid'] == 0) { + $this->getStart(1); + } else { + $this->setStartByRank($get['aid'], "id"); + } + break; + case 41: + $this->procAAttRankArray(); + if($get['aid'] == 0) { + $this->getStart(1); + } else { + $this->setStartByRank($get['aid'], "id"); + } + break; + case 42: + $this->procADefRankArray(); + if($get['aid'] == 0) { + $this->getStart(1); + } else { + $this->setStartByRank($get['aid'], "id"); + } + break; + } + } else { + $this->procRankArray(); + $this->setStartByRank($session->uid, "userid"); + } + } + + /***************************************** + Function to Process Ranking + *****************************************/ + + public function procRank($post) { if(isset($post['ft'])) { switch($post['ft']) { case "r1": @@ -152,8 +187,12 @@ } } } + + /***************************************** + Function to get start point + *****************************************/ - private function getStart($search) { + private function getStart($search) { $multiplier = 1; if(!is_numeric($search)) { $_SESSION['search'] = htmlspecialchars($search); @@ -169,28 +208,32 @@ $_SESSION['start'] = htmlspecialchars($start); } } + + /***************************************** + Function to get alliance rank + *****************************************/ - public function getAllianceRank($id) { - $this->procARankArray(); - while(true) { - if(count($this->rankarray) > 1) { - $key = key($this->rankarray); - if(isset ($this->rankarray[$key]["id"]) && $this->rankarray[$key]["id"] === $id) { - return $key; - break; - } else { - if(!next($this->rankarray)) { - return false; - break; - } - } - } else { - return 1; - } - } - } + public function getAllianceRank($id) { + $this->procARankArray(); + if(count($this->rankarray) <= 1) { + return 1; + } + foreach($this->rankarray as $key => $row) { + if($row === "pad") { + continue; + } + if(isset($row['id']) && $row['id'] == $id) { + return $key; + } + } + return false; + } + + /***************************************** + Function to search in rank + *****************************************/ - public function searchRank($name, $field) { + public function searchRank($name, $field) { $count = count($this->rankarray); for ($key = 1; $key < $count; $key++) { if (!isset($this->rankarray[$key]) || $this->rankarray[$key] === "pad") { @@ -206,8 +249,12 @@ } return 0; } + + /***************************************** + Function to process rank array + *****************************************/ - public function procRankArray() { + public function procRankArray() { global $multisort, $database; if($GLOBALS['db']->countUser() > 0){ @@ -252,13 +299,15 @@ $holder[] = $value; } } - $newholder = ["pad"]; - foreach($holder as $key) array_push($newholder, $key); - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } } + + /***************************************** + Function to process rank race array + *****************************************/ - public function procRankRaceArray($race) { + public function procRankRaceArray($race) { global $multisort, $database; $race = $database->escape((int) $race); $holder = array(); @@ -290,14 +339,14 @@ $value['totalvillage'] = ""; $holder[] = $value; } - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } + + /***************************************** + Function to process attack rank by array + *****************************************/ - public function procAttRankArray() { + public function procAttRankArray() { global $multisort, $database; $holder = array(); $q = "SELECT u.id AS userid, u.username, u.apall, COUNT(CASE WHEN v.type != 99 THEN v.wref END) AS totalvillages, COALESCE(SUM(v.pop),0) AS pop @@ -318,14 +367,14 @@ $value['apall'] = $row['apall']; $holder[] = $value; } - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } + + /***************************************** + Function to process deffence rank by array + *****************************************/ - public function procDefRankArray() { + public function procDefRankArray() { global $database; $holder = array(); $q = "SELECT u.id AS userid, u.username, u.dpall, COUNT(CASE WHEN v.type != 99 THEN v.wref END) AS totalvillages, COALESCE(SUM(v.pop),0) AS pop @@ -346,14 +395,14 @@ $value['dpall'] = $row['dpall']; $holder[] = $value; } - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } + + /***************************************** + Function to process V rank array + *****************************************/ - public function procVRankArray() { + public function procVRankArray() { global $multisort; $array = $GLOBALS['db']->getVRanking(); $holder = array(); @@ -365,14 +414,14 @@ $holder[] = $value; } $holder = $multisort->sorte($holder, "x", true, 2, "y", true, 2, "pop", false, 2); - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } + + /***************************************** + Function to process A rank array + *****************************************/ - public function procARankArray() { + public function procARankArray() { global $multisort, $database; $array = $GLOBALS['db']->getARanking(); $holder = array(); @@ -399,14 +448,14 @@ $holder[] = $value; } $holder = $multisort->sorte($holder, "totalpop", false, 2); - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } + + /***************************************** + Function to process hero array + *****************************************/ - public function procHeroRankArray() { + public function procHeroRankArray() { global $multisort; $array = $GLOBALS['db']->getHeroRanking(); $holder = array(); @@ -418,14 +467,14 @@ $holder[] = $value; } $holder = $multisort->sorte($holder, "experience", false, 2); - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } + + /***************************************** + Function to process ATT array + *****************************************/ - public function procAAttRankArray() { + public function procAAttRankArray() { global $multisort; $array = $GLOBALS['db']->getARanking(); $holder = array(); @@ -445,14 +494,14 @@ $holder[] = $value; } $holder = $multisort->sorte($holder, "Aap", false, 2); - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } + + /***************************************** + Function to process DEFF array + *****************************************/ - public function procADefRankArray() { + public function procADefRankArray() { global $multisort; $array = $GLOBALS['db']->getARanking(); $holder = array(); @@ -472,11 +521,7 @@ $holder[] = $value; } $holder = $multisort->sorte($holder, "Adp", false, 2); - $newholder = array("pad"); - foreach($holder as $key) { - array_push($newholder, $key); - } - $this->rankarray = $newholder; + $this->finalizeRankArray($holder); } } ; diff --git a/Templates/Ajax/plusmap.tpl b/Templates/Ajax/plusmap.tpl deleted file mode 100644 index e757d845..00000000 --- a/Templates/Ajax/plusmap.tpl +++ /dev/null @@ -1,187 +0,0 @@ -getCoor($z); -$y = $currentcoor['y']; -$x = $currentcoor['x']; - -$xm13 = ($x-13) < -WORLD_MAX? $x+WORLD_MAX+WORLD_MAX-12 : $x-13; -$xm6 = ($x-6) < -WORLD_MAX? $x+WORLD_MAX+WORLD_MAX-5 : $x-6; -$xm5 = ($x-5) < -WORLD_MAX? $x+WORLD_MAX+WORLD_MAX-4 : $x-5; -$xm4 = ($x-4) < -WORLD_MAX? $x+WORLD_MAX+WORLD_MAX-3 : $x-4; -$xm3 = ($x-3) < -WORLD_MAX? $x+WORLD_MAX+WORLD_MAX-2 : $x-3; -$xm2 = ($x-2) < -WORLD_MAX? $x+WORLD_MAX+WORLD_MAX-1 : $x-2; -$xm1 = ($x-1) < -WORLD_MAX? $x+WORLD_MAX+WORLD_MAX : $x-1; -$xp1 = ($x+1) > WORLD_MAX? $x-WORLD_MAX-WORLD_MAX : $x+1; -$xp2 = ($x+2) > WORLD_MAX? $x-WORLD_MAX-WORLD_MAX+1 : $x+2; -$xp3 = ($x+3) > WORLD_MAX? $x-WORLD_MAX-WORLD_MAX+2: $x+3; -$xp4 = ($x+4) > WORLD_MAX? $x-WORLD_MAX-WORLD_MAX+3 : $x+4; -$xp5 = ($x+5) > WORLD_MAX? $x-WORLD_MAX-WORLD_MAX+4 : $x+5; -$xp6 = ($x+6) > WORLD_MAX? $x-WORLD_MAX-WORLD_MAX+5: $x+6; -$xp13 = ($x+13) > WORLD_MAX? $x-WORLD_MAX-WORLD_MAX+12: $x+13; -$ym13 = ($y-13) < -WORLD_MAX? $y+WORLD_MAX+WORLD_MAX-12 : $y-13; -$ym6 = ($y-6) < -WORLD_MAX? $y+WORLD_MAX+WORLD_MAX-5 : $y-6; -$ym5 = ($y-5) < -WORLD_MAX? $y+WORLD_MAX+WORLD_MAX-4 : $y-5; -$ym4 = ($y-4) < -WORLD_MAX? $y+WORLD_MAX+WORLD_MAX-3 : $y-4; -$ym3 = ($y-3) < -WORLD_MAX? $y+WORLD_MAX+WORLD_MAX-2 : $y-3; -$ym2 = ($y-2) < -WORLD_MAX? $y+WORLD_MAX+WORLD_MAX-1 : $y-2; -$ym1 = ($y-1) < -WORLD_MAX? $y+WORLD_MAX+WORLD_MAX : $y-1; -$yp1 = ($y+1) > WORLD_MAX? $y-WORLD_MAX-WORLD_MAX : $y+1; -$yp2 = ($y+2) > WORLD_MAX? $y-WORLD_MAX-WORLD_MAX+1 : $y+2; -$yp3 = ($y+3) > WORLD_MAX? $y-WORLD_MAX-WORLD_MAX+2: $y+3; -$yp4 = ($y+4) > WORLD_MAX? $y-WORLD_MAX-WORLD_MAX+3: $y+4; -$yp5 = ($y+5) > WORLD_MAX? $y-WORLD_MAX-WORLD_MAX+4 : $y+5; -$yp6 = ($y+6) > WORLD_MAX? $y-WORLD_MAX-WORLD_MAX+5: $y+6; -$yp13 = ($y+13) > WORLD_MAX? $y-WORLD_MAX-WORLD_MAX+12: $y+13; -$xarray = array($xm6,$xm5,$xm4,$xm3,$xm2,$xm1,$x,$xp1,$xp2,$xp3,$xp4,$xp5,$xp6); -$yarray = array($ym6,$ym5,$ym4,$ym3,$ym2,$ym1,$y,$yp1,$yp2,$yp3,$yp4,$yp5,$yp6); -$maparray = array(); -$xcount = 0; -for($i=0;$i<=12;$i++) { - if($xcount != 13) { - array_push($maparray,$database->getMInfo($generator->getBaseID($xarray[$xcount],$yarray[$i]))); - if($i==12) { - $i = -1; - $xcount +=1; - } - } -} -echo "{\"lm\":\"