/api/run for the vscode extension
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import "server-only";
|
||||
import { LiveRun, LiveRunMove } from "./types";
|
||||
import { SupabaseClient } from "@supabase/supabase-js";
|
||||
|
||||
const liveRuns: LiveRun[] = [];
|
||||
const liveRunMoves: Record<string, LiveRunMove[]> = {};
|
||||
@@ -38,3 +39,47 @@ export async function addLiveRunMoves(id: string, moves: LiveRunMove[]) {
|
||||
liveRunMoves[id] = liveRunMoves[id].filter((move) => !moves.includes(move));
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
export async function removeLiveRun(runId: string) {
|
||||
delete liveRunMoves[runId];
|
||||
|
||||
const index = liveRuns.findIndex((run) => run.id === runId);
|
||||
|
||||
if (index !== -1) {
|
||||
liveRuns.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
export async function submitRun(
|
||||
supabase: SupabaseClient,
|
||||
run: LiveRun,
|
||||
user_id: string,
|
||||
) {
|
||||
const now = new Date();
|
||||
const durationMs = now.getTime() - run.start;
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from("runs")
|
||||
.insert([
|
||||
{
|
||||
id: run.id,
|
||||
user_id,
|
||||
problem_id: run.problem,
|
||||
category: run.category,
|
||||
assisted: false,
|
||||
duration_ms: durationMs,
|
||||
started_at: new Date(run.start),
|
||||
finished_at: now,
|
||||
valid: false,
|
||||
invalid_reason: null,
|
||||
},
|
||||
])
|
||||
.select();
|
||||
|
||||
if (error) {
|
||||
console.error("Error inserting run:", error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user