Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 10x 10x 6x 6x 6x 5x 5x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 13x 13x 13x 13x 13x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 4x 4x 4x 2x 2x 2x 2x 4x 4x 4x 1x 1x 1x 1x 3x 3x 3x 3x | /**
* Arcade room manager
* Handles database operations for arcade rooms
*/
import { and, desc, eq, or } from 'drizzle-orm'
import { db, schema } from '@/db'
import { generateRoomCode } from './room-code'
import type { GameName } from './validation'
export interface CreateRoomOptions {
name: string | null
createdBy: string // User/guest ID
creatorName: string
gameName: GameName
gameConfig: unknown
ttlMinutes?: number // Default: 60
accessMode?: 'open' | 'password' | 'approval-only' | 'restricted' | 'locked' | 'retired'
password?: string
}
export interface UpdateRoomOptions {
name?: string
status?: 'lobby' | 'playing' | 'finished'
currentSessionId?: string | null
totalGamesPlayed?: number
}
/**
* Create a new arcade room
* Generates a unique room code and creates the room in the database
*/
export async function createRoom(options: CreateRoomOptions): Promise<schema.ArcadeRoom> {
const now = new Date()
// Generate unique room code (retry up to 5 times if collision)
let code = generateRoomCode()
let attempts = 0
const MAX_ATTEMPTS = 5
while (attempts < MAX_ATTEMPTS) {
const existing = await getRoomByCode(code)
if (!existing) break
code = generateRoomCode()
attempts++
}
if (attempts === MAX_ATTEMPTS) {
throw new Error('Failed to generate unique room code')
}
const newRoom: schema.NewArcadeRoom = {
code,
name: options.name,
createdBy: options.createdBy,
creatorName: options.creatorName,
createdAt: now,
lastActivity: now,
ttlMinutes: options.ttlMinutes || 60,
accessMode: options.accessMode || 'open', // Default to open access
password: options.password || null,
gameName: options.gameName,
gameConfig: options.gameConfig as any,
status: 'lobby',
currentSessionId: null,
totalGamesPlayed: 0,
}
const [room] = await db.insert(schema.arcadeRooms).values(newRoom).returning()
console.log('[Room Manager] Created room:', room.id, 'code:', room.code)
return room
}
/**
* Get a room by ID
*/
export async function getRoomById(roomId: string): Promise<schema.ArcadeRoom | undefined> {
return await db.query.arcadeRooms.findFirst({
where: eq(schema.arcadeRooms.id, roomId),
})
}
/**
* Get a room by code
*/
export async function getRoomByCode(code: string): Promise<schema.ArcadeRoom | undefined> {
return await db.query.arcadeRooms.findFirst({
where: eq(schema.arcadeRooms.code, code.toUpperCase()),
})
}
/**
* Update a room
*/
export async function updateRoom(
roomId: string,
updates: UpdateRoomOptions
): Promise<schema.ArcadeRoom | undefined> {
const now = new Date()
// Always update lastActivity on any room update
const updateData = {
...updates,
lastActivity: now,
}
const [updated] = await db
.update(schema.arcadeRooms)
.set(updateData)
.where(eq(schema.arcadeRooms.id, roomId))
.returning()
return updated
}
/**
* Update room activity timestamp
* Call this on any room activity to refresh TTL
*/
export async function touchRoom(roomId: string): Promise<void> {
await db
.update(schema.arcadeRooms)
.set({ lastActivity: new Date() })
.where(eq(schema.arcadeRooms.id, roomId))
}
/**
* Delete a room
* Cascade deletes all room members
*/
export async function deleteRoom(roomId: string): Promise<void> {
await db.delete(schema.arcadeRooms).where(eq(schema.arcadeRooms.id, roomId))
console.log('[Room Manager] Deleted room:', roomId)
}
/**
* List active rooms
* Returns rooms ordered by most recently active
* Only returns openly accessible rooms (accessMode: 'open' or 'password')
*/
export async function listActiveRooms(gameName?: GameName): Promise<schema.ArcadeRoom[]> {
const whereConditions = []
// Filter by game if specified
if (gameName) {
whereConditions.push(eq(schema.arcadeRooms.gameName, gameName))
}
// Only return accessible rooms in lobby or playing status
// Exclude locked, retired, restricted, and approval-only rooms
whereConditions.push(
or(eq(schema.arcadeRooms.accessMode, 'open'), eq(schema.arcadeRooms.accessMode, 'password')),
or(eq(schema.arcadeRooms.status, 'lobby'), eq(schema.arcadeRooms.status, 'playing'))
)
return await db.query.arcadeRooms.findMany({
where: whereConditions.length > 0 ? and(...whereConditions) : undefined,
orderBy: [desc(schema.arcadeRooms.lastActivity)],
limit: 50, // Limit to 50 most recent rooms
})
}
/**
* Clean up expired rooms
* Delete rooms that have exceeded their TTL
*/
export async function cleanupExpiredRooms(): Promise<number> {
const now = new Date()
// Find rooms where lastActivity + ttlMinutes < now
const expiredRooms = await db.query.arcadeRooms.findMany({
columns: { id: true, ttlMinutes: true, lastActivity: true },
})
const toDelete = expiredRooms.filter((room) => {
const expiresAt = new Date(room.lastActivity.getTime() + room.ttlMinutes * 60 * 1000)
return expiresAt < now
})
if (toDelete.length > 0) {
const ids = toDelete.map((r) => r.id)
await db.delete(schema.arcadeRooms).where(or(...ids.map((id) => eq(schema.arcadeRooms.id, id))))
console.log(`[Room Manager] Cleaned up ${toDelete.length} expired rooms`)
}
return toDelete.length
}
/**
* Check if a user is the creator of a room
*/
export async function isRoomCreator(roomId: string, userId: string): Promise<boolean> {
const room = await getRoomById(roomId)
return room?.createdBy === userId
}
|