From 544de58737c422637f6704fd37e98824b31e9a56 Mon Sep 17 00:00:00 2001 From: yi12345 Date: Tue, 14 May 2013 00:40:10 +0300 Subject: [PATCH] optimize climber points update (work really fast now) --- GameEngine/Alliance.php | 1 + GameEngine/Automation.php | 134 +++++++++++++++++++++++------- GameEngine/Database/db_MYSQL.php | 27 +++++- GameEngine/Database/db_MYSQLi.php | 31 +++++-- 4 files changed, 154 insertions(+), 39 deletions(-) diff --git a/GameEngine/Alliance.php b/GameEngine/Alliance.php index 3bf96e35..64597bbf 100644 --- a/GameEngine/Alliance.php +++ b/GameEngine/Alliance.php @@ -233,6 +233,7 @@ $max = $bid18[$village->resarray['f' . $post['id']]]['attri']; $aid = $database->createAlliance($database->RemoveXSS($post['ally1']), $database->RemoveXSS($post['ally2']), $session->uid, $max); $database->updateUserField($database->RemoveXSS($session->uid), "alliance", $database->RemoveXSS($aid), 1); + $database->procAllyPop($aid); // Asign Permissions $database->createAlliPermissions($database->RemoveXSS($session->uid), $database->RemoveXSS($aid), 'Alliance founder', '1', '1', '1', '1', '1', '1', '1', '1'); // log the notice diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 1ac5da4d..7798cd32 100644 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -94,6 +94,8 @@ class Automation { $this->recountCP($vid); $q = "UPDATE ".TB_PREFIX."vdata set pop = $popTot where wref = $vid"; mysql_query($q); + $owner = $database->getVillageField($vid,"owner"); + $this->procClimbers($owner); return $popTot; @@ -145,7 +147,7 @@ class Automation { public function Automation() { - $this->procClimbers(); + $this->procNewClimbers(); $this->ClearUser(); $this->ClearInactive(); $this->oasisResourcesProduce(); @@ -614,6 +616,7 @@ class Automation { $level = $database->getFieldLevel($indi['wid'],$indi['field']); $pop = $this->getPop($indi['type'],($level-1)); $database->modifyPop($indi['wid'],$pop[0],0); + $this->procClimbers($database->getVillageField($indi['wid'],'owner')); $database->addCP($indi['wid'],$pop[1]); if($indi['type'] == 10) { @@ -3799,6 +3802,7 @@ $crannyimg = "query($q); $pop=$this->getPop($type,$level-1); $database->modifyPop($vil['vref'],$pop[0],1); + $this->procClimbers($database->getVillageField($vil['vref'],'owner')); $database->delDemolition($vil['vref']); } } @@ -4185,14 +4189,14 @@ $crannyimg = "procRankArray(); - if(mysql_num_rows(mysql_query($users)) > 0){ + $ranking->procRankArray(); + $climbers = $ranking->getRank(); + if(count($ranking->getRank()) > 0){ $q = "SELECT * FROM ".TB_PREFIX."medal order by week DESC LIMIT 0, 1"; $result = mysql_query($q); if(mysql_num_rows($result)) { @@ -4201,34 +4205,106 @@ $crannyimg = "getUserRank($row['userid']); - if($row['oldrank'] == 0){ - $database->updateoldrank($row['userid'], $oldrank); - }else{ + $q = "SELECT * FROM ".TB_PREFIX."users where oldrank = 0 and id > 5"; + $array = $database->query_return($q); + foreach($array as $user){ + $newrank = $ranking->getUserRank($user['id']); if($week > 1){ - if($row['oldrank'] > $oldrank) { - $totalpoints = $row['oldrank'] - $oldrank; - $database->addclimberrankpop($row['userid'], $totalpoints); - $database->updateoldrank($row['userid'], $oldrank); - } else - if($row['oldrank'] < $oldrank) { - $totalpoints = $oldrank - $row['oldrank']; - $database->removeclimberrankpop($row['userid'], $totalpoints); - $database->updateoldrank($row['userid'], $oldrank); - } + for($i=$newrank+1;$igetRank());$i++) { + $oldrank = $ranking->getUserRank($climbers[$i]['userid']); + $totalpoints = $oldrank - $climbers[$i]['oldrank']; + $database->removeclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + } + $database->updateoldrank($user['id'], $newrank); }else{ - $totalpoints = mysql_num_rows(mysql_query($users)) - $oldrank; - $database->setclimberrankpop($row['userid'], $totalpoints+1); - $database->updateoldrank($row['userid'], $oldrank); + $totalpoints = count($ranking->getRank()) - $newrank; + $database->setclimberrankpop($user['id'], $totalpoints); + $database->updateoldrank($user['id'], $newrank); + for($i=1;$i<$newrank;$i++){ + $oldrank = $ranking->getUserRank($climbers[$i]['userid']); + $totalpoints = count($ranking->getRank()) - $oldrank; + $database->setclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + } + for($i=$newrank+1;$igetRank());$i++){ + $oldrank = $ranking->getUserRank($climbers[$i]['userid']); + $totalpoints = count($ranking->getRank()) - $oldrank; + $database->setclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + } } } } - } - $alliance = $database->getARanking(); - $ranking->procARankArray(); + if(file_exists("GameEngine/Prevention/climbers.txt")) { + unlink("GameEngine/Prevention/climbers.txt"); + } + } + + private function procClimbers($uid) { + global $database, $ranking; + $ranking->procRankArray(); + $climbers = $ranking->getRank(); if(count($ranking->getRank()) > 0){ - foreach($alliance as $ally){ + $q = "SELECT * FROM ".TB_PREFIX."medal order by week DESC LIMIT 0, 1"; + $result = mysql_query($q); + if(mysql_num_rows($result)) { + $row=mysql_fetch_assoc($result); + $week=($row['week']+1); + } else { + $week='1'; + } + $myrank = $ranking->getUserRank($uid); + if($climbers[$myrank]['oldrank'] > $myrank){ + for($i=$myrank+1;$i<=$climbers[$myrank]['oldrank'];$i++) { + $oldrank = $ranking->getUserRank($climbers[$i]['userid']); + if($week > 1){ + $totalpoints = $oldrank - $climbers[$i]['oldrank']; + $database->removeclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + }else{ + $totalpoints = count($ranking->getRank()) - $oldrank; + $database->setclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + } + } + if($week > 1){ + $totalpoints = $climbers[$myrank]['oldrank'] - $myrank; + $database->addclimberrankpop($climbers[$myrank]['userid'], $totalpoints); + $database->updateoldrank($climbers[$myrank]['userid'], $myrank); + }else{ + $totalpoints = count($ranking->getRank()) - $myrank; + $database->setclimberrankpop($climbers[$myrank]['userid'], $totalpoints); + $database->updateoldrank($climbers[$myrank]['userid'], $myrank); + } + }else if($climbers[$myrank]['oldrank'] < $myrank){ + for($i=$climbers[$myrank]['oldrank'];$i<$myrank;$i++) { + $oldrank = $ranking->getUserRank($climbers[$i]['userid']); + if($week > 1){ + $totalpoints = $climbers[$i]['oldrank'] - $oldrank; + $database->addclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + }else{ + $totalpoints = count($ranking->getRank()) - $oldrank; + $database->setclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + } + } + if($week > 1){ + $totalpoints = $myrank - $climbers[$myrank-1]['oldrank']; + $database->removeclimberrankpop($climbers[$myrank-1]['userid'], $totalpoints); + $database->updateoldrank($climbers[$myrank-1]['userid'], $myrank); + }else{ + $totalpoints = count($ranking->getRank()) - $myrank; + $database->setclimberrankpop($climbers[$myrank-1]['userid'], $totalpoints); + $database->updateoldrank($climbers[$myrank-1]['userid'], $myrank); + } + } + } + $ranking->procARankArray(); + $aid = $database->getUserField($uid,"alliance",0); + if(count($ranking->getRank()) > 0 && $aid != 0){ + $ally = $database->getAlliance($aid); $memberlist = $database->getAllMember($ally['id']); $oldrank = 0; foreach($memberlist as $member) { @@ -4247,10 +4323,6 @@ $crannyimg = " time()){ - $time = strtotime(START_TIME); + $timep = (strtotime(START_TIME) + PROTECTION); } - $timep = ($time + PROTECTION); $q = "INSERT INTO " . TB_PREFIX . "users (username,password,access,email,timestamp,tribe,act,protect,lastupdate,regtime) VALUES ('$username', '$password', " . USER . ", '$email', $time, $tribe, '$act', $timep, $time, $time)"; if(mysql_query($q, $this->connection)) { return mysql_insert_id($this->connection); @@ -1161,6 +1161,27 @@ class MYSQL_DB { mysql_query($q, $this->connection); return mysql_insert_id($this->connection); } + + function procAllyPop($aid) { + $ally = $this->getAlliance($aid); + $memberlist = $this->getAllMember($ally['id']); + $oldrank = 0; + foreach($memberlist as $member) { + $oldrank += $this->getVSumField($member['id'],"pop"); + } + if($ally['oldrank'] != $oldrank){ + if($ally['oldrank'] < $oldrank) { + $totalpoints = $oldrank - $ally['oldrank']; + $this->addclimberrankpopAlly($ally['id'], $totalpoints); + $this->updateoldrankAlly($ally['id'], $oldrank); + } else + if($ally['oldrank'] > $oldrank) { + $totalpoints = $ally['oldrank'] - $oldrank; + $this->removeclimberrankpopAlly($ally['id'], $totalpoints); + $this->updateoldrankAlly($ally['id'], $oldrank); + } + } + } /***************************************** Function to insert an alliance new diff --git a/GameEngine/Database/db_MYSQLi.php b/GameEngine/Database/db_MYSQLi.php index 106bbd10..44184829 100644 --- a/GameEngine/Database/db_MYSQLi.php +++ b/GameEngine/Database/db_MYSQLi.php @@ -22,14 +22,14 @@ class MYSQLi_DB { } function register($username, $password, $email, $tribe, $act) { - $time = time(); + $time = time(); + $timep = time() + PROTECTION; if(strtotime(START_TIME) > time()){ - $time = strtotime(START_TIME); + $timep = (strtotime(START_TIME) + PROTECTION); } - $timep = ($time + PROTECTION); $q = "INSERT INTO " . TB_PREFIX . "users (username,password,access,email,timestamp,tribe,act,protect,lastupdate,regtime) VALUES ('$username', '$password', " . USER . ", '$email', $time, $tribe, '$act', $timep, $time, $time)"; - if(mysqli_query($this->connection, $q)) { - return mysqli_insert_id($this->connection); + if(mysql_query($q, $this->connection)) { + return mysql_insert_id($this->connection); } else { return false; } @@ -1164,6 +1164,27 @@ class MYSQLi_DB { mysqli_query($this->connection, $q); return mysqli_insert_id($this->connection); } + + function procAllyPop($aid) { + $ally = $this->getAlliance($aid); + $memberlist = $this->getAllMember($ally['id']); + $oldrank = 0; + foreach($memberlist as $member) { + $oldrank += $this->getVSumField($member['id'],"pop"); + } + if($ally['oldrank'] != $oldrank){ + if($ally['oldrank'] < $oldrank) { + $totalpoints = $oldrank - $ally['oldrank']; + $this->addclimberrankpopAlly($ally['id'], $totalpoints); + $this->updateoldrankAlly($ally['id'], $oldrank); + } else + if($ally['oldrank'] > $oldrank) { + $totalpoints = $ally['oldrank'] - $oldrank; + $this->removeclimberrankpopAlly($ally['id'], $totalpoints); + $this->updateoldrankAlly($ally['id'], $oldrank); + } + } + } /***************************************** Function to insert an alliance new