All files / web/src/lib/arcade session-manager.ts

68.45% Statements 319/466
52.94% Branches 18/34
83.33% Functions 10/12
68.45% Lines 319/466

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 4671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 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 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                     1x 1x 1x 1x 1x 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 1x 1x 1x 1x 2x                       2x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 7x       4x 4x 4x 4x 4x 4x 7x       4x 4x 4x 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 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 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 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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                    
/**
 * Arcade session manager
 * Handles database operations and validation for arcade sessions
 */
 
import { eq, and } from 'drizzle-orm'
import { db, schema } from '@/db'
import { buildPlayerOwnershipMap, type PlayerOwnershipMap } from './player-ownership'
import { getValidator, type GameName } from './validators'
import type { GameMove } from './validation/types'
 
/**
 * Namespaced game state structure.
 * Each game's state is stored under its game name key.
 * This allows switching games without losing state.
 */
export type NamespacedGameState = {
  [gameName: string]: unknown
}
 
/**
 * Check if gameState is in the new namespaced format.
 * Old format: flat state object
 * New format: { [gameName]: state }
 */
function isNamespacedState(gameState: unknown): gameState is NamespacedGameState {
  if (!gameState || typeof gameState !== 'object') return false
  // Check if it looks like a namespaced state (has game name keys)
  // The old format would have keys like 'gamePhase', 'regionsFound', etc.
  // The new format has keys like 'know-your-world', 'matching', etc.
  const keys = Object.keys(gameState)
  // If it has typical game state keys, it's the old format
  const oldFormatKeys = ['gamePhase', 'currentPrompt', 'cards', 'board', 'score']
  return !keys.some((key) => oldFormatKeys.includes(key))
}
 
/**
 * Extract a game's state from the session, handling both old and new formats.
 * @param session - The arcade session
 * @param gameName - The game to extract state for
 * @returns The game-specific state, or undefined if not found
 */
export function getGameStateFromSession(
  session: schema.ArcadeSession,
  gameName: string
): unknown | undefined {
  const gameState = session.gameState as unknown
 
  const isNamespaced = isNamespacedState(gameState)
 
  if (isNamespaced) {
    // New format: extract from namespace
    return (gameState as NamespacedGameState)[gameName]
  }

  // Old format: if currentGame matches, return the flat state
  // This provides backward compatibility during migration
  if (session.currentGame === gameName) {
    return gameState
  }

  // Game mismatch with old format - no state available
  return undefined
}
 
/**
 * Create a namespaced game state object with the given game's state.
 * Preserves existing game states when updating.
 */
export function setGameStateInNamespace(
  existingState: unknown,
  gameName: string,
  newState: unknown
): NamespacedGameState {
  const namespaced: NamespacedGameState = isNamespacedState(existingState)
    ? { ...existingState }
    : {}
 
  namespaced[gameName] = newState
  return namespaced
}
 
export interface CreateSessionOptions {
  userId: string // User who owns/created the session (typically room creator)
  gameName: GameName
  gameUrl: string
  initialState: unknown
  activePlayers: string[] // Player IDs (UUIDs)
  roomId: string // Required - PRIMARY KEY, one session per room
}
 
export interface SessionUpdateResult {
  success: boolean
  error?: string
  session?: schema.ArcadeSession
  versionConflict?: boolean
}
 
const TTL_HOURS = 24
 
/**
 * Get arcade session by room ID (for room-based multiplayer games)
 * Returns the shared session for all room members
 * @param roomId - The room ID (primary key)
 */
export async function getArcadeSessionByRoom(
  roomId: string
): Promise<schema.ArcadeSession | undefined> {
  // roomId is now the PRIMARY KEY, so direct lookup
  const [session] = await db
    .select()
    .from(schema.arcadeSessions)
    .where(eq(schema.arcadeSessions.roomId, roomId))
    .limit(1)
 
  if (!session) return undefined

  // Check if session has expired
  if (session.expiresAt < new Date()) {
    // Clean up expired room session
    await db.delete(schema.arcadeSessions).where(eq(schema.arcadeSessions.roomId, roomId))
    return undefined
  }

  return session
}
 
/**
 * Create a new arcade session
 * For room-based games, roomId is the PRIMARY KEY ensuring one session per room
 */
