From 0011d2abcbf8a9964e05eb3538e428e780d3bd47 Mon Sep 17 00:00:00 2001 From: iopietro Date: Wed, 25 Apr 2018 15:13:24 +0200 Subject: [PATCH] Artifacts fixes part 2 +Artifacts are now activated after a certain amount of time (based on the official Travian Rules and the server speed) and not immediately after the conquer +Artifacts are now activated based on official Travian Rules, you can only have 1 unique/account active artifacts Changes in 27_show.tpl: +Added the text "Can't be activated" to not-activable artifacts Changes in Automation.php: +Added a new method which activate activable artifacts Changes in Database.php: +Added some methods to support the new artifacts activation system --- GameEngine/Admin/function.php | 6 +-- GameEngine/Automation.php | 75 +++++++++++++++++++++++++++++ GameEngine/Database.php | 91 ++++++++++++++++++++++++++++++----- Templates/Build/27_show.tpl | 5 +- 4 files changed, 162 insertions(+), 15 deletions(-) diff --git a/GameEngine/Admin/function.php b/GameEngine/Admin/function.php index 1efe9805..bdc37c78 100755 --- a/GameEngine/Admin/function.php +++ b/GameEngine/Admin/function.php @@ -93,15 +93,15 @@ class funct { header("Location: admin.php?p=player&uid=".$get['uid'].$error); exit; case "reviveHero": - $livingHeroesCount = mysqli_fetch_array($database->query("SELECT Count(*) as Total FROM ".TB_PREFIX."hero WHERE uid=".(int) $get['uid']." AND (dead = 0 OR inrevive = 1)"), MYSQLI_ASSOC); + $livingHeroesCount = mysqli_fetch_array($database->query("SELECT Count(*) as Total FROM ".TB_PREFIX."hero WHERE uid=".(int) $get['uid']." AND (dead = 0 OR inrevive = 1 OR intraining = 1)"), MYSQLI_ASSOC); if ($livingHeroesCount['Total'] > 0) { header("Location: admin.php?p=player&uid=".$get['uid']."&re=1"); exit; } - $result=$database->query("SELECT * FROM ".TB_PREFIX."hero WHERE heroid = ".(int) $get['hid']." AND uid=".(int) $get['uid']); - $hdata=mysqli_fetch_array($result); + $result = $database->query("SELECT * FROM ".TB_PREFIX."hero WHERE heroid = ".(int) $get['hid']." AND uid=".(int) $get['uid']); + $hdata = mysqli_fetch_array($result); $database->query("UPDATE ".TB_PREFIX."units SET hero = 1 WHERE vref = ".(int) $hdata['wref']); $database->query("UPDATE ".TB_PREFIX."hero SET `dead` = '0', `inrevive` = '0', `health` = '100', `lastupdate` = ".time()." WHERE `heroid` = ".(int) $get['hid']." AND `uid` = ".(int) $get['uid']); header("Location: admin.php?p=player&uid=".$get['uid']."&rc=1"); diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 84a84e41..79579dba 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -89,6 +89,9 @@ class Automation { if(!file_exists("GameEngine/Prevention/settlers.txt") or time()-filemtime("GameEngine/Prevention/settlers.txt")>50) { $this->sendSettlersComplete(); } + if(!file_exists("GameEngine/Prevention/artifacts.txt") or time() - filemtime("GameEngine/Prevention/artifacts.txt") > 60) { + $this->activateArtifacts(); + } $this->updateGeneralAttack(); $this->checkInvitedPlayes(); $this->updateStore(); @@ -3700,6 +3703,78 @@ class Automation { unlink("GameEngine/Prevention/settlers.txt"); } } + + private function activateArtifacts() { + global $database, $autoprefix; + + if(file_exists($autoprefix."GameEngine/Prevention/artifacts.txt")) { + unlink($autoprefix."GameEngine/Prevention/artifacts.txt"); + } + + //Check if there's at least one artifact, if not, return + if(!$database->areArtifactsSpawned()) return; + + $ourFileHandle = fopen($autoprefix."GameEngine/Prevention/artifacts.txt", 'w'); + fclose($ourFileHandle); + + //Get all inactive artifacts that have to be activated --> (24 hours / Speed of the server) + $time = time(); + $artifacts = $database->getInactiveArtifacts(round($time - (86400 / (SPEED == 2 ? 1.5 : (SPEED == 3 ? 2 : SPEED))))); + + if(!empty($artifacts)){ + + //Cache inactive artifacts by owner + $inactiveArtifactsCache = []; + foreach($artifacts as $artifact) $inactiveArtifactsCache[$artifact['owner']][] = $artifact; + + foreach($inactiveArtifactsCache as $owner => $inactiveArtifacts){ + + //Initialize the array + $activeArtifacts = []; + + //Get cached active artifacts + $ownArtifacts = $database->getOwnArtifactsSum($owner, true); + + //Activate activable artifacts + foreach($inactiveArtifacts as $artifact){ + if($ownArtifacts['totals'] < 3) { + if($artifact['size'] == 1){ //Village effect + $database->activateArtifact($artifact['id']); + $ownArtifacts['totals']++; + $ownArtifacts['small']++; + }elseif($artifact['size'] == 2 && !$ownArtifacts['great']){ //Account effect + $database->activateArtifact($artifact['id']); + $ownArtifacts['totals']++; + $ownArtifacts['great']++; + }elseif($artifact['size'] == 3 && !$ownArtifacts['unique']){ //Unique effect + $database->activateArtifact($artifact['id']); + $ownArtifacts['totals']++; + $ownArtifacts['unique']++; + } + }elseif($ownArtifacts['small'] == 3 && $artifact['size'] > 1){ + //If we've 3 village effect artifacts activated and at least one account/unique effect not activated + //then we need to deactivate the most recent village effect artifact and activate the oldest account + //or unique effect artifact + + //Deactivate the most recent village effect artifact + $database->activateArtifact($database->getNewestArtifactBySize($owner, 1)['id'], 0); + + //Activate the great/unique artifact + $database->activateArtifact($artifact['id']); + + $ownArtifacts['small']--; + $ownArtifacts['totals']++; + if($size == 2) $ownArtifacts['great']++; + else $ownArtifacts['unique']++; + } + } + } + } + + if(file_exists("GameEngine/Prevention/artifacts.txt")) { + unlink("GameEngine/Prevention/artifacts.txt"); + } + } private function researchComplete() { global $database, $autoprefix; diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 518843d1..18389dfa 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -7018,7 +7018,7 @@ References: User ID/Message ID, Mode return (isset($cachedValue[$type.$size]) ? $cachedValue[$type.$size] : []); } - $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $id"; + $q = "SELECT * FROM " . TB_PREFIX . "artefacts WHERE owner = $id "; $result = $this->mysqli_fetch_all(mysqli_query($this->dblink,$q)); // cache all types and return the requested one @@ -7139,7 +7139,7 @@ References: User ID/Message ID, Mode list($vref, $ovref, $id) = $this->escape_input((int) $vref, (int) $ovref, (int) $id); $time = time(); - $q = "UPDATE " . TB_PREFIX . "artefacts SET vref = $vref, owner = $id, conquered = $time, active = 1 WHERE vref = $ovref"; + $q = "UPDATE " . TB_PREFIX . "artefacts SET vref = $vref, owner = $id, conquered = $time, active = 0 WHERE vref = $ovref"; if(mysqli_query($this->dblink, $q)) { @@ -7179,15 +7179,90 @@ References: User ID/Message ID, Mode return mysqli_query($this->dblink, $q); } + /** + * + * @return bool Returns if artefacts are already out or not + */ + + function areArtifactsSpawned(){ + $q = "SELECT Count(*) as Total FROM ".TB_PREFIX."artefacts LIMIT 1"; + $result = mysqli_fetch_array(mysqli_query($this->dblink, $q), MYSQLI_ASSOC); + return $result['Total'] > 0; + } + + /** + * Get all inactive artifacts which can be activated + * + * @return bool Returns all inactive artifacts + */ + + function getInactiveArtifacts($time){ + list($time) = $this->escape_input($time); + + $q = "SELECT * FROM ".TB_PREFIX."artefacts WHERE active = 0 AND owner > 5 AND conquered < $time ORDER BY conquered ASC, size ASC"; + $result = mysqli_query($this->dblink, $q); + return $this->mysqli_fetch_all($result); + } + + /** + * Get the sum of active artifacts by user ID, divided by: total, great, small and unique + * + * @param int $uid The User ID of the player + * @param bool $mode True if you want only active artifacts, false if you want all artifacts + * @return array Returns the artifacts sum of the account, divided by: total, great, small and unique + */ + + function getOwnArtifactsSum($uid, $mode = false){ + list($uid) = $this->escape_input((int) $uid, $mode); + + $q = "SELECT Count(size) AS totals, + SUM(IF(size = '1', 1, 0)) small, + SUM(IF(size = '2', 1, 0)) great, + SUM(IF(size = '3', 1, 0)) `unique` + FROM " . TB_PREFIX . "artefacts WHERE owner = ".(int) $uid.($mode ? " AND active = 1" : ""); + $result = mysqli_query($this->dblink, $q); + return $this->mysqli_fetch_all($result)[0]; + } + + /** + * Activate an artifact by his id + * + * @param int $id The id of the artifact + * @return bool Returns true if the query was successful, false otherwise + */ + + function activateArtifact($id, $mode = 1){ + list($id) = $this->escape_input((int) $id); + + $time = time(); + $q = "UPDATE " . TB_PREFIX . "artefacts SET active = $mode WHERE id = $id"; + return mysqli_query($this->dblink, $q); + } + + /** + * Get the newest active artifact by size + * + * @param int $size The size of the artifcat (village, account, unique) + * @return array Returns the newest active artifact infomations by size + */ + + function getNewestArtifactBySize($id, $size){ + list($id, $size) = $this->escape_input((int) $id, (int) $size); + + $q = "SELECT * FROM ".TB_PREFIX."artefacts WHERE active = 1 AND owner = $id AND size = $size ORDER BY conquered DESC LIMIT 1"; + $result = mysqli_query($this->dblink, $q); + return mysqli_fetch_array($result); + } + // no need to cache this method - public function canClaimArtifact($from,$vref,$size,$type) { + public function canClaimArtifact($from, $vref, $size, $type) { list($size,$type) = $this->escape_input((int) $size,(int) $type); //fix by Ronix global $session, $form; $size1 = $size2 = $size3 = 0; - $artifact = $this->getOwnArtefactInfo( $from ); + $artifact = $this->getOwnArtefactInfo($from); if (!empty($artifact)) { $form->addError( "error", "Treasury is full. Your hero could not claim the artefact" ); return false; @@ -7196,13 +7271,7 @@ References: User ID/Message ID, Mode $uid = $this->getVillageField($from, "owner"); $vuid = $this->getVillageField($vref, "owner"); - $q = "SELECT Count(size) AS totals, - SUM(IF(size = '1',1,0)) small, - SUM(IF(size = '2',1,0)) great, - SUM(IF(size = '3',1,0)) `unique` - FROM " . TB_PREFIX . "artefacts WHERE owner = " . (int) $uid; - $result = mysqli_query( $this->dblink, $q ); - $artifact = $this->mysqli_fetch_all( $result )[0]; + $artifact = $database->getOwnArtifactsSum($uid); if ( $artifact['totals'] < 3 || $uid == $vuid) { $DefenderFields = $this->getResourceLevel( $vref ); diff --git a/Templates/Build/27_show.tpl b/Templates/Build/27_show.tpl index bec9e120..3cd38be3 100644 --- a/Templates/Build/27_show.tpl +++ b/Templates/Build/27_show.tpl @@ -8,8 +8,11 @@ if($artefact['size'] == 1 && $artefact['type'] != 11){ $effect = ACCOUNT; } +$activationTime = 86400 / (SPEED == 2 ? 1.5 : (SPEED == 3 ? 2 : SPEED)); + if($artefact['owner'] == 3) $active = "-"; -elseif (!$artefact['active']) $active = date("Y-m-d H:i:s",$artefact['conquered'] + 86400); +elseif(!$artefact['active'] && $artefact['conquered'] < time() - $activationTime) $active = "Can't be activated"; +elseif (!$artefact['active']) $active = date("Y-m-d H:i:s", $artefact['conquered'] + $activationTime); else $active = "".ACTIVE.""; //// Added by brainiac - thank you