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.
47 lines
1.5 KiB
47 lines
1.5 KiB
1 week ago
|
const sqlite = require('better-sqlite3');
|
||
|
const db = new sqlite('the_big_db.db', { verbose: console.log });
|
||
|
|
||
|
var sockets = new Map();
|
||
|
|
||
|
function interpret(context, subject, command) {
|
||
|
const words = command.split(' ');
|
||
|
|
||
|
const verbs = findVerbs(context, subject);
|
||
|
// first word is either a subject or a verb. either way there must be a verb.
|
||
|
// check if the first word is in the list of verbs.
|
||
|
const [first, second, third, ...rest] = words;
|
||
|
if (second in verbs) {
|
||
|
executeVerb(verbs.get(second), first, third, ...rest);
|
||
|
} else {
|
||
|
executeVerb(verbs.get(first), subject, second, third, ...rest)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function findVerbs(location, actor) {
|
||
|
// check for verbs on actor
|
||
|
// check for verbs on objects in location
|
||
|
// check for verbs on location
|
||
|
const out = new Map();
|
||
|
out.set("look", 29);
|
||
|
return out;
|
||
|
}
|
||
|
|
||
|
function executeVerb(verb, subject, object, ...rest) {
|
||
|
lookUpObject(verb).fn(subject, object, ...rest)
|
||
|
}
|
||
|
|
||
|
const objectQuery = db.prepare('select * from pages where id=?');
|
||
|
function lookUpObject(id) {
|
||
|
// return objectQuery.get(id);
|
||
|
if (id == 30) return {name: "shoofle", contents: "this is a shoofle", location: 1};
|
||
|
if (id == 29) return {
|
||
|
name: "look",
|
||
|
contents: "send description of direct object to subject's socket",
|
||
|
fn: (subject, object, ...rest) => {
|
||
|
sockets.get(subject)?.send(`you looked around! subject: ${subject} object: ${object} args: ${rest}`);
|
||
|
console.log(`${subject} looked at ${object} with args ${rest}`);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = { interpret, sockets, lookUpObject };
|