inventions

This commit is contained in:
Devin Zuczek
2026-07-11 13:24:41 -04:00
parent 8aace02453
commit 84ac79427d
4 changed files with 368 additions and 13 deletions
+53 -2
View File
@@ -1,6 +1,7 @@
import { Hono } from 'hono'
import { authedId, unauthorized } from '../http'
import { createInvention, getInventionById, getInventionsByCreator } from '../inventions-db'
import type { App } from '../context'
@@ -68,5 +69,55 @@ export const avatarRoutes = new Hono<App>({ strict: false })
c.json({ Results: [], TotalResults: 0 })
)
// Saved inventions — empty list with no DB.
.get('/api/inventions/v2/mine', (c) => c.json([]))
// A single invention by id (`?inventionId=…`). Returns the stored RRInvention,
// or 404 when there's no such invention.
.get('/api/inventions/v1', async (c) => {
const inventionId = Number.parseInt(c.req.query('inventionId') ?? '', 10)
if (Number.isNaN(inventionId)) return c.json({ error: 'inventionId is required' }, 400)
const invention = await getInventionById(c.env.DB, inventionId)
return invention ? c.json(invention) : c.notFound()
})
// The signed-in player's saved inventions ("my inventions"), newest first.
// Auth-gated; returns a bare array (empty when the player has saved none).
.get('/api/inventions/v2/mine', async (c) => {
const id = await authedId(c)
if (id === null) return unauthorized(c)
return c.json(await getInventionsByCreator(c.env.DB, id))
})
// Save an invention's metadata. The data file itself is uploaded separately
// through the `storage` worker and referenced here by `inventionDataFilename`.
// Auth-gated; returns the stored invention (with its assigned inventionId).
.post('/api/inventions/v6/save', async (c) => {
const id = await authedId(c)
if (id === null) return unauthorized(c)
const body = (await c.req.json().catch(() => null)) as Record<string, unknown> | null
if (body === null) return c.json({ error: 'Invalid request body' }, 400)
const str = (v: unknown): string | undefined => (typeof v === 'string' ? v : undefined)
const num = (v: unknown): number | undefined => (typeof v === 'number' ? v : undefined)
const name = str(body.name)
if (name === undefined) return c.json({ error: 'name is required' }, 400)
const invention = await createInvention(c.env.DB, {
creatorPlayerId: id,
name,
description: str(body.description),
imageName: str(body.imageName),
instantiationCost: num(body.instantiationCost),
lightsCost: num(body.lightsCost),
chipsCost: num(body.chipsCost),
cloudVariablesCost: num(body.cloudVariablesCost),
aiCost: num(body.aiCost),
creationRoomId: num(body.creationRoomId),
inventionDataFilename: str(body.inventionDataFilename),
referencedInventions: Array.isArray(body.referencedInventions)
? body.referencedInventions.filter((v): v is number => typeof v === 'number')
: undefined,
creatorAccountRole: num(body.creatorAccountRole),
})
return c.json(invention)
})