diff --git a/src/app/api/run/move/route.ts b/src/app/api/run/move/route.ts deleted file mode 100644 index c0bc9cc..0000000 --- a/src/app/api/run/move/route.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { createClient } from "@/lib/supabase/server"; -import * as Core from "@/modules/live-run/core"; -import { getSession } from "@/modules/account/core"; - -export async function POST(req: NextRequest) { - const { runId, moves, file, language } = await req.json(); - - if (!runId || !moves || file === undefined || language === undefined) - return NextResponse.json( - { error: "body requires runId, moves, file and language" }, - { status: 400 }, - ); - - const supabase = await createClient(); - - const session = await getSession(supabase); - - if (!session) - return NextResponse.json({ error: "Invalid session" }, { status: 401 }); - - const liveRun = await Core.getLiveRun(runId); - - if (!liveRun) - return NextResponse.json( - { error: "Run is not live or does not exist" }, - { status: 401 }, - ); - - const run = await Core.addLiveRunEvent(liveRun.id, file, language, moves); - - return NextResponse.json({ ok: true, run }); -} diff --git a/src/app/api/run/route.ts b/src/app/api/run/route.ts index 060fc83..11e0694 100644 --- a/src/app/api/run/route.ts +++ b/src/app/api/run/route.ts @@ -109,6 +109,64 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) { break; } + case "move": { + const { runId, moves, file, language } = msg; + + if ( + !runId || + !moves || + file === undefined || + language === undefined + ) { + client.send( + JSON.stringify({ + ok: false, + error: "body requires runId, moves, file and language", + }), + ); + return; + } + + const supabase = await createClient(); + const session = await getSession(supabase); + + if (!session) { + client.send( + JSON.stringify({ ok: false, error: "Invalid session" }), + ); + return; + } + + const liveRun = await Core.getLiveRun(runId); + + if (!liveRun) { + client.send( + JSON.stringify({ + ok: false, + error: "Run is not live or does not exist", + }), + ); + return; + } + + const run = await Core.addLiveRunEvent( + liveRun.id, + file, + language, + moves, + ); + + client.send( + JSON.stringify({ + ok: true, + type: "move", + data: run, + }), + ); + + break; + } + default: client.send( JSON.stringify({ diff --git a/src/app/api/run/types.ts b/src/app/api/run/types.ts index 6dc9ffb..5b2ec6e 100644 --- a/src/app/api/run/types.ts +++ b/src/app/api/run/types.ts @@ -1,3 +1,5 @@ +import { LiveRunMove } from "@/modules/live-run/types"; + export type ClientMessage = | { type: "create"; @@ -7,7 +9,9 @@ export type ClientMessage = | { type: "move"; runId: string; - // moves, file, language + file: string; + language: string; + moves: LiveRunMove[]; } | { type: "submit";