From a516dedb767c5fc2e58c1457b13ce24cd88ea1e3 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 5 Apr 2026 20:05:02 +0000 Subject: [PATCH] Fix and() bug and add Swiss loop safety guard - cs2-major-stage.ts: markCs2StageEliminations and setCs2FinalPlacements were using JS && instead of Drizzle and(), causing WHERE to filter only by participantId (not scoringEventId), which would update rows across all events instead of just the target event - cs-major-simulator.ts: add break guard in simulateSwiss while loop to prevent infinite loop if pairGroups returns no pairs https://claude.ai/code/session_019w21Nkf5TvTZHH6oVHaQXR --- app/models/cs2-major-stage.ts | 14 +++++++------- app/services/simulations/cs-major-simulator.ts | 4 ++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/models/cs2-major-stage.ts b/app/models/cs2-major-stage.ts index 4875178..a04bb38 100644 --- a/app/models/cs2-major-stage.ts +++ b/app/models/cs2-major-stage.ts @@ -14,7 +14,7 @@ import { database } from "~/database/context"; import { cs2MajorStageResults, participants } from "~/database/schema"; -import { eq, inArray, sql } from "drizzle-orm"; +import { eq, and, inArray, sql } from "drizzle-orm"; export interface Cs2StageResult { id: string; @@ -157,10 +157,10 @@ export async function markCs2StageEliminations( stageEliminatedWins: winsAtElimination, updatedAt: now, }) - .where( - eq(cs2MajorStageResults.scoringEventId, scoringEventId) && + .where(and( + eq(cs2MajorStageResults.scoringEventId, scoringEventId), eq(cs2MajorStageResults.participantId, participantId) - ) + )) ) ); } @@ -184,10 +184,10 @@ export async function setCs2FinalPlacements( db .update(cs2MajorStageResults) .set({ finalPlacement: placements[participantId], updatedAt: now }) - .where( - eq(cs2MajorStageResults.scoringEventId, scoringEventId) && + .where(and( + eq(cs2MajorStageResults.scoringEventId, scoringEventId), eq(cs2MajorStageResults.participantId, participantId) - ) + )) ) ); } diff --git a/app/services/simulations/cs-major-simulator.ts b/app/services/simulations/cs-major-simulator.ts index 351baf7..31859a8 100644 --- a/app/services/simulations/cs-major-simulator.ts +++ b/app/services/simulations/cs-major-simulator.ts @@ -193,6 +193,10 @@ export function simulateSwiss(teams: TeamWithElo[], bo3: boolean): SwissResult { // (simplified: skip teams that can't be paired — they sit out this round) const pairs = pairGroups(groups); + // Safety: if no pairs can be formed (shouldn't happen with even team counts + // but guards against an infinite loop if an odd active count somehow occurs) + if (pairs.length === 0) break; + for (const [t1, t2] of pairs) { const e1 = eloMap.get(t1.id) ?? FALLBACK_ELO; const e2 = eloMap.get(t2.id) ?? FALLBACK_ELO;