export async function createArcadeSession(
  options: CreateSessionOptions
): Promise<schema.ArcadeSession> {
  const now = new Date()
  const expiresAt = new Date(now.getTime() + TTL_HOURS * 60 * 60 * 1000)
 
  // Check if session already exists for this room (roomId is PRIMARY KEY)
  const existingRoomSession = await getArcadeSessionByRoom(options.roomId)
  if (existingRoomSession) {
    return existingRoomSession
  }
 
  // Look up user by database user.id (the client always sends users.id now)
  const user = await db.query.users.findFirst({
    where: eq(schema.users.id, options.userId),
  })
 
  if (!user) {
    throw new Error(`User not found: ${options.userId}`)
  }
 
  // Delete any existing sessions for this user (to handle UNIQUE constraint on userId)
  // This ensures the user can start a new game session
  await db.delete(schema.arcadeSessions).where(eq(schema.arcadeSessions.userId, user.id))
 
  // Store game state in namespaced format: { [gameName]: state }
  // This allows switching games without losing state
  const namespacedState: NamespacedGameState = {
    [options.gameName]: options.initialState,
  }
 
  const newSession: schema.NewArcadeSession = {
    roomId: options.roomId, // PRIMARY KEY - one session per room
    userId: user.id, // Use the actual database ID, not the guestId
    currentGame: options.gameName,
    gameUrl: options.gameUrl,
    gameState: namespacedState as any,
    activePlayers: options.activePlayers as any,
    startedAt: now,
    lastActivityAt: now,
    expiresAt,
    isActive: true,
    version: 1,
  }
 
  try {
    const [session] = await db.insert(schema.arcadeSessions).values(newSession).returning()
    return session
  } catch (error) {
    // Handle PRIMARY KEY constraint violation (UNIQUE constraint on roomId)
    // This can happen if two users try to create a session for the same room simultaneously
    if (error instanceof Error && error.message.includes('UNIQUE constraint failed')) {
      const existingSession = await getArcadeSessionByRoom(options.roomId)
      if (existingSession) {
        return existingSession
      }
    }
    // Re-throw other errors
    throw error
  }
}
 
/**
 * Get active arcade session for a user
 * @param userId - The database user.id
 */
export async function getArcadeSession(userId: string): Promise<schema.ArcadeSession | undefined> {
  const [session] = await db
    .select()
    .from(schema.arcadeSessions)
    .where(eq(schema.arcadeSessions.userId, userId))
    .limit(1)
 
  if (!session) return undefined
 
  // Check if session has expired
  if (session.expiresAt < new Date()) {
    await deleteArcadeSessionByRoom(session.roomId)
    return undefined
  }
 
  // Verify the room still exists (roomId is now required/PRIMARY KEY)
  const room = await db.query.arcadeRooms.findFirst({
    where: eq(schema.arcadeRooms.id, session.roomId),
  })
 
  if (!room) {
    await deleteArcadeSessionByRoom(session.roomId)
    return undefined
  }
 
  return session
}
 
/**
 * Apply a game move to the session (with validation)
 * @param userId - The database user.id of the player making the move
 * @param move - The game move to apply
 * @param roomId - Optional room ID for room-based games (enables shared session)
 */
export async function applyGameMove(
  userId: string,
  move: GameMove,
  roomId?: string
): Promise<SessionUpdateResult> {
  // For room-based games, look up the shared room session
  // For solo games, look up the user's personal session
  const session = roomId ? await getArcadeSessionByRoom(roomId) : await getArcadeSession(userId)
 
  if (!session) {
    return {
      success: false,
      error: 'No active session found',
    }
  }
 
  if (!session.isActive) {
    return {
      success: false,
      error: 'Session is not active',
    }
  }
 
  // Get the validator for this game
  const gameName = session.currentGame as GameName
  const validator = await getValidator(gameName)
 
  // Extract the current game's state from the namespaced storage
  const currentGameState = getGameStateFromSession(session, gameName)
  if (currentGameState === undefined) {
    console.error(
      `[SessionManager] No state found for game ${gameName} in session ${session.roomId}`
    )
    return {
      success: false,
      error: `No state found for game ${gameName}`,
    }
  }
 
  // Fetch player ownership for authorization checks (room-based games)
  let playerOwnership: PlayerOwnershipMap | undefined
  if (session.roomId) {
    try {
      playerOwnership = await buildPlayerOwnershipMap(session.roomId)
    } catch (error) {
      console.error('[SessionManager] Failed to fetch player ownership:', error)
    }
  }
 
  // Validate the move with authorization context
  const validationResult = await validator.validateMove(currentGameState, move, {
    userId,
    playerOwnership,
  })
 
  if (!validationResult.valid) {
    return {
      success: false,
      error: validationResult.error || 'Invalid move',
    }
  }
 
  // Update the session with new state (using optimistic locking)
  // Store the new state in the namespaced format, preserving other games' states
  const updatedNamespacedState = setGameStateInNamespace(
    session.gameState,
    gameName,
    validationResult.newState
  )
 
  const now = new Date()
  const expiresAt = new Date(now.getTime() + TTL_HOURS * 60 * 60 * 1000)
 
  try {
    const [updatedSession] = await db
      .update(schema.arcadeSessions)
      .set({
        gameState: updatedNamespacedState as any,
        lastActivityAt: now,
        expiresAt,
        version: session.version + 1,
      })
      .where(
        and(
          eq(schema.arcadeSessions.roomId, session.roomId), // Use roomId (PRIMARY KEY)
          eq(schema.arcadeSessions.version, session.version) // Optimistic locking
        )
      )
      .returning()
 
    if (!updatedSession) {
      // Version conflict - another move was processed first
      // Query the current state to see what version we're at now
      const [currentSession] = await db
        .select()
        .from(schema.arcadeSessions)
        .where(eq(schema.arcadeSessions.roomId, session.roomId))
        .limit(1)

      const versionDiff = currentSession ? currentSession.version - session.version : 'unknown'
      console.warn(
        `[SessionManager] VERSION_CONFLICT room=${session.roomId} game=${session.currentGame} expected_v=${session.version} actual_v=${currentSession?.version} diff=${versionDiff} move=${move.type} user=${userId}`
      )
      return {
        success: false,
        error: 'Version conflict - please retry',
        versionConflict: true,
      }
    }
 
    return {
      success: true,
      session: updatedSession,
    }
  } catch (error) {
    console.error('Error updating session:', error)
    return {
      success: false,
      error: 'Database error',
    }
  }
}
 
