the-forest/server/routes/live.js

47 lines
1.3 KiB
JavaScript

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");
ws.send(`location change to: #${getAttribute(character, "location")}`);
ws.on('message', (msg) => {
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;