2024-10-21 11:56:48 -04:00
|
|
|
const { LuaFactory } = require('wasmoon');
|
|
|
|
|
2024-10-08 23:24:48 -04:00
|
|
|
const sqlite = require('better-sqlite3');
|
2024-10-26 15:40:10 -04:00
|
|
|
const db = new sqlite('the_big_db.db');
|
2024-10-08 23:24:48 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
const factory = new LuaFactory();
|
|
|
|
|
|
|
|
let lua;
|
|
|
|
|
|
|
|
async function makeLua() {
|
|
|
|
lua = await factory.createEngine();
|
|
|
|
lua.global.set('executeVerb', executeVerb);
|
|
|
|
lua.global.set('getAttribute', luaSafe(getAttribute));
|
|
|
|
lua.global.set('setAttribute', luaSafe(setAttribute));
|
|
|
|
lua.global.set('deleteAttribute', luaSafe(deleteAttribute));
|
|
|
|
lua.global.set('hasOwnAttribute', luaSafe(hasOwnAttribute));
|
|
|
|
lua.global.set('verifyObjectReference', luaSafe(verifyObjectReference));
|
|
|
|
lua.global.set('lookUpObject', luaSafe(lookUpObject));
|
|
|
|
lua.global.set('lookUpObjectAttributes', luaSafe(lookUpObjectAttributes));
|
|
|
|
lua.global.set('interpret', interpret);
|
2024-10-25 17:48:12 -04:00
|
|
|
|
|
|
|
// let's get cheeky with it
|
|
|
|
|
|
|
|
lua.global.set('console_log', console.log);
|
2024-10-21 11:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
makeLua();
|
|
|
|
|
2024-10-08 23:24:48 -04:00
|
|
|
var sockets = new Map();
|
|
|
|
|
2024-10-10 11:13:41 -04:00
|
|
|
function interpret(context, player, command) {
|
|
|
|
// interpret and execute a command entered by a player.
|
|
|
|
//
|
|
|
|
// context: the location the player has entered the command in from.
|
|
|
|
// may be null if it's executing from a script?
|
|
|
|
// player: the id of the player object who typed in the command.
|
|
|
|
// may? be null if it's executing from a script? used for output.
|
2024-10-10 22:54:22 -04:00
|
|
|
// so arguably
|
2024-10-10 11:13:41 -04:00
|
|
|
// command: the full string typed in by the player
|
2024-10-25 17:48:12 -04:00
|
|
|
console.log(context, player, command);
|
2024-10-08 23:24:48 -04:00
|
|
|
|
2024-10-10 11:13:41 -04:00
|
|
|
const socket = sockets.get(player)
|
2024-10-21 11:56:48 -04:00
|
|
|
const wordsAndQuotes = tokenizeQuotes(command.trim());
|
2024-10-10 11:13:41 -04:00
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
try {
|
|
|
|
//for the moment we'll only allow statements of the form "go north" with implicit subject: the player
|
2024-10-21 11:56:48 -04:00
|
|
|
|
|
|
|
const verb = findVerb(wordsAndQuotes[0], context, player);
|
|
|
|
|
|
|
|
if (verb) {
|
|
|
|
let prepositions = getAttribute(verb, "prepositions");
|
|
|
|
|
|
|
|
const wordsAndPreps = tokenizePrepositions(wordsAndQuotes, prepositions || []);
|
|
|
|
let words = wordsAndPreps.filter((x) => typeof(x) == "string");
|
|
|
|
let preps = wordsAndPreps.filter((x) => typeof(x) == "object");
|
|
|
|
let prepMap = {};
|
|
|
|
for (const obj of preps) {
|
|
|
|
prepMap[obj.preposition] = obj.contents;
|
|
|
|
}
|
2024-10-10 11:13:41 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
const [first, second, third, ...rest] = words;
|
2024-10-10 22:54:22 -04:00
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
return executeVerb(command, player, verb, prepMap, context, player, second, third, ...rest);
|
2024-10-21 11:56:48 -04:00
|
|
|
} else {
|
2024-10-26 15:40:10 -04:00
|
|
|
return interpret(1,1, `system_send_message '{"error": "verb ${verbId} not found in ${fullCommand}"}' to ${player}`);
|
2024-10-21 11:56:48 -04:00
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
} catch (error) {
|
2024-10-26 15:40:10 -04:00
|
|
|
return executeVerb('', player, 2, {to: player}, null, `{"error": "error found: ${error}"}`);
|
2024-10-10 22:54:22 -04:00
|
|
|
}
|
2024-10-10 11:13:41 -04:00
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
/*
|
2024-10-10 11:13:41 -04:00
|
|
|
// if the second word is a verb, but the first word is also a verb, what do we do?
|
2024-10-08 23:24:48 -04:00
|
|
|
if (second in verbs) {
|
2024-10-10 11:13:41 -04:00
|
|
|
// for now, assume that if the second word is a verb, it's the verb we want.
|
|
|
|
executeVerb(player, verbs.get(second), first, third, ...rest);
|
2024-10-08 23:24:48 -04:00
|
|
|
} else {
|
2024-10-10 11:13:41 -04:00
|
|
|
// if the second word is not a verb, then the first word is the verb, and the player is the implied subject.
|
|
|
|
executeVerb(player, verbs.get(first), player, second, third, ...rest)
|
2024-10-08 23:24:48 -04:00
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
*/
|
2024-10-08 23:24:48 -04:00
|
|
|
}
|
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
async function executeVerb(fullCommand, outputObject, verbId, prepositions, context, subject, object, ...rest) {
|
2024-10-21 11:56:48 -04:00
|
|
|
if (verbId == 2) {
|
|
|
|
// todo: make this more intelligently get the rright thing to send
|
|
|
|
if (prepositions["to"]) {
|
|
|
|
let destination = verifyObjectReference(prepositions["to"])
|
2024-10-26 15:40:10 -04:00
|
|
|
sockets.get(destination)?.send(object);
|
|
|
|
return object;
|
2024-10-21 11:56:48 -04:00
|
|
|
}
|
2024-10-26 15:40:10 -04:00
|
|
|
const msg = JSON.stringify({"message": `missing "to" clause trying to execute command: ${fullCommand}`})
|
|
|
|
sockets.get(outputObject)?.send();
|
|
|
|
return msg;
|
2024-10-21 11:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const fullVerb = lookUpObject(verbId);
|
|
|
|
if (!fullVerb) {
|
|
|
|
return interpret(1, 1, `system_send_message '{"error": "verb ${verbId} not found in ${fullCommand}"}' to ${outputObject}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a temp name for this verb, then define it and execute.
|
|
|
|
const verbName = "verb" + Math.random().toString(36).substring(2);
|
|
|
|
const body = fullVerb.description.replace(/ /g, " ");
|
|
|
|
const verbDeclaration = `
|
2024-10-25 17:48:12 -04:00
|
|
|
function ${verbName} (fullCommand, outputObject, prepositionMap, context, subject, object, ...)
|
2024-10-21 11:56:48 -04:00
|
|
|
${body}
|
|
|
|
end`;
|
|
|
|
console.log("verb we're running:");
|
|
|
|
console.log(verbDeclaration);
|
2024-10-25 17:48:12 -04:00
|
|
|
await lua.doString(verbDeclaration).catch((e) => {
|
|
|
|
console.log("found an error heyyoyyoyoyoyo", e);
|
|
|
|
let msg = {"error": "syntax error defining a verb!", "errorObject": e};
|
|
|
|
|
|
|
|
sockets.get(outputObject)?.send(JSON.stringify(msg));
|
|
|
|
})
|
2024-10-21 11:56:48 -04:00
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
let func = lua.global.get(verbName);
|
|
|
|
if (typeof func === "function") {
|
|
|
|
let returnValue;
|
|
|
|
try {
|
|
|
|
returnValue =
|
|
|
|
lua.global.get(verbName)(fullCommand, outputObject, prepositions, context, subject, object, ...rest)
|
|
|
|
} catch (error) {
|
|
|
|
sockets.get(outputObject)?.send(JSON.stringify({"error": "error executing a verb!"}));
|
|
|
|
}
|
|
|
|
// maybe unset it so we dno't have an ever-growing set of functions cluttering up the space?
|
|
|
|
//lua.global.set(verbName, null);
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
} else {
|
2024-10-26 15:40:10 -04:00
|
|
|
sockets.get(outputObject)?.send(JSON.stringify({"error": "found that word but it wasn't a verb"}));
|
2024-10-25 17:48:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2024-10-21 11:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const objectQuery = db.prepare('select * from pages where number=? order by time desc');
|
|
|
|
function lookUpObject(number) {
|
2024-10-25 17:48:12 -04:00
|
|
|
return objectQuery.get(number);
|
2024-10-21 11:56:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const attributeQuery = db.prepare(`select * from attributes where number=?`);
|
|
|
|
function lookUpObjectAttributes(number) {
|
|
|
|
return JSON.parse(attributeQuery.get(number).contents);
|
|
|
|
}
|
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
function findVerb(word, location, actor) {
|
|
|
|
// returns the number of a verb which matches the requested word.
|
2024-10-08 23:24:48 -04:00
|
|
|
// check for verbs on actor
|
2024-10-21 11:56:48 -04:00
|
|
|
let verb = [];
|
2024-10-10 22:54:22 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
if (verifyObjectReference(word)) {
|
|
|
|
return verifyObjectReference(word);
|
2024-10-10 22:54:22 -04:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
if (actor) {
|
|
|
|
verb = findVerbOnObject(word, actor);
|
2024-10-10 22:54:22 -04:00
|
|
|
if (verb) return verb;
|
2024-10-21 11:56:48 -04:00
|
|
|
|
|
|
|
let actorContents = getAttribute(actor, "contents");
|
|
|
|
for (const obj of (actorContents || [])) {
|
|
|
|
verb = findVerbOnObject(word, obj);
|
|
|
|
if (verb) return verb;
|
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
if (location) {
|
|
|
|
const locationContents = getAttribute(location, "contents");
|
|
|
|
for (const obj of (locationContents || [])) {
|
|
|
|
verb = findVerbOnObject(word, obj);
|
|
|
|
if (verb) return verb;
|
|
|
|
}
|
|
|
|
|
|
|
|
verb = findVerbOnObject(word, location);
|
|
|
|
if (verb) return verb;
|
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findVerbOnObject(word, object) {
|
|
|
|
// check for verbs on a single object, and its chain of parents. return a matching verbId, or undefined
|
|
|
|
if (!word) return undefined;
|
|
|
|
|
|
|
|
let focus = object;
|
|
|
|
while (focus) {
|
|
|
|
const focusVerbs = getAttribute(focus, "verbs");
|
|
|
|
for (const verbId of (focusVerbs || [])) {
|
|
|
|
const fullVerb = lookUpObject(verbId);
|
|
|
|
|
|
|
|
if (!fullVerb) continue;
|
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
// test our word against each verb in turn
|
2024-10-10 22:54:22 -04:00
|
|
|
if (word.toLowerCase() == fullVerb.title.toLowerCase()) {
|
|
|
|
return verbId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
focus = getAttribute(focus, "parent");
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
2024-10-08 23:24:48 -04:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
function findAllVerbsOnObject(object) {
|
|
|
|
let verbs = [];
|
|
|
|
let focus = object;
|
2024-10-08 23:24:48 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
while (focus) {
|
2024-10-25 17:48:12 -04:00
|
|
|
verbs = concatWithoutDuplicates(verbs, getAttribute(focus, "verbs"));
|
2024-10-10 22:54:22 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
focus = getAttribute(focus, "parent");
|
|
|
|
}
|
|
|
|
|
|
|
|
return verbs;
|
2024-10-10 22:54:22 -04:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
function findAllVerbsInArea(location, actor) {
|
|
|
|
// returns all verb ids in an area or on an actor
|
|
|
|
let verbs = findAllVerbsOnObject(actor);
|
2024-10-10 22:54:22 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
const actorContents = getAttribute(actor, "contents");
|
|
|
|
for (const obj of (actorContents || [])) {
|
|
|
|
verbs = concatWithoutDuplicates(verbs, findAllVerbsOnObject(obj));
|
2024-10-10 11:13:41 -04:00
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
if (location) {
|
|
|
|
const locationContents = getAttribute(location, "contents");
|
|
|
|
for (const obj of (locationContents || [])) {
|
|
|
|
verbs = concatWithoutDuplicates(verbs, findAllVerbsOnObject(obj));
|
2024-10-08 23:24:48 -04:00
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
verbs = concatWithoutDuplicates(verbs, findAllVerbsOnObject(location));
|
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
return verbs;
|
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
|
|
|
|
const pullAttribute = db.prepare(`select * from attributes where number=?`);
|
|
|
|
function getAttribute(obj, attributeName) {
|
2024-10-25 17:48:12 -04:00
|
|
|
if (!verifyObjectReference(obj)) return undefined;
|
2024-10-21 11:56:48 -04:00
|
|
|
|
|
|
|
let attributeStore = pullAttribute.get(verifyObjectReference(obj));
|
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
if (!attributeStore || !attributeStore.contents) return undefined;
|
2024-10-21 11:56:48 -04:00
|
|
|
let contents = JSON.parse(attributeStore.contents);
|
2024-10-10 22:54:22 -04:00
|
|
|
|
|
|
|
if (contents.hasOwnProperty(attributeName)) {
|
|
|
|
return contents[attributeName];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contents.hasOwnProperty("parent")) {
|
2024-10-21 11:56:48 -04:00
|
|
|
if (contents["parent"]) {
|
|
|
|
return getAttribute(verifyObjectReference(contents["parent"]), attributeName);
|
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasOwnAttribute(obj, attributeName) {
|
2024-10-21 11:56:48 -04:00
|
|
|
if (!verifyObjectReference(obj)) return undefined;
|
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
const attributeStore = pullAttribute.get(verifyObjectReference(obj));
|
|
|
|
const contents = JSON.parse(attributeStore.contents);
|
|
|
|
|
|
|
|
return contents.hasOwnProperty(attributeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
const insertAttribute = db.prepare(`update or replace attributes set contents=:contents where number=:number`);
|
|
|
|
function setAttribute(obj, attributeName, value) {
|
2024-10-21 11:56:48 -04:00
|
|
|
if (!verifyObjectReference(obj)) return undefined;
|
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
const attributeStore = pullAttribute.get(verifyObjectReference(obj));
|
|
|
|
const contents = JSON.parse(attributeStore.contents);
|
|
|
|
|
2024-12-03 10:31:50 -05:00
|
|
|
if (isEmptyObject(value) && isArray(contents[attributeName]))
|
2024-10-25 17:48:12 -04:00
|
|
|
contents[attributeName] = [];
|
|
|
|
else
|
|
|
|
contents[attributeName] = value;
|
|
|
|
// hacky fix so that an empty array can't be replaced with an empty object
|
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
|
|
|
|
insertAttribute.run({...attributeStore, contents: JSON.stringify(contents)});
|
|
|
|
}
|
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
function isArray(obj) {
|
2024-12-03 10:31:50 -05:00
|
|
|
return Object.prototype.toString.apply(obj) === '[object Array]';
|
2024-10-25 17:48:12 -04:00
|
|
|
}
|
|
|
|
function isEmptyObject(obj) {
|
2024-12-03 10:31:50 -05:00
|
|
|
if (typeof obj !== "object") return false;
|
|
|
|
|
|
|
|
if (isArray(obj)) return false;
|
2024-10-25 17:48:12 -04:00
|
|
|
|
|
|
|
for (const prop in obj) {
|
|
|
|
if (Object.hasOwn(obj, prop)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
function deleteAttribute(obj, attributeName) {
|
2024-10-21 11:56:48 -04:00
|
|
|
if (!verifyObjectReference(obj)) return undefined;
|
|
|
|
|
2024-10-10 22:54:22 -04:00
|
|
|
const attributeStore = pullAttribute.get(verifyObjectReference(obj));
|
|
|
|
const contents = JSON.parse(attributeStore.contents);
|
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
delete contents[attributeName];
|
2024-10-10 22:54:22 -04:00
|
|
|
|
|
|
|
insertAttribute.run({...attributeStore, contents: JSON.stringify(contents)});
|
|
|
|
}
|
|
|
|
|
|
|
|
function verifyObjectReference(obj) {
|
|
|
|
try {
|
|
|
|
if (typeof(obj) === "string") {
|
|
|
|
return Number(obj.replace("#", ""));
|
|
|
|
} else if (typeof(obj) === "number") {
|
|
|
|
return obj;
|
|
|
|
} else if (typeof(obj.number) == "number") {
|
|
|
|
return obj.number;
|
|
|
|
}
|
2024-10-21 11:56:48 -04:00
|
|
|
return undefined;
|
2024-10-10 22:54:22 -04:00
|
|
|
} catch (error) {
|
2024-10-21 11:56:48 -04:00
|
|
|
throw new Error(`Tried to get an attribute from something which wasn't an object: ${obj}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function concatWithoutDuplicates(a, b) {
|
2024-10-25 17:48:12 -04:00
|
|
|
let c = []
|
|
|
|
a?.forEach((x) => { if (!c.includes(x)) c.push(x) })
|
|
|
|
b?.forEach((x) => { if (!c.includes(x)) c.push(x) });
|
|
|
|
return c;
|
2024-10-21 11:56:48 -04:00
|
|
|
}
|
|
|
|
function luaSafe(func) {
|
|
|
|
return (...all) => {
|
|
|
|
let value = func(...all);
|
|
|
|
if (value == null) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function tokenizeQuotes(string) {
|
|
|
|
let arr = string.split("'");
|
|
|
|
let out = [];
|
|
|
|
let inQuotes = false;
|
|
|
|
for (const bit of arr) {
|
|
|
|
if (inQuotes) {
|
|
|
|
out.push(bit);
|
|
|
|
} else {
|
|
|
|
if (bit != '') {
|
|
|
|
out = out.concat(bit.trim().split(" ").filter((x) => x != ''));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inQuotes = !inQuotes;
|
2024-10-10 22:54:22 -04:00
|
|
|
}
|
2024-10-21 11:56:48 -04:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tokenizePrepositions(sequence, prepositions) {
|
|
|
|
let out = [];
|
|
|
|
|
|
|
|
let current = {};
|
|
|
|
|
|
|
|
let inPreposition = false;
|
|
|
|
|
|
|
|
for (const bit of sequence) {
|
|
|
|
if (inPreposition) {
|
|
|
|
current.contents = bit;
|
|
|
|
out.push(current);
|
|
|
|
current = {};
|
|
|
|
inPreposition = false;
|
|
|
|
} else if (prepositions.includes(bit)) {
|
|
|
|
current.preposition = bit;
|
|
|
|
inPreposition = true;
|
|
|
|
} else {
|
|
|
|
out.push(bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2024-10-10 22:54:22 -04:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
module.exports = { interpret, sockets,
|
|
|
|
lookUpObject,
|
|
|
|
getAttribute, setAttribute, deleteAttribute, hasOwnAttribute,
|
|
|
|
findVerbOnObject, findAllVerbsOnObject, findAllVerbsInArea };
|