Last Phase before v11 release. New Automation Cleanup

Last Phase before v11 release. New Automation Cleanup
This commit is contained in:
novgorodschi catalin
2026-07-23 11:49:27 +03:00
parent 9a3f26bc93
commit 8a2d727659
9 changed files with 288 additions and 3 deletions
+32
View File
@@ -113,6 +113,12 @@ $cronKey = defined('CRON_KEY') ? (string) CRON_KEY : '';
$cronBase = defined('HOMEPAGE') ? rtrim(HOMEPAGE, '/') : '';
$cronUrl = ($cronBase !== '' && $cronKey !== '') ? $cronBase . '/cron.php?key=' . $cronKey : '';
$cleanReports = defined('CLEANUP_REPORTS_DAYS') ? (int) CLEANUP_REPORTS_DAYS : 14;
$cleanChat = defined('CLEANUP_CHAT_DAYS') ? (int) CLEANUP_CHAT_DAYS : 7;
$cleanMessages = defined('CLEANUP_MESSAGES_DAYS') ? (int) CLEANUP_MESSAGES_DAYS : 0;
$cleanupMarker = __DIR__ . '/../../GameEngine/Prevention/cleanup_last.txt';
$cleanupInfo = @is_file($cleanupMarker) ? @json_decode((string) @file_get_contents($cleanupMarker), true) : null;
$cronPath = realpath(__DIR__ . '/../../cron.php');
$cronCmd = $cronPath ? '*/5 * * * * /usr/local/bin/php ' . $cronPath . ' >/dev/null 2>&1' : '';
@@ -175,6 +181,32 @@ $cronKeyMasked = ($cronKey === '')
</tr>
<?php } ?>
<tr>
<td>Database cleanup <em class="tooltip">?<span>Automation trims tables that would otherwise grow forever. Archived reports are never deleted. 0 disables a rule.</span></em></td>
<td><?php
$parts = array();
$parts[] = 'reports: ' . ($cleanReports > 0 ? $cleanReports . 'd' : 'off');
$parts[] = 'chat: ' . ($cleanChat > 0 ? $cleanChat . 'd' : 'off');
$parts[] = 'deleted messages: ' . ($cleanMessages > 0 ? $cleanMessages . 'd' : 'off');
echo implode(' &nbsp;|&nbsp; ', $parts);
?></td>
</tr>
<tr>
<td>Last cleanup run <em class="tooltip">?<span>Cleanup runs once per hour from Automation and deletes in batches, so a first run on an old server catches up over several passes.</span></em></td>
<td><?php
if (is_array($cleanupInfo) && !empty($cleanupInfo['time'])) {
$r = isset($cleanupInfo['removed']) ? $cleanupInfo['removed'] : array();
echo date('d.m.Y H:i:s', (int) $cleanupInfo['time'])
. ' <span style="color:#777">(' . (int) ($r['reports'] ?? 0) . ' reports, '
. (int) ($r['chat'] ?? 0) . ' chat, '
. (int) ($r['messages'] ?? 0) . ' messages)</span>';
} else {
echo "<span class='badge blue'>Not run yet</span>";
}
?></td>
</tr>
<?php if ($cronCmd !== '') { ?>
<tr>
<td>Cron job command <em class="tooltip">?<span class="classic">Add this in cPanel &rarr; Cron Jobs. The path is detected from this installation.</span></em></td>
+50
View File
@@ -27,6 +27,13 @@ $cronLoop = defined('CRON_LOOP_SECONDS') ? (int) CRON_LOOP_SECONDS : 300;
$cronTick = defined('CRON_TICK_SECONDS') ? (int) CRON_TICK_SECONDS : 60;
$cronKey = defined('CRON_KEY') ? (string) CRON_KEY : '';
$cleanReports = defined('CLEANUP_REPORTS_DAYS') ? (int) CLEANUP_REPORTS_DAYS : 14;
$cleanChat = defined('CLEANUP_CHAT_DAYS') ? (int) CLEANUP_CHAT_DAYS : 7;
$cleanMessages = defined('CLEANUP_MESSAGES_DAYS') ? (int) CLEANUP_MESSAGES_DAYS : 0;
$cleanupMarker = __DIR__ . '/../../GameEngine/Prevention/cleanup_last.txt';
$cleanupInfo = @is_file($cleanupMarker) ? @json_decode((string) @file_get_contents($cleanupMarker), true) : null;
$cronPath = realpath(__DIR__ . '/../../cron.php');
$cronCmd = $cronPath ? '*/5 * * * * /usr/local/bin/php ' . $cronPath . ' >> ' . dirname(dirname($cronPath)) . '/cron.log 2>&1' : '';
?>
@@ -132,6 +139,49 @@ code{background:#f1f5f9;padding:2px 5px;border-radius:4px;font-size:11px;word-br
</table>
</div>
<div class="config-card">
<div class="config-head"><span>Database cleanup</span></div>
<table class="config-table">
<tr>
<td class="b">Last cleanup run</td>
<td><?php
if (is_array($cleanupInfo) && !empty($cleanupInfo['time'])) {
$r = isset($cleanupInfo['removed']) ? $cleanupInfo['removed'] : array();
echo date('d.m.Y H:i:s', (int) $cleanupInfo['time']);
echo ' <span style="color:#777">(removed: '
. (int) ($r['reports'] ?? 0) . ' reports, '
. (int) ($r['chat'] ?? 0) . ' chat, '
. (int) ($r['messages'] ?? 0) . ' messages)</span>';
} else {
echo "<span class='badge blue'>Not run yet</span>";
}
?>
<span class="hint">Runs from Automation once per hour. Rows are removed in batches, so the first run on an old server catches up over several passes.</span></td>
</tr>
<tr>
<td class="b">Battle reports (days)
<em class="tooltip">?<span>Unarchived reports older than this are deleted. Reports a player archived are never touched. 0 disables the rule.</span></em>
</td>
<td><input class="fm" type="number" name="cleanup_reports" min="0" max="3650" value="<?php echo $cleanReports; ?>" style="width:100px">
<span class="hint">0 = keep forever.</span></td>
</tr>
<tr>
<td class="b">Chat messages (days)
<em class="tooltip">?<span>The chat window only ever shows the last 13 messages per alliance, so older history is not used anywhere in the game.</span></em>
</td>
<td><input class="fm" type="number" name="cleanup_chat" min="0" max="3650" value="<?php echo $cleanChat; ?>" style="width:100px">
<span class="hint">0 = keep forever.</span></td>
</tr>
<tr>
<td class="b">Deleted messages (days)
<em class="tooltip">?<span>Only messages deleted by BOTH the sender and the recipient are removed &mdash; they are no longer visible to anyone in the game. Disabled by default.</span></em>
</td>
<td><input class="fm" type="number" name="cleanup_messages" min="0" max="3650" value="<?php echo $cleanMessages; ?>" style="width:100px">
<span class="hint">0 = disabled (default).</span></td>
</tr>
</table>
</div>
<div class="config-actions">
<a href="../Admin/admin.php?p=config" class="btn-back">&lsaquo; <?php echo EDIT_BACK ?></a>
<button type="submit" class="btn-save">
+18
View File
@@ -85,6 +85,24 @@ if (!function_exists('admin_config_template_path')) {
$text = str_replace('%CRONTICK%', (string) $cronTick, $text);
}
// Retentiile de curatenie: la fel, editate din ACP, deci orice alt mod
// care regenereaza config.php trebuie sa le pastreze.
$cleanupDefaults = array(
'%CLEANUPREPORTS%' => array('CLEANUP_REPORTS_DAYS', 14),
'%CLEANUPCHAT%' => array('CLEANUP_CHAT_DAYS', 7),
'%CLEANUPMESSAGES%' => array('CLEANUP_MESSAGES_DAYS', 0),
);
foreach ($cleanupDefaults as $placeholder => $info) {
if (strpos($text, $placeholder) === false) {
continue;
}
list($constant, $default) = $info;
$value = defined($constant) ? (int) constant($constant) : $default;
$text = str_replace($placeholder, (string) $value, $text);
}
return $text;
}
}
+24 -1
View File
@@ -59,6 +59,17 @@ if ($loop > 0 && $loop < $tick) {
$loop = 0;
}
// Retentiile de curatenie (0 = regula dezactivata). Limita superioara de 3650
// zile (10 ani) e doar ca sa nu se scrie valori absurde in config.
$cleanReports = isset($_POST['cleanup_reports']) ? (int) $_POST['cleanup_reports'] : 14;
$cleanChat = isset($_POST['cleanup_chat']) ? (int) $_POST['cleanup_chat'] : 7;
$cleanMessages = isset($_POST['cleanup_messages']) ? (int) $_POST['cleanup_messages'] : 0;
foreach (array('cleanReports', 'cleanChat', 'cleanMessages') as $var) {
if ($$var < 0) { $$var = 0; }
if ($$var > 3650) { $$var = 3650; }
}
$regenerateKey = !empty($_POST['regenerate_key']);
$cronKey = (defined('CRON_KEY') && CRON_KEY !== '' && CRON_KEY !== '%CRONKEY%')
@@ -115,6 +126,18 @@ if (!cron_set_define($config, 'CRON_KEY', "'" . addcslashes($cronKey, "'\\") . "
$missing[] = "define('CRON_KEY', '" . addcslashes($cronKey, "'\\") . "');";
}
if (!cron_set_define($config, 'CLEANUP_REPORTS_DAYS', (string) $cleanReports)) {
$missing[] = "define('CLEANUP_REPORTS_DAYS', " . $cleanReports . ");";
}
if (!cron_set_define($config, 'CLEANUP_CHAT_DAYS', (string) $cleanChat)) {
$missing[] = "define('CLEANUP_CHAT_DAYS', " . $cleanChat . ");";
}
if (!cron_set_define($config, 'CLEANUP_MESSAGES_DAYS', (string) $cleanMessages)) {
$missing[] = "define('CLEANUP_MESSAGES_DAYS', " . $cleanMessages . ");";
}
// Server instalat inainte ca aceste constante sa existe: le adaugam dupa
// AUTOMATION_LOCK_FILE_NAME (locul lor din template).
if (!empty($missing)) {
@@ -140,7 +163,7 @@ fwrite($fh, $config);
fclose($fh);
$database->query(
"Insert into " . TB_PREFIX . "admin_log values (0," . $id . ",'Changed Cron & Automation Settings'," . time() . ")"
"Insert into " . TB_PREFIX . "admin_log values (0," . $id . ",'Changed Cron, Automation & Cleanup Settings'," . time() . ")"
);
header("Location: ../../../Admin/admin.php?p=config");
+5 -1
View File
@@ -72,6 +72,7 @@ include_once __DIR__ . '/Automation/AutomationHero.php';
include_once __DIR__ . '/Automation/AutomationStarvation.php';
include_once __DIR__ . '/Automation/AutomationNatarsWW.php';
include_once __DIR__ . '/Automation/AutomationMedals.php';
include_once __DIR__ . '/Automation/AutomationCleanup.php';
class Automation {
// === Faza S2: metodele clasei, grupate pe domenii ===
@@ -89,6 +90,7 @@ class Automation {
use AutomationCleanup;
/**
* @var object The artifacts class, used to create Natars, artifacts and obtaining info about them
*/
@@ -130,7 +132,9 @@ class Automation {
"sendUnitsComplete", "loyaltyRegeneration", "sendreinfunitsComplete",
"returnunitsComplete", "sendSettlersComplete", "spawnNatars",
"spawnWWVillages", "spawnWWBuildingPlans", "activateArtifacts",
"heroAdventureComplete"];
"heroAdventureComplete",
// P3: curatenie periodica (are interval propriu, nu ruleaza la fiecare tick)
"cleanupOldData"];
foreach($methodsArrays as $method){
$file = fopen($autoprefix."GameEngine/Prevention/".$method.".txt", "w");
+141
View File
@@ -0,0 +1,141 @@
<?php
#################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ##
## Filename : AutomationCleanup.php ##
## Type : Automation - periodic database cleanup (P3 - performance) ##
## --------------------------------------------------------------------------- ##
## Scop : Tabelele care cresc nelimitat (rapoarte de lupta, chat, ##
## mesaje sterse de ambele parti) incetinesc in timp intreg ##
## serverul si umfla backup-urile. Aici le taiem periodic, ##
## dupa reguli de retentie configurabile. ##
## --------------------------------------------------------------------------- ##
## Reglaje in GameEngine/config.php (0 = dezactivat pentru fiecare): ##
## define('CLEANUP_REPORTS_DAYS', 14); // rapoarte NEARHIVATE mai vechi ##
## define('CLEANUP_CHAT_DAYS', 7); // mesaje de chat mai vechi ##
## define('CLEANUP_MESSAGES_DAYS', 0); // mesaje sterse de AMBELE parti ##
## define('CLEANUP_INTERVAL', 3600); // la cat timp ruleaza curatenia ##
## define('CLEANUP_BATCH', 5000); // randuri sterse per tabel/rulare ##
## --------------------------------------------------------------------------- ##
## Contact : cata7007@gmail.com ##
## Project : TravianZ ##
## GitHub : https://github.com/Shadowss/TravianZ ##
#################################################################################
trait AutomationCleanup {
/**
* Curatenie periodica in tabelele care cresc nelimitat.
*
* Rulata din lista de metode a constructorului, deci ar fi apelata la
* fiecare tick. Nu vrem asta: stergerile sunt scumpe, iar datele nu se
* strang atat de repede. De aceea metoda are propriul interval (implicit o
* data pe ora), tinut intr-un fisier marker din Prevention/.
*
* Stergerile sunt facute in LOTURI (LIMIT), ca sa nu blocam tabelele minute
* intregi la prima rulare pe un server vechi cu milioane de randuri: ce nu
* intra intr-o rulare se sterge la urmatoarele, pana se ajunge din urma.
*/
private function cleanupOldData() {
global $database;
$interval = defined('CLEANUP_INTERVAL') ? (int) CLEANUP_INTERVAL : 3600;
if ($interval < 60) {
$interval = 60;
}
$markerFile = __DIR__ . '/../Prevention/cleanup_last.txt';
$lastRun = 0;
if (@is_file($markerFile)) {
$raw = @json_decode((string) @file_get_contents($markerFile), true);
if (is_array($raw) && isset($raw['time'])) {
$lastRun = (int) $raw['time'];
}
}
if ($lastRun > 0 && (time() - $lastRun) < $interval) {
return;
}
$batch = defined('CLEANUP_BATCH') ? (int) CLEANUP_BATCH : 5000;
if ($batch < 100) { $batch = 100; }
if ($batch > 100000) { $batch = 100000; }
$removed = array(
'reports' => 0,
'chat' => 0,
'messages' => 0,
);
// --- Rapoarte de lupta ------------------------------------------------
// Se sterg doar cele NEARHIVATE: arhivarea e alegerea explicita a
// jucatorului de a pastra un raport, deci nu o calcam.
// Coloana `time` e indexata, deci stergerea e ieftina.
$reportDays = defined('CLEANUP_REPORTS_DAYS') ? (int) CLEANUP_REPORTS_DAYS : 14;
if ($reportDays > 0) {
$cutoff = time() - ($reportDays * 86400);
$q = "DELETE FROM " . TB_PREFIX . "ndata
WHERE archive = 0 AND `time` < " . $cutoff . "
LIMIT " . $batch;
if (mysqli_query($database->dblink, $q)) {
$removed['reports'] = mysqli_affected_rows($database->dblink);
}
}
// --- Chat --------------------------------------------------------------
// ATENTIE: coloana `date` e varchar, dar contine un timestamp unix scris
// de Chat::add_data() (time()). Comparatia se face pe valoarea numerica.
// Chat-ul afiseaza oricum doar ultimele 13 mesaje per alianta, deci
// istoricul vechi nu e folosit nicaieri.
$chatDays = defined('CLEANUP_CHAT_DAYS') ? (int) CLEANUP_CHAT_DAYS : 7;
if ($chatDays > 0) {
$cutoff = time() - ($chatDays * 86400);
$q = "DELETE FROM " . TB_PREFIX . "chat
WHERE (`date` + 0) < " . $cutoff . "
LIMIT " . $batch;
if (mysqli_query($database->dblink, $q)) {
$removed['chat'] = mysqli_affected_rows($database->dblink);
}
}
// --- Mesaje sterse de ambele parti -------------------------------------
// Implicit DEZACTIVAT (0). Un mesaj cu deltarget = 1 SI delowner = 1 nu
// mai e vizibil nici expeditorului, nici destinatarului, deci purjarea
// lui nu se vede in joc. Activeaza din config daca vrei.
$messageDays = defined('CLEANUP_MESSAGES_DAYS') ? (int) CLEANUP_MESSAGES_DAYS : 0;
if ($messageDays > 0) {
$cutoff = time() - ($messageDays * 86400);
$q = "DELETE FROM " . TB_PREFIX . "mdata
WHERE deltarget = 1 AND delowner = 1 AND `time` < " . $cutoff . "
LIMIT " . $batch;
if (mysqli_query($database->dblink, $q)) {
$removed['messages'] = mysqli_affected_rows($database->dblink);
}
}
// Marker + ultimul rezultat (citit de panoul de admin pentru afisare).
@file_put_contents(
$markerFile,
json_encode(array(
'time' => time(),
'removed' => $removed,
)),
LOCK_EX
);
}
}
+1 -1
View File
@@ -44,7 +44,7 @@ $currentYear = date('Y');
* Build version
* Ușor de modificat ulterior
*/
$serverVersion = 'v.10.0 Full Refactor&Redesign';
$serverVersion = 'v.11 Full Refactor';
?>
<!-- ===================== FOOTER ===================== -->
+12
View File
@@ -42,6 +42,18 @@ define('CRON_TICK_SECONDS', %CRONTICK%);
// Generata automat la instalare; pastrata la salvarile de configuratie din ACP.
define('CRON_KEY', '%CRONKEY%');
//////////////////////////////////
// ***** DATABASE CLEANUP *****//
//////////////////////////////////
// Tabelele care cresc nelimitat (rapoarte, chat, mesaje sterse) sunt curatate
// periodic de Automation. 0 = dezactivat pentru fiecare regula in parte.
// Rapoartele ARHIVATE de jucator nu se sterg niciodata.
define('CLEANUP_REPORTS_DAYS', %CLEANUPREPORTS%);
define('CLEANUP_CHAT_DAYS', %CLEANUPCHAT%);
define('CLEANUP_MESSAGES_DAYS', %CLEANUPMESSAGES%);
define('CLEANUP_INTERVAL', 3600);
define('CLEANUP_BATCH', 5000);
//////////////////////////////////
// ***** SERVER SETTINGS *****//
//////////////////////////////////
+5
View File
@@ -114,6 +114,11 @@ class Process {
// din ACP (Config -> Cron & Automation -> edit).
$findReplace["%CRONLOOP%"] = 300;
$findReplace["%CRONTICK%"] = 60;
// Retentii implicite pentru curatenia periodica din Automation
// (0 = regula dezactivata).
$findReplace["%CLEANUPREPORTS%"] = 14;
$findReplace["%CLEANUPCHAT%"] = 7;
$findReplace["%CLEANUPMESSAGES%"] = 0;
$findReplace["%DOMAIN%"] = $_POST['domain'];
$findReplace["%HOMEPAGE%"] = $_POST['homepage'];
$findReplace["%SERVER%"] = $_POST['server'];