2024-10-21 11:56:48 -04:00
|
|
|
const express = require('express');
|
|
|
|
const app = express.Router();
|
|
|
|
const expressWs = require('express-ws')(app);
|
|
|
|
|
|
|
|
const sqlite = require('better-sqlite3');
|
|
|
|
const db = new sqlite('the_big_db.db', { verbose: console.log });
|
|
|
|
|
|
|
|
const { loginRequired } = require('../authStuff.js');
|
|
|
|
|
|
|
|
const { interpret, sockets, lookUpObject,
|
|
|
|
getAttribute, setAttribute, hasOwnAttribute, deleteAttribute,
|
|
|
|
findAllVerbsInArea } = require('../interpreter.js');
|
|
|
|
|
|
|
|
app.ws('/embody', (ws, req) => {
|
|
|
|
const character = req.session.characterId;
|
|
|
|
if (!character) {
|
|
|
|
ws.send(`user ${req.session.userId} does not have an associated character object`);
|
|
|
|
ws.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockets.set(character, ws);
|
|
|
|
console.log("sending location change, should get attribute for 30");
|
2024-10-25 17:48:12 -04:00
|
|
|
ws.send(JSON.stringify({setPageNumber: getAttribute(character, "location")}));
|
2024-10-21 11:56:48 -04:00
|
|
|
|
|
|
|
ws.on('message', (msg) => {
|
2024-10-25 17:48:12 -04:00
|
|
|
console.log("received message: ", msg);
|
2024-10-21 11:56:48 -04:00
|
|
|
const location = getAttribute(character, "location");
|
|
|
|
|
|
|
|
interpret(location, character, msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('close', () => sockets.delete(character));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/embody/verbs', loginRequired, (req, res) => {
|
|
|
|
const location = getAttribute(req.session.characterId, "location");
|
|
|
|
const verbs = findAllVerbsInArea(location, req.session.characterId);
|
|
|
|
|
|
|
|
const verbWords = Array.from(verbs.map((v) => lookUpObject(v).title));
|
|
|
|
|
|
|
|
res.status(200).json(verbWords);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = app;
|