diff --git a/server/bootstrap.php b/server/bootstrap.php index bf5933c..48db9c4 100644 --- a/server/bootstrap.php +++ b/server/bootstrap.php @@ -34,6 +34,7 @@ const MAP = [ // models 'Classes' => SERVER.'/models/Classes.php', 'Player' => SERVER.'/models/Player.php', + 'Spell' => SERVER.'/models/Spell.php', ]; // autoloader diff --git a/server/database/packs/Default/class_spells.csv b/server/database/packs/Default/class_spells.csv new file mode 100644 index 0000000..f7ff671 --- /dev/null +++ b/server/database/packs/Default/class_spells.csv @@ -0,0 +1,6 @@ +Level,"Class ID","Spell ID" +1,1,6 +1,1,18 +1,3,1 +1,3,15 +1,3,18 \ No newline at end of file diff --git a/server/database/packs/Default/classes.csv b/server/database/packs/Default/classes.csv index a0cefdb..dcdc26a 100644 --- a/server/database/packs/Default/classes.csv +++ b/server/database/packs/Default/classes.csv @@ -1,4 +1,4 @@ -Name,"Start HP","Start MP","Start STR","Start ATK","Start DEF","Start DEX","Growth HP","Growth MP","Growth STR","Growth ATK","Growth DEF","Growth DEX",Spells -Mage,10,10,5,5,5,5,3,5,1,3,1,3,"1:6,18" -Warrior,20,0,10,5,10,5,6,2,3,1,3,1, -Paladin,15,5,5,5,10,10,4,4,2,2,2,2,"1:1,15,18" +Name,"Start HP","Start MP","Start STR","Start ATK","Start DEF","Start DEX","Growth HP","Growth MP","Growth STR","Growth ATK","Growth DEF","Growth DEX" +Mage,10,10,5,5,5,5,3,5,1,3,1,3 +Warrior,20,0,10,5,10,5,6,2,3,1,3,1 +Paladin,15,5,5,5,10,10,4,4,2,2,2,2 diff --git a/server/models/Classes.php b/server/models/Classes.php index 14ad5a1..c75745a 100644 --- a/server/models/Classes.php +++ b/server/models/Classes.php @@ -14,18 +14,16 @@ class Classes return $res->fetch() ?: false; } - // Spell definitions are in the DB under the spell columns; - // level:spell_id,spell_id|level:spell_id - // This means that as level increases, more spells are available. - public static function spellsToArray(string $spells): array + public static function spells(int $id): array|false { - $list = []; - $groups = explode('|', $spells); - foreach ($groups as $group) { - $parts = explode(':', $group); - // first part will be level, second part will be spell_ids - $list[$parts[0]] = explode(',', $parts[1]); - } - return $list; + $res = App::$db->do("SELECT level, spell_id FROM class_spells WHERE class_id = ?", [$id]); + return $res->fetchAll() ?: false; + } + + public static function spellsAtLevel(int $id, int $level): array|false + { + // get all spells for the class under or equal to the level + $ids = App::$db->do("SELECT spell_id FROM class_spells WHERE class_id = ? AND level <= ?", [$id, $level]); + return $ids->fetchAll(PDO::FETCH_COLUMN) ?: false; } } \ No newline at end of file diff --git a/server/models/Player.php b/server/models/Player.php index d9df48c..7f79cde 100644 --- a/server/models/Player.php +++ b/server/models/Player.php @@ -22,6 +22,12 @@ class Player $data['stat_points'] = $data['stat_points'] ?? (App::$s['stat_point_gain'] * ($l - 1)); $data['exp2l'] = $data['exp2l'] ?? expToLevel($l + 1); + // award spells + if (!isset($data['spells']) || empty($data['spells'])) { + $spells = Classes::spellsAtLevel($class['id'], $l); + $data['spells'] = implode(',', $spells); + } + // compress data and generate placeholers $keys = implode(', ', array_keys($data)); $placeholders = implode(', ', array_fill(0, count($data), '?')); diff --git a/server/models/Spell.php b/server/models/Spell.php new file mode 100644 index 0000000..b3520eb --- /dev/null +++ b/server/models/Spell.php @@ -0,0 +1,27 @@ +do("SELECT * FROM spells"); + return $spells->fetchAll() ?: false; + } + + public static function get(int $id): array|false + { + $spell = App::$db->do("SELECT * FROM spells WHERE id = ?", [$id]); + return $spell->fetch() ?: false; + } + + public static function getFromList(string|array $list): array + { + if (is_string($list)) $list = explode(',', $list); + $spells = []; + foreach ($list as $id) { + $spell = self::get($id); + if ($spell !== false) $spells[] = $spell; + } + return $spells; + } +} \ No newline at end of file diff --git a/server/modules/InstallModule.php b/server/modules/InstallModule.php index 8dd7ce8..2d8461f 100644 --- a/server/modules/InstallModule.php +++ b/server/modules/InstallModule.php @@ -34,7 +34,7 @@ class InstallModule // @Settings App::$db->q("CREATE TABLE IF NOT EXISTS 'settings' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'game_name' TEXT DEFAULT 'Dragon Knight', 'game_version' TEXT DEFAULT '1.0', 'game_dev' TEXT DEFAULT 'Sharkk', @@ -57,7 +57,7 @@ class InstallModule // @Classes App::$db->q("CREATE TABLE IF NOT EXISTS 'classes' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' TEXT DEFAULT '', 'start_hp' INT DEFAULT 0, 'start_mp' INT DEFAULT 0, @@ -70,8 +70,7 @@ class InstallModule 'growth_str' INT DEFAULT 0, 'growth_atk' INT DEFAULT 0, 'growth_dex' INT DEFAULT 0, - 'growth_def' INT DEFAULT 0, - 'spells' TEXT DEFAULT '' + 'growth_def' INT DEFAULT 0 );"); if ($complete) { @@ -84,7 +83,7 @@ class InstallModule // @Babble App::$db->q("CREATE TABLE IF NOT EXISTS 'babble' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'author' INTEGER NOT NULL, 'babble' TEXT NOT NULL, 'posted' DATETIME DEFAULT CURRENT_TIMESTAMP @@ -92,7 +91,7 @@ class InstallModule // @Drops App::$db->q("CREATE TABLE IF NOT EXISTS 'drops' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' TEXT NOT NULL, 'level' INTEGER DEFAULT 1, 'type' INTEGER DEFAULT 1, @@ -104,7 +103,7 @@ class InstallModule // @Forum App::$db->q("CREATE TABLE IF NOT EXISTS 'forum' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'posted' DATETIME DEFAULT CURRENT_TIMESTAMP, 'new_post' DATETIME DEFAULT CURRENT_TIMESTAMP, 'author' INTEGER NOT NULL, @@ -120,7 +119,7 @@ class InstallModule // @Items App::$db->q("CREATE TABLE IF NOT EXISTS 'items' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'type' INTEGER DEFAULT 1, 'name' TEXT NOT NULL, 'cost' INTEGER DEFAULT 0, @@ -133,7 +132,7 @@ class InstallModule // @Monsters App::$db->q("CREATE TABLE IF NOT EXISTS 'monsters' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' TEXT NOT NULL, 'level' INTEGER DEFAULT 1, 'hp' INTEGER DEFAULT 1, @@ -150,7 +149,7 @@ class InstallModule // @News App::$db->q("CREATE TABLE IF NOT EXISTS 'news' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'author' INTEGER DEFAULT 1, 'title' TEXT DEFAULT '', 'content' TEXT DEFAULT '', @@ -159,7 +158,7 @@ class InstallModule // @Spells App::$db->q("CREATE TABLE IF NOT EXISTS 'spells' ( - 'id' INTEGER PRIMARY KEY, + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' TEXT NOT NULL, 'type' INTEGER DEFAULT 1, 'mp' INTEGER DEFAULT 0, @@ -170,6 +169,16 @@ class InstallModule // add default spells if complete install if ($complete) App::$db->insertFromCSV('spells', "$defaults/spells.csv"); + // @LearnedSpells + App::$db->q("CREATE TABLE IF NOT EXISTS 'class_spells' ( + 'level' INTEGER NOT NULL, + 'class_id' INTEGER NOT NULL, + 'spell_id' INTEGER NOT NULL + );"); + + // add default class spells if complete install + if ($complete) App::$db->insertFromCSV('class_spells', "$defaults/class_spells.csv"); + // @Towns App::$db->q("CREATE TABLE IF NOT EXISTS 'towns' ( 'id' INTEGER PRIMARY KEY, @@ -258,8 +267,8 @@ class InstallModule } } - // Make sure the class selection is valid - $class = isset($_POST['class']) ? $_POST['class'] : 1; + // Make sure the class selection is present + $class = $_POST['class'] ?? 1; // If we have any errors, bail to the form and let the user know if (!empty($errors)) { @@ -267,9 +276,6 @@ class InstallModule exit; } - // Create the .installed file in the server folder - file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s')); - // Create the admin account Player::create([ 'username' => trim($_POST['username']), @@ -283,6 +289,9 @@ class InstallModule // Render the finished page! echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]); + + // Create the .installed file in the server folder + file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s')); } private static function fourOhFour() diff --git a/server/templates/install/partials/adminForm.php b/server/templates/install/partials/adminForm.php index 09630bb..6a521ac 100644 --- a/server/templates/install/partials/adminForm.php +++ b/server/templates/install/partials/adminForm.php @@ -29,7 +29,7 @@
- +