DK2/database/scripts/create.php

598 lines
16 KiB
PHP

<?php
/*
This script is used to create the database and the tables for said database.
php create.php <database>
*/
require_once __DIR__ . '/../../color.php';
const AUTH = 'auth.db';
const LIVE = 'live.db';
const FIGHTS = 'fights.db';
const BPS = 'blueprints.db';
/**
* Echo a string with a newline.
*/
function eln(string $string): void
{
echo $string . PHP_EOL;
}
// pick the database to create
if (!isset($argv[1])) {
eln(red('Missing database name.'));
eln(blue('Usage: ') . 'php create.php auth.db|live.db|fight.db|blueprints.db [-d]');
exit(1);
}
// make sure it's a valid database
if (!in_array($argv[1], [AUTH, LIVE, FIGHTS, BPS])) {
eln(red('Invalid database: ') . $argv[1]);
exit(1);
}
$database = $argv[1];
// whether the -d flag is set
$drop = isset($argv[2]) && $argv[2] === '-d';
/*
================================================================================
Databases
================================================================================
*/
/*
The Auth database is used to store user information - not player info, but user info.
Usernames, passwords, email, session tokens, etc.
*/
if ($database === AUTH) {
if ($drop) {
unlink(__DIR__ . '/../' . AUTH);
eln(red('Dropped database: ') . 'auth.db');
}
$db = new SQLite3(__DIR__ . '/../' . AUTH);
// Users table
$db->exec('DROP TABLE IF EXISTS users');
$r = $db->exec('CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
auth INT NOT NULL DEFAULT 0,
char_id INTEGER NOT NULL DEFAULT 0,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
last_login DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'users');
// Sessions table
$db->exec('DROP TABLE IF EXISTS sessions');
$r = $db->exec('CREATE TABLE sessions (
user_id INTEGER NOT NULL,
token TEXT NOT NULL UNIQUE,
expires INTEGER NOT NULL
)');
created_or_error($r, 'sessions');
// Verification tokens
$db->exec('DROP TABLE IF EXISTS tokens');
$r = $db->exec('CREATE TABLE tokens (
user_id INTEGER NOT NULL,
token TEXT NOT NULL UNIQUE,
created INTEGER NOT NULL
)');
created_or_error($r, 'tokens');
eln(green('Created database: ') . 'auth.db');
exit(0);
}
/*
The Fights database is used to store information about fights.
A fight is a battle between two characters; players or NPCs.
*/
if ($database === FIGHTS) {
if ($drop) {
unlink(__DIR__ . '/../' . FIGHTS);
eln(red('Dropped database: ') . 'fights.db');
}
$db = new SQLite3(__DIR__ . '/../' . FIGHTS);
// PvE fights
$db->exec('DROP TABLE IF EXISTS pve');
$r = $db->exec('CREATE TABLE pve (
id INTEGER PRIMARY KEY AUTOINCREMENT,
char_id INTEGER NOT NULL,
char_hp INTEGER NOT NULL,
char_max_hp INTEGER NOT NULL,
char_mp INTEGER NOT NULL,
char_max_mp INTEGER NOT NULL,
char_power INTEGER NOT NULL,
char_accuracy INTEGER NOT NULL,
char_penetration INTEGER NOT NULL DEFAULT 0,
char_focus INTEGER NOT NULL DEFAULT 0,
char_toughness INTEGER NOT NULL,
char_armor INTEGER NOT NULL,
char_resist INTEGER NOT NULL,
char_crit INTEGER NOT NULL,
char_precision INTEGER NOT NULL,
char_ferocity INTEGER NOT NULL,
mob_id INTEGER NOT NULL,
mob_level INTEGER NOT NULL,
mob_rank INTEGER NOT NULL,
mob_hp INTEGER NOT NULL,
mob_max_hp INTEGER NOT NULL,
mob_mp INTEGER NOT NULL,
mob_max_mp INTEGER NOT NULL,
mob_power INTEGER NOT NULL,
mob_toughness INTEGER NOT NULL,
mob_armor INTEGER NOT NULL,
mob_precision INTEGER NOT NULL,
mob_penetration INTEGER NOT NULL DEFAULT 0,
mob_focus INTEGER NOT NULL DEFAULT 0,
mob_crit INTEGER NOT NULL,
mob_ferocity INTEGER NOT NULL,
mob_vitality INTEGER NOT NULL,
first_turn INTEGER NOT NULL,
turn INTEGER NOT NULL default 1,
winner INTEGER NOT NULL default 0,
flee INTEGER NOT NULL default 1,
escaped INTEGER NOT NULL default 0,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'pve');
// PvP fights
$db->exec('DROP TABLE IF EXISTS pvp');
$r = $db->exec('CREATE TABLE pvp (
id INTEGER PRIMARY KEY AUTOINCREMENT,
char1_id INTEGER NOT NULL,
char1_hp INTEGER NOT NULL,
char1_max_hp INTEGER NOT NULL,
char1_mp INTEGER NOT NULL,
char1_max_mp INTEGER NOT NULL,
char1_power INTEGER NOT NULL,
char1_accuracy INTEGER NOT NULL,
char1_penetration INTEGER NOT NULL DEFAULT 0,
char1_focus INTEGER NOT NULL DEFAULT 0,
char1_toughness INTEGER NOT NULL,
char1_armor INTEGER NOT NULL,
char1_resist INTEGER NOT NULL,
char1_crit INTEGER NOT NULL,
char1_precision INTEGER NOT NULL,
char1_ferocity INTEGER NOT NULL,
char2_id INTEGER NOT NULL,
char2_hp INTEGER NOT NULL,
char2_max_hp INTEGER NOT NULL,
char2_mp INTEGER NOT NULL,
char2_max_mp INTEGER NOT NULL,
char2_power INTEGER NOT NULL,
char2_accuracy INTEGER NOT NULL,
char2_penetration INTEGER NOT NULL DEFAULT 0,
char2_focus INTEGER NOT NULL DEFAULT 0,
char2_toughness INTEGER NOT NULL,
char2_armor INTEGER NOT NULL,
char2_resist INTEGER NOT NULL,
char2_crit INTEGER NOT NULL,
char2_precision INTEGER NOT NULL,
char2_ferocity INTEGER NOT NULL,
first_turn INTEGER NOT NULL,
turn INTEGER NOT NULL default 1,
winner INTEGER NOT NULL default 0,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'pvp');
// PvE fight logs
$db->exec('DROP TABLE IF EXISTS pve_logs');
$r = $db->exec('CREATE TABLE pve_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fight_id INTEGER NOT NULL,
info TEXT NOT NULL
)');
created_or_error($r, 'pve_logs');
// PvP fight logs
$db->exec('DROP TABLE IF EXISTS pvp_logs');
$r = $db->exec('CREATE TABLE pvp_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fight_id INTEGER NOT NULL,
info TEXT NOT NULL
)');
created_or_error($r, 'pvp_logs');
eln(green('Created database: ') . 'fights.db');
exit(0);
}
/*
The Blueprints database is used to store information about items, weapons, armor, etc.
*/
if ($database === BPS) {
if ($drop) {
unlink(__DIR__ . '/../' . BPS);
eln(red('Dropped database: ') . 'blueprints.db');
}
$db = new SQLite3(__DIR__ . '/../' . BPS);
// Items
$db->exec('DROP TABLE IF EXISTS items');
$r = $db->exec('CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type INTEGER NOT NULL DEFAULT 0,
subtype INTEGER NOT NULL DEFAULT 0,
slot INTEGER NOT NULL DEFAULT 0,
rarity INTEGER NOT NULL DEFAULT 0,
value INTEGER NOT NULL DEFAULT 0,
consumable INTEGER NOT NULL DEFAULT 0,
duration INTEGER NOT NULL DEFAULT 0,
durability INTEGER NOT NULL DEFAULT 0,
power INTEGER NOT NULL DEFAULT 0,
accuracy INTEGER NOT NULL DEFAULT 0,
penetration INTEGER NOT NULL DEFAULT 0,
focus INTEGER NOT NULL DEFAULT 0,
toughness INTEGER NOT NULL DEFAULT 0,
armor INTEGER NOT NULL DEFAULT 0,
resist INTEGER NOT NULL DEFAULT 0,
crit INTEGER NOT NULL DEFAULT 0,
precision INTEGER NOT NULL DEFAULT 0,
ferocity INTEGER NOT NULL DEFAULT 0,
luck INTEGER NOT NULL DEFAULT 0,
reqs TEXT NOT NULL DEFAULT "",
traits TEXT NOT NULL DEFAULT "",
lore TEXT NOT NULL DEFAULT "",
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'items');
// Mobs
$db->exec('DROP TABLE IF EXISTS mobs');
$r = $db->exec('CREATE TABLE mobs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type INTEGER NOT NULL,
rank INTEGER NOT NULL,
level INTEGER NOT NULL,
hp INTEGER NOT NULL,
max_hp INTEGER NOT NULL,
mp INTEGER NOT NULL,
max_mp INTEGER NOT NULL,
power INTEGER NOT NULL,
toughness INTEGER NOT NULL,
armor INTEGER NOT NULL,
precision INTEGER NOT NULL,
crit INTEGER NOT NULL,
ferocity INTEGER NOT NULL,
vitality INTEGER NOT NULL,
xp INTEGER NOT NULL,
silver INTEGER NOT NULL,
loot TEXT NOT NULL,
lore TEXT NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'mobs');
eln(green('Created database: ') . 'blueprints.db');
exit(0);
}
/*
The Live database is used to store information about players, NPCs, guilds, etc.
*/
if ($database === LIVE) {
if ($drop) {
unlink(__DIR__ . '/../' . LIVE);
eln(red('Dropped database: ') . 'live.db');
}
$db = new SQLite3(__DIR__ . '/../' . LIVE);
// Characters
$db->exec('DROP TABLE IF EXISTS characters');
$r = $db->exec('CREATE TABLE characters (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
name TEXT NOT NULL UNIQUE,
title_id INTEGER NOT NULL DEFAULT 0,
level INTEGER NOT NULL DEFAULT 1,
xp INTEGER NOT NULL DEFAULT 0,
xp_to_level INTEGER NOT NULL DEFAULT 100,
current_hp INTEGER NOT NULL DEFAULT 20,
max_hp INTEGER NOT NULL DEFAULT 20,
current_mp INTEGER NOT NULL DEFAULT 10,
max_mp INTEGER NOT NULL DEFAULT 10,
current_tp INTEGER NOT NULL DEFAULT 0,
max_tp INTEGER NOT NULL DEFAULT 0,
power INTEGER NOT NULL DEFAULT 0,
accuracy INTEGER NOT NULL DEFAULT 0,
penetration INTEGER NOT NULL DEFAULT 0,
focus INTEGER NOT NULL DEFAULT 0,
toughness INTEGER NOT NULL DEFAULT 0,
armor INTEGER NOT NULL DEFAULT 0,
resist INTEGER NOT NULL DEFAULT 0,
precision INTEGER NOT NULL DEFAULT 0,
ferocity INTEGER NOT NULL DEFAULT 0,
luck INTEGER NOT NULL DEFAULT 0,
inv_slots INTEGER NOT NULL DEFAULT 10,
attrib_points INTEGER NOT NULL DEFAULT 0
)');
created_or_error($r, 'characters');
// Player gear
$db->exec('DROP TABLE IF EXISTS char_gear');
$r = $db->exec('CREATE TABLE char_gear (
char_id INTEGER NOT NULL,
head INTEGER NOT NULL DEFAULT 0,
chest INTEGER NOT NULL DEFAULT 0,
boots INTEGER NOT NULL DEFAULT 0,
hands INTEGER NOT NULL DEFAULT 0,
main_hand INTEGER NOT NULL DEFAULT 0,
off_hand INTEGER NOT NULL DEFAULT 0,
rune INTEGER NOT NULL DEFAULT 0,
ring INTEGER NOT NULL DEFAULT 0,
amulet INTEGER NOT NULL DEFAULT 0,
power INTEGER NOT NULL DEFAULT 0,
accuracy INTEGER NOT NULL DEFAULT 0,
penetration INTEGER NOT NULL DEFAULT 0,
focus INTEGER NOT NULL DEFAULT 0,
toughness INTEGER NOT NULL DEFAULT 0,
armor INTEGER NOT NULL DEFAULT 0,
resist INTEGER NOT NULL DEFAULT 0,
crit INTEGER NOT NULL DEFAULT 0,
precision INTEGER NOT NULL DEFAULT 0,
ferocity INTEGER NOT NULL DEFAULT 0,
luck INTEGER NOT NULL DEFAULT 0,
max_hp INTEGER NOT NULL DEFAULT 0,
max_mp INTEGER NOT NULL DEFAULT 0
)');
created_or_error($r, 'char_gear');
// Player inventory
$db->exec('DROP TABLE IF EXISTS char_inventory');
$r = $db->exec('CREATE TABLE inventory (
char_id INTEGER NOT NULL,
item_id INTEGER NOT NULL
)');
created_or_error($r, 'inventory');
// Player wallet
$db->exec('DROP TABLE IF EXISTS char_wallets');
$r = $db->exec('CREATE TABLE char_wallets (
char_id INTEGER NOT NULL,
silver INTEGER NOT NULL DEFAULT 10,
stargem INTEGER NOT NULL DEFAULT 0
)');
created_or_error($r, 'char_wallets');
// Player bank
$db->exec('DROP TABLE IF EXISTS char_bank');
$r = $db->exec('CREATE TABLE bank (
char_id INTEGER NOT NULL,
slots INTEGER NOT NULL DEFAULT 5,
silver INTEGER NOT NULL DEFAULT 0,
tier INTEGER NOT NULL DEFAULT 0,
can_collect INTEGER NOT NULL DEFAULT 1,
last_collected DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'bank');
// Banked items
$db->exec('DROP TABLE IF EXISTS char_banked_items');
$r = $db->exec('CREATE TABLE banked_items (
char_id INTEGER NOT NULL,
item_id INTEGER NOT NULL
)');
created_or_error($r, 'banked_items');
// Towns
$db->exec('DROP TABLE IF EXISTS towns');
$r = $db->exec('CREATE TABLE towns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
type INTEGER NOT NULL,
lore TEXT NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'towns');
// Shops
$db->exec('DROP TABLE IF EXISTS shops');
$r = $db->exec('CREATE TABLE shops (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type INTEGER NOT NULL,
lore TEXT NOT NULL,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
items TEXT NOT NULL,
gear TEXT NOT NULL,
materials TEXT NOT NULL,
buy_modifier INTEGER NOT NULL DEFAULT 100,
sell_modifier INTEGER NOT NULL DEFAULT 100,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'shops');
// Inns
$db->exec('DROP TABLE IF EXISTS inns');
$r = $db->exec('CREATE TABLE inns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type INTEGER NOT NULL,
lore TEXT NOT NULL,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
cost INTEGER NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'inns');
// Guilds
$db->exec('DROP TABLE IF EXISTS guilds');
$r = $db->exec('CREATE TABLE guilds (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
lore TEXT NOT NULL DEFAULT "",
leader_id INTEGER NOT NULL,
silver INTEGER NOT NULL DEFAULT 0,
rep INTEGER NOT NULL DEFAULT 0,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'guilds');
// Guild ranks
$db->exec('DROP TABLE IF EXISTS guild_ranks');
$r = $db->exec('CREATE TABLE guild_ranks (
guild_id INTEGER NOT NULL,
rank INTEGER NOT NULL,
name TEXT NOT NULL,
permissions TEXT NOT NULL
)');
created_or_error($r, 'guild_ranks');
// Guild members
$db->exec('DROP TABLE IF EXISTS guild_members');
$r = $db->exec('CREATE TABLE guild_members (
guild_id INTEGER NOT NULL,
char_id INTEGER NOT NULL,
rank INTEGER NOT NULL,
rep INTEGER NOT NULL DEFAULT 0,
donated INTEGER NOT NULL DEFAULT 0,
joined DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'guild_members');
// NPCs
$db->exec('DROP TABLE IF EXISTS npcs');
$r = $db->exec('CREATE TABLE npcs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type INTEGER NOT NULL,
lore TEXT NOT NULL,
conversation TEXT NOT NULL,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'npcs');
// Town reputation
$db->exec('DROP TABLE IF EXISTS char_town_rep');
$r = $db->exec('CREATE TABLE char_town_rep (
char_id INTEGER NOT NULL,
town_id INTEGER NOT NULL,
rep INTEGER NOT NULL DEFAULT 0
)');
created_or_error($r, 'char_town_rep');
// Items
$db->exec('DROP TABLE IF EXISTS items');
$r = $db->exec('CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type TEXT NOT NULL DEFAULT 0,
rarity INTEGER NOT NULL DEFAULT 0,
forged INTEGER NOT NULL DEFAULT 0,
quality INTEGER NOT NULL DEFAULT 0,
value INTEGER NOT NULL DEFAULT 0,
consumable INTEGER NOT NULL DEFAULT 0,
duration INTEGER NOT NULL DEFAULT 0,
durability INTEGER NOT NULL DEFAULT 0,
max_durability INTEGER NOT NULL DEFAULT 0,
power INTEGER NOT NULL DEFAULT 0,
accuracy INTEGER NOT NULL DEFAULT 0,
penetration INTEGER NOT NULL DEFAULT 0,
focus INTEGER NOT NULL DEFAULT 0,
toughness INTEGER NOT NULL DEFAULT 0,
armor INTEGER NOT NULL DEFAULT 0,
resist INTEGER NOT NULL DEFAULT 0,
crit INTEGER NOT NULL DEFAULT 0,
precision INTEGER NOT NULL DEFAULT 0,
ferocity INTEGER NOT NULL DEFAULT 0,
luck INTEGER NOT NULL DEFAULT 0,
reqs TEXT NOT NULL DEFAULT "",
traits TEXT NOT NULL DEFAULT "",
lore TEXT NOT NULL DEFAULT "",
created DATETIME DEFAULT CURRENT_TIMESTAMP,
updated DATETIME DEFAULT CURRENT_TIMESTAMP
)');
created_or_error($r, 'items');
// Player traits
$db->exec('DROP TABLE IF EXISTS char_traits');
$r = $db->exec('CREATE TABLE char_traits (
char_id INTEGER NOT NULL,
trait_id INTEGER NOT NULL,
value INTEGER NOT NULL DEFAULT 0
)');
created_or_error($r, 'char_traits');
// Character location
$db->exec('DROP TABLE IF EXISTS char_locations');
$r = $db->exec('CREATE TABLE char_locations (
char_id INTEGER NOT NULL,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
currently INTEGER NOT NULL DEFAULT 0
)');
created_or_error($r, 'char_locations');
eln(green('Created database: ') . 'live.db');
exit(0);
}
function created_or_error(bool $result, string $table): void
{
if ($result === false) {
eln(red('Failed to create table: ') . $table);
exit(1);
}
eln(yellow('Created table: ') . $table);
}