From 201f95caa8b70e88fa651eac8c3c3aa7cd765bd0 Mon Sep 17 00:00:00 2001 From: slawkens Date: Thu, 5 Sep 2024 15:36:19 +0200 Subject: [PATCH] Do not create player if there is no players table in db --- install/index.php | 15 +++--- install/steps/6-admin.php | 1 + install/steps/7-finish.php | 60 +++++++++++++----------- install/tools/7-finish.php | 41 ++++++++-------- system/migrations/20.php | 4 ++ system/templates/install.admin.html.twig | 8 +++- 6 files changed, 74 insertions(+), 55 deletions(-) diff --git a/install/index.php b/install/index.php index 6536829e..1031a2b8 100644 --- a/install/index.php +++ b/install/index.php @@ -114,7 +114,7 @@ else if($step == 'finish') { $email = $_SESSION['var_email']; $password = $_SESSION['var_password']; $password_confirm = $_SESSION['var_password_confirm']; - $player_name = $_SESSION['var_player_name']; + $player_name = $_SESSION['var_player_name'] ?? null; // email check if(empty($email)) { @@ -159,12 +159,13 @@ else if($step == 'finish') { $errors[] = $locale['step_admin_password_confirm_error_not_same']; } - // player name check - if(empty($player_name)) { - $errors[] = $locale['step_admin_player_name_error_empty']; - } - else if(!Validator::characterName($player_name)) { - $errors[] = $locale['step_admin_player_name_error_format']; + if (isset($player_name)) { + // player name check + if (empty($player_name)) { + $errors[] = $locale['step_admin_player_name_error_empty']; + } else if (!Validator::characterName($player_name)) { + $errors[] = $locale['step_admin_player_name_error_format']; + } } if(!empty($errors)) { diff --git a/install/steps/6-admin.php b/install/steps/6-admin.php index e410fc93..ab30b477 100644 --- a/install/steps/6-admin.php +++ b/install/steps/6-admin.php @@ -18,6 +18,7 @@ if(!$error) { 'locale' => $locale, 'session' => $_SESSION, 'account' => $account, + 'hasTablePlayers' => $db->hasTable('players'), 'errors' => isset($errors) ? $errors : null, 'buttons' => next_buttons(true, $error ? false : true) )); diff --git a/install/steps/7-finish.php b/install/steps/7-finish.php index 84c2cce4..0ae302ea 100644 --- a/install/steps/7-finish.php +++ b/install/steps/7-finish.php @@ -42,23 +42,25 @@ if(isset($account)) else $account_db->load($account_id); -$player_name = $_SESSION['var_player_name']; -$player_db = new OTS_Player(); -$player_db->find($player_name); +if ($db->hasTable('players')) { + $player_name = $_SESSION['var_player_name']; + $player_db = new OTS_Player(); + $player_db->find($player_name); -if(!$player_db->isLoaded()) -{ - $player = new OTS_Player(); - $player->setName($player_name); + if(!$player_db->isLoaded()) + { + $player = new OTS_Player(); + $player->setName($player_name); - $player_used = &$player; + $player_used = &$player; + } + else { + $player_used = &$player_db; + } + + $groups = new OTS_Groups_List(); + $player_used->setGroupId($groups->getHighestId()); } -else { - $player_used = &$player_db; -} - -$groups = new OTS_Groups_List(); -$player_used->setGroupId($groups->getHighestId()); $email = $_SESSION['var_email']; if($account_db->isLoaded()) { @@ -100,10 +102,16 @@ if($db->hasColumn('accounts', 'group_id')) if($db->hasColumn('accounts', 'type')) $account_used->setCustomField('type', 6); -if(!$player_db->isLoaded()) - $player->setAccountId($account_used->getId()); -else - $player_db->setAccountId($account_used->getId()); +if ($db->hasTable('players')) { + if(!$player_db->isLoaded()) { + $player->setAccountId($account_used->getId()); + $player->save(); + } + else { + $player_db->setAccountId($account_used->getId()); + $player_db->save(); + } +} success($locale['step_database_created_account']); @@ -111,18 +119,14 @@ setSession('account', $account_used->getId()); setSession('password', encrypt($password)); setSession('remember_me', true); -if($player_db->isLoaded()) { - $player_db->save(); -} -else { - $player->save(); -} - if(!News::all()->count()) { $player_id = 0; - $tmpNewsPlayer = \MyAAC\Models\Player::where('name', $player_name)->first(); - if($tmpNewsPlayer) { - $player_id = $tmpNewsPlayer->id; + + if ($db->hasTable('players')) { + $tmpNewsPlayer = \MyAAC\Models\Player::where('name', $player_name)->first(); + if($tmpNewsPlayer) { + $player_id = $tmpNewsPlayer->id; + } } News::create([ diff --git a/install/tools/7-finish.php b/install/tools/7-finish.php index 37914a5f..8101b07d 100644 --- a/install/tools/7-finish.php +++ b/install/tools/7-finish.php @@ -25,30 +25,33 @@ if(isset($config['installed']) && $config['installed'] && !isset($_SESSION['save require SYSTEM . 'init.php'; -$deleted = 'deleted'; -if($db->hasColumn('players', 'deletion')) - $deleted = 'deletion'; +if ($db->hasTable('players')) { + $deleted = 'deleted'; + if ($db->hasColumn('players', 'deletion')) + $deleted = 'deletion'; -$time = time(); -function insert_sample_if_not_exist($p) { - global $db, $success, $deleted, $time; + $time = time(); + function insert_sample_if_not_exist($p) + { + global $db, $success, $deleted, $time; - $query = $db->query('SELECT `id` FROM `players` WHERE `name` = ' . $db->quote($p['name'])); - if($query->rowCount() == 0) { - if(!query("INSERT INTO `players` (`id`, `name`, `group_id`, `account_id`, `level`, `vocation`, `health`, `healthmax`, `experience`, `lookbody`, `lookfeet`, `lookhead`, `looklegs`, `looktype`, `maglevel`, `mana`, `manamax`, `manaspent`, `soul`, `town_id`, `posx`, `posy`, `posz`, `conditions`, `cap`, `sex`, `lastlogin`, `lastip`, `save`, `lastlogout`, `balance`, `$deleted`, `created`, `hide`, `comment`) VALUES (null, " . $db->quote($p['name']) . ", 1, " . getSession('account') . ", " . $p['level'] . ", " . $p['vocation_id'] . ", " . $p['health'] . ", " . $p['healthmax'] . ", " . $p['experience'] . ", 118, 114, 38, 57, " . $p['looktype'] . ", 0, " . $p['mana'] . ", " . $p['manamax'] . ", 0, " . $p['soul'] . ", 1, 1000, 1000, 7, '', " . $p['cap'] . ", 1, " . $time . ", 2130706433, 1, " . $time . ", 0, 0, " . $time . ", 1, '');")) - $success = false; + $query = $db->query('SELECT `id` FROM `players` WHERE `name` = ' . $db->quote($p['name'])); + if ($query->rowCount() == 0) { + if (!query("INSERT INTO `players` (`id`, `name`, `group_id`, `account_id`, `level`, `vocation`, `health`, `healthmax`, `experience`, `lookbody`, `lookfeet`, `lookhead`, `looklegs`, `looktype`, `maglevel`, `mana`, `manamax`, `manaspent`, `soul`, `town_id`, `posx`, `posy`, `posz`, `conditions`, `cap`, `sex`, `lastlogin`, `lastip`, `save`, `lastlogout`, `balance`, `$deleted`, `created`, `hide`, `comment`) VALUES (null, " . $db->quote($p['name']) . ", 1, " . getSession('account') . ", " . $p['level'] . ", " . $p['vocation_id'] . ", " . $p['health'] . ", " . $p['healthmax'] . ", " . $p['experience'] . ", 118, 114, 38, 57, " . $p['looktype'] . ", 0, " . $p['mana'] . ", " . $p['manamax'] . ", 0, " . $p['soul'] . ", 1, 1000, 1000, 7, '', " . $p['cap'] . ", 1, " . $time . ", 2130706433, 1, " . $time . ", 0, 0, " . $time . ", 1, '');")) + $success = false; + } } -} -$success = true; -insert_sample_if_not_exist(array('name' => 'Rook Sample', 'level' => 1, 'vocation_id' => 0, 'health' => 150, 'healthmax' => 150, 'experience' => 0, 'looktype' => 130, 'mana' => 0, 'manamax' => 0, 'soul' => 100, 'cap' => 400)); -insert_sample_if_not_exist(array('name' => 'Sorcerer Sample', 'level' => 8, 'vocation_id' => 1, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 130, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); -insert_sample_if_not_exist(array('name' => 'Druid Sample', 'level' => 8, 'vocation_id' => 2, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 130, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); -insert_sample_if_not_exist(array('name' => 'Paladin Sample', 'level' => 8, 'vocation_id' => 3, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 129, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); -insert_sample_if_not_exist(array('name' => 'Knight Sample', 'level' => 8, 'vocation_id' => 4, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 131, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); + $success = true; + insert_sample_if_not_exist(array('name' => 'Rook Sample', 'level' => 1, 'vocation_id' => 0, 'health' => 150, 'healthmax' => 150, 'experience' => 0, 'looktype' => 130, 'mana' => 0, 'manamax' => 0, 'soul' => 100, 'cap' => 400)); + insert_sample_if_not_exist(array('name' => 'Sorcerer Sample', 'level' => 8, 'vocation_id' => 1, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 130, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); + insert_sample_if_not_exist(array('name' => 'Druid Sample', 'level' => 8, 'vocation_id' => 2, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 130, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); + insert_sample_if_not_exist(array('name' => 'Paladin Sample', 'level' => 8, 'vocation_id' => 3, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 129, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); + insert_sample_if_not_exist(array('name' => 'Knight Sample', 'level' => 8, 'vocation_id' => 4, 'health' => 185, 'healthmax' => 185, 'experience' => 4200, 'looktype' => 131, 'mana' => 90, 'manamax' => 90, 'soul' => 100, 'cap' => 470)); -if($success) { - success($locale['step_database_imported_players']); + if ($success) { + success($locale['step_database_imported_players']); + } } Plugins::installMenus('kathrine', require TEMPLATES . 'kathrine/menus.php'); diff --git a/system/migrations/20.php b/system/migrations/20.php index e7b5ee0a..1255fa14 100644 --- a/system/migrations/20.php +++ b/system/migrations/20.php @@ -2,6 +2,10 @@ use MyAAC\Settings; +if (!$db->hasTable('players')) { + return; +} + $query = $db->query("SELECT `id` FROM `players` WHERE (`name` = " . $db->quote("Rook Sample") . " OR `name` = " . $db->quote("Sorcerer Sample") . " OR `name` = " . $db->quote("Druid Sample") . " OR `name` = " . $db->quote("Paladin Sample") . " OR `name` = " . $db->quote("Knight Sample") . " OR `name` = " . $db->quote("Account Manager") . ") ORDER BY `id`;"); $highscores_ignored_ids = array(); diff --git a/system/templates/install.admin.html.twig b/system/templates/install.admin.html.twig index a3a306a8..1d64eea9 100644 --- a/system/templates/install.admin.html.twig +++ b/system/templates/install.admin.html.twig @@ -9,7 +9,13 @@
- {% for value in ['email', 'account', 'password', 'password_confirm', 'player_name'] %} + {% set values = ['email', 'account', 'password', 'password_confirm'] %} + + {% if hasTablePlayers %} + {% set values = values|merge(['player_name']) %} + {% endif %} + + {% for value in values %}