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. ##
## ## ## ##
+159 -84
View File
@@ -3,142 +3,217 @@
################################################################################# #################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ## ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ## ## --------------------------------------------------------------------------- ##
## Filename Generator.php ## ## Project: TravianZ ##
## License: TravianZ Project ## ## Version: 18.05.2026 ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ## ## Filename: Generator.php ##
## Developed by: Dzoki ##
## Refactored by: Shadow ##
## License: TravianZ Project ##
## 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
for($i = 0; $i < $length; $i++){ */
public function generateRandStr($length)
{
$randstr = '';
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);
else $randstr .= chr($randnum + 61);
}
return $randstr;
}
public function encodeStr($str, $length) { if ($randnum < 10) {
$encode = md5($str); $randstr .= chr($randnum + 48);
return substr($encode, 0 ,$length); } elseif ($randnum < 36) {
} $randstr .= chr($randnum + 55);
} else {
public function procDistanceTime($coor, $thiscoor, $ref, $mode, $vid = 0) { $randstr .= chr($randnum + 61);
global $database, $bid28, $bid14, $village;
if($vid == 0) $vid = $village->wid;
$xdistance = ABS($thiscoor['x'] - $coor['x']);
if($xdistance > WORLD_MAX) $xdistance = (2 * WORLD_MAX + 1) - $xdistance;
$ydistance = ABS($thiscoor['y'] - $coor['y']);
if($ydistance > WORLD_MAX) $ydistance = (2 * WORLD_MAX + 1) - $ydistance;
$distance = SQRT(POW($xdistance,2) + POW($ydistance,2));
if(!$mode){
if($ref == 1) $speed = 16;
else if($ref == 2) $speed = 12;
else if($ref == 3) $speed = 24;
else if($ref == 300) $speed = 5;
else $speed = 1;
}else{
$speed = $ref;
if(($tSquareLevel = $database->getFieldLevelInVillage($vid, 14)) > 0 && $distance >= TS_THRESHOLD) {
$speed *= ($bid14[$tSquareLevel]['attri'] / 100) ;
} }
} }
if($speed > 0) return round(($distance / $speed) * 3600 / INCREASE_SPEED); return $randstr;
else return round($distance * 3600 / INCREASE_SPEED);
} }
public function getTimeFormat($time) { /**
$min = $hr = $days = 0; * Encode string to fixed-length md5 hash
*/
public function encodeStr($str, $length)
{
$encode = md5($str);
return substr($encode, 0, $length);
}
while($time >= 60){ /**
* Calculate travel/distance time between coordinates
*/
public function procDistanceTime($coor, $thiscoor, $ref, $mode, $vid = 0)
{
global $database, $bid28, $bid14, $village;
if ($vid == 0) {
$vid = $village->wid;
}
$xdistance = abs($thiscoor['x'] - $coor['x']);
if ($xdistance > WORLD_MAX) {
$xdistance = (2 * WORLD_MAX + 1) - $xdistance;
}
$ydistance = abs($thiscoor['y'] - $coor['y']);
if ($ydistance > WORLD_MAX) {
$ydistance = (2 * WORLD_MAX + 1) - $ydistance;
}
$distance = sqrt(pow($xdistance, 2) + pow($ydistance, 2));
if (!$mode) {
if ($ref == 1) $speed = 16;
elseif ($ref == 2) $speed = 12;
elseif ($ref == 3) $speed = 24;
elseif ($ref == 300) $speed = 5;
else $speed = 1;
} else {
$speed = $ref;
$tSquareLevel = $database->getFieldLevelInVillage($vid, 14);
if ($tSquareLevel > 0 && $distance >= TS_THRESHOLD) {
$speed *= ($bid14[$tSquareLevel]['attri'] / 100);
}
}
if ($speed > 0) {
return round(($distance / $speed) * 3600 / INCREASE_SPEED);
}
return round($distance * 3600 / INCREASE_SPEED);
}
/**
* Format seconds into H:i:s
*/
public function getTimeFormat($time)
{
$min = 0;
$hr = 0;
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;
if($time < 10) $time = "0" . $time; if ($time < 10) $time = "0" . $time;
return $hr . ":" . $min . ":" . $time; return $hr . ":" . $min . ":" . $time;
} }
public function procMtime($time, $pref = 3){ /**
/* * Format timestamp into readable date/time
* $timezone = 7; */
* switch($timezone) { public function procMtime($time, $pref = 3)
* case 7: {
* $time -= 3600; $time += 0; // placeholder for timezone adjustments
* break;
* }
*/
// $time += 3600*0; //Edit this yourself
$time += 0; // Edit this yourself
$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)) {
switch($pref){ $day = "yesterday";
case 1 : } else {
switch ($pref) {
case 1:
$day = date("m/j/y", $time); $day = date("m/j/y", $time);
break; break;
case 2 : case 2:
$day = date("j/m/y", $time); $day = date("j/m/y", $time);
break; break;
case 3 : case 3:
$day = date("j.m.y", $time); $day = date("j.m.y", $time);
break; break;
default : default:
$day = date("y/m/j", $time); $day = date("y/m/j", $time);
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;
+105 -43
View File
@@ -3,108 +3,170 @@
################################################################################# #################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ## ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ## ## --------------------------------------------------------------------------- ##
## Filename Logging.php ## ## Project: TravianZ ##
## License: TravianZ Project ## ## Version: 12.05.2026 ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ## ## Filename: Logging.php ##
## Developed by: Shadow ##
## Refactored by: Shadow ##
## License: TravianZ Project ##
## 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 ##
## ## ## ##
################################################################################# #################################################################################
class Logging { 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);
if(LOG_ILLEGAL) { list($uid, $ref, $type) = $database->escape_input((int)$uid, $ref, $type);
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;
} }
$q = "Insert into ".TB_PREFIX."illegal_log SET user = $uid, log = '$log'";
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "illegal_log SET user = $uid, log = '$log'";
$database->query($q); $database->query($q);
} }
} }
public function addLoginLog($id,$ip) { public function addLoginLog($id, $ip) {
global $database; global $database;
list($id,$ip) = $database->escape_input((int) $id,$ip);
if(LOG_LOGIN) { list($id, $ip) = $database->escape_input((int)$id, $ip);
$q = "Insert into ".TB_PREFIX."login_log SET uid = $id, ip = '".$_SERVER['REMOTE_ADDR']."'";
if (LOG_LOGIN) {
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);
if(LOG_BUILD) { list($wid, $building, $level, $type) = $database->escape_input((int)$wid, $building, $level, $type);
if($type) {
if (LOG_BUILD) {
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;
$q = "Insert into ".TB_PREFIX."build_log SET wid = $wid, log = '$log'"; $log .= $building . " to level " . $level;
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "build_log SET wid = $wid, log = '$log'";
$database->query($q); $database->query($q);
} }
} }
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);
if(LOG_TECH) { list($wid, $tech, $level) = $database->escape_input((int)$wid, $tech, $level);
$log = "Upgrading of tech ".$tech." to level ".$level;
$q = "Insert into ".TB_PREFIX."tech_log SET wid = $wid, log = '$log'"; if (LOG_TECH) {
$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'";
$database->query($q); $database->query($q);
} }
} }
public function goldFinLog($wid) { public function goldFinLog($wid) {
global $database; global $database;
list($wid) = $database->escape_input((int) $wid);
if(LOG_GOLD_FIN) { list($wid) = $database->escape_input((int)$wid);
if (LOG_GOLD_FIN) {
$log = "Finish construction and research with gold"; $log = "Finish construction and research with gold";
$q = "Insert into ".TB_PREFIX."gold_fin_log values (0,$wid,'$log')"; list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "gold_fin_log values (0,$wid,'$log')";
$database->query($q); $database->query($q);
} }
} }
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);
if(LOG_MARKET) { list($wid, $type, $data) = $database->escape_input((int)$wid, $type, $data);
if($type == 1) {
$log = "Sent ".$data[0].",".$data[1].",".$data[2].",".$data[3]." to village ".$data[4]; if (LOG_MARKET) {
if ($type == 1) {
$log = "Sent " . $data[0] . "," . $data[1] . "," . $data[2] . "," . $data[3] . " to village " . $data[4];
} else if ($type == 2) {
$log = "Traded resource between " . $wid . " and " . $data[0] . " market ref is " . $data[1];
} else {
$log = "Unknown market action";
} }
else if($type == 2) {
$log = "Traded resource between ".$wid." and ".$data[0]." market ref is ".$data[1]; 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);
} }
} }
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();
?> ?>
+56 -35
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) /**
* Multi-key array sorter
* Usage: sorte($array, 'key1', true, 3, 'key2', false, 2, ...)
*/
public function sorte($array)
{ {
for($i = 1; $i < func_num_args(); $i += 3) $args = func_get_args();
$array = $args[0];
// iterate key/order/type triplets
for ($i = 1; $i < count($args); $i += 3)
{ {
$key = func_get_arg($i); $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;
$order = true; if ($key === null) {
if($i + 1 < func_num_args()) continue;
$order = func_get_arg($i + 1); }
$type = 0; // comparator
if($i + 2 < func_num_args()) $cmp = function ($a, $b) use ($key, $type, $order)
$type = func_get_arg($i + 2);
$t = function($a, $b) use ($key, $type, $order)
{ {
switch($type) $va = isset($a[$key]) ? $a[$key] : null;
{ $vb = isset($b[$key]) ? $b[$key] : null;
case 1: // Case insensitive natural.
$result = strcasenatcmp($a[$key], $b[$key]); switch ($type)
break; {
case 2: // Numeric. case 1: // Case insensitive natural
$result = $a[$key] - $b[$key]; $result = strnatcasecmp($va, $vb);
break; break;
case 3: // Case sensitive string.
$result = strcmp($a[$key], $b[$key]); case 2: // Numeric
break; $result = ($va == $vb) ? 0 : (($va < $vb) ? -1 : 1);
case 4: // Case insensitive string. break;
$result = strcasecmp($a[$key], $b[$key]);
break; case 3: // Case sensitive string
default: // Case sensitive natural. $result = strcmp((string)$va, (string)$vb);
$result = strnatcmp($a[$key], $b[ $key]); break;
break;
} case 4: // Case insensitive string
return $result*($order ? 1 : -1); $result = strcasecmp((string)$va, (string)$vb);
break;
default: // Case sensitive natural
$result = strnatcmp((string)$va, (string)$vb);
break;
}
return $order ? $result : -$result;
}; };
usort($array, $t); usort($array, $cmp);
} }
return $array; return $array;
} }
}
$multisort = new multiSort();
};
$multisort = new multiSort;
?> ?>