an experiment in putting together a wiki and an object-oriented mud.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
the-forest/server/db_scripts/initialize_db-24-10-10.1.js

187 lines
5.2 KiB

// wipe and load the database from a dump file created by dump_blahblah.js
const hljs = require('highlight.js/lib/core');
hljs.registerLanguage('lua', require('highlight.js/lib/languages/lua'));
const fs = require('node:fs');
const sqlite = require("better-sqlite3");
const showdown = require("showdown");
const db = new sqlite('the_big_db.db', { verbose: console.log });
const converter = new showdown.Converter();
const dump_file_name = process.argv[2]
//get argument out of 'node db_scripts/initialize_db-blah-blah.js dumpfile'
let data = "{}";
try {
data = fs.readFileSync(dump_file_name, 'utf8');
} catch (error) {
console.log(error);
return;
}
//open that file as text (unicode i guess?)
console.log("opened file");
const filejson = JSON.parse(data);
//parse it as json
const pages = filejson["pages"];
const createPages = db.prepare(`
create table if not exists pages (
id integer primary key,
number integer,
title varchar(255),
description text,
html text,
lua text,
time timestamp default current_timestamp,
author integer,
type integer
)
`);
/* an object has:
id = primary key
number = object identifier number
title / NAME = a short display name for an object, or the title of a page
description / TEXT? = the markdown description of an object, or the lua script of the source code of a verb
html / HTML_MARKDOWN? = the markdown for that object, rendered into html for display as a page
HTML_LUA? = the lua source code of the object, rendered into html with syntax highlighting?
time = when this edit was saved
author = the user id of whoever authored this revision
TYPE = whether this represents an object or a verb
*/
function migratePages(pages) {
console.log("moving old table to a temporary");
console.log(db.prepare(`alter table pages rename to old_pages`).run())
console.log("creating new page table")
console.log(createPages.run());
console.log('iterating over dump pages');
const insertPage = db.prepare(`insert into pages
(id, number, title, description, html, lua, time, author, type)
values (:id, :number, :title, :description, :html, :lua, :time, :author, :type)`);
pages.forEach((pageData) => {
let {id, number, description} = pageData;
console.log(`rendering page number ${number}.${id}`)
description = description.replace(/<br>/g, "\n");
const renderedPage = converter.makeHtml(description);
const highlightedLua = hljs.highlight(description, {language: 'lua'}).value;
args = { ...pageData, description: description, html: renderedPage, lua: highlightedLua, type: "noun"};
console.log("inserting args", args);
insertPage.run(args);
});
console.log("getting rid of old table");
console.log(db.prepare("drop table old_pages").run());
//throw new Error("blahhh i'm a dracula");
}
try {
db.transaction(migratePages)(filejson["pages"]);
} catch (error) {
console.log(error);
}
const createAttributesQuery = db.prepare(`
create table if not exists attributes (
number integer unique,
contents json
)
`);
/* an attribute store has:
number = the object number it's attached to
contents = a `text` object holding a json store of attributes defined on object #number, containing:
LOCATION? = the object which contains this object? should this be in the DB or in the json store?
PROTOTYPE = the number of the prototype for this object, to which we should look for attributes not found here
ATTRIBUTES? = json object of attached, script-controlled attributes.
CONTENTS = json array of objectss contained inside this one
VERBS = json array of objecsts which are verbs on this one
*/
function createAttributes() {
console.log("creating new attribute table")
console.log(createAttributesQuery.run());
console.log('iterating over all pages');
const insertAttribute = db.prepare(`insert into attributes
(number, contents) values (:number, :contents)`);
db.prepare(`select * from pages`).all().forEach((pageData) => {
let { number } = pageData;
let args = {contents: JSON.stringify({}), number: number};
console.log("inserting args", args);
insertAttribute.run(args);
});
//throw new Error("blahhh i'm a dracula");
}
try {
db.transaction(createAttributes)();
} catch (error) {
console.log(error);
}
const createUsers = db.prepare(`
create table if not exists users (
id integer primary key,
name varchar(64) unique,
password varchar(128),
character integer
)
`);
/* a user has:
id = primary key
name = name string
password = argon2 hash of their password
character = object in the game world representing your character
*/
function migrateUsers(users) {
console.log("moving old users");
console.log(db.prepare("alter table users rename to old_users").run());
console.log("creating new users table");
console.log(createUsers.run());
const insertUser = db.prepare("insert into users (name, password) values (:name, :password)")
users.forEach((user) => {
console.log(insertUser.run(user));
});
console.log("clearing old table");
console.log(db.prepare("drop table old_users").run());
//throw new Error("i'm dracula 2");
}
try {
db.transaction(migrateUsers)(filejson["users"]);
} catch (error) {
console.log(error);
}