Skip to content

Commit

Permalink
setup worms management
Browse files Browse the repository at this point in the history
  • Loading branch information
Entkenntnis committed Dec 6, 2024
1 parent 04ce787 commit 10ec90a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -84,6 +85,8 @@ setupPleaseFixMe(App)
setupEnough(App)
setupWorms(App)

setupWormsManagement(App)

// keep it for now
_deprecated__setupDecodeMe(App)

Expand Down
56 changes: 56 additions & 0 deletions src/content/worms/worms-management.js
Original file line number Diff line number Diff line change
@@ -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: `
<p>${JSON.stringify(bots)}</p>
<p>Neuen Bot erstellen</p>
`,
})
})

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')
})
}
13 changes: 13 additions & 0 deletions src/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface App {
Session: ModelStatic<SessionModel>
Room: ModelStatic<RoomModel>
KVPair: ModelStatic<KVPairModel>
WormsBotDraft: ModelStatic<WormsBotDraftModel>
}
}
i18n: { get(lng: 'de' | 'en'): i18n }
Expand Down Expand Up @@ -160,6 +161,18 @@ export class KVPairModel extends Model<
declare updatedAt: CreationOptional<string | Date>
}

export class WormsBotDraftModel extends Model<
InferAttributes<WormsBotDraftModel>,
InferCreationAttributes<WormsBotDraftModel>
> {
declare id: CreationOptional<number>
declare name: string
declare code: string
declare UserId: number
declare createdAt: CreationOptional<string | Date>
declare updatedAt: CreationOptional<string | Date>
}

declare global {
namespace Express {
interface Request {
Expand Down
24 changes: 24 additions & 0 deletions src/server/lib/dbModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,28 @@ export function dbModel(App) {
type: DataTypes.TEXT,
},
})

// Additional tables
const WormsBotDraft = App.db.define('WormsBotDraft', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING(255),
allowNull: false,
},
code: {
type: DataTypes.TEXT,
allowNull: false,
},
})

User.hasMany(WormsBotDraft, { onDelete: 'cascade' })
WormsBotDraft.belongsTo(User)

// WormsArenaBot

// WormsArenaReplay
}

0 comments on commit 10ec90a

Please sign in to comment.