From 21179d346f74c9f09af2944817251b2ae7f6784b Mon Sep 17 00:00:00 2001 From: iopietro Date: Mon, 9 Apr 2018 21:31:50 +0200 Subject: [PATCH] Fixed some bugs and errors +Part 1 of the starvation refactor, attacking troops will now be killed +Fixed some errors in Units.php, Technology.php, Battle.php and some 16_incomming.tpl -Removed an unused function in Database.php +Fixed a bug that could have destroyed a village after taking an artefact even if it had more than 0 pop --- GameEngine/Automation.php | 258 +++++++++++++++++-------------- GameEngine/Battle.php | 2 +- GameEngine/Database.php | 18 +-- GameEngine/Technology.php | 4 +- GameEngine/Units.php | 14 +- Templates/Build/16_incomming.tpl | 2 +- 6 files changed, 149 insertions(+), 149 deletions(-) diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index f278250c..72cf545d 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -2614,8 +2614,9 @@ class Automation { $newLevels_fieldNames = []; $newLevels_fieldValues = []; - $pop1 = $database->getTotalPopulation($AttackerID); - $pop2 = $database->getTotalPopulation($DefenderID); + $poparray = $database->getVSumField([$AttackerID, $DefenderID] ,"pop"); + $pop1 = $poparray[0]['Total']; + $pop2 = $poparray[1]['Total']; if($pop1 > $pop2){ $buildlevel = $database->getResourceLevel($data['to']); @@ -2746,9 +2747,11 @@ class Automation { if ($database->canClaimArtifact($data['from'],$artifact['vref'],$artifact['size'],$artifact['type'])) { $database->claimArtefact($data['from'],$data['to'],$database->getVillageField($data['from'],"owner")); $info_hero = $hero_pic.",Your hero is carrying home an artefact".$xp; - if($pop == 0){ + + // if the defender pop is 0 with no artefact, then destroy the village + if($database->getVillageField($data['to'], "pop") == 0){ $can_destroy = $village_destroyed = 1; - $info_hero .= ". The village has been destroyed."; + $info_hero .= " The village has been destroyed."; } } else { $info_hero = $hero_pic.",".$form->getError("error").$xp; @@ -4893,15 +4896,14 @@ class Automation { if ($crop < $village_upkeep){ // add starv data $database->setVillageField($village->wid, 'starv', $village_upkeep); - if($starv==0) - $database->setVillageField($village->wid, 'starvupdate', time()); + if($starv==0) $database->setVillageField($village->wid, 'starvupdate', time()); } unset($crop,$unitarrays,$getvillage,$village_upkeep); } // load villages with minus prod - $starvarray = array(); + $starvarray = []; $starvarray = $database->getStarvation(); $vilIDs = []; @@ -4912,17 +4914,17 @@ class Automation { $database->getEnforceVillage($vilIDs, 0); $database->getOasisEnforce($vilIDs, 2); $database->getOasisEnforce($vilIDs, 3); + $database->getMovement(3, $vilIDs, 0); foreach ($starvarray as $starv){ $unitarrays = $this->getAllUnits($starv['wref']); - $howweeating=$this->getUpkeep($unitarrays, 0,$starv['wref']); - $upkeep = $starv['pop'] + $howweeating; - + $upkeep = $starv['pop'] + $this->getUpkeep($unitarrays, 0, $starv['wref']); + //TODO: getting enforcements could be done by using a recursive function + // get enforce other player from oasis $enforceoasis = $database->getOasisEnforce($starv['wref'], 2); - $maxcount=0; - $totalunits=0; + $maxcount = $totalunits = 0; if(count($enforceoasis)>0){ foreach ($enforceoasis as $enforce){ for($i=1;$i<=50;$i++){ @@ -4930,33 +4932,36 @@ class Automation { if($enforce['u'.$i] > $maxcount){ $maxcount = $enforce['u'.$i]; $maxtype = $i; - $enf = $enforce['id']; + $enf = $enforce; } $totalunits += $enforce['u'.$i]; } - if($totalunits == 0){ + if($totalunits == 0 && $enforce['hero'] > 0){ $maxcount = $enforce['hero']; $maxtype = "hero"; + $heroinfo = $database->getHero($database->getVillageField($enf['from'], "owner"))[0]; + $enf = $enforce; } - $other_reinf=true; } }else{ //own troops from oasis $enforceoasis = $database->getOasisEnforce($starv['wref'], 3); if(count($enforceoasis)>0){ foreach ($enforceoasis as $enforce){ - for($i=1;$i<=50;$i++){ + for($i = 1; $i <= 50; $i++){ $units = $enforce['u'.$i]; if($enforce['u'.$i] > $maxcount){ $maxcount = $enforce['u'.$i]; $maxtype = $i; - $enf = $enforce['id']; + $enf = $enforce; } $totalunits += $enforce['u'.$i]; } - if($totalunits == 0){ + if($totalunits == 0 && $enforce['hero'] > 0){ $maxcount = $enforce['hero']; $maxtype = "hero"; + $heroinfo = $database->getHero($database->getVillageField($enf['from'], "owner"))[0]; + $enf = $enforce; } } }else{ @@ -4964,18 +4969,20 @@ class Automation { $enforcearray = $database->getEnforceVillage($starv['wref'], 2); if(count($enforcearray)>0){ foreach ($enforcearray as $enforce){ - for($i = 0 ; $i <= 50 ; $i++){ + for($i = 1 ; $i <= 50 ; $i++){ $units = (isset($enforce['u'.$i]) ? $enforce['u'.$i] : 0); if($units > $maxcount){ $maxcount = $units; $maxtype = $i; - $enf = $enforce['id']; + $enf = $enforce; } $totalunits += $units; } - if($totalunits == 0){ + if($totalunits == 0 && $enforce['hero'] > 0){ $maxcount = $enforce['hero']; $maxtype = "hero"; + $heroinfo = $database->getHero($database->getVillageField($enf['from'], "owner"))[0]; + $enf = $enforce; } } }else{ @@ -4983,24 +4990,26 @@ class Automation { $enforcearray = $database->getEnforceVillage($starv['wref'], 3); if(count($enforcearray)>0){ foreach ($enforcearray as $enforce){ - for($i = 0 ; $i <= 50 ; $i++){ + for($i = 1 ; $i <= 50 ; $i++){ $units = (isset($enforce['u'.$i]) ? $enforce['u'.$i] : 0); if($units > $maxcount){ $maxcount = $units; $maxtype = $i; - $enf = $enforce['id']; + $enf = $enforce; } $totalunits += $units; } - if($totalunits == 0){ + if($totalunits == 0 && $enforce['hero'] > 0){ $maxcount = $enforce['hero']; $maxtype = "hero"; + $heroinfo = $database->getHero($database->getVillageField($enf['from'], "owner"))[0]; + $enf = $enforce; } } }else{ //get own unit - $unitarray = $database->getUnit($starv['wref']); - for($i = 0 ; $i <= 50 ; $i++){ + $unitarray = $database->getUnit($starv['wref']); + for($i = 1 ; $i <= 50 ; $i++){ $units = (isset($unitarray['u'.$i]) ? $unitarray['u'.$i] : 0); if($units > $maxcount){ $maxcount = $units; @@ -5009,9 +5018,35 @@ class Automation { $totalunits += $units; } if($totalunits == 0){ - $maxcount = $unitarray['hero']; - $maxtype = "hero"; - } + if($unitarray['hero'] > 0){ + $maxcount = $unitarray['hero']; + $maxtype = "hero"; + $heroinfo = $database->getHero($starv['owner'])[0]; + }else{ + //get own travelling troops + $attackarray = $database->getMovement(3, $starv['wref'], 0); + $tribe = $database->getUserField($starv['owner'], "tribe", 0); + foreach ($attackarray as $attack){ + for($i = 1 ; $i <= 10; $i++){ + $units = (isset($attack['t'.$i]) ? $attack['t'.$i] : 0); + if($units > $maxcount){ + $maxcount = $units; + $maxtypeatt = $i; + $maxtype = $maxtypeatt + (($tribe - 1) * 10); + $att = $attack['moveid']; + } + $totalunits += $units; + } + if($totalunits == 0 && $attack['t11'] > 0){ + $maxcount = $attack['t11']; + $maxtype = "hero"; + $maxtypeatt = 11; + $heroinfo = $database->getHero($starv['owner'])[0]; + $att = $attack['moveid']; + } + } + } + } } } } @@ -5020,102 +5055,81 @@ class Automation { // counting $timedif = $time-$starv['starvupdate']; - $skolko=$database->getCropProdstarv($starv['wref'])-$starv['starv']; - if($skolko<0){$golod=true;} - else{$golod=false;} - if($golod){ + $skolko = $database->getCropProdstarv($starv['wref'])-$starv['starv']; + if($skolko < 0) + { $starvsec = (abs($skolko)/3600); $difcrop = ($timedif*$starvsec); //crop eat up over time $newcrop = 0; $oldcrop = $database->getVillageField($starv['wref'], 'crop'); - if ($oldcrop > 100){ //if the grain is then tries to send all + if ($oldcrop > 100) //if the grain is then tries to send all + { $difcrop = $difcrop-$oldcrop; - if($difcrop < 0){ + if($difcrop < 0) + { $difcrop = 0; $newcrop = $oldcrop-$difcrop; $database->setVillageField($starv['wref'], 'crop', $newcrop); } } + + if($difcrop > 0) + { + global ${'u'.$maxtype}, ${'h'.$heroinfo['unit'].'_full'}; - if($difcrop > 0){ - global ${'u'.$maxtype}; + $hungry = []; + $hungry = ($maxtype != "hero") ? ${'u'.$maxtype} : ${'h'.$heroinfo['unit'].'_full'}[min($heroinfo['level'], 60)]; - $hungry=array(); - $hungry=${'u'.$maxtype}; + if ($hungry['crop'] > 0 && $oldcrop <= 0) $killunits = ceil($difcrop/$hungry['crop']); + else $killunits = 0; - if ($hungry['crop']>0 && $oldcrop <=0) { - $killunits = intval($difcrop/$hungry['crop']); - }else $killunits=0; + if($killunits > 0) + { + $pskolko = abs($skolko); + if($killunits > $pskolko && $skolko < 0) $killunits = $pskolko; - if($killunits > 0){ - $pskolko = abs($skolko); - if($killunits > $pskolko && $skolko <0){ - $killunits = $pskolko; + if($maxtype == "hero" && $maxcount > 0) + { + $totalunits = $maxcount; + $database->modifyHero("dead", 1, $heroinfo['heroid']); + $database->modifyHero("health", 0, $heroinfo['heroid']); } - if (isset($enf)){ - if($killunits < $maxcount){ - $database->modifyEnforce($enf, $maxtype, $killunits, 0); - $database->setVillageField( - $starv['wref'], - ['starv', 'starvupdate'], - [$upkeep, $time] - ); - $database->modifyResource($starv['wref'],0,0,0,$hungry['crop'],1); - - if($maxtype == "hero"){ - $heroid = $database->getHeroField($database->getVillageField($enf,"owner"),"heroid"); - $database->modifyHero("dead", 1, $heroid); - } - }else{ - $database->deleteReinf($enf); - $database->setVillageField( - $starv['wref'], - ['starv', 'starvupdate'], - [$upkeep, $time] - ); - } - }else{ - if($killunits < $maxcount){ - $database->modifyUnit($starv['wref'], array($maxtype), array($killunits), array(0)); - $database->setVillageField( - $starv['wref'], - ['starv', 'starvupdate'], - [$upkeep, $time] - ); - $database->modifyResource($starv['wref'],0,0,0,$hungry['crop'],1); - if($maxtype == "hero"){ - $heroid = $database->getHeroField($starv['owner'],"heroid"); - $database->modifyHero("dead", 1, $heroid); - } - }elseif($killunits > $maxcount){ - $killunits = $maxcount; - $database->modifyUnit($starv['wref'], array($maxtype), array($killunits), array(0)); - $database->setVillageField( - $starv['wref'], - ['starv', 'starvupdate'], - [$upkeep, $time] - ); - - if($maxtype == "hero"){ - $heroid = $database->getHeroField($starv['owner'],"heroid"); - $database->modifyHero("dead", 1, $heroid); - } + + if (isset($enf)) + { + if($killunits < $totalunits) + { + $database->modifyEnforce($enf['id'], $maxtype, min($killunits, $maxcount), 0); } + else $database->deleteReinf($enf['id']); } + elseif(isset($att)) + { + if($killunits < $totalunits) + { + $database->modifyAttack($att, $maxtypeatt, min($killunits, $maxcount)); + } + else $database->setMovementProc($att); + } + elseif(isset($maxtype)) + { + $database->modifyUnit($starv['wref'], [$maxtype], [min($killunits, $maxcount)], [0]); + } + + if($totalunits > 0) + { + $database->modifyResource($starv['wref'], 0, 0, 0, $hungry['crop'], 1); + $database->setVillageField($starv['wref'], ['starv', 'starvupdate'], [$upkeep, $time]); + } } } } $crop = $database->getCropProdstarv($starv['wref'], false); - if ($crop > $upkeep){ - $database->setVillageField( - $starv['wref'], - ['starv', 'starvupdate'], - [0, 0] - ); - } + + if ($crop > $upkeep) $database->setVillageField($starv['wref'], ['starv', 'starvupdate'], [0, 0]); - unset ($starv,$unitarrays,$enforcearray,$enforce,$starvarray); + unset ($starv,$unitarrays,$enforcearray,$enforce,$starvarray,$enf,$att,$attackarray,$maxtype,$maxtypeatt,$heroinfo); } if(file_exists("GameEngine/Prevention/starvation.txt")) { @@ -5133,10 +5147,10 @@ class Automation { if(file_exists("GameEngine/Prevention/climbers.txt")) { unlink("GameEngine/Prevention/climbers.txt"); } - global $database,$ranking; + global $database, $ranking; $ranking->procRankArray(); $climbers = $ranking->getRank(); - if(count($ranking->getRank()) > 0){ + if(count($climbers) > 0){ $q = "SELECT week FROM ".TB_PREFIX."medal order by week DESC LIMIT 0, 1"; $result = mysqli_query($database->dblink,$q); if(mysqli_num_rows($result)) { @@ -5150,28 +5164,34 @@ class Automation { foreach($array as $user){ $newrank = $ranking->getUserRank($user['id']); if($week > 1){ - 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); + for($i=$newrank+1;$igetUserRank($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 = count($ranking->getRank()) - $newrank; + $totalpoints = count($climbers) - $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); + if(isset($climbers[$i]['userid'])){ + $oldrank = $ranking->getUserRank($climbers[$i]['userid']); + $totalpoints = count($climbers) - $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); + for($i=$newrank+1;$igetUserRank($climbers[$i]['userid']); + $totalpoints = count($climbers) - $oldrank; + $database->setclimberrankpop($climbers[$i]['userid'], $totalpoints); + $database->updateoldrank($climbers[$i]['userid'], $oldrank); + } } } } diff --git a/GameEngine/Battle.php b/GameEngine/Battle.php index 44df1ffc..5af448f8 100755 --- a/GameEngine/Battle.php +++ b/GameEngine/Battle.php @@ -673,7 +673,7 @@ class Battle { } - $result[6] = pow($rap/$rdp*$moralbonus,$Mfactor); + $result[6] = pow($rap/($rdp*$moralbonus > 0 ? $rdp*$moralbonus : 1),$Mfactor); $total_att_units = count($units['Att_unit']); $start = intval(($att_tribe-1)*10+1); diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 62012fc4..3f98475a 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -1869,22 +1869,6 @@ class MYSQLi_DB implements IDbConnection { } } } - - /** - * Used to get the population of the whole account - * - * @param int $uid the user id - * @return int Returns the total population of the account - */ - - function getTotalPopulation($uid){ - list($uid) = $this->escape_input((int) $uid); - - $q = "SELECT Sum(pop) FROM " . TB_PREFIX . "vdata WHERE owner = ".$uid.""; - $result = mysqli_query($this->dblink, $q); - $row = mysqli_fetch_row($result); - return $row[0]; - } function getVillageByWorldID($vid, $use_cache = true) { $array_passed = is_array($vid); @@ -7788,7 +7772,7 @@ References: function KillMyHero($id) { list( $id ) = $this->escape_input( (int) $id ); - $q = "UPDATE " . TB_PREFIX . "hero set dead = 1, intraining = 0, inrevive = 0 where uid = " . $id . " AND dead = 0"; + $q = "UPDATE " . TB_PREFIX . "hero set dead = 1, intraining = 0, inrevive = 0, health = 0 where uid = " . $id . " AND dead = 0"; return mysqli_query( $this->dblink, $q ); } diff --git a/GameEngine/Technology.php b/GameEngine/Technology.php index 0b07e3bb..58ec5b72 100755 --- a/GameEngine/Technology.php +++ b/GameEngine/Technology.php @@ -730,7 +730,7 @@ class Technology { $ABUpgradesCount = count($ABUpgrades); $ups = 0; - if(count($ABUpgradesCount) > 0){ + if($ABUpgradesCount > 0){ foreach($ABUpgrades as $upgrade){ if(in_array(("b".$get['a']), $upgrade)) $ups++; } @@ -759,7 +759,7 @@ class Technology { $ABUpgradesCount = count($ABUpgrades); $ups = 0; - if(count($ABUpgradesCount) > 0){ + if($ABUpgradesCount > 0){ foreach($ABUpgrades as $upgrade){ if(in_array(("a".$get['a']), $upgrade)) $ups++; } diff --git a/GameEngine/Units.php b/GameEngine/Units.php index 95a4cb63..ca8a2172 100755 --- a/GameEngine/Units.php +++ b/GameEngine/Units.php @@ -239,7 +239,7 @@ class Units { public function returnTroops($wref,$mode=0) { global $database; - if (!mode) { + if (!$mode) { $getenforce=$database->getEnforceVillage($wref,0); foreach($getenforce as $enforce) { $this->processReturnTroops($enforce); @@ -414,14 +414,10 @@ class Units { array( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ); - $data21 = $database->getUserField( $database->getVrefField( $village->wid, 'owner' )['owner'], 'tribe', 0 ); - $eigen = $database->getCoor( $village->wid ); - $from = array( 'x' => $eigen['x'], 'y' => $eigen['y'] ); - $ander = $database->getCoor( $data['to_vid'] ); - $to = array( 'x' => $ander['x'], 'y' => $ander['y'] ); - $start = ( $data21['tribe'] - 1 ) * 10 + 1; - $end = ( $data21['tribe'] * 10 ); - + $fromcoor = $database->getCoor( $village->wid ); + $from = array('x' => $fromcoor['x'], 'y' => $fromcoor['y']); + $tocoor = $database->getCoor( $data['to_vid'] ); + $to = array('x' => $tocoor['x'], 'y' => $tocoor['y']); $speeds = array(); $scout = 1; diff --git a/Templates/Build/16_incomming.tpl b/Templates/Build/16_incomming.tpl index c0d981b5..200da0cb 100644 --- a/Templates/Build/16_incomming.tpl +++ b/Templates/Build/16_incomming.tpl @@ -8,7 +8,7 @@ $total_for2 = count($send); for($y=0;$y < $total_for;$y++){ for($i=0;$i < $total_for2;$i++){ if($units[$y]['ref'] == $send[$i]['ref2']){ -$res1 = mysqli_query($GLOBALS['link'],"SELECT wood, clay, iron, crop FROM " . TB_PREFIX . "send where id = ".(int) $send[$i]['ref'].""); +$res1 = mysqli_query($database->dblink,"SELECT wood, clay, iron, crop FROM " . TB_PREFIX . "send where id = ".(int) $send[$i]['ref'].""); $res = mysqli_fetch_array($res1); } }