/**
 * Delete an arcade session by room ID
 * @param roomId - The room ID (PRIMARY KEY)
 */
export async function deleteArcadeSessionByRoom(roomId: string): Promise<void> {
  await db.delete(schema.arcadeSessions).where(eq(schema.arcadeSessions.roomId, roomId))
}
 
/**
 * Delete an arcade session by user (finds the user's session first)
 * @param userId - The database user.id
 */
export async function deleteArcadeSession(userId: string): Promise<void> {
  // First find the session to get its roomId
  const session = await getArcadeSession(userId)
  if (!session) return
 
  // Delete by roomId (PRIMARY KEY)
  await deleteArcadeSessionByRoom(session.roomId)
}
 
/**
 * Update session activity timestamp (keep-alive)
 * @param userId - The database user.id
 */
export async function updateSessionActivity(userId: string): Promise<void> {
  // First find the session to get its roomId
  const session = await getArcadeSession(userId)
  if (!session) return
 
  const now = new Date()
  const expiresAt = new Date(now.getTime() + TTL_HOURS * 60 * 60 * 1000)
 
  // Update using roomId (PRIMARY KEY)
  await db
    .update(schema.arcadeSessions)
    .set({
      lastActivityAt: now,
      expiresAt,
    })
    .where(eq(schema.arcadeSessions.roomId, session.roomId))
}
 
/**
 * Update session's active players (only if game hasn't started)
 * Used when new members join a room
 * @param roomId - The room ID (PRIMARY KEY)
 * @param playerIds - Array of player IDs to set as active players
 * @returns true if updated, false if game already started or session not found
 */
export async function updateSessionActivePlayers(
  roomId: string,
  playerIds: string[]
): Promise<boolean> {
  const session = await getArcadeSessionByRoom(roomId)
  if (!session) return false

  // Extract the current game's state
  const gameName = session.currentGame as string
  const currentGameState = getGameStateFromSession(session, gameName) as Record<string, unknown>

  if (!currentGameState) {
    console.error(
      `[SessionManager] No state found for game ${gameName} when updating active players`
    )
    return false
  }

  // Only update if game is in setup phase (not started yet)
  if (currentGameState.gamePhase !== 'setup') {
    return false
  }

  // Update the game state with new player IDs
  const updatedGameState = {
    ...currentGameState,
    activePlayers: playerIds,
  }

  // Store back in namespaced format
  const updatedNamespacedState = setGameStateInNamespace(
    session.gameState,
    gameName,
    updatedGameState
  )

  const now = new Date()
  await db
    .update(schema.arcadeSessions)
    .set({
      activePlayers: playerIds as any,
      gameState: updatedNamespacedState as any,
      lastActivityAt: now,
      version: session.version + 1,
    })
    .where(eq(schema.arcadeSessions.roomId, roomId))

  return true
}
 
/**
 * Clean up expired sessions (should be called periodically)
 */
export async function cleanupExpiredSessions(): Promise<number> {
  const now = new Date()
  const result = await db
    .delete(schema.arcadeSessions)
    .where(eq(schema.arcadeSessions.expiresAt, now))
    .returning()

  return result.length
}