mirror of
https://github.com/djdevin/recflare.git
synced 2026-09-09 07:01:27 -07:00
mark room as in progress
This commit is contained in:
@@ -4,7 +4,12 @@ import { useWorkersLogger } from 'workers-tagged-logger'
|
|||||||
import { withNotFound, withOnError } from '@repo/hono-helpers'
|
import { withNotFound, withOnError } from '@repo/hono-helpers'
|
||||||
|
|
||||||
import { validateAndGetAccountId } from './jwt'
|
import { validateAndGetAccountId } from './jwt'
|
||||||
import { createRoomInstance, getJoinableInstance, getRoomInstancesByRoom } from './room-instance-db'
|
import {
|
||||||
|
createRoomInstance,
|
||||||
|
getJoinableInstance,
|
||||||
|
getRoomInstancesByRoom,
|
||||||
|
setRoomInstanceInProgress,
|
||||||
|
} from './room-instance-db'
|
||||||
import { getOrCreateDormRoom, getRoomById, getRoomByName } from './rooms-db'
|
import { getOrCreateDormRoom, getRoomById, getRoomByName } from './rooms-db'
|
||||||
|
|
||||||
import type { Context } from 'hono'
|
import type { Context } from 'hono'
|
||||||
@@ -488,6 +493,24 @@ const app = new Hono<App>()
|
|||||||
// ---- Room instance -------------------------------------------------------
|
// ---- Room instance -------------------------------------------------------
|
||||||
.post('/roominstance/:id/reportjoinresult', (c) => c.body(null, 200))
|
.post('/roominstance/:id/reportjoinresult', (c) => c.body(null, 200))
|
||||||
|
|
||||||
|
// The room owner flips the instance's in-progress flag once the session starts
|
||||||
|
// (e.g. a game round begins). Body is a form post: `inProgress=True|False`.
|
||||||
|
.put('/roominstance/:id/inprogress', async (c) => {
|
||||||
|
const id = await authedId(c)
|
||||||
|
if (id === null) return unauthorized(c)
|
||||||
|
|
||||||
|
const instanceId = Number.parseInt(c.req.param('id'), 10)
|
||||||
|
if (Number.isNaN(instanceId)) return c.body(null, 404)
|
||||||
|
|
||||||
|
const body = await c.req.parseBody().catch(() => ({}) as Record<string, unknown>)
|
||||||
|
const inProgress =
|
||||||
|
typeof body.inProgress === 'string' && body.inProgress.toLowerCase() === 'true'
|
||||||
|
|
||||||
|
const instance = await setRoomInstanceInProgress(c.env.DB, instanceId, inProgress)
|
||||||
|
if (!instance) return c.body(null, 404)
|
||||||
|
return c.body(null, 200)
|
||||||
|
})
|
||||||
|
|
||||||
// Rooms flagged as needing a developer/moderator to spawn in. No such queue
|
// Rooms flagged as needing a developer/moderator to spawn in. No such queue
|
||||||
// yet → empty list.
|
// yet → empty list.
|
||||||
.get('/rooms/requiring/developer', (c) => c.json([]))
|
.get('/rooms/requiring/developer', (c) => c.json([]))
|
||||||
|
|||||||
@@ -176,10 +176,34 @@ export async function getRoomInstance(db: D1Database, id: number): Promise<RoomI
|
|||||||
return row ? toDto(parse(row.data)) : null
|
return row ? toDto(parse(row.data)) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flip an instance's `isInProgress` flag, rewriting the JSON blob (the generated
|
||||||
|
* `is_in_progress` column follows it). Returns the updated DTO, or null when the
|
||||||
|
* instance doesn't exist.
|
||||||
|
*/
|
||||||
|
export async function setRoomInstanceInProgress(
|
||||||
|
db: D1Database,
|
||||||
|
id: number,
|
||||||
|
isInProgress: boolean
|
||||||
|
): Promise<RoomInstanceDto | null> {
|
||||||
|
const row = await db
|
||||||
|
.prepare('SELECT data FROM room_instance WHERE id = ?1')
|
||||||
|
.bind(id)
|
||||||
|
.first<{ data: string }>()
|
||||||
|
if (!row) return null
|
||||||
|
const stored = parse(row.data)
|
||||||
|
stored.isInProgress = isInProgress
|
||||||
|
await db
|
||||||
|
.prepare('UPDATE room_instance SET data = ?1 WHERE id = ?2')
|
||||||
|
.bind(JSON.stringify(stored), id)
|
||||||
|
.run()
|
||||||
|
return toDto(stored)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The oldest joinable public instance of a room (not private, not full, joins
|
* The oldest joinable public instance of a room (not private, not full, joins
|
||||||
* enabled), or null when there's none to join. Used by matchmaking to reuse an
|
* enabled, not already in progress), or null when there's none to join. Used by
|
||||||
* existing instance before creating a new one.
|
* matchmaking to reuse an existing instance before creating a new one.
|
||||||
*/
|
*/
|
||||||
export async function getJoinableInstance(
|
export async function getJoinableInstance(
|
||||||
db: D1Database,
|
db: D1Database,
|
||||||
@@ -189,6 +213,7 @@ export async function getJoinableInstance(
|
|||||||
.prepare(
|
.prepare(
|
||||||
`SELECT data FROM room_instance
|
`SELECT data FROM room_instance
|
||||||
WHERE room_id = ?1 AND is_private = 0 AND is_full = 0 AND join_disabled = 0
|
WHERE room_id = ?1 AND is_private = 0 AND is_full = 0 AND join_disabled = 0
|
||||||
|
AND is_in_progress = 0
|
||||||
ORDER BY id LIMIT 1`
|
ORDER BY id LIMIT 1`
|
||||||
)
|
)
|
||||||
.bind(roomId)
|
.bind(roomId)
|
||||||
|
|||||||
Reference in New Issue
Block a user