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');
|
|
|
|
const db = new sqlite('the_big_db.db', { verbose: console.log });
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-21 11:56:48 -04:00
|
|
|
executeVerb(command, player, verb, prepMap, player, second, third, ...rest)
|
|
|
|
} else {
|
|
|
|
interpret(1,1, `system_send_message '{"error": "verb ${verbId} not found in ${fullCommand}"}' to ${player}`);
|
|
|
|
}
|
2024-10-10 22:54:22 -04:00
|
|
|
} catch (error) {
|
2024-10-21 11:56:48 -04:00
|
|
|
interpret(1,1, `system_send_message '{"error": "error found: ${error}"}' to ${player}`);
|
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-21 11:56:48 -04:00
|
|
|
async function executeVerb(fullCommand, outputObject, verbId, prepositions, subject, object, ...rest) {
|
|
|
|
if (verbId == 2) {
|
|
|
|
// todo: make this more intelligently get the rright thing to send
|
|
|
|
let theObject;
|
|
|
|
try {
|
|
|
|
theObject = verifyObjectReference(object);
|
|
|
|
} catch (error) {
|
|
|
|
theObject = verifyObjectReference(outputObject);
|
|
|
|
}
|
|
|
|
if (prepositions["to"]) {
|
|
|
|
let destination = verifyObjectReference(prepositions["to"])
|
|
|
|
return sockets.get(destination)?.send(object);
|
|
|
|
}
|
|
|
|
return sockets
|
|
|
|
.get(theObject)
|
|
|
|
?.send(`full command: ${fullCommand}. verb id: ${verbId}. subject: ${subject} object: ${object}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = `
|
|
|
|
function ${verbName} (fullCommand, outputObject, prepositionMap, subject, object, ...)
|
|
|
|
${body}
|
|
|
|
end`;
|
|
|
|
console.log("verb we're running:");
|
|
|
|
console.log(verbDeclaration);
|
|
|
|
await lua.doString(verbDeclaration);
|
|
|
|
|
|
|
|
|
|
|
|
let returnValue = await lua.global.get(verbName)(fullCommand, outputObject, prepositions, subject, object, ...rest);
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const objectQuery = db.prepare('select * from pages where number=? order by time desc');
|
|
|
|
function lookUpObject(number) {
|
|
|
|
return objectQuery.get(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// test our word against
|
|
|
|
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) {
|
|
|
|
const newVerbs = (getAttribute(focus, "verbs") || []).filter((verb) => !(verb in verbs));
|
|
|
|
verbs = verbs.concat(newVerbs);
|
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-21 11:56:48 -04:00
|
|
|
if (!verifyObjectReference(obj)) return undefined;
|
|
|
|
|
|
|
|
let attributeStore = pullAttribute.get(verifyObjectReference(obj));
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
contents[attributeName] = value;
|
|
|
|
|
|
|
|
insertAttribute.run({...attributeStore, contents: JSON.stringify(contents)});
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
let b2 = b.filter((x) => !(x in a));
|
|
|
|
return a.concat(b2);
|
|
|
|
}
|
|
|
|
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 };
|