diff --git a/src/app.js b/src/app.js index 2414717..7b2d7ee 100644 --- a/src/app.js +++ b/src/app.js @@ -37,6 +37,7 @@ import { setupWorms } from './content/worms.js' import { _deprecated__setupDecodeMe } from './content/decode-me-deprecated.js' import { setupHints } from './server/routes/hints.js' import { withVersion } from './server/lib/withVersion.js' +import { setupWormsManagement } from './content/worms/worms-management.js' /** @type {any} App will be assembled step-wise */ const preApp = { @@ -84,6 +85,8 @@ setupPleaseFixMe(App) setupEnough(App) setupWorms(App) +setupWormsManagement(App) + // keep it for now _deprecated__setupDecodeMe(App) diff --git a/src/content/worms/worms-management.js b/src/content/worms/worms-management.js new file mode 100644 index 0000000..31c0adb --- /dev/null +++ b/src/content/worms/worms-management.js @@ -0,0 +1,56 @@ +import { renderPage } from '../../helper/render-page.js' + +/** + * + * @param {import("../../data/types.js").App} App + */ +export function setupWormsManagement(App) { + App.express.get('/worms/drafts', async (req, res) => { + if (!req.user) { + res.redirect('/') + return + } + + const bots = await App.db.models.WormsBotDraft.findAll({ + where: { UserId: req.user.id }, + raw: true, + }) + + renderPage(App, req, res, { + page: 'worms-drafts', + heading: 'Deine Bots', + backButton: false, + content: ` +
${JSON.stringify(bots)}
+ +Neuen Bot erstellen
+ `, + }) + }) + + App.express.get('/worms/drafts/create', async (req, res) => { + if (!req.user) { + res.redirect('/') + return + } + + const name = req.query.name ? req.query.name.toString() : '' + + if (!name || name.length >= 200) { + res.send('Fehler: Name fehlt oder zu lang (maximal 200 Zeichen)') + return + } + + await App.db.models.WormsBotDraft.create({ + name, + UserId: req.user.id, + code: ` +function think(dx, dy, board, dir, x, y, z) { + +} +`, + }) + + res.redirect('/worms/drafts') + }) +} diff --git a/src/data/types.ts b/src/data/types.ts index f769e6c..e15f749 100644 --- a/src/data/types.ts +++ b/src/data/types.ts @@ -30,6 +30,7 @@ export interface App { Session: ModelStatic