Crop finder @lietuvis10

Fix crop_finder.php by @lietuvis10
This commit is contained in:
Catalin Novgorodschi
2025-10-23 10:27:45 +03:00
parent 1dde2599f3
commit db3d9bfff1
5 changed files with 651 additions and 192 deletions
+27
View File
@@ -612,6 +612,33 @@ class MYSQLi_DB implements IDbConnection {
public function is_connected() {
return ($this->dblink ? true : false);
}
/***************************
Function to calc oasis bonus
References: lietuvis10
***************************/
public function getBestOasisCropBonus($x, $y) {
$x = (int)$x;
$y = (int)$y;
// Adjust oasis type codes if your fork differs:
// - 50% crop only: type IN (12)
// - 25% crop (pure or mixed w/ wood/clay/iron): type IN (4,9,10,11)
$sql = "SELECT COALESCE(SUM(bonus), 0) AS total FROM (SELECT CASE
WHEN o.type IN (12) THEN 50 WHEN o.type IN (4,9,10,11) THEN 25 ELSE 0
END AS bonus FROM " . TB_PREFIX . "wdata w JOIN " . TB_PREFIX . "odata o ON o.wref = w.id
WHERE w.fieldtype = 0 AND ABS(w.x - $x) <= 3 AND ABS(w.y - $y) <= 3
AND o.type IN (12,4,9,10,11) -- only crop-giving oases ORDER BY bonus DESC LIMIT 3) t";
$q = mysqli_query($this->dblink, $sql);
$row = mysqli_fetch_assoc($q);
$total = (int)($row['total'] ?? 0);
if ($total > 150) $total = 150; // safety cap
return $total;
}
/***************************
Function to process MYSQLi->fetch_all (Only exist in MYSQL)
+1
View File
@@ -44,6 +44,7 @@ div.c1 {text-align: center}
} ?> <?php if($session->access == ADMIN) {
echo "<a href=\"Admin/admin.php\"><font color=\"Red\">".ADMIN_PANEL."</font></a>";
echo "<a href=\"massmessage.php\">".MASS_MESSAGE."</a>";
echo '<a href="build_croppers.php">Build Cropper</a>';
echo "<a href=\"sysmsg.php\">".SYSTEM_MESSAGE."</a>";
} ?> <a href="logout.php"><?php echo LOGOUT;?></a></p>
<p>
+348
View File
@@ -0,0 +1,348 @@
<?php
/**
* crop_builder.php
* Admin UI to build/rebuild the precomputed croppers table (9c/15c + best oasis bonus).
* The UI is rendered inside the pages main content area with normal “player” styling.
*/
use App\Utils\AccessLogger;
include_once("GameEngine/Generator.php");
$start_timer = $generator->pageLoadTimeStart();
include_once("GameEngine/Session.php");
include_once("GameEngine/config.php");
include_once("GameEngine/Database.php");
include_once("GameEngine/Village.php");
AccessLogger::logRequest();
// ---------- Admin gate ----------
if (!isset($session) || !isset($session->access) || (int)$session->access < 8) {
header('Location: dorf1.php');
exit;
}
// ---------- Setup ----------
@session_start();
$TBP = defined('TB_PREFIX') ? TB_PREFIX : 's1_';
$CROP_TABLE = $TBP . 'croppers';
$WDATA = $TBP . 'wdata';
// Build an absolute-safe asset prefix for CSS/JS
$assetBase = $session->gpack ?: GP_LOCATE;
$assetBase = '/'.ltrim($assetBase, '/');
// CSRF
if (empty($_SESSION['csrf_cb'])) {
$_SESSION['csrf_cb'] = bin2hex(random_bytes(16));
}
$csrf = $_SESSION['csrf_cb'];
function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
// Ensure table exists (minimal schema, unsigned tinyints)
mysqli_query($database->dblink, "CREATE TABLE IF NOT EXISTS `$CROP_TABLE` (
`wref` INT UNSIGNED NOT NULL PRIMARY KEY,
`x` INT NOT NULL,
`y` INT NOT NULL,
`fieldtype` TINYINT UNSIGNED NOT NULL,
`best_oasis_bonus` TINYINT UNSIGNED NOT NULL,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CHECK (`best_oasis_bonus` IN (0,25,50,75,100,125,150))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
// Helpful indexes (ignore errors if already exist)
@mysqli_query($database->dblink, "CREATE INDEX `idx_ft_bonus_xy` ON `$CROP_TABLE` (`fieldtype`, `best_oasis_bonus`, `x`, `y`)");
@mysqli_query($database->dblink, "CREATE INDEX `idx_xy` ON `$CROP_TABLE` (`x`, `y`)");
@mysqli_query($database->dblink, "CREATE INDEX `idx_bonus` ON `$CROP_TABLE` (`best_oasis_bonus`)");
// ---------- Helpers ----------
function worldSizeLabel(): string {
if (defined('WORLD_MIN') && defined('WORLD_MAX')) {
$min = (int)WORLD_MIN; $max = (int)WORLD_MAX;
return ($max - $min + 1) . "×" . ($max - $min + 1) . " (" . $min . " .. " . $max . ")";
}
if (defined('WORLD_MAX')) {
$max = (int)WORLD_MAX; $min = -$max;
return ($max - $min + 1) . "×" . ($max - $min + 1) . " (" . $min . " .. " . $max . ")";
}
return "unknown";
}
function getCounts($db, $WDATA, $CROP_TABLE) {
$c1 = mysqli_fetch_assoc(mysqli_query($db, "SELECT COUNT(*) c FROM `$WDATA` WHERE fieldtype IN (1,6)"));
$c2 = mysqli_fetch_assoc(mysqli_query($db, "SELECT COUNT(*) c FROM `$CROP_TABLE`"));
$lu = mysqli_fetch_assoc(mysqli_query($db, "SELECT MAX(updated_at) lu FROM `$CROP_TABLE`"));
return [
'croppers_world' => (int)($c1['c'] ?? 0),
'croppers_table' => (int)($c2['c'] ?? 0),
'last_updated' => $lu['lu'] ?? null,
];
}
// stream log
function startStreaming() {
@ini_set('output_buffering','off');
@ini_set('zlib.output_compression', 0);
while (ob_get_level()) { @ob_end_flush(); }
ob_implicit_flush(true);
echo "<pre id=\"log\" style=\"background:#0b0f17;color:#d7e1f8;padding:12px;border-radius:12px;max-height:60vh;overflow:auto;\">";
echo htmlspecialchars("[".date('H:i:s')."] Croppers builder started")."\n";
flush();
}
function logLine($msg) { echo htmlspecialchars("[".date('H:i:s')."] ".$msg)."\n"; flush(); }
function endStreaming() { echo "</pre>"; flush(); }
// ---------- Actions ----------
$action = $_POST['action'] ?? null;
$okCsrf = isset($_POST['csrf']) && hash_equals($_SESSION['csrf_cb'], $_POST['csrf']);
$notice = null;
if ($action && !$okCsrf) {
$notice = "Invalid CSRF token. Please reload the page.";
$action = null;
}
if ($action === 'truncate') {
mysqli_query($database->dblink, "TRUNCATE TABLE `$CROP_TABLE`");
$notice = "Croppers table truncated.";
}
if ($action === 'reindex') {
@mysqli_query($database->dblink, "DROP INDEX `idx_ft_bonus_xy` ON `$CROP_TABLE`");
@mysqli_query($database->dblink, "DROP INDEX `idx_xy` ON `$CROP_TABLE`");
@mysqli_query($database->dblink, "DROP INDEX `idx_bonus` ON `$CROP_TABLE`");
@mysqli_query($database->dblink, "CREATE INDEX `idx_ft_bonus_xy` ON `$CROP_TABLE` (`fieldtype`, `best_oasis_bonus`, `x`, `y`)");
@mysqli_query($database->dblink, "CREATE INDEX `idx_xy` ON `$CROP_TABLE` (`x`, `y`)");
@mysqli_query($database->dblink, "CREATE INDEX `idx_bonus` ON `$CROP_TABLE` (`best_oasis_bonus`)");
$notice = "Indexes rebuilt.";
}
if ($action === 'estimate') {
$notice = "Estimated counts refreshed.";
}
$stats = getCounts($database->dblink, $WDATA, $CROP_TABLE);
$worldLabel = worldSizeLabel();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php echo SERVER_NAME ?> - Mass Message</title>
<link rel="shortcut icon" href="favicon.ico"/>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="mt-full.js?0ac37" type="text/javascript"></script>
<script src="unx.js?f4b7h" type="text/javascript"></script>
<script src="new.js?0ac37" type="text/javascript"></script>
<link href="<?php echo GP_LOCATE; ?>lang/en/lang.css?f4b7d" rel="stylesheet" type="text/css" />
<link href="<?php echo GP_LOCATE; ?>lang/en/compact.css?f4b7i" rel="stylesheet" type="text/css" />
<?php
if($session->gpack == null || GP_ENABLE == false) {
echo "
<link href='".GP_LOCATE."travian.css?e21d2' rel='stylesheet' type='text/css' />
<link href='".GP_LOCATE."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
} else {
echo "
<link href='".$session->gpack."travian.css?e21d2' rel='stylesheet' type='text/css' />
<link href='".$session->gpack."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
}
?>
<script language="javascript" type="text/javascript">
function smilie(text) {
document.myform.message.value += text;
}
</script>
<script language="javascript">
function toggleDisplay(e){
element = document.getElementById(e).style;
element.display == 'none' ? element.display = 'block' :
element.display='none';
}
</script>
<script type="text/javascript">
window.addEvent('domready', start);
</script>
<?php
if($session->gpack == null || GP_ENABLE == false) {
echo "
<link href='".GP_LOCATE."travian.css?e21d2' rel='stylesheet' type='text/css' />
<link href='".GP_LOCATE."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
} else {
echo "
<link href='".$session->gpack."travian.css?e21d2' rel='stylesheet' type='text/css' />
<link href='".$session->gpack."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
}
?>
<script type="text/javascript">
window.addEvent('domready', start);
</script>
<style>
.cb-container{ max-width:980px;margin:0 auto;padding:0 12px; }
.cb-grid{ display:grid; grid-template-columns:1fr 1fr; gap:12px; }
.cb-card{ background:#fff;border:1px solid #dcdde1;border-radius:12px;padding:14px; box-shadow:0 1px 2px rgba(0,0,0,.04); }
.cb-title{ font-size:20px;margin:0 0 8px 0; }
.cb-muted{ color:#666;font-size:12px; }
.cb-kpis{ display:flex; gap:16px; margin-top:8px; flex-wrap:wrap; }
.cb-kpi{ background:#f7f9fb;border-radius:10px;padding:10px 12px; border:1px solid #e6ecf3; }
.cb-actions form{ display:flex; flex-wrap:wrap; gap:8px; align-items:center; }
.cb-btn{ background:#2f7d32;color:#fff;border:none;border-radius:10px;padding:10px 14px;cursor:pointer; }
.cb-btn.red{ background:#b23b3b; }
.cb-btn.gray{ background:#5c6b7a; }
.cb-input{ padding:8px 10px;border:1px solid #c9d3df;border-radius:10px;width:120px; }
.cb-note{ background:#fff4cc;border:1px solid #f5d36b;border-radius:10px;padding:10px 12px;margin-top:8px; }
.cb-notice{ background:#e8f6ff;border:1px solid #b3e0ff;color:#0b5380;border-radius:10px;padding:10px 12px;margin:12px 0; }
@media (max-width: 900px){ .cb-grid{ grid-template-columns:1fr; } }
pre#log { margin-top:14px; }
</style>
</head>
<body class="v35 ie ie8">
<div class="wrapper">
<img style="filter:chroma();" src="img/x.gif" id="msfilter" alt="" />
<div id="dynamic_header"></div>
<?php include("Templates/header.tpl"); ?>
<div id="mid">
<?php include("Templates/menu.tpl"); ?>
<!-- IMPORTANT: Use the normal game content style instead of "login" -->
<div id="content" class="player">
<div class="cb-container">
<h1 style="text-align:center;margin:12px 0 16px;">Build crop finder</h1>
<?php if ($notice): ?>
<div class="cb-notice"><?php echo h($notice); ?></div>
<?php endif; ?>
<div class="cb-grid">
<div class="cb-card">
<h3 class="cb-title">Status</h3>
<div class="cb-muted">World: <?php echo h($worldLabel); ?></div>
<div class="cb-kpis">
<div class="cb-kpi"><b>9c/15c in map:</b> <?php echo number_format($stats['croppers_world']); ?></div>
<div class="cb-kpi"><b>Rows in table:</b> <?php echo number_format($stats['croppers_table']); ?></div>
<div class="cb-kpi"><b>Last updated:</b> <?php echo $stats['last_updated'] ? h($stats['last_updated']) : '—'; ?></div>
</div>
<div class="cb-note">
The croppers table stores only <b>wref,x,y,fieldtype,best_oasis_bonus</b>.
Ownership and occupied status are pulled live from <code>vdata/users</code> by the finder.
</div>
</div>
<div class="cb-card cb-actions">
<h3 class="cb-title">Actions</h3>
<form method="post">
<input type="hidden" name="csrf" value="<?php echo h($csrf); ?>" />
<label>Batch size:</label>
<input class="cb-input" type="number" min="1000" max="20000" step="1000" name="batch" value="<?php echo isset($_POST['batch']) ? (int)$_POST['batch'] : 5000; ?>" />
<button class="cb-btn" name="action" value="build">Build / Rebuild</button>
<button class="cb-btn gray" name="action" value="estimate" type="submit">Estimate</button>
<button class="cb-btn gray" name="action" value="reindex" type="submit">Reindex</button>
<button class="cb-btn red" name="action" value="truncate" type="submit" onclick="return confirm('Really truncate the table?');">Truncate</button>
</form>
<div class="cb-muted" style="margin-top:8px;">
Building streams progress below. You can leave this page; the process stops when the request ends.
</div>
</div>
</div>
<?php
// Stream build logs INSIDE the content area
if ($action === 'build' && $okCsrf) {
$batch = max(1000, min(20000, (int)($_POST['batch'] ?? 5000)));
startStreaming();
$cnt = mysqli_fetch_assoc(mysqli_query($database->dblink, "SELECT COUNT(*) AS c FROM `$WDATA` WHERE `fieldtype` IN (1,6)"));
$target = (int)($cnt['c'] ?? 0);
logLine("Detected $target croppers.");
$offset = 0; $total = 0;
while (true) {
$sql = "SELECT id AS wref, x, y, fieldtype
FROM `$WDATA`
WHERE `fieldtype` IN (1,6)
LIMIT $offset, $batch";
$res = mysqli_query($database->dblink, $sql);
if (!$res) { logLine('Query failed: '.mysqli_error($database->dblink)); break; }
$rows = [];
while ($r = mysqli_fetch_assoc($res)) { $rows[] = $r; }
if (!$rows) break;
$values = [];
foreach ($rows as $r) {
$x = (int)$r['x']; $y = (int)$r['y'];
$bonus = (int)$database->getBestOasisCropBonus($x, $y);
if (!in_array($bonus, [0,25,50,75,100,125,150], true)) {
$bonus = max(0, min(150, $bonus));
}
$values[] = sprintf("(%d,%d,%d,%d,%d)",
(int)$r['wref'], $x, $y, (int)$r['fieldtype'], $bonus);
}
if ($values) {
$sql = "REPLACE INTO `$CROP_TABLE`
(`wref`,`x`,`y`,`fieldtype`,`best_oasis_bonus`)
VALUES ".implode(',', $values);
if (!mysqli_query($database->dblink, $sql)) {
logLine('Upsert failed: '.mysqli_error($database->dblink));
break;
}
}
$countThis = count($rows);
$total += $countThis;
$offset += $batch;
logLine("Processed $total / $target");
}
@mysqli_query($database->dblink, "ANALYZE TABLE `$CROP_TABLE`");
logLine("Analyze complete.");
logLine("Done.");
endStreaming();
// Refresh stats after build
$stats = getCounts($database->dblink, $WDATA, $CROP_TABLE);
}
?>
</div>
</div>
<br /><br /><br /><br />
<div id="side_info">
<?php
include("Templates/multivillage.tpl");
include("Templates/quest.tpl");
include("Templates/news.tpl");
if(!NEW_FUNCTIONS_DISPLAY_LINKS) {
echo "<br><br><br><br>";
include("Templates/links.tpl");
}
?>
</div>
<div class="clear"></div>
</div>
<div class="footer-stopper"></div>
<div class="clear"></div>
<?php include("Templates/footer.tpl"); include("Templates/res.tpl"); ?>
<div id="stime">
<div id="ltime">
<div id="ltimeWrap">
<?php echo CALCULATED_IN;?> <b><?php echo round(($generator->pageLoadTimeEnd()-$start_timer)*1000); ?></b> ms
<br /><?php echo SERVER_TIME;?> <span id="tp1" class="b"><?php echo date('H:i:s'); ?></span>
</div>
</div>
</div>
<div id="ce"></div>
</div>
</body>
</html>
+252 -190
View File
@@ -1,220 +1,291 @@
<?php
// crop_finder_fast.php — fast crop finder using precomputed <TB_PREFIX>croppers
// Fixes:
// - Uses WORLD_MIN/WORLD_MAX from config if present (works for 0..N or -W..+W)
// - If not defined in config, auto-detects bounds from DB
// - Larger window cap + extra expansion
// - Global fallback so it never returns only a few rows
include_once("GameEngine/Generator.php");
$start_timer = $generator->pageLoadTimeStart();
include_once("GameEngine/config.php");
use App\Utils\AccessLogger;
include_once("GameEngine/Village.php");
AccessLogger::logRequest();
/*-------------------------------------------------------*\
| ********* DO NOT REMOVE THIS COPYRIGHT NOTICE ********* |
+---------------------------------------------------------+
| Developed by: Manni < manuel_mannhardt@web.de > |
| Dzoki < dzoki.travian@gmail.com > |
| Copyright: TravianX Project All rights reserved |
\*-------------------------------------------------------*/
if ($session->goldclub == 0) { header("Location: plus.php?id=3"); exit; }
use App\Utils\AccessLogger;
// Tables
$TBP = defined('TB_PREFIX') ? TB_PREFIX : 's1_';
$CROP_TABLE = $TBP . 'croppers';
$VDATA = $TBP . 'vdata';
$USERS = $TBP . 'users';
include_once("GameEngine/Village.php");
AccessLogger::logRequest();
$RENDER_MAX = 100;
if($session->goldclub == 0) {
header("Location: plus.php?id=3");
exit;
}
// ---------- POST -> GET ----------
if (!empty($_POST['type'])) {
$x = isset($_POST['x']) ? preg_replace("/[^0-9-]/", "", $_POST['x']) : '0';
$y = isset($_POST['y']) ? preg_replace("/[^0-9-]/", "", $_POST['y']) : '0';
$b = isset($_POST['bonus_getreide']) ? preg_replace("/[^0-9a-zA-Z]/", "", $_POST['bonus_getreide']) : 'all';
if ($_POST['type'] == 15) header("Location: " . $_SERVER['PHP_SELF'] . "?s=1&x=$x&y=$y&b=$b");
elseif ($_POST['type'] == 9) header("Location: " . $_SERVER['PHP_SELF'] . "?s=2&x=$x&y=$y&b=$b");
else header("Location: " . $_SERVER['PHP_SELF'] . "?s=3&x=$x&y=$y&b=$b");
exit;
}
if ( !empty( $_POST['type'] ) ) {
if ( $_POST['type'] == 15 ) {
header( "Location: " . $_SERVER['PHP_SELF'] . "?s=1&x=" . preg_replace( "/[^a-zA-Z0-9_-]/", "", $_POST['x'] ) . '&y=' . preg_replace( "/[^a-zA-Z0-9_-]/", "", $_POST['y'] ) );
exit;
} elseif ( $_POST['type'] == 9 ) {
header( "Location: " . $_SERVER['PHP_SELF'] . "?s=2&x=" . preg_replace( "/[^a-zA-Z0-9_-]/", "", $_POST['x'] ) . '&y=' . preg_replace( "/[^a-zA-Z0-9_-]/", "", $_POST['y'] ) );
exit;
} elseif ( $_POST['type'] == 'both' ) {
header( "Location: " . $_SERVER['PHP_SELF'] . "?s=3&x=" . preg_replace( "/[^a-zA-Z0-9_-]/", "", $_POST['x'] ) . '&y=' . preg_replace( "/[^a-zA-Z0-9_-]/", "", $_POST['y'] ) );
exit;
}
}
// ---------- Helpers ----------
/**
* Wrap-aware BETWEEN that works for arbitrary [min..max] (e.g. 0..49 or -300..300)
*/
function betweenWrapFlexible($col, $center, $R, $min, $max) {
$span = $max - $min + 1;
$lo = $center - $R;
$hi = $center + $R;
// normalize to [min,max]
$norm = function($v) use ($min,$span) {
$n = ($v - $min) % $span;
if ($n < 0) $n += $span;
return $min + $n;
};
$loN = $norm($lo);
$hiN = $norm($hi);
if ($loN <= $hiN) return "($col BETWEEN $loN AND $hiN)";
return "(($col BETWEEN $min AND $hiN) OR ($col BETWEEN $loN AND $max))";
}
// ---------- Coordinates ----------
if (!empty($_GET['x']) && !empty($_GET['y']) && is_numeric($_GET['x']) && is_numeric($_GET['y'])) {
$coor2 = ['x'=>(int)$_GET['x'], 'y'=>(int)$_GET['y']];
} else {
$wref2 = $village->wid;
$coor2 = $database->getCoor($wref2);
}
$startX = isset($coor2['x']) ? (int)$coor2['x'] : 0;
$startY = isset($coor2['y']) ? (int)$coor2['y'] : 0;
// ---------- UI selections ----------
// UI selections
$selBonus = isset($_GET['b']) ? $_GET['b'] : 'all';
$selType = (!empty($_GET['s'])) ? (int)$_GET['s'] : 0;
$minBonus = ($selBonus !== 'all') ? (int)$selBonus : null;
$fieldWhere = ($selType === 1) ? "6" : (($selType === 2) ? "1" : "1,6");
$bonusCond = is_null($minBonus) ? "1" : ("c.best_oasis_bonus >= ".(int)$minBonus);
// Only run the queries after the user pressed Search (i.e., we have s, x, y in the URL)
$rows = [];
$out = [];
$searchTriggered = isset($_GET['s']) && isset($_GET['x']) && isset($_GET['y']);
if ($searchTriggered) {
// --- Windowed fetch with bigger cap and one more expansion ---
$R = 40; // start radius
$tries= 0;
$CAP = 2000; // increased cap per window
do {
$tries++;
$condX = betweenWrapFlexible('c.x', $startX, $R, $MIN_X, $MAX_X);
$condY = betweenWrapFlexible('c.y', $startY, $R, $MIN_Y, $MAX_Y);
$sql = "SELECT c.wref, c.x, c.y, c.fieldtype, c.best_oasis_bonus
FROM `$CROP_TABLE` c
WHERE c.fieldtype IN ($fieldWhere) AND $bonusCond AND $condX AND $condY
LIMIT $CAP";
$res = mysqli_query($database->dblink, $sql);
$rows = [];
if ($res) {
while ($r = mysqli_fetch_assoc($res)) {
$r['__dist'] = $database->getDistance($startX, $startY, (int)$r['x'], (int)$r['y']);
$rows[] = $r;
}
}
if (count($rows) < $RENDER_MAX && $tries < 4) { // 40 -> 80 -> 160 -> 320
$R *= 2;
} else {
break;
}
} while (true);
// --- Global fallback if window was too sparse ---
if (count($rows) < $RENDER_MAX) {
$sql = "SELECT c.wref, c.x, c.y, c.fieldtype, c.best_oasis_bonus
FROM `$CROP_TABLE` c
WHERE c.fieldtype IN ($fieldWhere) AND $bonusCond
LIMIT 5000";
$res = mysqli_query($database->dblink, $sql);
$rows = [];
if ($res) {
while ($r = mysqli_fetch_assoc($res)) {
$r['__dist'] = $database->getDistance($startX, $startY, (int)$r['x'], (int)$r['y']);
$rows[] = $r;
}
}
}
// Sort by distance and keep first RENDER_MAX
usort($rows, function($a,$b){ return $a['__dist'] <=> $b['__dist']; });
$out = array_slice($rows, 0, $RENDER_MAX);
}
// Live owner info for visible rows
$wrefs = array_map(function($r){ return (int)$r['wref']; }, $out);
$owners = [];
if ($wrefs) {
$in = implode(',', array_unique($wrefs));
$sql = "SELECT v.wref, v.name AS vname, v.owner AS owner_id, u.username
FROM `$VDATA` v
JOIN `$USERS` u ON u.id = v.owner
WHERE v.wref IN ($in)";
$res = mysqli_query($database->dblink, $sql);
if ($res) while ($row = mysqli_fetch_assoc($res)) { $owners[(int)$row['wref']] = $row; }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php
echo SERVER_NAME
?> - Crop Finder</title>
<link rel="shortcut icon" href="favicon.ico"/>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="mt-full.js?0faab" type="text/javascript"></script>
<script src="unx.js?f4b7h" type="text/javascript"></script>
<script src="new.js?0faab" type="text/javascript"></script>
<link href="<?php
echo GP_LOCATE;
?>lang/en/lang.css?f4b7d" rel="stylesheet" type="text/css" />
<link href="<?php
echo GP_LOCATE;
?>lang/en/compact.css?f4b7i" rel="stylesheet" type="text/css" />
<?php
if($session->gpack == null || GP_ENABLE == false) {
echo "
<link href='".GP_LOCATE."travian.css?e21d2' rel='stylesheet' type='text/css' />
<link href='".GP_LOCATE."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
}
else {
echo "
<link href='".$session->gpack."travian.css?e21d2' rel='stylesheet' type='text/css' />
<link href='".$session->gpack."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
}
?>
<script type="text/javascript">
window.addEvent('domready', start);
</script>
<title><?php echo SERVER_NAME ?> - Crop Finder</title>
<link rel="shortcut icon" href="favicon.ico"/>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="mt-full.js?0faab" type="text/javascript"></script>
<script src="unx.js?f4b7h" type="text/javascript"></script>
<script src="new.js?0faab" type="text/javascript"></script>
<link href="<?php echo GP_LOCATE; ?>lang/en/lang.css?f4b7d" rel="stylesheet" type="text/css" />
<link href="<?php echo GP_LOCATE; ?>lang/en/compact.css?f4b7i" rel="stylesheet" type="text/css" />
<?php if($session->gpack == null || GP_ENABLE == false) {
echo " <link href='".GP_LOCATE."travian.css?e21d2' rel='stylesheet' type='text/css' />";
echo " <link href='".GP_LOCATE."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
} else {
echo " <link href='".$session->gpack."travian.css?e21d2' rel='stylesheet' type='text/css' />";
echo " <link href='".$session->gpack."lang/en/lang.css?e21d2' rel='stylesheet' type='text/css' />";
} ?>
<script type="text/javascript">window.addEvent('domready', start);</script>
</head>
<body class="v35 ie ie8">
<div class="wrapper">
<img style="filter:chroma();" src="img/x.gif" id="msfilter" alt="" />
<div id="dynamic_header">
</div>
<?php
include ("Templates/header.tpl");
?>
<div id="dynamic_header"></div>
<?php include ("Templates/header.tpl"); ?>
<div id="mid">
<?php
include ("Templates/menu.tpl");
if( !empty( $_GET['x'] ) && !empty( $_GET['y'] ) && is_numeric($_GET['x']) && is_numeric($_GET['y']) ) {
$coor2['x'] = preg_replace("/[^a-zA-Z0-9_-]/","",$_GET['x']);
$coor2['y'] = preg_replace("/[^a-zA-Z0-9_-]/","",$_GET['y']);
} else {
$wref2 = $village->wid;
$coor2 = $database->getCoor($wref2);
}
?>
<div id="content" class="player">
<?php include ("Templates/menu.tpl"); ?>
<div id="content" class="player">
<h1>Crop Finder</h1>
<div style="text-align: center">
<img width="200" src="gpack/travian_default/img/g/f6.jpg" />
<img width="200" src="gpack/travian_default/img/g/f1.jpg" />
<img width="200" src="gpack/travian_default/img/g/f6.jpg" />
<img width="200" src="gpack/travian_default/img/g/f1.jpg" />
</div>
<br /><br />
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?s" method="post">
<table>
<tr>
<td width="100">Cropper Type:</td>
<td width="250">
<input type="radio" class="radio" name="type" value="15" <?php if ( !empty( $_GET['s'] ) && $_GET['s'] == 1 ) echo 'checked="checked"'; ?> /> 15 crop
<input type="radio" class="radio" name="type" value="9" <?php if ( !empty( $_GET['s'] ) && $_GET['s'] == 2 ) echo 'checked="checked"'; ?> /> 9 crop
<input type="radio" class="radio" name="type" value="both" <?php if ( !empty( $_GET['s'] ) && $_GET['s'] == 3 ) echo 'checked="checked"'; ?> /> both<br />
</td>
</tr>
<tr>
<td width="100">Oasis Crop Bonus (at least):</td>
<td>
<select class="dropdown" name="bonus_getreide">
<option value="all" selected="selected">either</option>
<option value="25" >+25%</option>
<option value="50" >+50%</option>
<option value="75" >+75%</option>
<option value="100" >+100%</option>
<option value="125" >+125%</option>
<option value="150" >+150%</option>
</select>
</td>
</tr>
<tr>
<td>Startposition:</td>
<td>x: <input type="text" name="x" value="<?php print $coor2['x']; ?>" size="3" /> y: <input type="text" name="y" value="<?php print $coor2['y']; ?>" size="3" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" class="trav_buttons" value="Search">Search</button></td>
</tr>
</table>
<table>
<tr>
<td width="120">Cropper Type:</td>
<td width="400">
<input type="radio" class="radio" name="type" value="15" <?php if ($selType === 1) echo 'checked="checked"'; ?> /> 15 crop
<input type="radio" class="radio" name="type" value="9" <?php if ($selType === 2) echo 'checked="checked"'; ?> /> 9 crop
<input type="radio" class="radio" name="type" value="both" <?php if ($selType === 3 || $selType === 0) echo 'checked="checked"'; ?> /> both<br />
</td>
</tr>
<tr>
<td>Oasis Crop Bonus (min):</td>
<td>
<select class="dropdown" name="bonus_getreide">
<option value="all" <?php if($selBonus==='all') echo 'selected="selected"'; ?>>either</option>
<option value="25" <?php if($selBonus==='25') echo 'selected="selected"'; ?>>+25%</option>
<option value="50" <?php if($selBonus==='50') echo 'selected="selected"'; ?>>+50%</option>
<option value="75" <?php if($selBonus==='75') echo 'selected="selected"'; ?>>+75%</option>
<option value="100" <?php if($selBonus==='100') echo 'selected="selected"'; ?>>+100%</option>
<option value="125" <?php if($selBonus==='125') echo 'selected="selected"'; ?>>+125%</option>
<option value="150" <?php if($selBonus==='150') echo 'selected="selected"'; ?>>+150%</option>
</select>
</td>
</tr>
<tr>
<td>Start position:</td>
<td>x: <input type="text" name="x" value="<?php print $startX; ?>" size="4" />
y: <input type="text" name="y" value="<?php print $startY; ?>" size="4" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" class="trav_buttons" value="Search">Search</button></td>
</tr>
</table>
</form>
<?php
$fieldType = ( !empty( $_GET['s'] ) && $_GET['s'] == 1) ? "fieldtype = 6" : ( ( !empty( $_GET['s'] ) && $_GET['s'] == 2 ) ? "fieldtype = 1" : "fieldtype = 1 OR fieldtype = 6");
$type = mysqli_query($database->dblink,"SELECT id, x, y, occupied, fieldtype FROM ".TB_PREFIX."wdata WHERE $fieldType");
if ( !empty( $_GET['x'] ) && is_numeric($_GET['x']) && !empty( $_GET['y'] ) && is_numeric($_GET['y'])) {
$coor['x'] = $_GET['x'];
$coor['y'] = $_GET['y'];
} else {
$wref = $village->wid;
$coor = $database->getCoor($wref);
// Debug panel (toggle with ?debug=1)
if (!empty($_GET['debug'])) {
echo "<div style='margin:8px 0;padding:6px 8px;background:#fffbe6;border:1px solid #ffe58f;border-radius:8px'>";
echo "<strong>Debug:</strong> bounds=[$MIN_X..$MAX_X]x[$MIN_Y..$MAX_Y], R=$R, tries=$tries, fetched=".count($rows).", render=".count($out).", type={$selType}, minBonus=".($minBonus??'all');
echo "</div>";
}
if ( !empty( $_GET['s'] ) && $_GET['s'] >= 1 && $_GET['s'] <= 3 ) {
?>
<?php if ($searchTriggered) { ?>
<table id="member">
<thead>
<tr>
<th colspan='6'>Crop Finder - 9c and 15c</th>
</tr>
<tr>
<td>Type</td>
<td>Coordinates</td>
<td>Owner</td>
<td>Occupied</td>
<td>Distance</td>
<td>Oasis</td>
</tr>
</thead><tbody>
<thead>
<tr><th colspan='6'>Crop Finder - <?php if($selType === 1) echo '15c'; elseif($selType === 2) echo '9c'; else echo '9c and 15c'; if($selBonus==='all') echo ' and any bonus'; elseif($selBonus==='25') echo ' and atleast 25% bonus';
elseif($selBonus==='50') echo ' and atleast 50% bonus'; elseif($selBonus==='75') echo ' and atleast 75% bonus'; elseif($selBonus==='100') echo ' and atleast 100% bonus'; elseif($selBonus==='125') echo ' and atleast 125% bonus'; else echo ' and atleast 150% bonus';
?></th></tr>
<tr>
<td>Type</td>
<td>Coordinates</td>
<td>Owner</td>
<td>Occupied</td>
<td>Distance</td>
<td>Oasis</td>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($type)) {
$dist = $database->getDistance($coor['x'], $coor['y'], $row['x'], $row['y']);
$rows[$dist] = $row;
}
ksort($rows);
foreach($rows as $row) {
$field = $row['fieldtype'] == 1 ? '9c' : '15c';
echo "<tr><td>" . $field . "</td>";
if($row['occupied'] == 0) {
echo "<td><a href=\"karte.php?d=".$row['id']."&c=".$generator->getMapCheck($row['id'])."\">".ABANDVALLEY." (".$row['x']."|".$row['y'].")</td>";
echo "<td>-</td>";
echo "<td><b><font color=\"green\">".UNOCCUPIED."</b></font></td>";
} else {
echo "<td><a href=\"karte.php?d=".$row['id']."&c=".$generator->getMapCheck($row['id'])."\">".$database->getVillageField($row['id'], "name")." (".$row['x']."|".$row['y'].")</a></td>";
echo "<td><a href=\"spieler.php?uid=".$database->getVillageField($row['id'], "owner")."\">".$database->getUserField($database->getVillageField($row['id'], "owner"), "username", 0)."</a></td>";
echo "<td><b><font color=\"red\">".OCCUPIED."</b></font></td>";
}
echo "<td><div style=\"text-align: center\">".$database->getDistance($coor['x'], $coor['y'], $row['x'], $row['y'])."</div></td>";
echo "<td>-</td>";
if (empty($out)) {
echo "<tr><td colspan='6' style='text-align:center; padding:10px;'>
<em style=\"color:#999;\">No crops fields found for the selected filters.</em>
</td></tr>";
} else {
foreach ($out as $row) {
$field = ($row['fieldtype'] == 1) ? '9c' : '15c';
$x=(int)$row['x']; $y=(int)$row['y']; $id=(int)$row['wref'];
$ov = $owners[$id] ?? null;
$isOcc = $ov !== null;
echo "<tr><td>$field</td>";
if (!$isOcc) {
echo "<td><a href=\"karte.php?d=".$id."&c=".$generator->getMapCheck($id)."\">".ABANDVALLEY." ($x|$y)</a></td>";
echo "<td>-</td>";
echo "<td><b><font color=\"green\">".UNOCCUPIED."</font></b></td>";
} else {
$vname = htmlspecialchars($ov['vname'] ?? '', ENT_QUOTES, 'UTF-8');
$owner = (int)($ov['owner_id'] ?? 0);
$uname = htmlspecialchars($ov['username'] ?? '', ENT_QUOTES, 'UTF-8');
echo "<td><a href=\"karte.php?d=".$id."&c=".$generator->getMapCheck($id)."\">".$vname." ($x|$y)</a></td>";
echo "<td><a href=\"spieler.php?uid=".$owner."\">".$uname."</a></td>";
echo "<td><b><font color=\"red\">".OCCUPIED."</font></b></td>";
}
echo "<td><div style=\"text-align: center\">".(int)$row['__dist']."</div></td>";
echo "<td>+".(int)$row['best_oasis_bonus']."%</td>";
echo "</tr>";
}
}
?>
</tbody>
</tbody></table>
<?php
}
?>
</table>
<?php } ?>
</div>
<br /><br /><br /><br /><div id="side_info">
<br /><br /><br /><br />
<div id="side_info">
<?php
include("Templates/multivillage.tpl");
include("Templates/quest.tpl");
include("Templates/news.tpl");
if(!NEW_FUNCTIONS_DISPLAY_LINKS) {
echo "<br><br><br><br>";
include("Templates/links.tpl");
echo "<br><br><br><br>";
include("Templates/links.tpl");
}
?>
</div>
@@ -222,25 +293,16 @@ if(!NEW_FUNCTIONS_DISPLAY_LINKS) {
</div>
<div class="footer-stopper"></div>
<div class="clear"></div>
<?php
include ("Templates/footer.tpl");
include ("Templates/res.tpl");
?>
<?php include ("Templates/footer.tpl"); include ("Templates/res.tpl"); ?>
<div id="stime">
<div id="ltime">
<div id="ltimeWrap">
<?php echo CALCULATED_IN;?> <b><?php
echo round(($generator->pageLoadTimeEnd()-$start_timer)*1000);
?></b> ms
<?php echo CALCULATED_IN;?> <b><?php echo round(($generator->pageLoadTimeEnd()-$start_timer)*1000); ?></b> ms
<br /><?php echo SERVER_TIME;?> <span id="tp1" class="b"><?php echo date('H:i:s'); ?></span>
</div>
</div>
</div>
</div>
<div id="ce"></div>
</div>
</body>
</html>
+23 -2
View File
@@ -1731,14 +1731,35 @@ CREATE TABLE IF NOT EXISTS `%PREFIX%password` (
--
CREATE TABLE IF NOT EXISTS `%PREFIX%ww_attacks` (
`vid` int(25) DEFAULT NULL,
`vid` int(25) DEFAULT NULL,
`attack_time` int(11) DEFAULT NULL,
KEY `attack_time` (`attack_time`),
KEY `vid` (`vid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
--
-- Dumping data for table `%prefix%password`
-- Dumping data for table `%prefix%ww_attacks`
--
-- --------------------------------------------------------
--
-- Table structure for table `%prefix%croppers`
--
CREATE TABLE IF NOT EXISTS %PREFIX%croppers (
wref INT UNSIGNED NOT NULL PRIMARY KEY,
x INT NOT NULL,
y INT NOT NULL,
fieldtype TINYINT UNSIGNED NOT NULL, -- 1 = 9c, 6 = 15c
best_oasis_bonus TINYINT UNSIGNED NOT NULL, -- 0,25,50,75,100,125,150
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CHECK (best_oasis_bonus IN (0,25,50,75,100,125,150))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE INDEX idx_ft_bonus_xy ON %PREFIX%croppers (fieldtype, best_oasis_bonus, x, y);
CREATE INDEX idx_xy ON %PREFIX%croppers (x, y);
CREATE INDEX idx_bonus ON %PREFIX%croppers (best_oasis_bonus);
--
-- Dumping data for table `%prefix%croppers`
--