Merge pull request #149 from hdmaniak2/fix-parallel-recall-units

This commit is contained in:
Catalin Novgorodschi
2026-03-27 07:50:57 +02:00
committed by GitHub
2 changed files with 98 additions and 77 deletions
+12
View File
@@ -4592,6 +4592,18 @@ References: User ID/Message ID, Mode
mysqli_query($this->dblink, "SELECT RELEASE_LOCK('train_village_$wid')"); mysqli_query($this->dblink, "SELECT RELEASE_LOCK('train_village_$wid')");
} }
function getEnforceLock($id) {
$id = (int) $id;
$result = mysqli_query($this->dblink, "SELECT GET_LOCK('enforce_$id', 10) AS locked");
$row = mysqli_fetch_assoc($result);
return $row['locked'] == 1;
}
function releaseEnforceLock($id) {
$id = (int) $id;
mysqli_query($this->dblink, "SELECT RELEASE_LOCK('enforce_$id')");
}
/** /**
* Get the time required to build a specified building * Get the time required to build a specified building
* *
+79 -70
View File
@@ -417,90 +417,99 @@ class Units {
private function sendTroopsBack($post) { private function sendTroopsBack($post) {
global $form, $database, $village, $session, $technology; global $form, $database, $village, $session, $technology;
$enforce = $database->getEnforceArray( $post['ckey'], 0 ); if (!$database->getEnforceLock($post['ckey'])) return;
$enforceoasis = $database->getOasisEnforceArray( $post['ckey'], 0 ); try {
if ( ( $enforce['from'] == $village->wid ) || ( $enforce['vref'] == $village->wid ) || ( $enforceoasis['conqured'] == $village->wid ) ) { // Re-fetch after lock to prevent TOCTOU race condition
$to = $database->getVillage( $enforce['from'] ); $enforce = $database->getEnforceArray( $post['ckey'], 0 );
$Gtribe = ($ownerTribe = $database->getUserField( $to['owner'], 'tribe', 0)) == 1 ? "" : $ownerTribe - 1; $enforceoasis = $database->getOasisEnforceArray( $post['ckey'], 0 );
if ( ( $enforce['from'] == $village->wid ) || ( $enforce['vref'] == $village->wid ) || ( $enforceoasis['conqured'] == $village->wid ) ) {
$to = $database->getVillage( $enforce['from'] );
$Gtribe = ($ownerTribe = $database->getUserField( $to['owner'], 'tribe', 0)) == 1 ? "" : $ownerTribe - 1;
for ( $i = 1; $i < 10; $i ++ ) { for ( $i = 1; $i < 10; $i ++ ) {
if ( isset( $post[ 't' . $i ] ) ) { if ( isset( $post[ 't' . $i ] ) ) {
if ( $i != 10 ) { if ( $i != 10 ) {
if ( $post[ 't' . $i ] > $enforce[ 'u' . $Gtribe . $i ] ) { if ( $post[ 't' . $i ] > $enforce[ 'u' . $Gtribe . $i ] ) {
$form->addError( "error", "You can't send back more units than you have" ); $form->addError( "error", "You can't send back more units than you have" );
break; break;
} }
if ( $post[ 't' . $i ] < 0 ) { if ( $post[ 't' . $i ] < 0 ) {
$form->addError( "error", "You can't send back negative units." ); $form->addError( "error", "You can't send back negative units." );
break; break;
}
} }
} else {
$post[ 't' . $i . '' ] = '0';
}
}
if ( isset( $post['t11'] ) ) {
if ( $post['t11'] > $enforce['hero'] ) {
$form->addError( "error", "You can't send back more units than you have" );
}
if ( $post['t11'] < 0 ) {
$form->addError( "error", "You can't send back negative units." );
} }
} else { } else {
$post[ 't' . $i . '' ] = '0'; $post['t11'] = '0';
}
}
if ( isset( $post['t11'] ) ) {
if ( $post['t11'] > $enforce['hero'] ) {
$form->addError( "error", "You can't send back more units than you have" );
} }
if ( $post['t11'] < 0 ) { if ( $form->returnErrors() > 0 ) {
$form->addError( "error", "You can't send back negative units." ); $_SESSION['errorarray'] = $form->getErrors();
} $_SESSION['valuearray'] = $_POST;
} else { $database->releaseEnforceLock($post['ckey']);
$post['t11'] = '0'; header( "Location: a2b.php" );
} exit;
} else {
if ( $form->returnErrors() > 0 ) { //change units
$_SESSION['errorarray'] = $form->getErrors(); $tribe = $database->getUserField($to['owner'], 'tribe', 0);
$_SESSION['valuearray'] = $_POST; $start = ($tribe - 1 ) * 10 + 1;
header( "Location: a2b.php" ); $end = $tribe * 10 ;
exit;
} else {
//change units $units = [];
$tribe = $database->getUserField($to['owner'], 'tribe', 0); $amounts = [];
$start = ($tribe - 1 ) * 10 + 1; $modes = [];
$end = $tribe * 10 ;
$units = []; $j = 1;
$amounts = []; for ( $i = $start; $i <= $end; $i ++ ) {
$modes = []; $units[] = $i;
$amounts[] = $post[ 't' . $j . '' ];
$modes[] = 0;
$j ++;
}
$j = 1; $units[] = 'hero';
for ( $i = $start; $i <= $end; $i ++ ) { $amounts[] = $post['t11'];
$units[] = $i;
$amounts[] = $post[ 't' . $j . '' ];
$modes[] = 0; $modes[] = 0;
$j ++;
$database->modifyEnforce($post['ckey'], $units, $amounts, $modes);
$j++;
$troopsTime = $this->getWalkingTroopsTime($enforce['from'], $enforce['vref'], $to['owner'], $tribe, $post, 1, 't');
$time = $database->getArtifactsValueInfluence($session->uid, $village->wid, 2, $troopsTime);
$reference = $database->addAttack($enforce['from'], $post['t1'], $post['t2'], $post['t3'], $post['t4'], $post['t5'], $post['t6'], $post['t7'], $post['t8'], $post['t9'], $post['t10'], $post['t11'], 2, 0, 0, 0, 0);
$database->addMovement(4, $village->wid, $enforce['from'], $reference, time(), ($time + time()));
$technology->checkReinf($post['ckey'], false);
$database->releaseEnforceLock($post['ckey']);
header("Location: build.php?id=39&refresh=1");
exit();
}
}else{
$form->addError("error", "You cant change someones troops.");
if($form->returnErrors() > 0){
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
$database->releaseEnforceLock($post['ckey']);
header("Location: a2b.php");
exit();
} }
$units[] = 'hero';
$amounts[] = $post['t11'];
$modes[] = 0;
$database->modifyEnforce($post['ckey'], $units, $amounts, $modes);
$j++;
$troopsTime = $this->getWalkingTroopsTime($enforce['from'], $enforce['vref'], $to['owner'], $tribe, $post, 1, 't');
$time = $database->getArtifactsValueInfluence($session->uid, $village->wid, 2, $troopsTime);
$reference = $database->addAttack($enforce['from'], $post['t1'], $post['t2'], $post['t3'], $post['t4'], $post['t5'], $post['t6'], $post['t7'], $post['t8'], $post['t9'], $post['t10'], $post['t11'], 2, 0, 0, 0, 0);
$database->addMovement(4, $village->wid, $enforce['from'], $reference, time(), ($time + time()));
$technology->checkReinf($post['ckey'], false);
header("Location: build.php?id=39&refresh=1");
exit();
}
}else{
$form->addError("error", "You cant change someones troops.");
if($form->returnErrors() > 0){
$_SESSION['errorarray'] = $form->getErrors();
$_SESSION['valuearray'] = $_POST;
header("Location: a2b.php");
exit();
} }
} finally {
$database->releaseEnforceLock($post['ckey']);
} }
} }