All files / web/src/app/api/arcade-session route.ts

0% Statements 0/107
0% Branches 0/1
0% Functions 0/1
0% Lines 0/107

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                                                                                                                                                                                                                       
import { NextResponse } from 'next/server'
import {
  createArcadeSession,
  deleteArcadeSession,
  getArcadeSession,
} from '@/lib/arcade/session-manager'
import { withAuth } from '@/lib/auth/withAuth'
import type { GameName } from '@/lib/arcade/validation'

/**
 * GET /api/arcade-session?userId=xxx
 * Get the active arcade session for a user
 */
export const GET = withAuth(async (request) => {
  try {
    const userId = request.nextUrl.searchParams.get('userId')

    if (!userId) {
      return NextResponse.json({ error: 'userId required' }, { status: 400 })
    }

    const session = await getArcadeSession(userId)

    if (!session) {
      return NextResponse.json({ error: 'No active session' }, { status: 404 })
    }

    return NextResponse.json({
      session: {
        currentGame: session.currentGame,
        gameUrl: session.gameUrl,
        gameState: session.gameState,
        activePlayers: session.activePlayers,
        version: session.version,
        expiresAt: session.expiresAt,
      },
    })
  } catch (error) {
    console.error('Error fetching arcade session:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
})

/**
 * POST /api/arcade-session
 * Create a new arcade session
 */
export const POST = withAuth(async (request) => {
  try {
    const body = await request.json()
    const { userId, gameName, gameUrl, initialState, activePlayers, roomId } = body

    if (!userId || !gameName || !gameUrl || !initialState || !activePlayers || !roomId) {
      return NextResponse.json(
        {
          error:
            'Missing required fields (userId, gameName, gameUrl, initialState, activePlayers, roomId)',
        },
        { status: 400 }
      )
    }

    const session = await createArcadeSession({
      userId,
      gameName: gameName as GameName,
      gameUrl,
      initialState,
      activePlayers,
      roomId,
    })

    return NextResponse.json({
      session: {
        currentGame: session.currentGame,
        gameUrl: session.gameUrl,
        gameState: session.gameState,
        activePlayers: session.activePlayers,
        version: session.version,
        expiresAt: session.expiresAt,
      },
    })
  } catch (error) {
    console.error('Error creating arcade session:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
})

/**
 * DELETE /api/arcade-session?userId=xxx
 * Delete an arcade session
 */
export const DELETE = withAuth(async (request) => {
  try {
    const userId = request.nextUrl.searchParams.get('userId')

    if (!userId) {
      return NextResponse.json({ error: 'userId required' }, { status: 400 })
    }

    await deleteArcadeSession(userId)

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Error deleting arcade session:', error)
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
  }
})