Update class spell concept, re-finish install, add level control for admin account

This commit is contained in:
Sky Johnson 2024-07-15 16:24:52 -05:00
parent b328263aef
commit 28789a26e7
8 changed files with 80 additions and 33 deletions

View File

@ -34,6 +34,7 @@ const MAP = [
// models // models
'Classes' => SERVER.'/models/Classes.php', 'Classes' => SERVER.'/models/Classes.php',
'Player' => SERVER.'/models/Player.php', 'Player' => SERVER.'/models/Player.php',
'Spell' => SERVER.'/models/Spell.php',
]; ];
// autoloader // autoloader

View File

@ -0,0 +1,6 @@
Level,"Class ID","Spell ID"
1,1,6
1,1,18
1,3,1
1,3,15
1,3,18
1 Level Class ID Spell ID
2 1 1 6
3 1 1 18
4 1 3 1
5 1 3 15
6 1 3 18

View File

@ -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 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,"1:6,18" 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, 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" Paladin,15,5,5,5,10,10,4,4,2,2,2,2

1 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
2 Mage 10 10 5 5 5 5 3 5 1 3 1 3 1:6,18
3 Warrior 20 0 10 5 10 5 6 2 3 1 3 1
4 Paladin 15 5 5 5 10 10 4 4 2 2 2 2 1:1,15,18

View File

@ -14,18 +14,16 @@ class Classes
return $res->fetch() ?: false; return $res->fetch() ?: false;
} }
// Spell definitions are in the DB under the spell columns; public static function spells(int $id): array|false
// 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
{ {
$list = []; $res = App::$db->do("SELECT level, spell_id FROM class_spells WHERE class_id = ?", [$id]);
$groups = explode('|', $spells); return $res->fetchAll() ?: false;
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;
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;
} }
} }

View File

