Moving run to Websocket #19

This commit is contained in:
2026-02-15 23:48:04 +01:00
parent d5db0d1352
commit 8c7a05efde
2 changed files with 146 additions and 69 deletions
+100 -46
View File
@@ -1,80 +1,134 @@
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";
import { WebSocketServer, WebSocket } from "ws";
import { randomUUID } from "crypto";
import { createClient } from "@/lib/supabase/server";
import { getSession } from "@/modules/account/core";
import * as Core from "@/modules/live-run/core";
import { ClientMessage } from "./types";
export async function PUT(req: NextRequest) {
const { problem, category } = await req.json();
if (!problem || !category)
return NextResponse.json(
{ error: "body requires problem and category" },
{ status: 400 },
);
export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.on("message", async (raw) => {
try {
const msg: ClientMessage = JSON.parse(raw.toString());
const supabase = await createClient();
const session = await getSession(supabase);
if (!session)
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
if (!session) {
client.send(JSON.stringify({ ok: false, error: "Invalid session" }));
return;
}
switch (msg.type) {
/* ---------------- CREATE ---------------- */
case "create": {
const { problem, category } = msg;
if (!problem || !category) {
client.send(
JSON.stringify({
ok: false,
error: "problem and category required",
}),
);
return;
}
const runId = randomUUID();
await Core.addLiveRun({
id: runId,
problem,
category,
id: runId,
username: session[1].username,
views: 0,
start: new Date().getTime(),
start: Date.now(),
runsCount: 0,
});
return NextResponse.json({ ok: true, runId });
client.send(
JSON.stringify({
ok: true,
type: "create",
data: { runId },
}),
);
break;
}
export async function POST(req: NextRequest) {
const { runId } = await req.json();
/* ---------------- SUBMIT ---------------- */
case "submit": {
const { runId } = msg;
if (!runId)
return NextResponse.json({ error: "body requires runId" }, { status: 400 });
const supabase = await createClient();
const session = await getSession(supabase);
if (!session)
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
if (!runId) {
client.send(JSON.stringify({ ok: false, error: "runId required" }));
return;
}
const liveRun = await Core.getLiveRun(runId);
if (!liveRun)
return NextResponse.json(
{ error: "Run is not live or does not exist" },
{ status: 401 },
if (!liveRun) {
client.send(
JSON.stringify({
ok: false,
error: "Run is not live or does not exist",
}),
);
return;
}
const run = await Core.submitRun(supabase, liveRun, session[0].id);
return NextResponse.json({ ok: true, run });
client.send(
JSON.stringify({
ok: true,
type: "submit",
data: run,
}),
);
break;
}
export async function DELETE(req: NextRequest) {
const runId = req.nextUrl.searchParams.get("runId");
/* ---------------- DELETE ---------------- */
case "delete": {
const { runId } = msg;
if (!runId)
return NextResponse.json({ error: "missing ?runId=" }, { status: 400 });
const supabase = await createClient();
const session = await getSession(supabase);
if (!session)
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
if (!runId) {
client.send(JSON.stringify({ ok: false, error: "runId required" }));
return;
}
const run = await Core.removeLiveRun(runId);
return NextResponse.json({ ok: true, run });
client.send(
JSON.stringify({
ok: true,
type: "delete",
data: run,
}),
);
break;
}
default:
client.send(
JSON.stringify({
ok: false,
error: "Unknown message type",
}),
);
}
} catch (err) {
console.error(err);
client.send(
JSON.stringify({
ok: false,
error: "Malformed message",
}),
);
}
});
client.on("close", () => {
// optional cleanup
});
}
+23
View File
@@ -0,0 +1,23 @@
export type ClientMessage =
| {
type: "create";
problem: string;
category: "any%" | "100%";
}
| {
type: "move";
runId: string;
// moves, file, language
}
| {
type: "submit";
runId: string;
}
| {
type: "delete";
runId: string;
};
export type ServerMessage =
| { ok: true; type: string; data?: any }
| { ok: false; error: string };