Current run management

This commit is contained in:
2026-02-17 02:50:57 +01:00
parent 7ec8b9ae4d
commit abda0b52af
+51 -24
View File
@@ -32,7 +32,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.send(JSON.stringify({ requestId, ok: true })); client.send(JSON.stringify({ requestId, ok: true }));
console.log(`User ${session[1].username} connected to live run`); let currentRun: string | undefined;
client.on("message", async (raw) => { client.on("message", async (raw) => {
try { try {
@@ -54,6 +54,18 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
return; return;
} }
if (currentRun) {
client.send(
JSON.stringify({
requestId,
ok: false,
error:
"Cannot do multiple runs at a time, please delete or submit the current run",
}),
);
return;
}
const runId = randomUUID(); const runId = randomUUID();
await Core.addLiveRun({ await Core.addLiveRun({
@@ -79,14 +91,20 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
/* ---------------- DELETE ---------------- */ /* ---------------- DELETE ---------------- */
case "move": { case "move": {
const { runId, moves, file, language } = msg; const { moves, file, language } = msg;
if ( if (!currentRun) {
!runId || client.send(
!moves || JSON.stringify({
file === undefined || requestId,
language === undefined ok: false,
) { error: "No current run, please create a run first",
}),
);
return;
}
if (!moves || file === undefined || language === undefined) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId, requestId,
@@ -97,7 +115,7 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
return; return;
} }
const liveRun = await Core.getLiveRun(runId); const liveRun = await Core.getLiveRun(currentRun);
if (!liveRun) { if (!liveRun) {
client.send( client.send(
@@ -131,20 +149,18 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
/* ---------------- SUBMIT ---------------- */ /* ---------------- SUBMIT ---------------- */
case "submit": { case "submit": {
const { runId } = msg; if (!currentRun) {
if (!runId) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId, requestId,
ok: false, ok: false,
error: "runId required", error: "No current run, please create a run first",
}), }),
); );
return; return;
} }
const liveRun = await Core.getLiveRun(runId); const liveRun = await Core.getLiveRun(currentRun);
if (!liveRun) { if (!liveRun) {
client.send( client.send(
@@ -172,20 +188,18 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
/* ---------------- DELETE ---------------- */ /* ---------------- DELETE ---------------- */
case "delete": { case "delete": {
const { runId } = msg; if (!currentRun) {
if (!runId) {
client.send( client.send(
JSON.stringify({ JSON.stringify({
requestId, requestId,
ok: false, ok: false,
error: "runId required", error: "No current run, please create a run first",
}), }),
); );
return; return;
} }
const run = await Core.removeLiveRun(runId); const run = await Core.removeLiveRun(currentRun);
client.send( client.send(
JSON.stringify({ JSON.stringify({
@@ -199,8 +213,18 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
} }
case "getText": case "getText":
const { runId } = msg; if (!currentRun) {
const data = await redis.get(`liveRunText:${runId}`); client.send(
JSON.stringify({
requestId,
ok: false,
error: "No current run, please create a run first",
}),
);
return;
}
const data = await redis.get(`liveRunText:${currentRun}`);
client.send( client.send(
JSON.stringify({ JSON.stringify({
@@ -231,10 +255,13 @@ export function UPGRADE(client: WebSocket, server: WebSocketServer) {
); );
} }
}); });
});
client.on("close", () => { client.on("close", async () => {
// optional cleanup if (!currentRun) return;
Core.removeLiveRun(currentRun);
await redis.del(`liveRunText:${currentRun}`);
});
}); });
} }