const sqlite = require('better-sqlite3'); const db = new sqlite('the_big_db.db', { verbose: console.log }); var sockets = new Map(); 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. // command: the full string typed in by the player const socket = sockets.get(player) const words = command.trim().split(' '); socket?.send(`interpreting and executing the received command: ${command}`); const verbs = findAvailableVerbs(context, player, command); socket?.send(`found these verbs: ${Array.from(verbs.keys()).join(', ')}`) // first word is either a subject or a verb. either way there must be a verb. const [first, second, third, ...rest] = words; // if the second word is a verb, but the first word is also a verb, what do we do? if (second in verbs) { // 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); } else { // 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) } } function findAvailableVerbs(location, actor, command) { // 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); out.set("warp", 31); return out; } function executeVerb(outputObject, verbId, subject, object, ...rest) { const fullVerb = lookUpObject(verbId); if (!fullVerb) { return sockets.get(outputObject)?.send(`verb not found`); } sockets.get(outputObject)?.send(`verb found as ${fullVerb.name}`); const func = fullVerb.fn; if (!func) { return sockets.get(outputObject)?.send(`verb was found but didn't have a fn attribute`); } return fullVerb.fn(outputObject, 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", verb: false, contents: "send description of direct object to subject's socket", fn: (logReceiver, subject, object, ...rest) => { sockets.get(logReceiver)?.send(`you looked around! subject ${subject} object: ${object} args: ${rest}`); console.log(`${subject} looked at ${object} with args ${rest}, and output was directed to ${logReceiver}`); } } if (id == 31) return { name: "warp", verb: true, contents: "this built-in function changes the player's location, as well as telling their client to move", fn: (logReceiver, subject, object, ...rest) => { // TODO: change subject's location sockets.get(logReceiver)?.send(`location change to: ${object}`); } } } module.exports = { interpret, sockets, lookUpObject };