General fixes

+Fixed a bug that would have killed all troops by starvation, if added
through the admin panel
This commit is contained in:
iopietro
2018-07-25 17:17:52 +02:00
parent 1f86162179
commit b9a442b423
6 changed files with 93 additions and 141 deletions
+12 -14
View File
@@ -13,25 +13,23 @@
if(!isset($_SESSION)) session_start();
if($_SESSION['access'] < 9) die(ACCESS_DENIED_ADMIN);
include_once("../../Database.php");
include_once("../../Technology.php");
include_once("../../Data/unitdata.php");
$id = $_POST['id'];
$id = (int)$_POST['id'];
$village = $database->getVillage($id);
$user = $database->getUserArray($village['owner'],1);
$units="";
$tribe = $user['tribe'];
if($tribe ==1){ $u = 0;}
if($tribe ==2){ $u = 10;}
if($tribe ==3){ $u = 20;}
if($tribe ==4){ $u = 30;}
if($tribe ==5){ $u = 40;}
if($tribe ==6){ $u = 50;}
$user = $database->getUserArray($village['owner'],1);
$units = "";
$tribe = $user['tribe'];
$u = ($tribe - 1) * 10;
for($i=1; $i<11; $i++) {
$units.="u".($u+$i)."=".$database->escape($_POST['u'.($u+$i)].(($i < 10) ? ", " : ""));
for($i = 1; $i < 11; $i++) {
$units.="u".($u + $i)."=".$database->escape($_POST['u'.($u + $i)].(($i < 10) ? ", " : ""));
}
$q = "UPDATE ".TB_PREFIX."units SET ".$units." WHERE vref = ".(int) $id;
$q = "UPDATE ".TB_PREFIX."units SET ".$units." WHERE vref = ".$id;
$database->query($q);
$database->query("Insert into ".TB_PREFIX."admin_log values (0,".(int) $_SESSION['id'].",'Changed troop amounts in village <a href=\'admin.php?p=village&did=$id\'>$id</a> ',".time().")");
$database->addStarvationData($id);
header("Location: ../../../Admin/admin.php?p=village&did=".$id."&d");
?>
Executable → Regular
+21 -47
View File
@@ -383,7 +383,7 @@ class Automation {
}
private function buildComplete() {
global $database, $bid18, $bid10, $bid11, $bid38, $bid39;
global $database, $technology, $bid18, $bid10, $bid11, $bid38, $bid39;
$time = time();
// IDs of villages that were affected by this building completion update,
@@ -476,7 +476,7 @@ class Automation {
}
//Update starvation data
$this->addStarvationData($indi['wid']);
$database->addStarvationData($indi['wid']);
// update the requested fields, all at once
$database->setVillageFields($indi['wid'], array_keys($fieldsToSet), array_values($fieldsToSet));
@@ -2488,7 +2488,7 @@ class Automation {
}
//Update starvation data
$this->addStarvationData($to['wref']);
$database->addStarvationData($to['wref']);
//Returning units back to village is not necessary because it will be taken care when processing movement
// Fix by AL-Kateb
@@ -2579,7 +2579,7 @@ class Automation {
}
private function sendreinfunitsComplete() {
global $bid23, $database, $battle;
global $bid23, $database, $technology, $battle;
$time = time();
$q = "
@@ -2717,7 +2717,7 @@ class Automation {
}
//Update starvation data
$this->addStarvationData($data['to']);
$database->addStarvationData($data['to']);
//check empty reinforcement in rally point
$e_units = '';
@@ -2733,7 +2733,7 @@ class Automation {
}
private function returnunitsComplete() {
global $database;
global $database, $technology;
$time = time();
$q = "
@@ -2783,7 +2783,7 @@ class Automation {
$movementProcIDs[] = $data['moveid'];
//Update starvation data
$this->addStarvationData($data['to']);
$database->addStarvationData($data['to']);
}
$database->setMovementProc(implode(', ', $movementProcIDs));
@@ -3233,7 +3233,7 @@ class Automation {
}
private function trainingComplete() {
global $database;
global $database, $technology;
$time = time();
$trainlist = $database->getTrainingList();
@@ -3287,7 +3287,7 @@ class Automation {
if ($valuesUpdated) call_user_func(get_class($database).'::clearUnitsCache');
//Update starvation data
$this->addStarvationData($train['vref']);
$database->addStarvationData($train['vref']);
}
}
}
@@ -3677,30 +3677,6 @@ class Automation {
}
}
}
/**
* Adds the starvation data in villages with a negative value of crop
*
* @param int $wref The village ID where the crop is negative
*/
private function addStarvationData($wref){
global $database, $technology;
$getVillage = $database->getVillage($wref);
//Exlude Support, Nature, Natars, TaskMaster and Multihunter
if ($getVillage['owner'] > 5){
$crop = $database->getCropProdstarv($wref, false);
$unitArrays = $this->getAllUnits($wref, false);
$villageUpkeep = $getVillage['pop'] + $technology->getUpkeep($unitArrays, 0, $wref);
$starv = $getVillage['starv'];
if ($crop < $villageUpkeep){
//Add starvation data
$database->setVillageFields($wref, ['starv'], [$villageUpkeep]);
}
}
}
/**
* Function for starvation - by brainiacX and Shadow
@@ -3712,23 +3688,23 @@ class Automation {
private function starvation() {
global $database, $technology;
//starvation is disabled during Easter/Holidays/Christmas
//Starvation is disabled during Easter/Holidays/Christmas
if(PEACE) return;
$time = time();
//update starvation in every village
//Update starvation in every village
$starvarray = $database->getProfileVillages(0, 7);
foreach($starvarray as $starv) $this->addStarvationData($starv['wref']);
foreach($starvarray as $starv) $database->addStarvationData($starv['wref']);
// load villages with minus prod
//Load villages with minus prod
$starvarray = [];
$starvarray = $database->getStarvation();
$vilIDs = [];
foreach ($starvarray as $starv) $vilIDs[] = $starv['wref'];
// cache
//Cache
$database->getEnforceVillage($vilIDs, 0);
$database->getOasisEnforce($vilIDs, 2);
$database->getOasisEnforce($vilIDs, 3);
@@ -3758,7 +3734,7 @@ class Automation {
$allTroopsArray = [$enforceArrays, $prisonerArrays, $unitArrays, $attackArrays];
// find the first not-empty array
//Find the first not-empty array
foreach($allTroopsArray as $type => $allTroops)
{
if(!empty($allTroops)){
@@ -3771,10 +3747,10 @@ class Automation {
}
}
// if the player has no troops, then skip the next instructions
//If the player has no troops, then skip the next instructions
if(empty($starvingTroops)) continue;
// counting
//Counting
$timedif = $time - $starv['starvupdate'];
$cropProd = $database->getCropProdstarv($starv['wref']) - $starv['starv'];
if($cropProd < 0){
@@ -3783,7 +3759,7 @@ class Automation {
$newcrop = 0;
$oldcrop = $database->getVillageField($starv['wref'], 'crop');
//if the grain is then tries to send all
//If the grain is then tries to send all
if ($oldcrop > 100){
$difcrop = $difcrop - $oldcrop;
if($difcrop < 0){
@@ -3804,7 +3780,7 @@ class Automation {
$counting = true;
while($difcrop > 0)
{
// search the highest troop
//S earch the highest troop
$maxcount = $maxtype = 0;
for($i = $start ; $i <= $end ; $i++)
{
@@ -3837,10 +3813,8 @@ class Automation {
else if($maxtype == 0) $newCrop = 0;
else $newCrop = $GLOBALS['u'.$maxtype]['crop'];
if($totalKilledUnits > 0)
{
switch($type)
{
if($totalKilledUnits > 0){
switch($type){
case 0:
if($totalKilledUnits < $totalUnits){
$database->modifyEnforce($starvingTroops['id'], array_keys($killedUnits), array_values($killedUnits), 0);
+36 -2
View File
@@ -7697,7 +7697,6 @@ References: User ID/Message ID, Mode
foreach($detailsArray as $field => $value) $values[] = $field."=".$value;
$q = "UPDATE ".TB_PREFIX."artefacts SET ".implode(",", $values)." WHERE id = $id";
echo $q;
return mysqli_query($this->dblink, $q);
}
@@ -7858,7 +7857,7 @@ References: User ID/Message ID, Mode
case 9:
case 10:
case 11:
$cropo ++;
$cropo++;
break;
case 12:
$cropo += 2;
@@ -7883,6 +7882,41 @@ References: User ID/Message ID, Mode
return self::$cropProductionStarvationValueCache[$wref];
}
/**
* Adds the starvation data in villages with a negative value of crop
*
* @param int $wref The village ID where the crop is negative
*/
public function addStarvationData($wref){
global $technology;
$getVillage = $this->getVillage($wref);
//Exlude Support, Nature, Natars, TaskMaster and Multihunter
if ($getVillage['owner'] > 5){
$crop = $this->getCropProdstarv($wref, false);
$unitArrays = $technology->getAllUnits($wref, false, 0, false);
$villageUpkeep = $getVillage['pop'] + $technology->getUpkeep($unitArrays, 0, $wref);
$starv = $getVillage['starv'];
if ($crop < $villageUpkeep){
//Add starvation data
$fields = ['starv'];
$values = [$villageUpkeep];
//Update the starvupdate if it's set to 0
if($getVillage['starvupdate'] == 0) {
$fields[] = 'starvupdate';
$values[] = time();
}
//Update the starvation datas
$this->setVillageFields($wref, $fields, $values);
}
}
}
//general statistics
function addGeneralAttack($casualties) {
+6 -6
View File
@@ -236,13 +236,13 @@ class Technology {
return $ownunit;
}
function getAllUnits($base, $InVillageOnly = false, $mode=0) {
function getAllUnits($base, $InVillageOnly = false, $mode = 0, $useCache = true) {
global $database;
$ownunit = $database->getUnit($base);
$ownunit = $database->getUnit($base, $useCache);
$ownunit['u99'] -= $ownunit['u99'];
$ownunit['u99o'] -= $ownunit['u99o'];
$enforcementarray = $database->getEnforceVillage($base, 0);
$enforcementarray = $database->getEnforceVillage($base, 0, $useCache);
if(count($enforcementarray) > 0){
foreach($enforcementarray as $enforce){
for($i = 1; $i <= 50; $i++){
@@ -252,7 +252,7 @@ class Technology {
}
}
if($mode == 0){
$enforceoasis = $database->getOasisEnforce($base, 0);
$enforceoasis = $database->getOasisEnforce($base, 0, $useCache);
if(count($enforceoasis) > 0){
foreach($enforceoasis as $enforce){
for($i = 1; $i <= 50; $i++){
@@ -261,7 +261,7 @@ class Technology {
$ownunit['hero'] += $enforce['hero'];
}
}
$enforceoasis1 = $database->getOasisEnforce($base, 1);
$enforceoasis1 = $database->getOasisEnforce($base, 1, $useCache);
if(count($enforceoasis1) > 0){
foreach($enforceoasis1 as $enforce){
for($i = 1; $i <= 50; $i++){
@@ -271,7 +271,7 @@ class Technology {
}
}
$prisoners = $database->getPrisoners($base, 1);
$prisoners = $database->getPrisoners($base, 1, $useCache);
if(!empty($prisoners)){
foreach($prisoners as $prisoner){
$owner = $database->getVillageField($base, "owner");