refactor: installation happens at MySQL side

A lot has happened here. Here's the list:

1. autoloader was added that should prevent various "failed to open
stream" errors when trying to load new classes

2. PHP-based data generation was replaced by MySQL-based one, hopefully
fixing the installation problems when insert payload it too high and the
DB wouldn't populate with some users

3. thanks to step 2, the installation should be really almost
instantenious, instead of waiting a long time for many SQL connections
and queries to populate the world

4. World Data & Oasis generation and population is done in 1 step now
This commit is contained in:
Martin Ambrus
2017-10-27 18:27:03 +02:00
parent 93fbba445e
commit 1066fff9a2
26 changed files with 957 additions and 447 deletions
-1713
View File
File diff suppressed because it is too large Load Diff
-30
View File
@@ -1,30 +0,0 @@
<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////
// TRAVIANX //
// Only for advanced users, do not edit if you dont know what are you doing! //
// Made by: Dzoki & Dixie (TravianX) //
// - TravianX = Travian Clone Project - //
// DO NOT REMOVE COPYRIGHT NOTICE! //
//////////////////////////////////////////////////////////////////////////////////////////////////////
include("constant.php");
class MYSQLi_DB {
var $connection;
function MYSQLi_DB() {
$this->connection = mysqli_connect(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB) or die(mysqli_error($database->dblink));
}
function query($query) {
return mysqli_query($this->connection, $query);
}
function query_return($query) {
$q = mysqli_query($this->connection, $query);
return mysqli_fetch_assoc($q);
}
};
if(DB_TYPE) {
$database = new MYSQLi_DB;
}
?>
-28
View File
@@ -1,28 +0,0 @@
<?php
// don't let SQL time out when 30-500 seconds (depending on php.ini) is not enough
@set_time_limit(0);
if(!isset($_SESSION)) session_start();
$gameinstall = 1;
include ("../../GameEngine/config.php");
include ("../../GameEngine/Database.php");
include ("../../GameEngine/Admin/database.php");
// check if we don't already have world data
$data_exist = $database->query_return("SELECT * FROM " . TB_PREFIX . "odata LIMIT 1");
if (count($data_exist)) {
header("Location: ../index.php?s=6&err=1");
exit;
}
$database->populateOasisdata();
$database->populateOasis();
$database->populateOasisUnits2();
header("Location: ../index.php?s=7");
?>
-140
View File
@@ -1,140 +0,0 @@
<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////
// TRAVIANX //
// Only for advanced users, do not edit if you dont know what are you doing! //
// Made by: Dzoki & Dixie (TravianX) //
// - TravianX = Travian Clone Project - //
// DO NOT REMOVE COPYRIGHT NOTICE! //
//////////////////////////////////////////////////////////////////////////////////////////////////////
// don't let SQL time out when 30-500 seconds (depending on php.ini) is not enough
@set_time_limit(0);
include("database.php");
// check if we don't already have world data
$data_exist = $database->query_return("SELECT * FROM " . TB_PREFIX . "wdata LIMIT 1");
if (count($data_exist)) {
header("Location: ../index.php?s=3&err=1");
exit;
}
$xyas=(1+(2*WORLD_MAX));
$values_batch = [];
$max_batch_size = 10000;
for($i=0; $i<$xyas; $i++){
$y=(WORLD_MAX-$i);
for($j=0; $j<$xyas; $j++){
$x=((WORLD_MAX*-1)+$j);
//choose a field type
if(($x == 0 & $y == 0) || ($x == WORLD_MAX & $y == WORLD_MAX)) {
$typ='3';
$otype='0';
}else{
$rand=rand(1, 1000);
if("10" >= $rand){
$typ='1';
$otype='0';
} else if("90" >= $rand){
$typ='2';
$otype='0';
} else if("400" >= $rand){
$typ='3';
$otype='0';
} else if("480" >= $rand){
$typ='4';
$otype='0';
} else if("560" >= $rand){
$typ='5';
$otype='0';
} else if("570" >= $rand){
$typ='6';
$otype='0';
} else if("600" >= $rand){
$typ='7';
$otype='0';
} else if("630" >= $rand){
$typ='8';
$otype='0';
} else if("660" >= $rand){
$typ='9';
$otype='0';
} else if("740" >= $rand){
$typ='10';
$otype='0';
} else if("820" >= $rand){
$typ='11';
$otype='0';
} else if("900" >= $rand){
$typ='12';
$otype='0';
} else if("908" >= $rand){
$typ='0';
$otype='1';
} else if("916" >= $rand){
$typ='0';
$otype='2';
} else if("924" >= $rand){
$typ='0';
$otype='3';
} else if("932" >= $rand){
$typ='0';
$otype='4';
} else if("940" >= $rand){
$typ='0';
$otype='5';
} else if("948" >= $rand){
$typ='0';
$otype='6';
} else if("956" >= $rand){
$typ='0';
$otype='7';
} else if("964" >= $rand){
$typ='0';
$otype='8';
} else if("972" >= $rand){
$typ='0';
$otype='9';
} else if("980" >= $rand){
$typ='0';
$otype='10';
} else if("988" >= $rand){
$typ='0';
$otype='11';
} else {
$typ='0';
$otype='12';
}
}
//image pick
if($otype=='0'){
$image="t".rand(0,9)."";
} else {
$image="o".$otype."";
}
//into database
$values_batch[] = "(0,'".$typ."','".$otype."','".$x."','".$y."',0,'".$image."')";
// insert the full batch when threshold is reached
if (count($values_batch) === $max_batch_size) {
$q = "INSERT into ".TB_PREFIX."wdata VALUES ".implode(',', $values_batch);
$database->query($q);
$values_batch = [];
}
}
}
// last batch may not be as big as $max_batch_size
if (count($values_batch)) {
$q = "INSERT into ".TB_PREFIX."wdata VALUES ".implode(',', $values_batch);
$database->query($q);
$values_batch = [];
}
header("Location: ../index.php?s=4");
?>
+14 -9
View File
@@ -53,8 +53,16 @@ function refresh(tz) {
location="?s=1&t="+tz;
}
function proceed() {
document.dataform.Submit.disabled=true;
return(true);
var e = document.getElementById('Submit');
// if we disable the button right away, we wouldn't be able to submit the form
setTimeout(function() {
e.disabled = "disabled";
}, 500);
e.value = "Processing...";
return true;
}
</script>
<div class="wrapper">
@@ -74,7 +82,7 @@ function proceed() {
<div id="content" class="login">
<?php
IHG_Progressbar::draw_css();
$bar = new IHG_Progressbar(8, 'Step %d from %d ');
$bar = new IHG_Progressbar(7, 'Step %d from %d ');
$bar->draw();
for($i = 0; $i < ($_GET['s']+1); $i++) {
$bar->tick();
@@ -88,8 +96,8 @@ function proceed() {
if(substr(sprintf('%o', fileperms('../')), -4)<'700'){
echo"<span class='f18 c5'>ERROR!</span><br />It's not possible to write the config file. Change the permission to '777'. After that, refresh this page!";
}
else if (file_exists("installation_done")) {
echo"<span class='f18 c5'>ERROR!</span><br />Installation appears to have been completed.<br />If this is an error remove installation_done file in install directory.";
else if (file_exists("../var/installed")) {
echo"<span class='f18 c5'>ERROR!</span><br />Installation appears to have been completed.<br />If this is an error remove /var/installed file in install directory.";
}
else
switch($_GET['s']){
@@ -103,7 +111,7 @@ function proceed() {
include("templates/dataform.tpl");
break;
case 3:
include("templates/field.tpl");
include("templates/wdata.tpl");
break;
case 4:
include("templates/multihunter.tpl");
@@ -112,9 +120,6 @@ function proceed() {
include("templates/support.tpl");
break;
case 6:
include("templates/oasis.tpl");
break;
case 7:
include("templates/end.tpl");
break;
}
+49 -23
View File
@@ -12,12 +12,9 @@
// don't let SQL time out when 30-500 seconds (depending on php.ini) is not enough
@set_time_limit(0);
if(file_exists("include/constant.php")) {
include ("include/database.php");
}
class Process {
function Process() {
function __construct() {
if(isset($_POST['subconst'])) {
$this->constForm();
} else
@@ -34,15 +31,16 @@ class Process {
}
}
function constForm() {
$myFile = "include/constant.php";
$fh = fopen($myFile, 'w') or die("<br/><br/><br/>Can't open file: install\include\constant.php");
private function constForm() {
$myFile = "../GameEngine/config.php";
$fh = @fopen($myFile, 'w') or die("<br/><br/><br/>Can't create or update file: GameEngine\config.php");
$text = file_get_contents("data/constant_format.tpl");
$text = preg_replace("'%SERVERNAME%'", $_POST['servername'], $text);
$text = preg_replace("'%SSTARTDATE%'", $_POST['start_date'], $text);
$text = preg_replace("'%SSTARTTIME%'", $_POST['start_time'], $text);
$tz = explode(",",$_POST['tzone']);
$text = preg_replace("'%STIMEZONE%'", $tz[1], $text);
$text = preg_replace("'%STIMEZONE%'", $tz[1], $text);
$text = preg_replace("'%LANG%'", $_POST['lang'], $text);
$text = preg_replace("'%SPEED%'", $_POST['speed'], $text);
$text = preg_replace("'%INCSPEED%'", $_POST['incspeed'], $text);
@@ -125,7 +123,7 @@ class Process {
fwrite($fh, $text);
if(file_exists("include/constant.php")) {
if(file_exists("../GameEngine/config.php")) {
header("Location: index.php?s=2");
} else {
header("Location: index.php?s=1&c=1");
@@ -134,24 +132,52 @@ class Process {
fclose($fh);
}
/**
* Creates database structure for the game.
*/
function createStruc() {
global $database;
$str = file_get_contents("data/sql.sql");
$str = preg_replace("'%PREFIX%'", TB_PREFIX, $str);
if(DB_TYPE) {
$result = $database->connection->multi_query($str);
} else {
$result = $database->mysql_exec_batch($str);
}
if($result) {
header("Location: index.php?s=3");
} else {
header("Location: index.php?s=2&c=1");
}
global $database;
include ("../GameEngine/config.php");
include ("../GameEngine/Database.php");
include ("../GameEngine/Admin/database.php");
// create table structure
$result = $database->createDbStructure();
if ($result === false) {
header("Location: index.php?s=2&err=1");
exit;
} else if ($result === -1) {
header("Location: index.php?s=2&c=1");
exit;
}
header("Location: index.php?s=3");
exit;
}
/**
* Generates map data and populates it with oasis.
*/
function createWdata() {
header("Location: include/wdata.php");
global $database;
include ("../GameEngine/config.php");
include ("../GameEngine/Database.php");
include ("../GameEngine/Admin/database.php");
// populate world data
$result = $database->populateWorldData();
if ($result === false) {
header("Location: index.php?s=3&err=1");
exit;
} else if ($result === -1) {
header("Location: index.php?s=3&c=1");
exit;
}
header("Location: index.php?s=4");
exit;
}
}
+29 -9
View File
@@ -14,20 +14,40 @@
## Source code: https://github.com/Shadowss/TravianZ ##
## ##
#################################################################################
include_once('../GameEngine/config.php');
if(isset($_GET['c']) && $_GET['c'] == 1) {
echo "<div class=\"headline\"><span class=\"f10 c5\">Error importing database. Check configuration.</span></div><br>";
echo "<div class=\"headline\"><span class=\"f10 c5\">Error importing database. Check configuration.</span></div><br>";
}
if(isset($_GET['err']) && $_GET['err'] == 1) {
echo "<br /><hr /><br /><div class=\"headline\"><span class=\"f10 c5\">Existing structure was found in the database! Please remove old game tables with the <i>".TB_PREFIX."</i> prefix from the '<strong>".SQL_DB."</strong>' database before continuing.</span></div><br /><br />";
}
?>
<form action="process.php" method="post" id="dataform">
<input type="hidden" name="substruc" value="1">
<input type="hidden" name="substruc" value="1">
<p>
<span class="f10 c">Create SQL Structure</span>
<table>
<tr><td>Warning: This can take some time. Wait till the next page has been loaded! Click Create to proceed..</td></tr>
<tr><td><center><input type="submit" name="Submit" id="Submit" value="Create.." onClick="return proceed()"></center></td></tr>
</table>
</p>
<p>
<span class="f10 c">Create Database Structure</span>
<table>
<tr>
<td>
Warning: This can take some time. Please wait until the next page has been loaded. Click Create to proceed...
<br />
<br />
</td>
</tr>
<tr>
<td>
<center>
<input type="submit" name="Submit" id="Submit" value="Create" onClick="return proceed()" />
<br />
<br />
</center>
</td>
</tr>
</table>
</p>
</form>
</div>
+1 -1
View File
@@ -37,7 +37,7 @@ All the files are placed. The database is created, so you can now start playing
<?php include("../GameEngine/config.php");
$time = time();
rename("../install/","../installed_".$time);
touch("../installation_done");
touch('../var/installed');
?>
<p>
<center><font size="4"><a href="<?php echo HOMEPAGE; ?>">> My TravianZ homepage <</font></a></center>
+7 -10
View File
@@ -19,28 +19,25 @@
switch($_GET['s']) {
case 0:
echo "<li class=\"c2 f9\">Intro</li><li class=\"c1 f9\">Configuration</li><li class=\"c1 f9\">Database</li><li class= \"c1 f9\">Field</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">Oasis</li><li class=\"c1 f9\">End</li>";
echo "<li class=\"c2 f9\">Intro</li><li class=\"c1 f9\">Configuration</li><li class=\"c1 f9\">Database</li><li class= \"c1 f9\">World Data</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">End</li>";
break;
case 1:
echo "<li class=\"c3 f9\">Intro</li><li class=\"c2 f9\">Configuration</li><li class=\"c1 f9\">Database</li><li class= \"c1 f9\">Field</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">Oasis</li><li class=\"c1 f9\">End</li>";
echo "<li class=\"c3 f9\">Intro</li><li class=\"c2 f9\">Configuration</li><li class=\"c1 f9\">Database</li><li class= \"c1 f9\">World Data</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">End</li>";
break;
case 2:
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c2 f9\">Database</li><li class= \"c1 f9\">Field</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">Oasis</li><li class=\"c1 f9\">End</li>";
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c2 f9\">Database</li><li class= \"c1 f9\">World Data</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">End</li>";
break;
case 3:
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c2 f9\">Field</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">Oasis</li><li class=\"c1 f9\">End</li>";
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c2 f9\">World Data</li><li class=\"c1 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">End</li>";
break;
case 4:
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c3 f9\">Field</li><li class=\"c2 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">Oasis</li><li class=\"c1 f9\">End</li>";
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c3 f9\">World Data</li><li class=\"c2 f9\">Multihunter</li><li class=\"c1 f9\">Support</li><li class=\"c1 f9\">End</li>";
break;
case 5:
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c3 f9\">Field</li><li class=\"c3 f9\">Multihunter</li><li class=\"c2 f9\">Support</li><li class=\"c1 f9\">Oasis</li><li class=\"c1 f9\">End</li>";
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c3 f9\">World Data</li><li class=\"c3 f9\">Multihunter</li><li class=\"c2 f9\">Support</li><li class=\"c1 f9\">End</li>";
break;
case 6:
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c3 f9\">Field</li><li class=\"c3 f9\">Multihunter</li><li class=\"c3 f9\">Support</li><li class=\"c2 f9\">Oasis</li><li class=\"c1 f9\">End</li>";
break;
case 7:
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c3 f9\">Field</li><li class=\"c3 f9\">Multihunter</li><li class=\"c3 f9\">Support</li><li class=\"c3 f9\">Oasis</li><li class=\"c2 f9\">End</li>";
echo "<li class=\"c3 f9\">Intro</li><li class=\"c3 f9\">Configuration</li><li class=\"c3 f9\">Database</li><li class= \"c3 f9\">World Data</li><li class=\"c3 f9\">Multihunter</li><li class=\"c3 f9\">Support</li><li class=\"c2 f9\">End</li>";
break;
}
-1
View File
@@ -14,7 +14,6 @@
## Source code: https://github.com/Shadowss/TravianZ ##
## ##
#################################################################################
rename("include/constant.php","../GameEngine/config.php");
?>
-24
View File
@@ -1,24 +0,0 @@
<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////
// TRAVIANX //
// Only for advanced users, do not edit if you dont know what are you doing! //
// Made by: Dzoki & Dixie (TravianX) //
// - TravianX = Travian Clone Project - //
// DO NOT REMOVE COPYRIGHT NOTICE! //
//////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($_GET['err']) && $_GET['err'] == 1) {
echo "<br /><hr /><br /><div class=\"headline\"><span class=\"f10 c5\">Existing Oasis data found in the database! Please clear the 'odata' table before continuing.</span></div><br /><br />";
}
?>
<form action="include/oasis.php" method="post" id="dataform">
<p>
<span class="f10 c">Populate Oasis</span>
<table>
<tr><td><b>Warning</b>: This can take some time. Wait till the next page has been loaded! Click Create to proceed..</td></tr>
<tr><td><center><input type="submit" name="Submit" id="Submit" value="Create.." onClick="return proceed()"></center></td></tr>
</table>
</p>
</form>
</div>
@@ -4,7 +4,7 @@
## --------------------------------------------------------------------------- ##
## Project: TravianZ ##
## Version: 22.06.2015 ##
## Filename field.tpl ##
## Filename wdata.tpl ##
## Developed by: Mr.php , Advocaite , brainiacX , yi12345 , Shadow , ronix ##
## Fixed by: Shadow - STARVATION , HERO FIXED COMPL. ##
## Fixed by: InCube - double troops ##
@@ -15,12 +15,14 @@
## ##
#################################################################################
include_once('../GameEngine/config.php');
if(isset($_GET['c']) && $_GET['c'] == 1) {
echo "<br /><hr /><br /><div class=\"headline\"><span class=\"f10 c5\">Error creating wdata. Check configuration or file.</span></div><br><br>";
}
if(isset($_GET['err']) && $_GET['err'] == 1) {
echo "<br /><hr /><br /><div class=\"headline\"><span class=\"f10 c5\">Existing World Data found in the database! Please clear your database before continuing.</span></div><br /><br />";
echo "<br /><hr /><br /><div class=\"headline\"><span class=\"f10 c5\">Existing World Data found in the database! Please empty tables <i>".TB_PREFIX."odata, ".TB_PREFIX."units, ".TB_PREFIX."vdata, ".TB_PREFIX."wdata</i> before continuing.</span></div><br /><br />";
}
?>
<form action="process.php" method="post" id="dataform">