@ -22,6 +22,12 @@ class Player
$data['stat_points'] = $data['stat_points'] ?? (App::$s['stat_point_gain'] * ($l - 1)); $data['stat_points'] = $data['stat_points'] ?? (App::$s['stat_point_gain'] * ($l - 1));
$data['exp2l'] = $data['exp2l'] ?? expToLevel($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 // compress data and generate placeholers
$keys = implode(', ', array_keys($data)); $keys = implode(', ', array_keys($data));
$placeholders = implode(', ', array_fill(0, count($data), '?')); $placeholders = implode(', ', array_fill(0, count($data), '?'));

27
server/models/Spell.php Normal file
View File

@ -0,0 +1,27 @@
<?php
class Spell
{
public static function all(): array|false
{
$spells = App::$db->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;
}
}

View File

@ -34,7 +34,7 @@ class InstallModule
// @Settings // @Settings
App::$db->q("CREATE TABLE IF NOT EXISTS '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_name' TEXT DEFAULT 'Dragon Knight',
'game_version' TEXT DEFAULT '1.0', 'game_version' TEXT DEFAULT '1.0',
'game_dev' TEXT DEFAULT 'Sharkk', 'game_dev' TEXT DEFAULT 'Sharkk',
@ -57,7 +57,7 @@ class InstallModule
// @Classes // @Classes
App::$db->q("CREATE TABLE IF NOT EXISTS 'classes' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'classes' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'name' TEXT DEFAULT '', 'name' TEXT DEFAULT '',
'start_hp' INT DEFAULT 0, 'start_hp' INT DEFAULT 0,
'start_mp' INT DEFAULT 0, 'start_mp' INT DEFAULT 0,
@ -70,8 +70,7 @@ class InstallModule
'growth_str' INT DEFAULT 0, 'growth_str' INT DEFAULT 0,
'growth_atk' INT DEFAULT 0, 'growth_atk' INT DEFAULT 0,
'growth_dex' INT DEFAULT 0, 'growth_dex' INT DEFAULT 0,
'growth_def' INT DEFAULT 0, 'growth_def' INT DEFAULT 0
'spells' TEXT DEFAULT ''
);"); );");
if ($complete) { if ($complete) {
@ -84,7 +83,7 @@ class InstallModule
// @Babble // @Babble
App::$db->q("CREATE TABLE IF NOT EXISTS 'babble' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'babble' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'author' INTEGER NOT NULL, 'author' INTEGER NOT NULL,
'babble' TEXT NOT NULL, 'babble' TEXT NOT NULL,
'posted' DATETIME DEFAULT CURRENT_TIMESTAMP 'posted' DATETIME DEFAULT CURRENT_TIMESTAMP
@ -92,7 +91,7 @@ class InstallModule
// @Drops // @Drops
App::$db->q("CREATE TABLE IF NOT EXISTS 'drops' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'drops' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'name' TEXT NOT NULL, 'name' TEXT NOT NULL,
'level' INTEGER DEFAULT 1, 'level' INTEGER DEFAULT 1,
'type' INTEGER DEFAULT 1, 'type' INTEGER DEFAULT 1,
@ -104,7 +103,7 @@ class InstallModule
// @Forum // @Forum
App::$db->q("CREATE TABLE IF NOT EXISTS 'forum' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'forum' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'posted' DATETIME DEFAULT CURRENT_TIMESTAMP, 'posted' DATETIME DEFAULT CURRENT_TIMESTAMP,
'new_post' DATETIME DEFAULT CURRENT_TIMESTAMP, 'new_post' DATETIME DEFAULT CURRENT_TIMESTAMP,
'author' INTEGER NOT NULL, 'author' INTEGER NOT NULL,
@ -120,7 +119,7 @@ class InstallModule
// @Items // @Items
App::$db->q("CREATE TABLE IF NOT EXISTS 'items' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'items' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'type' INTEGER DEFAULT 1, 'type' INTEGER DEFAULT 1,
'name' TEXT NOT NULL, 'name' TEXT NOT NULL,
'cost' INTEGER DEFAULT 0, 'cost' INTEGER DEFAULT 0,
@ -133,7 +132,7 @@ class InstallModule
// @Monsters // @Monsters
App::$db->q("CREATE TABLE IF NOT EXISTS 'monsters' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'monsters' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'name' TEXT NOT NULL, 'name' TEXT NOT NULL,
'level' INTEGER DEFAULT 1, 'level' INTEGER DEFAULT 1,
'hp' INTEGER DEFAULT 1, 'hp' INTEGER DEFAULT 1,
@ -150,7 +149,7 @@ class InstallModule
// @News // @News
App::$db->q("CREATE TABLE IF NOT EXISTS 'news' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'news' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'author' INTEGER DEFAULT 1, 'author' INTEGER DEFAULT 1,
'title' TEXT DEFAULT '', 'title' TEXT DEFAULT '',
'content' TEXT DEFAULT '', 'content' TEXT DEFAULT '',
@ -159,7 +158,7 @@ class InstallModule
// @Spells // @Spells
App::$db->q("CREATE TABLE IF NOT EXISTS 'spells' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'spells' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY AUTOINCREMENT,
'name' TEXT NOT NULL, 'name' TEXT NOT NULL,
'type' INTEGER DEFAULT 1, 'type' INTEGER DEFAULT 1,
'mp' INTEGER DEFAULT 0, 'mp' INTEGER DEFAULT 0,
@ -170,6 +169,16 @@ class InstallModule
// add default spells if complete install // add default spells if complete install
if ($complete) App::$db->insertFromCSV('spells', "$defaults/spells.csv"); 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 // @Towns
App::$db->q("CREATE TABLE IF NOT EXISTS 'towns' ( App::$db->q("CREATE TABLE IF NOT EXISTS 'towns' (
'id' INTEGER PRIMARY KEY, 'id' INTEGER PRIMARY KEY,
@ -258,8 +267,8 @@ class InstallModule
} }
} }
// Make sure the class selection is valid // Make sure the class selection is present
$class = isset($_POST['class']) ? $_POST['class'] : 1; $class = $_POST['class'] ?? 1;
// If we have any errors, bail to the form and let the user know // If we have any errors, bail to the form and let the user know
if (!empty($errors)) { if (!empty($errors)) {
@ -267,9 +276,6 @@ class InstallModule
exit; 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 // Create the admin account
Player::create([ Player::create([
'username' => trim($_POST['username']), 'username' => trim($_POST['username']),
@ -283,6 +289,9 @@ class InstallModule
// Render the finished page! // Render the finished page!
echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]); 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() private static function fourOhFour()

View File

@ -29,7 +29,7 @@
<div class="form-group"> <div class="form-group">
<label for="level">Level</label> <label for="level">Level</label>
<input type="number" name="level" id="level" min="1" placeholder="1" required> <input type="number" name="level" id="level" min="1" placeholder="1" value="1" required>
</div> </div>
<button type="submit" name="submit">Submit</button> <button type="submit" name="submit">Submit</button>