diff --git a/.gitignore b/.gitignore index 97253b11..a93799b4 100644 --- a/.gitignore +++ b/.gitignore @@ -19,11 +19,10 @@ GameEngine/Prevention/* GameEngine/Notes/* # Install instalation_done marker left by install script -install/installation_done /.buildpath /.project /installed_*/ -/installation_done +/var/installed # Eclipse settings .settings diff --git a/.htaccess b/.htaccess index 3f010a84..69a1060b 100644 --- a/.htaccess +++ b/.htaccess @@ -1,9 +1,14 @@ - Order Deny,Allow + Order Allow,Deny Deny from all - Order Deny,Allow + Order Allow,Deny + Deny from all + + + + Order Allow,Deny Deny from all \ No newline at end of file diff --git a/Admin/Templates/resetServer.php b/Admin/Templates/resetServer.php index bcbaa444..855c0090 100644 --- a/Admin/Templates/resetServer.php +++ b/Admin/Templates/resetServer.php @@ -20,6 +20,11 @@ if (!isset($_SESSION)) { } if($_SESSION['access'] != ADMIN) die("

Access Denied: You are not Admin!

"); set_time_limit(0); +// TODO: truncate ALL tables (in a single query, not like this), +// then perform install steps (createDbStructure() && populateWorldData()) +// .. no need for updates and inserts here, as that would keep autoincrements high +// and one nice day, after 100th reset, there will be no more integers to go for +// and the whole game would be screwed :P mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."a2b"); mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."abdata"); mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."activate"); @@ -71,12 +76,11 @@ mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."tdata"); mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."tech_log"); mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."training"); mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."units"); +mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."wdata"); $time=time(); mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."odata"); -$database->populateOasisdata(); -$database->populateOasis(); -$database->populateOasisUnits2(); + $uid=$database->getVillageID(5); $passw=password_hash("12345", PASSWORD_BCRYPT,['cost' => 12]); @@ -93,7 +97,6 @@ mysqli_query($GLOBALS["link"], "INSERT INTO ".TB_PREFIX."tdata (vref) VALUES ($u mysqli_query($GLOBALS["link"], "INSERT INTO ".TB_PREFIX."fdata (vref, f1t, f2t, f3t, f4t, f5t, f6t, f7t, f8t, f9t, f10t, f11t, f12t, f13t, f14t, f15t, f16t, f17t, f18t, f26, f26t, wwname) VALUES ($uid, '1', '4', '1', '3', '2', '2', '3', '4', '4', '3', '3', '4', '4', '1', '4', '2', '1', '2', '1', '15', 'World Wonder')"); mysqli_query($GLOBALS["link"], "DELETE FROM ".TB_PREFIX."vdata WHERE owner<>5"); -mysqli_query($GLOBALS["link"], "UPDATE ".TB_PREFIX."wdata SET occupied=0 WHERE id<>$uid"); mysqli_query($GLOBALS["link"], "TRUNCATE TABLE ".TB_PREFIX."ww_attacks"); header("Location: ../admin.php?p=resetdone"); diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index f11d5cb8..c7c0903a 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -4726,11 +4726,15 @@ $wallimg = "query_return($q); - foreach($array as $oasis) { - $database->populateOasisUnits($oasis['wref'],$oasis['high']); - $database->updateOasis2($oasis['wref'], $time2); + if (count($array)) { + $ids = []; + foreach($array as $oasis) { + $ids[] = $oasis['wref']; + } + + $database->regenerateOasisUnits($ids, true); } } diff --git a/GameEngine/Database.php b/GameEngine/Database.php index c2d15d4c..8e6dc795 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -15,25 +15,26 @@ ## ## ################################################################################# -if (!isset($src_prefix)) { - $src_prefix = ''; - - if (substr(getcwd(), -5) === 'Admin') { - $src_prefix = '../'; - } - - if (substr(getcwd(), -4) === 'Mods') { - $src_prefix = '../../../'; - } - - if (substr(getcwd(), -7) === 'include') { - $src_prefix = '../../'; +global $autoprefix; + +// even with autoloader created, we can't use it here yet, as it's not been created +// ... so, let's see where it is and include it +$autoloader_found = false; +// go max 5 levels up - we don't have folders that go deeper than that +for ($i = 0; $i < 5; $i++) { + $autoprefix = str_repeat('../', $i); + if (file_exists($autoprefix.'autoloader.php')) { + $autoloader_found = true; + include_once $autoprefix.'autoloader.php'; + break; } } +if (!$autoloader_found) { + die('Could not find autoloading class.'); +} + include_once("config.php"); -include_once($src_prefix."src/Database/IDbConnection.php"); -include_once($src_prefix."src/Utils/Math.php"); use App\Database\IDbConnection; use App\Utils\Math; @@ -117,7 +118,7 @@ class MYSQLi_DB implements IDbConnection { // connect to the DB if (!$this->connect()) { - die(mysqli_errno($this->dblink)); + die(mysqli_error($this->dblink)); } // we will operate in UTF8 @@ -903,110 +904,25 @@ class MYSQLi_DB implements IDbConnection { } } - function populateOasis() { - $q = "INSERT INTO " . TB_PREFIX . "units (vref) SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype <> 0"; - mysqli_query($this->dblink,$q); - } + function regenerateOasisUnits($wid, $automation = false) { + global $autoprefix; - function populateOasisUnits($wid, $high) { - list($wid, $high) = $this->escape_input((int) $wid, $high); + if (is_array($wid)) { + $wid = implode(',', $wid); + } else { + $wid = (int) $wid; + } - $basearray = $this->getOasisInfo($wid); - if($high == 0){ - $max = rand(15,30); - }elseif($high == 1){ - $max = rand(50,70); - }elseif($high == 2){ - $max = rand(90,120); - } else { - $max = rand(50,70); - } - $max2 = 0; - $rand = rand(0,3); - if($rand == 1){ - $max2 = 3; - } - //each Troop is a Set for oasis type like mountains have rats spiders and snakes fields tigers elphants clay wolves so on stonger one more not so less - switch($basearray['type']) { - case 1: - case 2: - //+25% lumber per hour - $q = "UPDATE " . TB_PREFIX . "units SET u35 = u35 + '".rand(0,5)."', u36 = u36 + '".rand(0,5)."', u37 = u37 + '".rand(0,5)."' WHERE vref = '" . $wid . "' AND (u35 <= ".$max." OR u36 <= ".$max." OR u37 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - case 3: - //+25% lumber and +25% crop per hour - $q = "UPDATE " . TB_PREFIX . "units SET u35 = u35 + '".rand(0,5)."', u36 = u36 + '".rand(0,5)."', u37 = u37 + '".rand(0,5)."', u38 = u38 + '".rand(0,5)."', u40 = u40 + '".rand(0,$max2)."' WHERE vref = '" . $wid . "' AND (u36 <= ".$max." OR u37 <= ".$max." OR u38 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - case 4: - case 5: - //+25% clay per hour - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(0,5)."', u32 = u32 + '".rand(0,5)."', u35 = u35 + '".rand(0,5)."' WHERE vref = '" . $wid . "' AND (u31 <= ".$max." OR u32 <= ".$max." OR u35 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - case 6: - //+25% clay and +25% crop per hour - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(0,5)."', u32 = u32 + '".rand(0,5)."', u35 = u35 + '".rand(0,5)."', u40 = u40 + '".rand(0,$max2)."' WHERE vref = '" . $wid . "' AND (u31 <= ".$max." OR u32 <= ".$max." OR u35 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - case 7: - case 8: - //+25% iron per hour - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(0,5)."', u32 = u32 + '".rand(0,5)."', u34 = u34 + '".rand(0,5)."' WHERE vref = '" . $wid . "' AND (u31 <= ".$max." OR u32 <= ".$max." OR u34 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - case 9: - //+25% iron and +25% crop - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + '".rand(0,5)."', u32 = u32 + '".rand(0,5)."', u34 = u34 + '".rand(0,5)."', u39 = u39 + '".rand(0,$max2)."' WHERE vref = '" . $wid . "' AND (u31 <= ".$max." OR u32 <= ".$max." OR u34 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - case 10: - case 11: - //+25% crop per hour - $q = "UPDATE " . TB_PREFIX . "units SET u33 = u33 + '".rand(0,5)."', u37 = u37 + '".rand(0,5)."', u38 = u38 + '".rand(0,5)."', u39 = u39 + '".rand(0,$max2)."' WHERE vref = '" . $wid . "' AND (u33 <= ".$max." OR u37 <= ".$max." OR u38 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - case 12: - //+50% crop per hour - $q = "UPDATE " . TB_PREFIX . "units SET u33 = u33 + '".rand(0,5)."', u37 = u37 + '".rand(0,5)."', u38 = u38 + '".rand(0,5)."', u39 = u39 + '".rand(0,5)."', u40 = u40 + '".rand(0,$max2)."' WHERE vref = '" . $wid . "' AND (u33 <= ".$max." OR u37 <= ".$max." OR u38 <= ".$max." OR u39 <= ".$max.")"; - $result = mysqli_query($this->dblink,$q); - break; - } - } - - function populateOasisUnits2() { - // +25% lumber oasis - $q = "UPDATE " . TB_PREFIX . "units SET u35 = u35 + (FLOOR(5 + RAND() * 10)), u36 = u36 + (FLOOR(0 + RAND() * 5)), u37 = u37 + (FLOOR(0 + RAND() * 5)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(1,2)) AND u35 <= 10 AND u36 <= 10 AND u37 <= 10"; - mysqli_query($this->dblink,$q); - - // +25% lumber and +25% crop oasis - $q = "UPDATE " . TB_PREFIX . "units SET u35 = u35 + (FLOOR(5 + RAND() * 15)), u36 = u36 + (FLOOR(0 + RAND() * 5)), u37 = u37 + (FLOOR(0 + RAND() * 5)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(3)) AND u35 <= 10 AND u36 <= 10 AND u37 <= 10"; - mysqli_query($this->dblink,$q); - - // +25% clay oasis - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + (FLOOR(10 + RAND() * 15)), u32 = u32 + (FLOOR(5 + RAND() * 15)), u35 = u35 + (FLOOR(0 + RAND() * 10)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(4,5)) AND u31 <= 10 AND u32 <= 10 AND u35 <= 10"; - mysqli_query($this->dblink,$q); - - // +25% clay and +25% crop oasis - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + (FLOOR(15 + RAND() * 20)), u32 = u32 + (FLOOR(10 + RAND() * 15)), u35 = u35 + (FLOOR(0 + RAND() * 10)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(6)) AND u31 <= 10 AND u32 <= 10 AND u35 <= 10"; - mysqli_query($this->dblink,$q); - - // +25% iron oasis - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + (FLOOR(10 + RAND() * 15)), u32 = u32 + (FLOOR(5 + RAND() * 15)), u34 = u34 + (FLOOR(0 + RAND() * 10)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(7,8)) AND u31 <= 10 AND u32 <= 10 AND u34 <= 10"; - mysqli_query($this->dblink,$q); - - // +25% iron and +25% crop oasis - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + (FLOOR(15 + RAND() * 20)), u32 = u32 + (FLOOR(10 + RAND() * 15)), u34 = u34 + (FLOOR(0 + RAND() * 10)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(9)) AND u31 <= 10 AND u32 <= 10 AND u34 <= 10"; - mysqli_query($this->dblink,$q); - - // +25% crop oasis - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + (FLOOR(5 + RAND() * 15)), u33 = u33 + (FLOOR(5 + RAND() * 10)), u37 = u37 + (FLOOR(0 + RAND() * 10)), u39 = u39 + (FLOOR(0 + RAND() * 5)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(10,11)) AND u31 <= 10 AND u33 <= 10 AND u37 <= 10 AND u39 <= 10"; - mysqli_query($this->dblink,$q); - - // +50% crop oasis - $q = "UPDATE " . TB_PREFIX . "units SET u31 = u31 + (FLOOR(10 + RAND() * 15)), u33 = u33 + (FLOOR(5 + RAND() * 10)), u38 = u38 + (FLOOR(0 + RAND() * 5)), u39 = u39 + (FLOOR(0 + RAND() * 5)) WHERE vref IN(SELECT id FROM " . TB_PREFIX . "wdata WHERE oasistype IN(12)) AND u31 <= 10 AND u33 <= 10 AND u38 <= 10 AND u39 <= 10"; - mysqli_query($this->dblink,$q); + // load the oasis regeneration (in-game) and units generation (during install) SQL file + // and replace village ID for the given $wid + $str = file_get_contents($autoprefix."var/db/datagen-oasis-troops-regen.sql"); + $str = preg_replace(["'%PREFIX%'", "'%VILLAGEID%'", "'%NATURE_REG_TIME%'"], [TB_PREFIX, $wid, ($automation ? NATURE_REGTIME : -1)], $str); + $result = $this->dblink->multi_query($str); + if (!$result) { + return false; + } + + return true; } function removeOases($wref) { @@ -2279,15 +2195,6 @@ class MYSQLi_DB implements IDbConnection { return mysqli_query($this->dblink,$q); } - function updateOasis2($vid, $time) { - list($vid, $time) = $this->escape_input((int) $vid, $time); - - $time = time(); - $time2 = NATURE_REGTIME; - $q = "UPDATE " . TB_PREFIX . "odata set lastupdated2 = $time + $time2 where wref = $vid"; - return mysqli_query($this->dblink,$q); - } - function setVillageName($vid, $name) { list($vid, $name) = $this->escape_input((int) $vid, $name); @@ -3953,10 +3860,88 @@ class MYSQLi_DB implements IDbConnection { } } - function populateOasisdata() { - $q = "INSERT INTO " . TB_PREFIX . "odata SELECT id, oasistype, 0, 800, 800, 800, 800, 800, 800, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 100, 2 , \"Unoccupied Oasis\", CASE WHEN oasistype < 4 THEN 1 WHEN oasistype < 10 THEN 2 ELSE 0 END FROM " . TB_PREFIX . "wdata WHERE oasistype <> 0"; - mysqli_query($this->dblink,$q) OR DIE (mysqli_error($this->dblink)); - } + /** + * Creates a database structure for the game. + * Used during installation. + * + * @return boolean|number Returns TRUE, FALSE or -1. True is for successful data import + * (from prepared SQL file), false is in case of an SQL error. + * -1 will be returned in case of any unexpected behavior + * and unhandled exceptions. + */ + + public function createDbStructure() { + global $autoprefix; + + try { + // check that we don't have the structure in place already + // (we'd have at least 1 user present, since 4 are being created by default - Support, Nature, Multihunter & Taskmaster) + $data_exist = $this->query_return("SELECT * FROM " . TB_PREFIX . "users LIMIT 1"); + if ($data_exist && count($data_exist)) { + return false; + } + + // load the DB structure SQL file + $str = file_get_contents($autoprefix."var/db/struct.sql"); + $str = preg_replace("'%PREFIX%'", TB_PREFIX, $str); + $result = $this->dblink->multi_query($str); + if (!$result) { + echo mysqli_error($this->dblink); exit; + return false; + } + } catch (\Exception $e) { + return -1; + } + + return true; + } + + /** + * Populates the game database with Map World Data (i.e. creates the whole + * world with X,Y coordinate squares and their types). + * + * Also populates oasis' table data for the squares where there are oasis. + * + * @return boolean|number Returns TRUE, FALSE or -1. True is for successful data import + * (from prepared SQL file), false is in case of an SQL error. + * -1 will be returned in case of any unexpected behavior + * and unhandled exceptions. + */ + public function populateWorldData() { + global $autoprefix; + + try { + // check if we don't already have world data + $data_exist = $this->query_return("SELECT * FROM " . TB_PREFIX . "wdata LIMIT 1"); + if ($data_exist && count($data_exist)) { + return false; + } + + // load the data generation SQL file + $str = file_get_contents($autoprefix."var/db/datagen-world-data.sql"); + $str = preg_replace(["'%PREFIX%'", "'%WORLDSIZE%'"], [TB_PREFIX, WORLD_MAX], $str); + $result = $this->dblink->multi_query($str); + if (!$result) { + return -1; + } else { + // we need to flush the result of our previous multi_query + // or the next multi_query in regenerateOasisUnits() will cause + // a "Commands out of sync; you can't run this command now" error + // more info: https://stackoverflow.com/a/27926561/467164 + while (mysqli_next_result($this->dblink)) {;} + } + + $result = $this->regenerateOasisUnits(-1); + if (!$result) { + echo mysqli_error($this->dblink); + return -1; + } + } catch (\Exception $e) { + return -1; + } + + return true; + } public function getAvailableExpansionTraining() { global $building, $session, $technology, $village; diff --git a/GameEngine/Session.php b/GameEngine/Session.php index eee5a124..10b523f8 100755 --- a/GameEngine/Session.php +++ b/GameEngine/Session.php @@ -20,10 +20,9 @@ mb_internal_encoding("UTF-8"); // Add for utf8 varriables. ## ## ################################################################################# -if(file_exists('GameEngine/config.php') || file_exists('../../GameEngine/config.php') || file_exists('../../config.php') || file_exists('../GameEngine/config.php')) { -}else{ -header("Location: install/"); -exit; +if(!file_exists('GameEngine/config.php') && !file_exists('../GameEngine/config.php') && !file_exists('../../GameEngine/config.php') && !file_exists('../../config.php')) { + header("Location: install/"); + exit; } $script_name = ($_SERVER['REQUEST_URI'] == 'karte.php') ? 'karte' : $_SERVER['REQUEST_URI']; diff --git a/GameEngine/Units.php b/GameEngine/Units.php index 222c62ea..f262e8b1 100755 --- a/GameEngine/Units.php +++ b/GameEngine/Units.php @@ -248,11 +248,7 @@ class Units { } //set oasis to default if (count($getenforce1)>0) { - $q = "DELETE FROM ".TB_PREFIX."ndata WHERE toWref=".(int) $getenforce1[0]['vref']; - $database->query($q); - $database->populateOasisUnits($getenforce1[0]['vref'],$getenforce1[0]['high']); - $q = "UPDATE ".TB_PREFIX."odata SET conqured=0,wood=800,iron=800,clay=800,maxstore=800,crop=800,maxcrop=800,lastupdated=". time().",lastupdated2=".time().",loyalty=100,owner=2,name='Unoccupied Oasis' WHERE conqured=".(int) $wref; - $database->query($q); + $database->regenerateOasisUnits($getenforce1[0]['vref']); } } diff --git a/autoloader.php b/autoloader.php new file mode 100644 index 00000000..29caea20 --- /dev/null +++ b/autoloader.php @@ -0,0 +1,23 @@ +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; -} -?> diff --git a/install/include/oasis.php b/install/include/oasis.php deleted file mode 100644 index 226fd38f..00000000 --- a/install/include/oasis.php +++ /dev/null @@ -1,28 +0,0 @@ -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"); - -?> \ No newline at end of file diff --git a/install/include/wdata.php b/install/include/wdata.php deleted file mode 100644 index 5f794eae..00000000 --- a/install/include/wdata.php +++ /dev/null @@ -1,140 +0,0 @@ -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"); - -?> diff --git a/install/index.php b/install/index.php index 44f9b2ec..3873ceb4 100644 --- a/install/index.php +++ b/install/index.php @@ -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; }
@@ -74,7 +82,7 @@ function proceed() {
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"ERROR!
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"ERROR!
Installation appears to have been completed.
If this is an error remove installation_done file in install directory."; + else if (file_exists("../var/installed")) { + echo"ERROR!
Installation appears to have been completed.
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; } diff --git a/install/process.php b/install/process.php index d703ea96..68f4938e 100755 --- a/install/process.php +++ b/install/process.php @@ -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("


Can't open file: install\include\constant.php"); + private function constForm() { + $myFile = "../GameEngine/config.php"; + $fh = @fopen($myFile, 'w') or die("


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; } } diff --git a/install/templates/dataform.tpl b/install/templates/dataform.tpl index 76ec6380..a7404f31 100644 --- a/install/templates/dataform.tpl +++ b/install/templates/dataform.tpl @@ -14,20 +14,40 @@ ## Source code: https://github.com/Shadowss/TravianZ ## ## ## ################################################################################# +include_once('../GameEngine/config.php'); if(isset($_GET['c']) && $_GET['c'] == 1) { -echo "
Error importing database. Check configuration.

"; + echo "
Error importing database. Check configuration.

"; +} + +if(isset($_GET['err']) && $_GET['err'] == 1) { + echo "


Existing structure was found in the database! Please remove old game tables with the ".TB_PREFIX." prefix from the '".SQL_DB."' database before continuing.


"; } ?>
- + -

- Create SQL Structure - - - -
Warning: This can take some time. Wait till the next page has been loaded! Click Create to proceed..
-

+

+ Create Database Structure + + + + + + + + +
+ Warning: This can take some time. Please wait until the next page has been loaded. Click Create to proceed... +
+
+
+
+ +
+
+
+
+

diff --git a/install/templates/end.tpl b/install/templates/end.tpl index 32023afb..7d6ec50c 100755 --- a/install/templates/end.tpl +++ b/install/templates/end.tpl @@ -37,7 +37,7 @@ All the files are placed. The database is created, so you can now start playing

> My TravianZ homepage <
diff --git a/install/templates/menu.tpl b/install/templates/menu.tpl index 74b1ca8e..f21ac635 100644 --- a/install/templates/menu.tpl +++ b/install/templates/menu.tpl @@ -19,28 +19,25 @@ switch($_GET['s']) { case 0: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; + echo "
  • Intro
  • Configuration
  • Database
  • World Data
  • Multihunter
  • Support
  • End
  • "; break; case 1: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; + echo "
  • Intro
  • Configuration
  • Database
  • World Data
  • Multihunter
  • Support
  • End
  • "; break; case 2: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; + echo "
  • Intro
  • Configuration
  • Database
  • World Data
  • Multihunter
  • Support
  • End
  • "; break; case 3: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; + echo "
  • Intro
  • Configuration
  • Database
  • World Data
  • Multihunter
  • Support
  • End
  • "; break; case 4: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; + echo "
  • Intro
  • Configuration
  • Database
  • World Data
  • Multihunter
  • Support
  • End
  • "; break; case 5: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; + echo "
  • Intro
  • Configuration
  • Database
  • World Data
  • Multihunter
  • Support
  • End
  • "; break; case 6: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; - break; - case 7: - echo "
  • Intro
  • Configuration
  • Database
  • Field
  • Multihunter
  • Support
  • Oasis
  • End
  • "; + echo "
  • Intro
  • Configuration
  • Database
  • World Data
  • Multihunter
  • Support
  • End
  • "; break; } diff --git a/install/templates/multihunter.tpl b/install/templates/multihunter.tpl index 24b4407c..942536ae 100644 --- a/install/templates/multihunter.tpl +++ b/install/templates/multihunter.tpl @@ -14,7 +14,6 @@ ## Source code: https://github.com/Shadowss/TravianZ ## ## ## ################################################################################# -rename("include/constant.php","../GameEngine/config.php"); ?> diff --git a/install/templates/oasis.tpl b/install/templates/oasis.tpl deleted file mode 100644 index a67cc68e..00000000 --- a/install/templates/oasis.tpl +++ /dev/null @@ -1,24 +0,0 @@ -

    Existing Oasis data found in the database! Please clear the 'odata' table before continuing.


    "; -} - -?> -
    -

    - Populate Oasis - - - -
    Warning: This can take some time. Wait till the next page has been loaded! Click Create to proceed..
    -

    -
    -
    diff --git a/install/templates/field.tpl b/install/templates/wdata.tpl similarity index 85% rename from install/templates/field.tpl rename to install/templates/wdata.tpl index 99e478f9..d1e7b409 100644 --- a/install/templates/field.tpl +++ b/install/templates/wdata.tpl @@ -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 "


    Error creating wdata. Check configuration or file.


    "; } if(isset($_GET['err']) && $_GET['err'] == 1) { -echo "


    Existing World Data found in the database! Please clear your database before continuing.


    "; +echo "


    Existing World Data found in the database! Please empty tables ".TB_PREFIX."odata, ".TB_PREFIX."units, ".TB_PREFIX."vdata, ".TB_PREFIX."wdata before continuing.


    "; } ?>
    diff --git a/password.php b/password.php index cb5956cb..4f1e0661 100644 --- a/password.php +++ b/password.php @@ -9,7 +9,7 @@ ## Copyright: TravianX (c) 2010-2011. All rights reserved. ## ## ## ################################################################################# -if(!file_exists('GameEngine/config.php')) { +if(!file_exists('var/installed')) { header("Location: install/"); exit; } diff --git a/src/Entity/User.php b/src/Entity/User.php index 19a56dbc..87247920 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -14,21 +14,6 @@ namespace App\Entity; -if (!isset($src_prefix)) { - $src_prefix = ''; - - if (substr(getcwd(), -5) === 'Admin') { - $src_prefix = '../'; - } - - if (substr(getcwd(), -4) === 'Mods') { - $src_prefix = '../../../'; - } -} - -include_once($src_prefix."src/Database/IDbConnection.php"); -include_once($src_prefix."src/Utils/Math.php"); - use App\Database\IDbConnection; use App\Utils\Math; diff --git a/todo.txt b/todo.txt index 209295b2..df285918 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,5 @@ - link username to user when sending alliance forum notification message +- link username to user in message sender - add checkbox when sending units for a raid to include the attack in the raid list, if eligible - change "incoming attacker" title of the raid list icon when actually attacking a farm (it should be "Own attacking troops") - change "select all" button to actually select all checkboxes via JS, not via page reload diff --git a/var/db/datagen-oasis-troops-regen.sql b/var/db/datagen-oasis-troops-regen.sql new file mode 100644 index 00000000..6113cbcb --- /dev/null +++ b/var/db/datagen-oasis-troops-regen.sql @@ -0,0 +1,535 @@ +-- ---------------------------------------------------------------------------------------- +-- oasis regeneration script +-- used during installation, server reset, oasis reset & automation (nature repopulation) +-- +-- author: martinambrus +-- ---------------------------------------------------------------------------------------- + + +-- Nature regeneration time. +-- +-- type: int +-- description: when > -1, used to update the last oasis reset time (Automation nature regeneration) +-- when == -1, used to reset oasis data into original state (conquered > unoccupied) + +SET @natureRegTime = %NATURE_REG_TIME%; + + + +-- Oasis village ID. +-- +-- type: int +-- description: when > -1, used to regenerate units of a single oasis (when conquered > unoccupied) +-- when == -1, installation or server reset in progress, all oasis data are updated + +SET @village = %VILLAGEID%; + + +-- minimum and maximum number of units for oasis with "high" field set to 0 +SET @minUnitsForOasis0 = 15; +SET @maxUnitsForOasis0 = 30; + +-- minimum and maximum number of units for oasis with "high" field set to 1 +SET @minUnitsForOasis1 = 50; +SET @maxUnitsForOasis1 = 70; + +-- minimum and maximum number of units for oasis with "high" field set to 2 +SET @minUnitsForOasis2 = 90; +SET @maxUnitsForOasis2 = 120; + + + +-- ---------------------------------------- +-- reset oasis data (conquered > unoccupied) +-- ------------------------------------------ +UPDATE %PREFIX%odata + SET + conqured = 0, + wood = 800, + iron = 800, + clay = 800, + maxstore = 800, + crop = 800, + maxcrop = 800, + lastupdated = UNIX_TIMESTAMP(), + lastupdated2 = UNIX_TIMESTAMP(), + loyalty=100, + owner=2, + name='Unoccupied Oasis' + WHERE + @natureRegTime = -1 + AND + conqured IN ( @village ); + +-- --------------------------------------------- +-- remove past reports (conquered > unoccupied) +-- --------------------------------------------- +DELETE FROM %PREFIX%ndata + WHERE + @natureRegTime = -1 + AND + toWref IN ( @village ); + + +-- ---------------------------------------------------------------- +-- update next regeneration time (Automation, nature regeneration) +-- ---------------------------------------------------------------- +UPDATE + %PREFIX%odata +SET + lastupdated2 = UNIX_TIMESTAMP() + @natureRegTime +WHERE + @natureRegTime > -1 + AND + wref IN ( @village ); + + +-- ----------------------------------------------------------------------- +-- update number of units depending on the oasis type -- +-- the more lucrative the oasis is, the better defense will it get :-P -- +-- ----------------------------------------------------------------------- + + +-- +25% lumber oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u.u35 = u.u35 + (FLOOR(5 + RAND() * 10)), + u36 = u36 + (FLOOR(0 + RAND() * 5)), + u37 = u37 + (FLOOR(0 + RAND() * 5)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(1,2) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND + ( + u35 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + OR u36 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + OR u37 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + ); + +-- +25% lumber and +25% crop oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u35 = u35 + (FLOOR(5 + RAND() * 15)), + u36 = u36 + (FLOOR(0 + RAND() * 5)), + u37 = u37 + (FLOOR(0 + RAND() * 5)), + u38 = u38 + (FLOOR(0 + RAND() * 5)), + u40 = u40 + (FLOOR(0 + RAND() * 3)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(3) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND + ( + u36 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + OR u37 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + OR u38 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + ); + +-- +25% clay oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u31 = u31 + (FLOOR(10 + RAND() * 15)), + u32 = u32 + (FLOOR(5 + RAND() * 15)), + u35 = u35 + (FLOOR(0 + RAND() * 10)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(4,5) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND u31 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u32 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u35 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ); + +-- +25% clay and +25% crop oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u31 = u31 + (FLOOR(15 + RAND() * 20)), + u32 = u32 + (FLOOR(10 + RAND() * 15)), + u35 = u35 + (FLOOR(0 + RAND() * 10)), + u40 = u40 + (FLOOR(0 + RAND() * 3)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(6) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND u31 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u32 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u35 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ); + +-- +25% iron oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u31 = u31 + (FLOOR(10 + RAND() * 15)), + u32 = u32 + (FLOOR(5 + RAND() * 15)), + u34 = u34 + (FLOOR(0 + RAND() * 10)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(7,8) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND u31 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u32 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u34 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ); + +-- +25% iron and +25% crop oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u31 = u31 + (FLOOR(15 + RAND() * 20)), + u32 = u32 + (FLOOR(10 + RAND() * 15)), + u34 = u34 + (FLOOR(0 + RAND() * 10)), + u39 = u39 + (FLOOR(0 + RAND() * 3)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(9) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND u31 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u32 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u34 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ); + +-- +25% crop oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u31 = u31 + (FLOOR(5 + RAND() * 15)), + u33 = u33 + (FLOOR(5 + RAND() * 10)), + u37 = u37 + (FLOOR(0 + RAND() * 10)), + u38 = u38 + (FLOOR(0 + RAND() * 5)), + u39 = u39 + (FLOOR(0 + RAND() * 5)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(10,11) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND u31 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u33 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u37 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u38 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ); + +-- +50% crop oasis +UPDATE %PREFIX%units u + JOIN %PREFIX%odata o + ON u.vref = o.wref + SET + u31 = u31 + (FLOOR(10 + RAND() * 15)), + u33 = u33 + (FLOOR(5 + RAND() * 10)), + u37 = u37 + (FLOOR(0 + RAND() * 10)), + u38 = u38 + (FLOOR(0 + RAND() * 5)), + u39 = u39 + (FLOOR(0 + RAND() * 5)), + u40 = u40 + (FLOOR(0 + RAND() * 3)) + WHERE + ( + ( + @village = -1 + AND + vref IN( + SELECT + id + FROM + s1_wdata + WHERE + oasistype IN(12) + ) + ) + OR + ( + @village > -1 + AND + vref = @village + ) + ) + AND u31 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u33 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u37 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u38 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ) + AND u39 <= ( + CASE o.high + WHEN 0 THEN (FLOOR(@minUnitsForOasis0 + RAND() * @maxUnitsForOasis0)) + WHEN 1 THEN (FLOOR(@minUnitsForOasis1 + RAND() * @maxUnitsForOasis1)) + WHEN 2 THEN (FLOOR(@minUnitsForOasis2 + RAND() * @maxUnitsForOasis2)) + END + ); \ No newline at end of file diff --git a/var/db/datagen-world-data.sql b/var/db/datagen-world-data.sql new file mode 100644 index 00000000..1187f2ce --- /dev/null +++ b/var/db/datagen-world-data.sql @@ -0,0 +1,148 @@ +-- generate world map data +INSERT INTO %PREFIX%wdata + + -- this select gets the right number of columns for the wdata table + SELECT 0 as id, fieldtype, oasistype, x, y, 0 as occupied, image FROM + + -- this select prepares (i.e. generates) the world data + (SELECT + + -- save a random number from 1 to 1000 into a variable + @rnd := (FLOOR(1 + RAND() * 1000)), + + -- fieldtype is always 3 for the middle and the word border + IF ( + (x = 0 AND y = 0) OR (x = 100 AND y = 100), + 3, + -- get a field type based on the random number previously generated + CASE + WHEN @rnd <= 10 THEN @ftype := 1 + WHEN @rnd <= 90 THEN @ftype := 2 + WHEN @rnd <= 400 THEN @ftype := 3 + WHEN @rnd <= 480 THEN @ftype := 4 + WHEN @rnd <= 560 THEN @ftype := 5 + WHEN @rnd <= 570 THEN @ftype := 6 + WHEN @rnd <= 600 THEN @ftype := 7 + WHEN @rnd <= 630 THEN @ftype := 8 + WHEN @rnd <= 660 THEN @ftype := 9 + WHEN @rnd <= 740 THEN @ftype := 10 + WHEN @rnd <= 820 THEN @ftype := 11 + WHEN @rnd <= 900 THEN @ftype := 12 + WHEN @rnd <= 1000 THEN @ftype := 0 + END + ) as fieldtype, + + -- there are no oasis' in the middle and by the word border + IF ( + (x = 0 AND y = 0) OR (x = 100 AND y = 100), + 0, + -- get an oasis type if the field type generated in the previous IF statement + -- is 0, based on the random number previously generated + CASE + WHEN @ftype > 0 THEN @otype := 0 + WHEN @rnd <= 908 THEN @otype := 1 + WHEN @rnd <= 916 THEN @otype := 2 + WHEN @rnd <= 924 THEN @otype := 3 + WHEN @rnd <= 932 THEN @otype := 4 + WHEN @rnd <= 940 THEN @otype := 5 + WHEN @rnd <= 948 THEN @otype := 6 + WHEN @rnd <= 956 THEN @otype := 7 + WHEN @rnd <= 964 THEN @otype := 8 + WHEN @rnd <= 972 THEN @otype := 9 + WHEN @rnd <= 980 THEN @otype := 10 + WHEN @rnd <= 988 THEN @otype := 11 + ELSE @otype := 12 + END + ) as oasistype, + + -- x and y coordinates come from the subqueries below + x, y, + + -- create a random image name for the field or the oasis square + IF (@otype = 0, CONCAT("t", (FLOOR(0 + RAND() * 9)) ), CONCAT("o", @otype) ) as image + FROM + + -- the following select will generate a number from -%WORLDSIZE% to +%WORLDSIZE% as an X coordinate + -- (courtesy of Unreason, https://stackoverflow.com/a/2652051/467164) + -- this first line will keep incrementing @row until we run out of all the data provided by the "t" subselects below + (SELECT @row := @row + 1 as x FROM + + -- t and t2 each contain 10 rows of dummy data, + -- cartesian product of these is 10^4, i.e. 10000 rows of dummy data + -- and the outer select is just mysql version of rownumber + (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t, + (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, + + -- in t3, we only need 4 rows of dummy data, as 400 is currently the maximum allowed map size + -- which brings us to `t1` x `t2` x `t3` = 10 x 10 x 4 = 400 + (select 0 union select 1 union select 2 union select 3) t3, + + -- here we tell MySQL where to start, so if we have a world 100x100, this will set @row to -101 + -- (not -100 because the first select already increments the @row by 1, so we'd start at -99 instead) + (SELECT @row := -(%WORLDSIZE% + 1)) as final + + -- since we maxed out the potential of world generation to number 400, we need to use a WHERE here + -- as to only get the number of rows we need for the current map size + WHERE @row <= (%WORLDSIZE% - 1)) as x, + + -- this query is the same as previous query for X coordinate but will generate numbers + -- for the Y coordinate - both of these joined together this way will generate data such as: + -- -100, -100; -100, -99; -100, -98 ... + (SELECT @row2 := @row2 + 1 as y FROM + (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t, + (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, + (select 0 union select 1 union select 2) t3, + (SELECT @row2 := -(%WORLDSIZE% + 1)) as final + WHERE @row2 <= (%WORLDSIZE% - 1)) as y + ) as generator; + +-- populate oasis data +INSERT INTO %PREFIX%odata + SELECT + -- automatic ID + id, + -- type of oasis from wdata table + oasistype, + -- oasis is not conquered + 0, + -- wood + 800, + -- iron + 800, + -- clay + 800, + -- maximum storage for the 3 resources above + 800, + -- crop + 800, + -- maximum storage for crop + 800, + -- last updated timestamps + UNIX_TIMESTAMP(), + UNIX_TIMESTAMP(), + -- loyalty (100%) + 100, + -- owner (2 = Nature) + 2, + -- name for this square + "Unoccupied Oasis", + -- how many units would be (re)generated for this oasis, based on its type + CASE + WHEN oasistype < 4 THEN 1 + WHEN oasistype < 10 THEN 2 + ELSE 0 + END + FROM + %PREFIX%wdata + WHERE + oasistype <> 0; + + +-- create some defensive units for existing oasis +INSERT INTO %PREFIX%units (vref) + SELECT + id + FROM + %PREFIX%wdata + WHERE + oasistype <> 0; \ No newline at end of file diff --git a/install/data/sql.sql b/var/db/struct.sql similarity index 99% rename from install/data/sql.sql rename to var/db/struct.sql index 07fe5762..8c5bb42f 100644 --- a/install/data/sql.sql +++ b/var/db/struct.sql @@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS `%PREFIX%a2b` ( -- Table structure for table `%PREFIX%links` -- -CREATE TABLE `%PREFIX%links` ( +CREATE TABLE IF NOT EXISTS `%PREFIX%links` ( `id` INT( 25 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `userid` INT( 25 ) NULL , `name` VARCHAR( 50 ) NULL ,