- Add prominent clock badge to tab bar (lights up on your turn) with correct Fischer increment chess-clock model: bank starts at initialTime, += incrementTime after each pick, other teams' banks untouched - Extract pure timer helpers to app/lib/draft-timer.ts and add 29 unit tests covering formatClockTime, calculateTimeAfterPick, getTimerColorClass, and full snake-draft lifecycle regression scenarios - Fix make-pick.ts: add status !== 'draft' and draftPaused server guards - Fix draft-utils.ts: replace (global as any).__socketIO with getSocketIO(), fix timeUsed to store actual timeRemaining at pick moment (not always 120), add null timer warning, move timer fetch before pick insert - Fix draft.start.ts: batch timer inserts, guard against empty draftSlots - Fix DraftGridSection: memoize currentTeamId, widen teamTimers type to Record<string, number | undefined> - Fix duplicate animate-pulse (getTimerColorClass already includes it) - Clamp negative seconds in formatClockTime to guard against timer drift Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
98 lines
2.7 KiB
TypeScript
98 lines
2.7 KiB
TypeScript
import { getAuth } from "@clerk/react-router/server";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
import { eq, and } from "drizzle-orm";
|
|
import { getSocketIO } from "../../../server/socket";
|
|
|
|
export async function action(args: any) {
|
|
const { request } = args;
|
|
const auth = await getAuth(args);
|
|
const userId = (auth as any).userId as string | null;
|
|
|
|
if (!userId) {
|
|
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const formData = await request.formData();
|
|
const seasonId = formData.get("seasonId") as string;
|
|
|
|
if (!seasonId) {
|
|
return Response.json({ error: "Missing seasonId" }, { status: 400 });
|
|
}
|
|
|
|
const db = database();
|
|
|
|
// Get season details
|
|
const season = await db.query.seasons.findFirst({
|
|
where: eq(schema.seasons.id, seasonId),
|
|
});
|
|
|
|
if (!season) {
|
|
return Response.json({ error: "Season not found" }, { status: 404 });
|
|
}
|
|
|
|
// Check if user is commissioner
|
|
const isCommissioner = await db.query.commissioners.findFirst({
|
|
where: and(
|
|
eq(schema.commissioners.leagueId, season.leagueId),
|
|
eq(schema.commissioners.userId, userId)
|
|
),
|
|
});
|
|
|
|
if (!isCommissioner) {
|
|
return Response.json({ error: "Only commissioners can start the draft" }, { status: 403 });
|
|
}
|
|
|
|
// Check if draft already started
|
|
if (season.status === "draft" || season.status === "active" || season.status === "completed") {
|
|
return Response.json({ error: "Draft already started or completed" }, { status: 400 });
|
|
}
|
|
|
|
// Update season status to draft
|
|
await db
|
|
.update(schema.seasons)
|
|
.set({
|
|
status: "draft",
|
|
currentPickNumber: 1,
|
|
})
|
|
.where(eq(schema.seasons.id, seasonId));
|
|
|
|
// Initialize timers for all teams
|
|
const draftSlots = await db.query.draftSlots.findMany({
|
|
where: eq(schema.draftSlots.seasonId, seasonId),
|
|
});
|
|
|
|
if (draftSlots.length === 0) {
|
|
return Response.json({ error: "No draft slots found for this season" }, { status: 400 });
|
|
}
|
|
|
|
const initialTime = season.draftInitialTime || 120;
|
|
|
|
// Delete existing timers for this season
|
|
await db
|
|
.delete(schema.draftTimers)
|
|
.where(eq(schema.draftTimers.seasonId, seasonId));
|
|
|
|
// Insert new timers for all teams in a single batch
|
|
await db
|
|
.insert(schema.draftTimers)
|
|
.values(
|
|
draftSlots.map((slot) => ({
|
|
seasonId,
|
|
teamId: slot.teamId,
|
|
timeRemaining: initialTime,
|
|
}))
|
|
);
|
|
|
|
// Emit socket event
|
|
try {
|
|
getSocketIO().to(`draft-${seasonId}`).emit("draft-started", {
|
|
seasonId,
|
|
currentPickNumber: 1,
|
|
});
|
|
} catch (error) {
|
|
console.error("Socket.IO error:", error);
|
|
}
|
|
|
|
return Response.json({ success: true });
|
|
}
|