From cd72390f42d0c104d029f9d1281c851798f0e217 Mon Sep 17 00:00:00 2001 From: Jonas Kappa Date: Sun, 28 Dec 2025 12:51:15 +0100 Subject: [PATCH] Added game creation and deletion --- index.js | 2 + src/games.js | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.js | 13 ++++++ 3 files changed, 128 insertions(+) create mode 100644 src/games.js create mode 100644 src/util.js diff --git a/index.js b/index.js index 078bcfc..e05c1f6 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ import { initAuth } from './src/auth.js'; import { initDbConnection, db } from './src/db.js'; import { initUsers } from './src/user.js'; import { initCdn } from './src/cdn.js'; +import { initGames } from './src/games.js'; const app = express(); const appWs = expressWs(app); const port = 12345; @@ -25,6 +26,7 @@ initAuth(app, db); initUsers(app, db); initWebsocket(app); initCdn(app, db); +initGames(app, db); app.listen(port, () => { console.log(`Listening on port ${port}`); diff --git a/src/games.js b/src/games.js new file mode 100644 index 0000000..78ea626 --- /dev/null +++ b/src/games.js @@ -0,0 +1,113 @@ +import { Collection, Db, ObjectId } from 'mongodb'; +import { checkStringProp } from './util.js'; + +/** + * @type {Collection} + */ +let games; + +/** + * + * @param {*} app + * @param {Db} db + */ +export function initGames(app, db) { + games = db.collection('games'); + app.get('/game', fetchGame); + app.post('/game', createGame); + app.delete('/game/:gameid', deleteGame); + app.get('/games', fetchGames); +} + +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +async function createGame(req, res) { + if (!checkStringProp(req.body, 'name')) { + res.sendStatus(400); + return; + } + + const name = req.body.name; + + games + .insertOne({ + name, + walls: [], + owner: req.user._id, + }) + .then(() => { + res.sendStatus(200); + }) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +} + +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +async function fetchGames(req, res) { + let list = games.find({ + owner: req.user._id, + }); + + res.status(200).send(await list.toArray()); +} + +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +async function fetchGame(req, res) { + if (req.query.id === undefined || req.query.id.length <= 0) { + res.sendStatus(400); + return; + } + + const id = new ObjectId(req.query.id); + + let game = await games.findOne({ + _id: id, + owner: req.user._id, + }); + + if (game) { + res.status(200).send(game); + } else { + res.sendStatus(404); + } +} + +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +async function deleteGame(req, res) { + let game = await games.findOne({ + owner: req.user._id, + _id: new ObjectId(req.params.gameid), + }); + + if (!game) { + res.sendStatus(404); + return; + } + + games + .deleteOne({ _id: game._id }) + .then(() => { + res.sendStatus(200); + }) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +} diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000..e551e82 --- /dev/null +++ b/src/util.js @@ -0,0 +1,13 @@ +/** + * + * @param {any} body + * @param {string} property + */ +export function checkStringProp(body, property) { + if (body === undefined) return false; + if (Object.hasOwn(body, property)) { + if (typeof body[property] === 'string') { + return body[property].length > 0; + } else return false; + } else return false; +}