Moving run to Websocket #19
This commit is contained in:
+123
-69
@@ -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 {
|
||||||
|
const msg: ClientMessage = JSON.parse(raw.toString());
|
||||||
|
|
||||||
if (!problem || !category)
|
const supabase = await createClient();
|
||||||
return NextResponse.json(
|
const session = await getSession(supabase);
|
||||||
{ error: "body requires problem and category" },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
|
|
||||||
const supabase = await createClient();
|
if (!session) {
|
||||||
|
client.send(JSON.stringify({ ok: false, error: "Invalid session" }));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const session = await getSession(supabase);
|
switch (msg.type) {
|
||||||
|
/* ---------------- CREATE ---------------- */
|
||||||
|
case "create": {
|
||||||
|
const { problem, category } = msg;
|
||||||
|
|
||||||
if (!session)
|
if (!problem || !category) {
|
||||||
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
|
client.send(
|
||||||
|
JSON.stringify({
|
||||||
|
ok: false,
|
||||||
|
error: "problem and category required",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const runId = randomUUID();
|
const runId = randomUUID();
|
||||||
|
|
||||||
await Core.addLiveRun({
|
await Core.addLiveRun({
|
||||||
problem,
|
id: runId,
|
||||||
category,
|
problem,
|
||||||
id: runId,
|
category,
|
||||||
username: session[1].username,
|
username: session[1].username,
|
||||||
views: 0,
|
views: 0,
|
||||||
start: new Date().getTime(),
|
start: Date.now(),
|
||||||
runsCount: 0,
|
runsCount: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
client.send(
|
||||||
|
JSON.stringify({
|
||||||
|
ok: true,
|
||||||
|
type: "create",
|
||||||
|
data: { runId },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------- SUBMIT ---------------- */
|
||||||
|
case "submit": {
|
||||||
|
const { runId } = msg;
|
||||||
|
|
||||||
|
if (!runId) {
|
||||||
|
client.send(JSON.stringify({ ok: false, error: "runId required" }));
|
||||||
|
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.submitRun(supabase, liveRun, session[0].id);
|
||||||
|
|
||||||
|
client.send(
|
||||||
|
JSON.stringify({
|
||||||
|
ok: true,
|
||||||
|
type: "submit",
|
||||||
|
data: run,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------- DELETE ---------------- */
|
||||||
|
case "delete": {
|
||||||
|
const { runId } = msg;
|
||||||
|
|
||||||
|
if (!runId) {
|
||||||
|
client.send(JSON.stringify({ ok: false, error: "runId required" }));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const run = await Core.removeLiveRun(runId);
|
||||||
|
|
||||||
|
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",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json({ ok: true, runId });
|
client.on("close", () => {
|
||||||
}
|
// optional cleanup
|
||||||
|
});
|
||||||
export async function POST(req: NextRequest) {
|
|
||||||
const { runId } = await req.json();
|
|
||||||
|
|
||||||
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 });
|
|
||||||
|
|
||||||
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.submitRun(supabase, liveRun, session[0].id);
|
|
||||||
|
|
||||||
return NextResponse.json({ ok: true, run });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function DELETE(req: NextRequest) {
|
|
||||||
const runId = req.nextUrl.searchParams.get("runId");
|
|
||||||
|
|
||||||
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 });
|
|
||||||
|
|
||||||
const run = await Core.removeLiveRun(runId);
|
|
||||||
|
|
||||||
return NextResponse.json({ ok: true, run });
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 };
|
||||||
Reference in New Issue
Block a user