Incremental refactor Generator/Logging/Multisort

Incremental refactor Generator/Logging/Multisort
This commit is contained in:
Catalin Novgorodschi
2026-05-12 13:22:51 +03:00
parent 2bb0aa0c10
commit c8d9fae4b4
5 changed files with 331 additions and 175 deletions
-2
View File
@@ -8,9 +8,7 @@
## Filename: Building.php ## ## Filename: Building.php ##
## Developed by: Dzoki & Dixie ## ## Developed by: Dzoki & Dixie ##
## Refactored by: Shadow ## ## Refactored by: Shadow ##
## Fixed by: InCube - double troops ##
## Reworked/Fix: ronix ## ## Reworked/Fix: ronix ##
## Thanks to: Akakori, Elmar & Kirilloid ##
## License: TravianZ Project ## ## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2026. All rights reserved. ## ## Copyright: TravianZ (c) 2010-2026. All rights reserved. ##
## ## ## ##
+125 -50
View File
@@ -3,46 +3,90 @@
################################################################################# #################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ## ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ## ## --------------------------------------------------------------------------- ##
## Filename Generator.php ## ## Project: TravianZ ##
## Version: 18.05.2026 ##
## Filename: Generator.php ##
## Developed by: Dzoki ##
## Refactored by: Shadow ##
## License: TravianZ Project ## ## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ## ## Copyright: TravianZ (c) 2010-2026. All rights reserved. ##
## ##
## * Rules applied: ##
## - No logic changes ##
## - No functional behavior changes ##
## - Improved readability & structure ##
## - Removed obvious redundancy ##
## - PHP 7+ / legacy compatible ##
## ##
## URLs: https://travianz.org ##
## https://github.com/Shadowss/TravianZ ##
## ## ## ##
################################################################################# #################################################################################
class MyGenerator { class MyGenerator
{
public function generateRandID(){ /**
* Generate hashed random ID
*/
public function generateRandID()
{
return md5($this->generateRandStr(16)); return md5($this->generateRandStr(16));
} }
public function generateRandStr($length){ /**
$randstr = ""; * Generate random string using secure random_int
*/
public function generateRandStr($length)
{
$randstr = '';
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
$randnum = random_int(0, 61); $randnum = random_int(0, 61);
if($randnum < 10) $randstr .= chr($randnum + 48);
else if($randnum < 36) $randstr .= chr($randnum + 55); if ($randnum < 10) {
else $randstr .= chr($randnum + 61); $randstr .= chr($randnum + 48);
} elseif ($randnum < 36) {
$randstr .= chr($randnum + 55);
} else {
$randstr .= chr($randnum + 61);
} }
}
return $randstr; return $randstr;
} }
public function encodeStr($str, $length) { /**
* Encode string to fixed-length md5 hash
*/
public function encodeStr($str, $length)
{
$encode = md5($str); $encode = md5($str);
return substr($encode, 0, $length); return substr($encode, 0, $length);
} }
public function procDistanceTime($coor, $thiscoor, $ref, $mode, $vid = 0) { /**
* Calculate travel/distance time between coordinates
*/
public function procDistanceTime($coor, $thiscoor, $ref, $mode, $vid = 0)
{
global $database, $bid28, $bid14, $village; global $database, $bid28, $bid14, $village;
if($vid == 0) $vid = $village->wid; if ($vid == 0) {
$vid = $village->wid;
}
$xdistance = ABS($thiscoor['x'] - $coor['x']); $xdistance = abs($thiscoor['x'] - $coor['x']);
if($xdistance > WORLD_MAX) $xdistance = (2 * WORLD_MAX + 1) - $xdistance; if ($xdistance > WORLD_MAX) {
$xdistance = (2 * WORLD_MAX + 1) - $xdistance;
}
$ydistance = ABS($thiscoor['y'] - $coor['y']); $ydistance = abs($thiscoor['y'] - $coor['y']);
if($ydistance > WORLD_MAX) $ydistance = (2 * WORLD_MAX + 1) - $ydistance; if ($ydistance > WORLD_MAX) {
$ydistance = (2 * WORLD_MAX + 1) - $ydistance;
}
$distance = sqrt(pow($xdistance, 2) + pow($ydistance, 2));
$distance = SQRT(POW($xdistance,2) + POW($ydistance,2));
if (!$mode) { if (!$mode) {
if ($ref == 1) $speed = 16; if ($ref == 1) $speed = 16;
elseif ($ref == 2) $speed = 12; elseif ($ref == 2) $speed = 12;
@@ -51,26 +95,37 @@ class MyGenerator {
else $speed = 1; else $speed = 1;
} else { } else {
$speed = $ref; $speed = $ref;
if(($tSquareLevel = $database->getFieldLevelInVillage($vid, 14)) > 0 && $distance >= TS_THRESHOLD) {
$tSquareLevel = $database->getFieldLevelInVillage($vid, 14);
if ($tSquareLevel > 0 && $distance >= TS_THRESHOLD) {
$speed *= ($bid14[$tSquareLevel]['attri'] / 100); $speed *= ($bid14[$tSquareLevel]['attri'] / 100);
} }
} }
if($speed > 0) return round(($distance / $speed) * 3600 / INCREASE_SPEED); if ($speed > 0) {
else return round($distance * 3600 / INCREASE_SPEED); return round(($distance / $speed) * 3600 / INCREASE_SPEED);
} }
public function getTimeFormat($time) { return round($distance * 3600 / INCREASE_SPEED);
$min = $hr = $days = 0; }
/**
* Format seconds into H:i:s
*/
public function getTimeFormat($time)
{
$min = 0;
$hr = 0;
while ($time >= 60) { while ($time >= 60) {
$time -= 60; $time -= 60;
$min += 1; $min++;
} }
while ($min >= 60) { while ($min >= 60) {
$min -= 60; $min -= 60;
$hr += 1; $hr++;
} }
if ($min < 10) $min = "0" . $min; if ($min < 10) $min = "0" . $min;
@@ -79,23 +134,20 @@ class MyGenerator {
return $hr . ":" . $min . ":" . $time; return $hr . ":" . $min . ":" . $time;
} }
public function procMtime($time, $pref = 3){ /**
/* * Format timestamp into readable date/time
* $timezone = 7;
* switch($timezone) {
* case 7:
* $time -= 3600;
* break;
* }
*/ */
// $time += 3600*0; //Edit this yourself public function procMtime($time, $pref = 3)
$time += 0; // Edit this yourself {
$time += 0; // placeholder for timezone adjustments
$today = date('d', time()) - 1; $today = date('d', time()) - 1;
if(date('Ymd', time()) == date('Ymd', $time)) $day = "today";
elseif($today == date('d', $time)) $day = "yesterday"; if (date('Ymd', time()) == date('Ymd', $time)) {
else $day = "today";
{ } elseif ($today == date('d', $time)) {
$day = "yesterday";
} else {
switch ($pref) { switch ($pref) {
case 1: case 1:
$day = date("m/j/y", $time); $day = date("m/j/y", $time);
@@ -111,34 +163,57 @@ class MyGenerator {
break; break;
} }
} }
$new = date("H:i:s", $time); $new = date("H:i:s", $time);
if($pref == "9" || $pref == 9) return $new;
else return array($day, $new); if ($pref == 9) {
return $new;
} }
return [$day, $new];
}
public function getBaseID($x, $y){ /**
* Convert map coordinates to base ID
*/
public function getBaseID($x, $y)
{
return ((WORLD_MAX - $y) * (WORLD_MAX * 2 + 1)) + (WORLD_MAX + $x + 1); return ((WORLD_MAX - $y) * (WORLD_MAX * 2 + 1)) + (WORLD_MAX + $x + 1);
} }
public function getMapCheck($wref){ /**
* Generate map checksum
*/
public function getMapCheck($wref)
{
return substr(md5($wref), 5, 2); return substr(md5($wref), 5, 2);
} }
public function pageLoadTimeStart(){ /**
if(isset($_SERVER["REQUEST_TIME_FLOAT"])) return $_SERVER["REQUEST_TIME_FLOAT"]; * Page load start time
*/
public function pageLoadTimeStart()
{
if (isset($_SERVER["REQUEST_TIME_FLOAT"])) {
return $_SERVER["REQUEST_TIME_FLOAT"];
}
$starttime = microtime(true); $starttime = microtime(true);
$startarray = explode(" ", $starttime); $startarray = explode(" ", $starttime);
//$starttime = $startarray[1] + $startarray[0];
return $startarray[0]; return $startarray[0];
} }
public function pageLoadTimeEnd(){ /**
* Page load end time
*/
public function pageLoadTimeEnd()
{
$endtime = microtime(true); $endtime = microtime(true);
$endarray = explode(" ", $endtime); $endarray = explode(" ", $endtime);
//$endtime = $endarray[1] + $endarray[0];
return $endarray[0]; return $endarray[0];
} }
}
}; $generator = new MyGenerator();
$generator = new MyGenerator;
+73 -11
View File
@@ -3,9 +3,19 @@
################################################################################# #################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ## ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ## ## --------------------------------------------------------------------------- ##
## Filename Logging.php ## ## Project: TravianZ ##
## Version: 12.05.2026 ##
## Filename: Logging.php ##
## Developed by: Shadow ##
## Refactored by: Shadow ##
## License: TravianZ Project ## ## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ## ## Copyright: TravianZ (c) 2010-2026. All rights reserved. ##
## ##
## Refactor: Incremental cleanup (structure / readability / safety) ##
## Compatibility: PHP 7+ / legacy ##
## ##
## URLs: https://travianz.org ##
## https://github.com/Shadowss/TravianZ ##
## ## ## ##
################################################################################# #################################################################################
@@ -13,14 +23,23 @@ class Logging {
public function addIllegal($uid, $ref, $type) { public function addIllegal($uid, $ref, $type) {
global $database; global $database;
list($uid, $ref, $type) = $database->escape_input((int)$uid, $ref, $type); list($uid, $ref, $type) = $database->escape_input((int)$uid, $ref, $type);
if (LOG_ILLEGAL) { if (LOG_ILLEGAL) {
$log = "Attempted to "; $log = "Attempted to ";
switch ($type) { switch ($type) {
case 1: case 1:
$log .= "access village $ref"; $log .= "access village $ref";
break; break;
default:
$log .= "perform illegal action";
break;
} }
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "illegal_log SET user = $uid, log = '$log'"; $q = "Insert into " . TB_PREFIX . "illegal_log SET user = $uid, log = '$log'";
$database->query($q); $database->query($q);
} }
@@ -28,24 +47,39 @@ class Logging {
public function addLoginLog($id, $ip) { public function addLoginLog($id, $ip) {
global $database; global $database;
list($id, $ip) = $database->escape_input((int)$id, $ip); list($id, $ip) = $database->escape_input((int)$id, $ip);
if (LOG_LOGIN) { if (LOG_LOGIN) {
$q = "Insert into ".TB_PREFIX."login_log SET uid = $id, ip = '".$_SERVER['REMOTE_ADDR']."'";
if (empty($ip)) {
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
}
list($ip) = $database->escape_input($ip);
$q = "Insert into " . TB_PREFIX . "login_log SET uid = $id, ip = '$ip'";
$database->query($q); $database->query($q);
} }
} }
public function addBuildLog($wid, $building, $level, $type) { public function addBuildLog($wid, $building, $level, $type) {
global $database; global $database;
list($wid, $building, $level, $type) = $database->escape_input((int)$wid, $building, $level, $type); list($wid, $building, $level, $type) = $database->escape_input((int)$wid, $building, $level, $type);
if (LOG_BUILD) { if (LOG_BUILD) {
if ($type) { if ($type) {
$log = "Start Construction of "; $log = "Start Construction of ";
} } else {
else {
$log = "Start Upgrade of "; $log = "Start Upgrade of ";
} }
$log .= $building . " to level " . $level; $log .= $building . " to level " . $level;
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "build_log SET wid = $wid, log = '$log'"; $q = "Insert into " . TB_PREFIX . "build_log SET wid = $wid, log = '$log'";
$database->query($q); $database->query($q);
} }
@@ -53,9 +87,14 @@ class Logging {
public function addTechLog($wid, $tech, $level) { public function addTechLog($wid, $tech, $level) {
global $database; global $database;
list($wid, $tech, $level) = $database->escape_input((int)$wid, $tech, $level); list($wid, $tech, $level) = $database->escape_input((int)$wid, $tech, $level);
if (LOG_TECH) { if (LOG_TECH) {
$log = "Upgrading of tech " . $tech . " to level " . $level; $log = "Upgrading of tech " . $tech . " to level " . $level;
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "tech_log SET wid = $wid, log = '$log'"; $q = "Insert into " . TB_PREFIX . "tech_log SET wid = $wid, log = '$log'";
$database->query($q); $database->query($q);
} }
@@ -63,9 +102,14 @@ class Logging {
public function goldFinLog($wid) { public function goldFinLog($wid) {
global $database; global $database;
list($wid) = $database->escape_input((int)$wid); list($wid) = $database->escape_input((int)$wid);
if (LOG_GOLD_FIN) { if (LOG_GOLD_FIN) {
$log = "Finish construction and research with gold"; $log = "Finish construction and research with gold";
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "gold_fin_log values (0,$wid,'$log')"; $q = "Insert into " . TB_PREFIX . "gold_fin_log values (0,$wid,'$log')";
$database->query($q); $database->query($q);
} }
@@ -73,18 +117,26 @@ class Logging {
public function addAdminLog() { public function addAdminLog() {
global $database; global $database;
// reserved
} }
public function addMarketLog($wid, $type, $data) { public function addMarketLog($wid, $type, $data) {
global $database; global $database;
list($wid, $type, $data) = $database->escape_input((int)$wid, $type, $data); list($wid, $type, $data) = $database->escape_input((int)$wid, $type, $data);
if (LOG_MARKET) { if (LOG_MARKET) {
if ($type == 1) { if ($type == 1) {
$log = "Sent " . $data[0] . "," . $data[1] . "," . $data[2] . "," . $data[3] . " to village " . $data[4]; $log = "Sent " . $data[0] . "," . $data[1] . "," . $data[2] . "," . $data[3] . " to village " . $data[4];
} } else if ($type == 2) {
else if($type == 2) {
$log = "Traded resource between " . $wid . " and " . $data[0] . " market ref is " . $data[1]; $log = "Traded resource between " . $wid . " and " . $data[0] . " market ref is " . $data[1];
} else {
$log = "Unknown market action";
} }
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "market_log SET wid = $wid, log = '$log'"; $q = "Insert into " . TB_PREFIX . "market_log SET wid = $wid, log = '$log'";
$database->query($q); $database->query($q);
} }
@@ -92,19 +144,29 @@ class Logging {
public function addWarLog() { public function addWarLog() {
global $database; global $database;
// reserved
} }
public function clearLogs() { public function clearLogs() {
global $database; global $database;
// reserved
} }
public static function debug($debug_info, $time = 0) { public static function debug($debug_info, $time = 0) {
global $database, $generator; global $database, $generator;
list($debug_info) = $database->escape_input($debug_info); list($debug_info) = $database->escape_input($debug_info);
echo '<script>console.log('.json_encode(($time > 0 ? "[".$generator->procMtime($time)[1]."] " : "").$debug_info).')</script>'; $prefix = "";
}
};
$logging = new Logging; if ($time > 0 && isset($generator)) {
$mtime = $generator->procMtime($time);
$prefix = "[" . ($mtime[1] ?? '') . "] ";
}
echo '<script>console.log(' . json_encode($prefix . $debug_info) . ')</script>';
}
}
$logging = new Logging();
?> ?>
+49 -28
View File
@@ -7,52 +7,73 @@
## License: TravianZ Project ## ## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ## ## Copyright: TravianZ (c) 2010-2025. All rights reserved. ##
## ## ## ##
## URLs: https://travianz.org ##
## https://github.com/Shadowss/TravianZ ##
#################################################################################
## Refactored: incremental cleanup (compat PHP 7+, readability, minor fixes) ##
################################################################################# #################################################################################
class multiSort { class multiSort
function sorte($array)
{ {
for($i = 1; $i < func_num_args(); $i += 3) /**
* Multi-key array sorter
* Usage: sorte($array, 'key1', true, 3, 'key2', false, 2, ...)
*/
public function sorte($array)
{ {
$key = func_get_arg($i); $args = func_get_args();
$array = $args[0];
$order = true; // iterate key/order/type triplets
if($i + 1 < func_num_args()) for ($i = 1; $i < count($args); $i += 3)
$order = func_get_arg($i + 1);
$type = 0;
if($i + 2 < func_num_args())
$type = func_get_arg($i + 2);
$t = function($a, $b) use ($key, $type, $order)
{ {
$key = isset($args[$i]) ? $args[$i] : null;
$order = isset($args[$i + 1]) ? $args[$i + 1] : true; // true = ASC
$type = isset($args[$i + 2]) ? $args[$i + 2] : 0;
if ($key === null) {
continue;
}
// comparator
$cmp = function ($a, $b) use ($key, $type, $order)
{
$va = isset($a[$key]) ? $a[$key] : null;
$vb = isset($b[$key]) ? $b[$key] : null;
switch ($type) switch ($type)
{ {
case 1: // Case insensitive natural. case 1: // Case insensitive natural
$result = strcasenatcmp($a[$key], $b[$key]); $result = strnatcasecmp($va, $vb);
break; break;
case 2: // Numeric.
$result = $a[$key] - $b[$key]; case 2: // Numeric
$result = ($va == $vb) ? 0 : (($va < $vb) ? -1 : 1);
break; break;
case 3: // Case sensitive string.
$result = strcmp($a[$key], $b[$key]); case 3: // Case sensitive string
$result = strcmp((string)$va, (string)$vb);
break; break;
case 4: // Case insensitive string.
$result = strcasecmp($a[$key], $b[$key]); case 4: // Case insensitive string
$result = strcasecmp((string)$va, (string)$vb);
break; break;
default: // Case sensitive natural.
$result = strnatcmp($a[$key], $b[ $key]); default: // Case sensitive natural
$result = strnatcmp((string)$va, (string)$vb);
break; break;
} }
return $result*($order ? 1 : -1);
return $order ? $result : -$result;
}; };
usort($array, $t); usort($array, $cmp);
} }
return $array; return $array;
} }
}
$multisort = new multiSort();
};
$multisort = new multiSort;
?> ?>