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 ##
## Developed by: Dzoki & Dixie ##
## Refactored by: Shadow ##
## Fixed by: InCube - double troops ##
## Reworked/Fix: ronix ##
## Thanks to: Akakori, Elmar & Kirilloid ##
## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2026. All rights reserved. ##
## ##
+165 -90
View File
@@ -3,142 +3,217 @@
#################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ##
## Filename Generator.php ##
## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ##
## Project: TravianZ ##
## Version: 18.05.2026 ##
## 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 {
public function generateRandID(){
class MyGenerator
{
/**
* Generate hashed random ID
*/
public function generateRandID()
{
return md5($this->generateRandStr(16));
}
public function generateRandStr($length){
$randstr = "";
for($i = 0; $i < $length; $i++){
/**
* Generate random string using secure random_int
*/
public function generateRandStr($length)
{
$randstr = '';
for ($i = 0; $i < $length; $i++) {
$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) {
$encode = md5($str);
return substr($encode, 0 ,$length);
}
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;
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 ($randnum < 10) {
$randstr .= chr($randnum + 48);
} elseif ($randnum < 36) {
$randstr .= chr($randnum + 55);
} else {
$randstr .= chr($randnum + 61);
}
}
if($speed > 0) return round(($distance / $speed) * 3600 / INCREASE_SPEED);
else return round($distance * 3600 / INCREASE_SPEED);
return $randstr;
}
public function getTimeFormat($time) {
$min = $hr = $days = 0;
while($time >= 60){
/**
* Encode string to fixed-length md5 hash
*/
public function encodeStr($str, $length)
{
$encode = md5($str);
return substr($encode, 0, $length);
}
/**
* 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;
$min += 1;
$min++;
}
while($min >= 60){
while ($min >= 60) {
$min -= 60;
$hr += 1;
$hr++;
}
if($min < 10) $min = "0" . $min;
if($time < 10) $time = "0" . $time;
if ($min < 10) $min = "0" . $min;
if ($time < 10) $time = "0" . $time;
return $hr . ":" . $min . ":" . $time;
}
public function procMtime($time, $pref = 3){
/*
* $timezone = 7;
* switch($timezone) {
* case 7:
* $time -= 3600;
* break;
* }
*/
// $time += 3600*0; //Edit this yourself
$time += 0; // Edit this yourself
/**
* Format timestamp into readable date/time
*/
public function procMtime($time, $pref = 3)
{
$time += 0; // placeholder for timezone adjustments
$today = date('d', time()) - 1;
if(date('Ymd', time()) == date('Ymd', $time)) $day = "today";
elseif($today == date('d', $time)) $day = "yesterday";
else
{
switch($pref){
case 1 :
if (date('Ymd', time()) == date('Ymd', $time)) {
$day = "today";
} elseif ($today == date('d', $time)) {
$day = "yesterday";
} else {
switch ($pref) {
case 1:
$day = date("m/j/y", $time);
break;
case 2 :
case 2:
$day = date("j/m/y", $time);
break;
case 3 :
case 3:
$day = date("j.m.y", $time);
break;
default :
default:
$day = date("y/m/j", $time);
break;
}
}
$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);
}
public function getMapCheck($wref){
/**
* Generate map checksum
*/
public function getMapCheck($wref)
{
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);
$startarray = explode(" ", $starttime);
//$starttime = $startarray[1] + $startarray[0];
return $startarray[0];
}
public function pageLoadTimeEnd(){
/**
* Page load end time
*/
public function pageLoadTimeEnd()
{
$endtime = microtime(true);
$endarray = explode(" ", $endtime);
//$endtime = $endarray[1] + $endarray[0];
return $endarray[0];
}
}
};
$generator = new MyGenerator;
$generator = new MyGenerator();
+1 -1
View File
@@ -1746,4 +1746,4 @@ $lang['screenshots']['desc5'] = 'Your attack report';
$lang['screenshots']['title6'] = 'Statistics';
$lang['screenshots']['desc6'] = 'View your ranking in statistics';
$lang['screenshots']['title7'] = 'Weapons or dough';
$lang['screenshots']['desc7'] = 'You can choose to play as military or economy';
$lang['screenshots']['desc7'] = 'You can choose to play as military or economy';
+108 -46
View File
@@ -3,108 +3,170 @@
#################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ##
## Filename Logging.php ##
## License: TravianZ Project ##
## Copyright: TravianZ (c) 2010-2025. All rights reserved. ##
## Project: TravianZ ##
## Version: 12.05.2026 ##
## 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 {
public function addIllegal($uid,$ref,$type) {
public function addIllegal($uid, $ref, $type) {
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 ";
switch($type) {
switch ($type) {
case 1:
$log .= "access village $ref";
break;
$log .= "access village $ref";
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);
}
}
public function addLoginLog($id,$ip) {
public function addLoginLog($id, $ip) {
global $database;
list($id,$ip) = $database->escape_input((int) $id,$ip);
if(LOG_LOGIN) {
$q = "Insert into ".TB_PREFIX."login_log SET uid = $id, ip = '".$_SERVER['REMOTE_ADDR']."'";
list($id, $ip) = $database->escape_input((int)$id, $ip);
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);
}
}
public function addBuildLog($wid,$building,$level,$type) {
public function addBuildLog($wid, $building, $level, $type) {
global $database;
list($wid,$building,$level,$type) = $database->escape_input((int) $wid,$building,$level,$type);
if(LOG_BUILD) {
if($type) {
list($wid, $building, $level, $type) = $database->escape_input((int)$wid, $building, $level, $type);
if (LOG_BUILD) {
if ($type) {
$log = "Start Construction of ";
}
else {
} else {
$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);
}
}
public function addTechLog($wid,$tech,$level) {
public function addTechLog($wid, $tech, $level) {
global $database;
list($wid,$tech,$level) = $database->escape_input((int) $wid,$tech,$level);
if(LOG_TECH) {
$log = "Upgrading of tech ".$tech." to level ".$level;
$q = "Insert into ".TB_PREFIX."tech_log SET wid = $wid, log = '$log'";
list($wid, $tech, $level) = $database->escape_input((int)$wid, $tech, $level);
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);
}
}
public function goldFinLog($wid) {
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";
$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);
}
}
public function addAdminLog() {
global $database;
// reserved
}
public function addMarketLog($wid,$type,$data) {
public function addMarketLog($wid, $type, $data) {
global $database;
list($wid,$type,$data) = $database->escape_input((int) $wid,$type,$data);
if(LOG_MARKET) {
if($type == 1) {
$log = "Sent ".$data[0].",".$data[1].",".$data[2].",".$data[3]." to village ".$data[4];
list($wid, $type, $data) = $database->escape_input((int)$wid, $type, $data);
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];
}
$q = "Insert into ".TB_PREFIX."market_log SET wid = $wid, log = '$log'";
list($log) = $database->escape_input($log);
$q = "Insert into " . TB_PREFIX . "market_log SET wid = $wid, log = '$log'";
$database->query($q);
}
}
public function addWarLog() {
global $database;
// reserved
}
public function clearLogs() {
global $database;
// reserved
}
public static function debug($debug_info, $time = 0) {
global $database, $generator;
list($debug_info) = $database->escape_input($debug_info);
echo '<script>console.log('.json_encode(($time > 0 ? "[".$generator->procMtime($time)[1]."] " : "").$debug_info).')</script>';
}
};
$logging = new Logging;
?>
list($debug_info) = $database->escape_input($debug_info);
$prefix = "";
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();
?>
+57 -36
View File
@@ -7,52 +7,73 @@
## License: TravianZ Project ##
## 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 {
function sorte($array)
class multiSort
{
/**
* 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($i + 1 < func_num_args())
$order = func_get_arg($i + 1);
if ($key === null) {
continue;
}
$type = 0;
if($i + 2 < func_num_args())
$type = func_get_arg($i + 2);
// comparator
$cmp = function ($a, $b) use ($key, $type, $order)
{
$va = isset($a[$key]) ? $a[$key] : null;
$vb = isset($b[$key]) ? $b[$key] : null;
$t = function($a, $b) use ($key, $type, $order)
{
switch($type)
{
case 1: // Case insensitive natural.
$result = strcasenatcmp($a[$key], $b[$key]);
break;
case 2: // Numeric.
$result = $a[$key] - $b[$key];
break;
case 3: // Case sensitive string.
$result = strcmp($a[$key], $b[$key]);
break;
case 4: // Case insensitive string.
$result = strcasecmp($a[$key], $b[$key]);
break;
default: // Case sensitive natural.
$result = strnatcmp($a[$key], $b[ $key]);
break;
}
return $result*($order ? 1 : -1);
switch ($type)
{
case 1: // Case insensitive natural
$result = strnatcasecmp($va, $vb);
break;
case 2: // Numeric
$result = ($va == $vb) ? 0 : (($va < $vb) ? -1 : 1);
break;
case 3: // Case sensitive string
$result = strcmp((string)$va, (string)$vb);
break;
case 4: // Case insensitive string
$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;
}
}
};
$multisort = new multiSort;
?>
$multisort = new multiSort();
?>