Websocket moves #19
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
@@ -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({
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user