Compact checkvacation function

Compact checkvacation function to reduce query
This commit is contained in:
novgorodschi catalin
2026-06-25 14:35:04 +03:00
parent 596f007139
commit 8f204ec4ae
+84 -179
View File
@@ -8604,202 +8604,107 @@ References: User ID/Message ID, Mode
} }
/***************************************** /*****************************************
Function to vacation mode remake by Shadow Function to vacation mode
References: Shadow Remake & Refactor: Shadow
*****************************************/ *****************************************/
function checkVacationRequirements($uid){ function checkVacationRequirements(int $uid): array|bool{
$uid = (int)$uid; $uid = (int)$uid;
$time = time(); $now = time();
$errors = []; $errors = [];
// 1. Troops moving OUT // HELPERS : returns true if it finds rows
$q = "SELECT COUNT(*) as num $exists = function(string $sql, array $params = []) {
FROM ".TB_PREFIX."movement m $stmt = mysqli_prepare($this->dblink, $sql);
JOIN ".TB_PREFIX."vdata v ON v.wref = m.from if ($params) {
WHERE v.owner=$uid $types = str_repeat('i', count($params));
AND m.proc=0 mysqli_stmt_bind_param($stmt, $types, ...$params);
AND m.endtime > $time }
AND m.sort_type = 3"; mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$found = mysqli_stmt_num_rows($stmt) > 0;
mysqli_stmt_close($stmt);
return $found;
};
$res = mysqli_query($this->dblink,$q); // HELPERS : returns a single field
if(!$res){ die("SQL ERROR (TROOPS_MOVING): ".mysqli_error($this->dblink)); } $fetchOne = function(string $sql, array $params = []) {
$stmt = mysqli_prepare($this->dblink, $sql);
if ($params) {
$types = str_repeat('i', count($params));
mysqli_stmt_bind_param($stmt, $types, ...$params);
}
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($res);
mysqli_stmt_close($stmt);
return $row;
};
$row = mysqli_fetch_assoc($res); // 1. TROOPS MOVING
if((int)$row['num'] > 0) $errors[] = "TROOPS_MOVING"; $sql = "SELECT 1 FROM ".TB_PREFIX."movement m
JOIN ".TB_PREFIX."vdata v ON v.wref = m.from
WHERE v.owner=? AND m.proc=0 AND m.endtime>? AND m.sort_type=3 LIMIT 1";
if ($exists($sql, [$uid, $now])) $errors[] = "TROOPS_MOVING";
// 2. INCOMING TROOPS
$sql = "SELECT 1 FROM ".TB_PREFIX."movement m
JOIN ".TB_PREFIX."vdata v ON v.wref = m.to
WHERE v.owner=? AND m.proc=0 AND m.endtime>? AND m.sort_type IN (3,4) LIMIT 1";
if ($exists($sql, [$uid, $now])) $errors[] = "INCOMING_TROOPS";
// 2. Incoming troops // 3. REINFORCEMENTS
$q = "SELECT COUNT(*) as num $sql = "SELECT 1 FROM ".TB_PREFIX."enforcement e
FROM ".TB_PREFIX."movement m WHERE (e.from IN (SELECT wref FROM ".TB_PREFIX."vdata WHERE owner=?)
JOIN ".TB_PREFIX."vdata v ON v.wref = m.to OR e.vref IN (SELECT wref FROM ".TB_PREFIX."vdata WHERE owner=?))
WHERE v.owner=$uid AND (e.u1+e.u2+e.u3+e.u4+e.u5+e.u6+e.u7+e.u8+e.u9+e.u10+
AND m.proc=0 e.u11+e.u12+e.u13+e.u14+e.u15+e.u16+e.u17+e.u18+e.u19+e.u20+
AND m.endtime > $time e.u21+e.u22+e.u23+e.u24+e.u25+e.u26+e.u27+e.u28+e.u29+e.u30+
AND (m.sort_type = 3 OR m.sort_type = 4)"; e.u41+e.u42+e.u43+e.u44+e.u45+e.u46+e.u47+e.u48+e.u49+e.u50) > 0
LIMIT 1";
if ($exists($sql, [$uid, $uid])) $errors[] = "REINFORCEMENTS";
$res = mysqli_query($this->dblink,$q); // 4. WONDER WORLD
if(!$res){ die("SQL ERROR (INCOMING): ".mysqli_error($this->dblink)); } $sql = "SELECT 1 FROM ".TB_PREFIX."fdata f
JOIN ".TB_PREFIX."vdata v ON v.wref=f.vref
WHERE v.owner=? AND f.f99t=40 LIMIT 1";
if ($exists($sql, [$uid])) $errors[] = "WW";
$row = mysqli_fetch_assoc($res); // 5. ARTEFACTS
if((int)$row['num'] > 0) $errors[] = "INCOMING_TROOPS"; $sql = "SELECT 1 FROM ".TB_PREFIX."artefacts WHERE owner=? LIMIT 1";
if ($exists($sql, [$uid])) $errors[] = "ARTEFACTS";
// 6 + 10. PROTECTION + ADMIN or MULTIHUNTER
// 3. Reinforcements $user = $fetchOne("SELECT protect, access FROM ".TB_PREFIX."users WHERE id=?", [$uid]);
$q = "SELECT COUNT(*) as num if ($user) {
FROM ".TB_PREFIX."enforcement e if ((int)$user['protect'] > $now) $errors[] = "PROTECTION";
WHERE ( if (in_array((int)$user['access'], [8,9])) $errors[] = "NO_VACATION_ACCESS";
e.from IN (SELECT wref FROM ".TB_PREFIX."vdata WHERE owner = $uid)
OR
e.vref IN (SELECT wref FROM ".TB_PREFIX."vdata WHERE owner = $uid)
)
AND (
e.u1 + e.u2 + e.u3 + e.u4 + e.u5 +
e.u6 + e.u7 + e.u8 + e.u9 + e.u10 +
e.u11 + e.u12 + e.u13 + e.u14 + e.u15 +
e.u16 + e.u17 + e.u18 + e.u19 + e.u20 +
e.u21 + e.u22 + e.u23 + e.u24 + e.u25 +
e.u26 + e.u27 + e.u28 + e.u29 + e.u30 +
e.u41 + e.u42 + e.u43 + e.u44 + e.u45 +
e.u46 + e.u47 + e.u48 + e.u49 + e.u50
) > 0";
$res = mysqli_query($this->dblink,$q);
if(!$res){ die("SQL ERROR (REINFORCEMENTS): ".mysqli_error($this->dblink)); }
$row = mysqli_fetch_assoc($res);
if((int)$row['num'] > 0) $errors[] = "REINFORCEMENTS";
// 4. Wonder of the World
$q = "SELECT COUNT(*) as num
FROM ".TB_PREFIX."fdata f
JOIN ".TB_PREFIX."vdata v ON v.wref=f.vref
WHERE v.owner=$uid AND f.f99t=40";
$res = mysqli_query($this->dblink,$q);
if(!$res){ die("SQL ERROR (WW): ".mysqli_error($this->dblink)); }
$row = mysqli_fetch_assoc($res);
if((int)$row['num'] > 0) $errors[] = "WW";
// 5. Artefacts
$q = "SELECT COUNT(*) as num
FROM ".TB_PREFIX."artefacts
WHERE owner=$uid";
$res = mysqli_query($this->dblink,$q);
if(!$res){ die("SQL ERROR (ARTEFACTS): ".mysqli_error($this->dblink)); }
$row = mysqli_fetch_assoc($res);
if((int)$row['num'] > 0) $errors[] = "ARTEFACTS";
// 6. Beginner protection (FIXAT)
$q = "SELECT protect
FROM ".TB_PREFIX."users
WHERE id=$uid";
$res = mysqli_query($this->dblink,$q);
if(!$res){ die("SQL ERROR (PROTECTION): ".mysqli_error($this->dblink)); }
$row = mysqli_fetch_assoc($res);
$protect = (int)$row['protect'];
if($protect > 0 && $protect > $time){
$errors[] = "PROTECTION";
} }
// 7. PRISONERS IN & OUT (Enemy troops trapped in your villages & Your troops trapped in enemy villages)
$sqlIn = "SELECT 1 FROM ".TB_PREFIX."prisoners p
JOIN ".TB_PREFIX."vdata v ON v.wref=p.wref
WHERE v.owner=? LIMIT 1";
if ($exists($sqlIn, [$uid])) $errors[] = "PRISONERS_IN";
// ========================= $sqlOut = "SELECT 1 FROM ".TB_PREFIX."prisoners p
// 7. PRISONERS (FIX FINAL) JOIN ".TB_PREFIX."vdata v ON v.wref=p.from
// ========================= WHERE v.owner=? LIMIT 1";
if ($exists($sqlOut, [$uid])) $errors[] = "PRISONERS_OUT";
// 7.1 Enemy troops trapped in your villages // 8. MARKETPLACE MOVING
$q = "SELECT COUNT(*) as num $sql = "SELECT 1 FROM ".TB_PREFIX."movement m
FROM ".TB_PREFIX."prisoners p JOIN ".TB_PREFIX."vdata v ON v.wref=m.from
JOIN ".TB_PREFIX."vdata v ON v.wref = p.wref WHERE v.owner=? AND m.proc=0 AND m.endtime>? AND m.sort_type=0 LIMIT 1";
WHERE v.owner = $uid"; if ($exists($sql, [$uid, $now])) $errors[] = "MARKET";
$res = mysqli_query($this->dblink,$q); // 9. ACCOUNT DELETION
if(!$res){ $del = $fetchOne("SELECT timestamp FROM ".TB_PREFIX."deleting WHERE uid=?", [$uid]);
die("SQL ERROR (PRISONERS_IN): ".mysqli_error($this->dblink)); if (!empty($del['timestamp']) && $del['timestamp'] > $now) {
$errors[] = "ACCOUNT_DELETION";
} }
$row = mysqli_fetch_assoc($res);
$hasEnemyTrapped = (int)$row['num'];
// 7.2 Your troops trapped in enemy villages
$q = "SELECT COUNT(*) as num
FROM ".TB_PREFIX."prisoners p
JOIN ".TB_PREFIX."vdata v ON v.wref = p.from
WHERE v.owner = $uid";
$res = mysqli_query($this->dblink,$q);
if(!$res){
die("SQL ERROR (PRISONERS_OUT): ".mysqli_error($this->dblink));
}
$row = mysqli_fetch_assoc($res);
$hasOwnCaptured = (int)$row['num'];
// FINAL RULE
if($hasEnemyTrapped > 0){
$errors[] = "PRISONERS_IN";
}
if($hasOwnCaptured > 0){
$errors[] = "PRISONERS_OUT";
}
// 8. Marketplace
$q = "SELECT COUNT(*) as num
FROM ".TB_PREFIX."movement m
JOIN ".TB_PREFIX."vdata v ON v.wref = m.from
WHERE v.owner = $uid
AND m.proc = 0
AND m.endtime > $time
AND m.sort_type = 0";
$res = mysqli_query($this->dblink,$q);
if(!$res){ die("SQL ERROR (MARKET): ".mysqli_error($this->dblink)); }
$row = mysqli_fetch_assoc($res);
if((int)$row['num'] > 0) $errors[] = "MARKET";
// 9. Account deletion
$q = "SELECT timestamp
FROM ".TB_PREFIX."deleting
WHERE uid = $uid";
$res = mysqli_query($this->dblink,$q);
if(!$res){ die("SQL ERROR (DELETING): ".mysqli_error($this->dblink)); }
$row = mysqli_fetch_assoc($res);
if(!empty($row['timestamp']) && $row['timestamp'] > $time){
$errors[] = "ACCOUNT_DELETION";
}
// 10. Admin / Multihunter restriction
$q = "SELECT access FROM ".TB_PREFIX."users WHERE id=$uid";
$res = mysqli_query($this->dblink,$q);
if(!$res){ die("SQL ERROR (ACCESS): ".mysqli_error($this->dblink)); }
$row = mysqli_fetch_assoc($res);
$access = (int)$row['access'];
// 0 = BANNED, 2 = USER, 8 = MH, 9 = ADMIN
if($access == 8 || $access == 9){
$errors[] = "NO_VACATION_ACCESS";
}
return empty($errors) ? true : $errors; return empty($errors) ? true : $errors;
} }
// no need to cache this method // no need to cache this method
function getHeroDeadReviveOrInTraining($id) { function getHeroDeadReviveOrInTraining($id) {