* Fix no-shadow and consistent-function-scoping lint violations
Resolves all 11 no-shadow and 16 consistent-function-scoping oxlint
warnings and promotes both rules to errors in .oxlintrc.json.
no-shadow: renamed Drizzle callback params (sports→s, matches→m,
seasons→s) to avoid shadowing outer imports; removed shadowed
destructures (eq, inArray) from where callbacks; renamed inner
template→bracketTemplate, prev→currentTimers, season→ss, name→teamName
(with name: teamName fix to preserve semantics).
consistent-function-scoping: moved formatDate, getRankBadge,
getMovementIndicator, getPositionBadge, getStatusBadge, toDateStr,
elo (×2), weightedPick, sortByMatchNumber (×2) to module scope;
moved formatTime (×2), isValidLeagueName, getDraftTimes,
makeSeasonQueues to file scope in test files.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix no-non-null-assertion lint violations and promote to error
Eliminates all 208 no-non-null-assertion warnings across 38 files.
Promotes typescript/no-non-null-assertion from warn to error in
.oxlintrc.json.
Fix patterns applied:
- Map.get(key)! after .has() check → extract with get() + null guard
- Map.get(key)! on pre-populated count maps → ?? 0 default
- .set(id, map.get(id)! + 1) increment → ?? 0 before adding
- participant1Id!/participant2Id! on DB matches → ?? "" fallback
- array.find()! in tests → guard + throw or expect().toBeDefined()
- bracketTemplateCache.get(id)! → null guard extract
- Various nullable field accesses → optional chain or ?? default
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix prefer-add-event-listener, no-unassigned-import, require-module-specifiers
Resolves all 9 remaining non-console lint warnings and promotes all
three rules to errors in .oxlintrc.json.
- prefer-add-event-listener: converted onchange/onclick/onload
assignments to addEventListener in useDraftNotifications.ts and
admin.data-sync.tsx; stored changeHandler ref for proper cleanup
with removeEventListener
- no-unassigned-import: configured rule with allow list for legitimate
side-effect imports (*.css, @testing-library/jest-dom,
@testing-library/cypress/add-commands)
- require-module-specifiers: removed redundant `export {}` from
cypress/support/e2e.ts (file already has an import)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Fix TypeScript errors from no-non-null-assertion fixes
Two fixes introduced by the non-null assertion cleanup produced type
errors:
- scoring-event.ts: `?? ""` was wrong type for a participant object map;
restructured to explicit null guards so TypeScript can narrow correctly
- standings-sync/index.ts: `?? null` after name-match lookup lost the
truthy guarantee, causing TS18047 on the write-back block; added
`participant &&` guard before accessing its properties
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Add npm run typecheck as Stop hook in Claude settings
Runs a full project typecheck at the end of each Claude turn so type
errors surface as feedback before the next message.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
345 lines
12 KiB
TypeScript
345 lines
12 KiB
TypeScript
import type { Socket } from "socket.io";
|
|
import { Server as SocketIOServer } from "socket.io";
|
|
import type { Server as HTTPServer } from "http";
|
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
import postgres from "postgres";
|
|
import * as schema from "~/database/schema";
|
|
import { eq, and, asc } from "drizzle-orm";
|
|
|
|
// Lazy-initialized DB for socket-level validation queries (team ownership checks)
|
|
let _socketDb: PostgresJsDatabase<typeof schema> | null = null;
|
|
function getSocketDb(): PostgresJsDatabase<typeof schema> {
|
|
if (!_socketDb) {
|
|
const url = process.env.DATABASE_URL;
|
|
if (!url) throw new Error("DATABASE_URL is required");
|
|
_socketDb = drizzle(postgres(url), { schema });
|
|
}
|
|
return _socketDb;
|
|
}
|
|
|
|
// Socket event types
|
|
interface ServerToClientEvents {
|
|
"test-message": (data: {
|
|
originalMessage: unknown;
|
|
serverResponse: string;
|
|
serverTimestamp: string;
|
|
socketId: string;
|
|
}) => void;
|
|
"pick-made": (data: unknown) => void;
|
|
"draft-started": (data: { seasonId: string; currentPickNumber: number }) => void;
|
|
"draft-completed": () => void;
|
|
"timer-update": (data: {
|
|
seasonId: string;
|
|
teamId: string;
|
|
timeRemaining: number;
|
|
currentPickNumber: number;
|
|
}) => void;
|
|
"autodraft-updated": (data: {
|
|
teamId: string;
|
|
isEnabled: boolean;
|
|
mode: "next_pick" | "while_on";
|
|
queueOnly: boolean;
|
|
source?: "commissioner" | "user";
|
|
}) => void;
|
|
"team-connected": (data: { teamId: string }) => void;
|
|
"team-disconnected": (data: { teamId: string }) => void;
|
|
"connected-teams-list": (data: { teamIds: string[] }) => void;
|
|
"draft-paused": (data: { seasonId: string; paused: boolean }) => void;
|
|
"draft-resumed": (data: { seasonId: string; paused: boolean }) => void;
|
|
"participant-removed-from-queues": (data: { participantId: string }) => void;
|
|
"queue-updated": (data: { queue: Array<{ id: string; teamId: string; seasonId: string; participantId: string; queuePosition: number }> }) => void;
|
|
"draft-state-sync": (data: {
|
|
currentPickNumber: number;
|
|
isPaused: boolean;
|
|
status: string;
|
|
picks: Array<{
|
|
id: string;
|
|
pickNumber: number;
|
|
round: number;
|
|
pickInRound: number;
|
|
timeUsed: number;
|
|
team: unknown;
|
|
participant: unknown;
|
|
sport: unknown;
|
|
}>;
|
|
timers: Array<{
|
|
teamId: string;
|
|
timeRemaining: number;
|
|
}>;
|
|
queue?: Array<{
|
|
id: string;
|
|
teamId: string;
|
|
seasonId: string;
|
|
participantId: string;
|
|
queuePosition: number;
|
|
}>;
|
|
}) => void;
|
|
}
|
|
|
|
interface ClientToServerEvents {
|
|
"join-draft": (seasonId: string, teamId?: string) => void;
|
|
"leave-draft": (seasonId: string) => void;
|
|
"test-event": (data: unknown) => void;
|
|
}
|
|
|
|
// Global type augmentation
|
|
declare global {
|
|
var __socketIO: SocketIOServer | undefined;
|
|
}
|
|
|
|
let io: SocketIOServer<ClientToServerEvents, ServerToClientEvents> | null = null;
|
|
|
|
// Track connected teams per season (in-memory, single-instance only).
|
|
// If the server restarts or runs as multiple instances this map resets.
|
|
// For multi-instance deployments, replace with a Redis-backed Socket.IO adapter.
|
|
const connectedTeams = new Map<string, Set<string>>(); // seasonId -> Set<teamId>
|
|
|
|
/**
|
|
* Initialize Socket.IO server
|
|
*/
|
|
export function initializeSocketIO(httpServer: HTTPServer): SocketIOServer {
|
|
if (io) {
|
|
console.log("Socket.IO already initialized");
|
|
return io;
|
|
}
|
|
|
|
// Create typed Socket.IO server
|
|
io = new SocketIOServer<ClientToServerEvents, ServerToClientEvents>(httpServer, {
|
|
// Faster dead-connection detection than the defaults (20s/25s).
|
|
// pingTimeout: how long to wait for a pong before declaring the connection dead.
|
|
// pingInterval: how often to send a ping.
|
|
// Together these detect a crashed server in ~15s instead of ~45s.
|
|
// Trade-off: more heartbeat traffic at scale; revisit if server load becomes a concern.
|
|
pingTimeout: 5000,
|
|
pingInterval: 10000,
|
|
cors: process.env.NODE_ENV === "production" && process.env.APP_URL
|
|
? {
|
|
origin: process.env.APP_URL,
|
|
credentials: true,
|
|
}
|
|
: undefined,
|
|
});
|
|
|
|
// Connection handling
|
|
io.on("connection", (socket: Socket<ClientToServerEvents, ServerToClientEvents>) => {
|
|
console.log("Client connected:", socket.id);
|
|
|
|
// Store team ID for this socket
|
|
let currentTeamId: string | undefined;
|
|
let currentSeasonId: string | undefined;
|
|
|
|
socket.on("join-draft", async (seasonId: string, teamId?: string) => {
|
|
if (!seasonId) {
|
|
console.error("No seasonId provided for join-draft");
|
|
return;
|
|
}
|
|
socket.join(`draft-${seasonId}`);
|
|
currentSeasonId = seasonId;
|
|
console.log(`Socket ${socket.id} joined draft-${seasonId}`);
|
|
|
|
// If teamId provided, validate it belongs to this season before tracking
|
|
if (teamId) {
|
|
try {
|
|
const db = getSocketDb();
|
|
const team = await db.query.teams.findFirst({
|
|
where: and(eq(schema.teams.id, teamId), eq(schema.teams.seasonId, seasonId)),
|
|
});
|
|
if (!team) {
|
|
console.warn(`[Socket] join-draft rejected: team ${teamId} does not belong to season ${seasonId}`);
|
|
return;
|
|
}
|
|
} catch (err) {
|
|
console.error("[Socket] join-draft team validation failed:", err);
|
|
return;
|
|
}
|
|
|
|
currentTeamId = teamId;
|
|
socket.join(`team-${teamId}`);
|
|
|
|
// Initialize the season's connected teams set if it doesn't exist
|
|
if (!connectedTeams.has(seasonId)) {
|
|
connectedTeams.set(seasonId, new Set());
|
|
}
|
|
|
|
// Get current connected teams for this season
|
|
const seasonConnectedTeams = connectedTeams.get(seasonId) ?? new Set<string>();
|
|
if (!connectedTeams.has(seasonId)) connectedTeams.set(seasonId, seasonConnectedTeams);
|
|
|
|
// Send the list of already-connected teams to this socket
|
|
socket.emit("connected-teams-list", { teamIds: Array.from(seasonConnectedTeams) });
|
|
|
|
// Add this team to the connected teams set
|
|
seasonConnectedTeams.add(teamId);
|
|
|
|
// Broadcast to everyone else that this team connected
|
|
io?.to(`draft-${seasonId}`).emit("team-connected", { teamId });
|
|
console.log(`Team ${teamId} connected to draft-${seasonId}. Total connected: ${seasonConnectedTeams.size}`);
|
|
}
|
|
|
|
// Send full draft state to the joining client so it can sync immediately.
|
|
// This is critical for mobile reconnection: the client may have been in the
|
|
// background for minutes/hours and missed many picks. The revalidate() path
|
|
// (HTTP loader re-fetch) covers this too, but it can fail due to expired auth
|
|
// tokens or flaky mobile networks. This socket-based sync provides an
|
|
// additional, more reliable path since the socket is already connected.
|
|
try {
|
|
const db = getSocketDb();
|
|
const [seasonData, picks, timerRows, queueItems] = await Promise.all([
|
|
db.query.seasons.findFirst({
|
|
where: eq(schema.seasons.id, seasonId),
|
|
}),
|
|
db
|
|
.select({
|
|
id: schema.draftPicks.id,
|
|
pickNumber: schema.draftPicks.pickNumber,
|
|
round: schema.draftPicks.round,
|
|
pickInRound: schema.draftPicks.pickInRound,
|
|
timeUsed: schema.draftPicks.timeUsed,
|
|
team: schema.teams,
|
|
participant: schema.participants,
|
|
sport: schema.sports,
|
|
})
|
|
.from(schema.draftPicks)
|
|
.innerJoin(schema.teams, eq(schema.draftPicks.teamId, schema.teams.id))
|
|
.innerJoin(
|
|
schema.participants,
|
|
eq(schema.draftPicks.participantId, schema.participants.id)
|
|
)
|
|
.innerJoin(
|
|
schema.sportsSeasons,
|
|
eq(schema.participants.sportsSeasonId, schema.sportsSeasons.id)
|
|
)
|
|
.innerJoin(
|
|
schema.sports,
|
|
eq(schema.sportsSeasons.sportId, schema.sports.id)
|
|
)
|
|
.where(eq(schema.draftPicks.seasonId, seasonId))
|
|
.orderBy(asc(schema.draftPicks.pickNumber)),
|
|
db.query.draftTimers.findMany({
|
|
where: eq(schema.draftTimers.seasonId, seasonId),
|
|
}),
|
|
teamId
|
|
? db.query.draftQueue.findMany({
|
|
where: and(
|
|
eq(schema.draftQueue.teamId, teamId),
|
|
eq(schema.draftQueue.seasonId, seasonId)
|
|
),
|
|
orderBy: asc(schema.draftQueue.queuePosition),
|
|
})
|
|
: Promise.resolve([]),
|
|
]);
|
|
|
|
if (seasonData) {
|
|
socket.emit("draft-state-sync", {
|
|
currentPickNumber: seasonData.currentPickNumber || 1,
|
|
isPaused: seasonData.draftPaused || false,
|
|
status: seasonData.status,
|
|
picks,
|
|
timers: timerRows.map((t) => ({
|
|
teamId: t.teamId,
|
|
timeRemaining: t.timeRemaining,
|
|
})),
|
|
queue: teamId ? queueItems : undefined,
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.error("[Socket] draft-state-sync query failed:", err);
|
|
// Non-fatal — the client will fall back to HTTP revalidation
|
|
}
|
|
});
|
|
|
|
socket.on("leave-draft", (seasonId: string) => {
|
|
if (!seasonId) return;
|
|
socket.leave(`draft-${seasonId}`);
|
|
console.log(`Socket ${socket.id} left draft-${seasonId}`);
|
|
|
|
// Emit disconnection event if team was tracked
|
|
if (currentTeamId) {
|
|
socket.leave(`team-${currentTeamId}`);
|
|
|
|
// Remove team from connected teams tracking
|
|
const seasonConnectedTeams = connectedTeams.get(seasonId);
|
|
if (seasonConnectedTeams) {
|
|
seasonConnectedTeams.delete(currentTeamId);
|
|
console.log(`Team ${currentTeamId} removed from tracking. Remaining: ${seasonConnectedTeams.size}`);
|
|
|
|
// Clean up empty sets
|
|
if (seasonConnectedTeams.size === 0) {
|
|
connectedTeams.delete(seasonId);
|
|
}
|
|
}
|
|
|
|
io?.to(`draft-${seasonId}`).emit("team-disconnected", { teamId: currentTeamId });
|
|
console.log(`Team ${currentTeamId} disconnected from draft-${seasonId}`);
|
|
}
|
|
});
|
|
|
|
socket.on("test-event", (data: unknown) => {
|
|
console.log("📨 Received test-event from client:", socket.id, data);
|
|
|
|
socket.emit("test-message", {
|
|
originalMessage: data,
|
|
serverResponse: "Hello from server!",
|
|
serverTimestamp: new Date().toISOString(),
|
|
socketId: socket.id,
|
|
});
|
|
|
|
console.log("✅ Sent test-message response to client:", socket.id);
|
|
});
|
|
|
|
socket.on("disconnect", () => {
|
|
console.log("Client disconnected:", socket.id);
|
|
|
|
// Emit disconnection event if team was tracked
|
|
if (currentTeamId && currentSeasonId) {
|
|
// Remove team from connected teams tracking
|
|
const seasonConnectedTeams = connectedTeams.get(currentSeasonId);
|
|
if (seasonConnectedTeams) {
|
|
seasonConnectedTeams.delete(currentTeamId);
|
|
console.log(`Team ${currentTeamId} removed from tracking on disconnect. Remaining: ${seasonConnectedTeams.size}`);
|
|
|
|
// Clean up empty sets
|
|
if (seasonConnectedTeams.size === 0) {
|
|
connectedTeams.delete(currentSeasonId);
|
|
}
|
|
}
|
|
|
|
io?.to(`draft-${currentSeasonId}`).emit("team-disconnected", { teamId: currentTeamId });
|
|
console.log(`Team ${currentTeamId} disconnected from draft-${currentSeasonId}`);
|
|
}
|
|
});
|
|
});
|
|
|
|
// Store globally for route handlers
|
|
global.__socketIO = io;
|
|
|
|
console.log("Socket.IO initialized");
|
|
|
|
// Start the draft timer system (async import but don't await)
|
|
import("./timer").then(({ startDraftTimerSystem }) => {
|
|
startDraftTimerSystem();
|
|
}).catch((error) => {
|
|
console.error("Failed to start timer system:", error);
|
|
});
|
|
|
|
// Start the daily snapshot system
|
|
import("./snapshots").then(({ startSnapshotSystem }) => {
|
|
startSnapshotSystem();
|
|
}).catch((error) => {
|
|
console.error("Failed to start snapshot system:", error);
|
|
});
|
|
|
|
return io;
|
|
}
|
|
|
|
/**
|
|
* Get the Socket.IO server instance
|
|
*/
|
|
export function getSocketIO(): SocketIOServer {
|
|
const instance = io || global.__socketIO;
|
|
if (!instance) {
|
|
throw new Error("Socket.IO not initialized. Call initializeSocketIO first.");
|
|
}
|
|
return instance;
|
|
}
|