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
+102 -48
View File
@@ -1,80 +1,134 @@
import { NextRequest, NextResponse } from "next/server"; import { WebSocketServer, WebSocket } from "ws";
import { createClient } from "@/lib/supabase/server";
import * as Core from "@/modules/live-run/core";
import { getSession } from "@/modules/account/core";
import { randomUUID } from "crypto"; 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) { export function UPGRADE(client: WebSocket, server: WebSocketServer) {
const { problem, category } = await req.json(); client.on("message", async (raw) => {
try {
if (!problem || !category) const msg: ClientMessage = JSON.parse(raw.toString());
return NextResponse.json(
{ error: "body requires problem and category" },
{ status: 400 },
);
const supabase = await createClient(); const supabase = await createClient();
const session = await getSession(supabase); const session = await getSession(supabase);
if (!session) if (!session) {
return NextResponse.json({ error: "Invalid session" }, { status: 401 }); 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(); const runId = randomUUID();
await Core.addLiveRun({ await Core.addLiveRun({
id: runId,
problem, problem,
category, category,
id: runId,
username: session[1].username, username: session[1].username,
views: 0, views: 0,
start: new Date().getTime(), start: Date.now(),
runsCount: 0, 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) { /* ---------------- SUBMIT ---------------- */
const { runId } = await req.json(); case "submit": {
const { runId } = msg;
if (!runId) if (!runId) {
return NextResponse.json({ error: "body requires runId" }, { status: 400 }); client.send(JSON.stringify({ ok: false, error: "runId required" }));
return;
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); const liveRun = await Core.getLiveRun(runId);
if (!liveRun) if (!liveRun) {
return NextResponse.json( client.send(
{ error: "Run is not live or does not exist" }, JSON.stringify({
{ status: 401 }, ok: false,
error: "Run is not live or does not exist",
}),
); );
return;
}
const run = await Core.submitRun(supabase, liveRun, session[0].id); 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) { /* ---------------- DELETE ---------------- */
const runId = req.nextUrl.searchParams.get("runId"); case "delete": {
const { runId } = msg;
if (!runId) if (!runId) {
return NextResponse.json({ error: "missing ?runId=" }, { status: 400 }); client.send(JSON.stringify({ ok: false, error: "runId required" }));
return;
const supabase = await createClient(); }
const session = await getSession(supabase);
if (!session)
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
const run = await Core.removeLiveRun(runId); 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 };