send scrunklos in real time

This commit is contained in:
Jill 2022-03-05 15:34:56 +03:00
parent 1986f71757
commit 838ad4eaf0
1 changed files with 27 additions and 11 deletions

View File

@ -17,6 +17,9 @@ const storage = multer.diskStorage({
}
});
const EventEmitter = require('events');
const images = new EventEmitter();
const jsonParser = bodyParser.json();
const upload = multer({ storage: storage, fileFilter: (req, file, cb) => {
@ -34,6 +37,8 @@ const sql = postgres(`postgres://${process.env.DB_USER || 'chartmaker'}:${proces
expressws(app);
app.use('/img', express.static('uploads'));
app.use((req, res, next) => {
res = res.setHeader('Access-Control-Allow-Origin', '*');
next();
@ -48,26 +53,28 @@ app.post('/create', jsonParser, async (req, res) => {
let body = req.body || {};
await sql`
INSERT INTO charts VALUES (${newid}, ${body.name || ''}, ${body.leftText || ''}, ${body.rightText || ''}, ${body.topText || ''}, ${body.bottomText || ''})
INSERT INTO charts VALUES (${newid}, ${body.name || ''}, ${body.left || ''}, ${body.right || ''}, ${body.top || ''}, ${body.bot || ''})
`;
res.status(200).send(hashids.encode(newid));
});
app.post('/add', upload.single('icon'), async (req, res) => {
if (!req.body) return res.sendStatus(401);
if (!req.body.id) return res.status(401).send('Supply a chart ID!');
if (!req.body.name) return res.status(401).send('Supply a name!');
if (!req.body.x || !req.body.y) return res.status(401).send('Supply a position!');
if (!req.body) return res.sendStatus(400);
if (!req.file) return res.status(400).send('Send a file!');
if (!req.body.id) return res.status(400).send('Supply a chart ID!');
if (!req.body.name) return res.status(400).send('Supply a name!');
if (!req.body.x || !req.body.y) return res.status(400).send('Supply a position!');
const id = hashids.decode(req.body.id);
if (!id[0]) return res.status(401).send('Invalid chart ID!');
if (!id[0]) return res.status(400).send('Invalid chart ID!');
let [exists] = await sql`
SELECT id FROM charts WHERE id = ${id[0]}
`;
if (!exists) return res.status(401).send('Chart doesn\'t exist!');
if (!exists) return res.status(400).send('Chart doesn\'t exist!');
await sql`
INSERT INTO images VALUES (${id[0]}, ${req.file.filename}, ${req.body.name}, ${req.body.x}, ${req.body.y})
`;
images.emit('imageAdd', id[0], {name: req.body.name, x: req.body.x, y: req.body.y, file: req.file.filename})
res.sendStatus(200);
});
@ -79,13 +86,22 @@ app.ws('/connect', async (ws, req) => {
SELECT * FROM charts WHERE id = ${id}
`;
if (!chart) return ws.close(1008, 'Chart doesn\'t exist!');
console.log(chart);
await ws.send(JSON.stringify({name: chart.name, leftText: chart.leftText, rightText: chart.rightText, topText: chart.topText, bottomText: chart.bottomText}));
await ws.send(JSON.stringify({name: chart.name, leftText: chart.lefttext, rightText: chart.righttext, topText: chart.toptext, bottomText: chart.bottomtext}));
await ws.send(JSON.stringify((await sql`SELECT * FROM images WHERE id = ${id}`).map(i => {return {name: i.name, x: i.x, y: i.y, file: i.filepath}})));
// and now send new scrunklys whenever they appear
// TODO
const callback = (scrunklyId, scrunkly) => {
console.log(scrunklyId, scrunkly);
if (scrunklyId === id) {
ws.send(JSON.stringify([scrunkly]));
}
};
images.on('imageAdd', callback);
ws.on('close', () => {images.off('imageAdd', callback)});
});
(async () => {
@ -101,7 +117,7 @@ app.ws('/connect', async (ws, req) => {
`;
await sql`
CREATE TABLE IF NOT EXISTS images (
id int PRIMARY KEY,
id int,
filepath text,
name text,
x real,