From e67d1d177499ea1ee76c2603fd00b3c6d0939182 Mon Sep 17 00:00:00 2001 From: nicksales72 Date: Thu, 12 Mar 2026 16:41:51 -0400 Subject: [PATCH] helpers.ts --- src/lib/server/match/engine.ts | 56 +--------------------------- src/lib/server/match/helpers.ts | 56 ++++++++++++++++++++++++++++ src/lib/server/match/orchestrator.ts | 3 +- 3 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 src/lib/server/match/helpers.ts diff --git a/src/lib/server/match/engine.ts b/src/lib/server/match/engine.ts index e0de7e3..57e0f79 100644 --- a/src/lib/server/match/engine.ts +++ b/src/lib/server/match/engine.ts @@ -12,6 +12,7 @@ import { eq } from 'drizzle-orm'; import { MATCH_STATES, GAME_STATES, type MatchConfig } from './types'; import { notifyMatchCreated, notifyMatchFinished } from '$lib/server/discord/client'; import { updateElo } from './rating'; +import { getExpectedPicker, getMatchOrThrow, assertState, getMatchFull } from './helpers'; // ── Create Match ──────────────────────────────────────────────────────── @@ -343,58 +344,3 @@ export async function cancelMatch(matchId: string) { return getMatchFull(matchId); } - -// ── Helpers ───────────────────────────────────────────────────────────── - -function getExpectedPicker( - participants: { id: string; pickOrder: number | null; score: number }[], - gameCount: number -) { - const sorted = [...participants].sort((a, b) => (a.pickOrder ?? 99) - (b.pickOrder ?? 99)); - const pickerIndex = gameCount % sorted.length; - return sorted[pickerIndex]; -} - -export async function getMatchFull(matchId: string) { - const m = await db.query.match.findFirst({ - where: eq(match.id, matchId), - with: { - mappool: { - with: { slots: true } - }, - participants: { - with: { - team: true, - players: true - } - }, - games: { - with: { - slot: true, - scores: { - with: { player: true }, - orderBy: (s, { desc }) => [desc(s.score)] - } - }, - orderBy: (g, { asc }) => [asc(g.gameNumber)] - } - } - }); - - if (!m) throw new Error('Match not found'); - return m; -} - -async function getMatchOrThrow(matchId: string) { - const m = await db.query.match.findFirst({ - where: eq(match.id, matchId) - }); - if (!m) throw new Error('Match not found'); - return m; -} - -function assertState(current: string, expected: string) { - if (current !== expected) { - throw new Error(`Match is ${current}, expected ${expected}`); - } -} diff --git a/src/lib/server/match/helpers.ts b/src/lib/server/match/helpers.ts new file mode 100644 index 0000000..0444351 --- /dev/null +++ b/src/lib/server/match/helpers.ts @@ -0,0 +1,56 @@ +import { db } from '$lib/server/db'; +import { match } from '$lib/server/db/schema'; +import { eq } from 'drizzle-orm'; + +export function getExpectedPicker( + participants: { id: string; pickOrder: number | null; score: number }[], + gameCount: number +) { + const sorted = [...participants].sort((a, b) => (a.pickOrder ?? 99) - (b.pickOrder ?? 99)); + const pickerIndex = gameCount % sorted.length; + return sorted[pickerIndex]; +} + +export async function getMatchFull(matchId: string) { + const m = await db.query.match.findFirst({ + where: eq(match.id, matchId), + with: { + mappool: { + with: { slots: true } + }, + participants: { + with: { + team: true, + players: true + } + }, + games: { + with: { + slot: true, + scores: { + with: { player: true }, + orderBy: (s, { desc }) => [desc(s.score)] + } + }, + orderBy: (g, { asc }) => [asc(g.gameNumber)] + } + } + }); + + if (!m) throw new Error('Match not found'); + return m; +} + +export async function getMatchOrThrow(matchId: string) { + const m = await db.query.match.findFirst({ + where: eq(match.id, matchId) + }); + if (!m) throw new Error('Match not found'); + return m; +} + +export function assertState(current: string, expected: string) { + if (current !== expected) { + throw new Error(`Match is ${current}, expected ${expected}`); + } +} diff --git a/src/lib/server/match/orchestrator.ts b/src/lib/server/match/orchestrator.ts index 0962680..376cbbc 100644 --- a/src/lib/server/match/orchestrator.ts +++ b/src/lib/server/match/orchestrator.ts @@ -2,7 +2,8 @@ import { db } from '$lib/server/db'; import { match, matchGame, matchParticipant, user } from '$lib/server/db/schema'; import { eq, and } from 'drizzle-orm'; import { MATCH_STATES, GAME_STATES, type MatchConfig } from './types'; -import { getMatchFull, submitRoll, pickMap, submitGameScores, cancelMatch } from './engine'; +import { submitRoll, pickMap, submitGameScores, cancelMatch } from './engine'; +import { getMatchFull } from './helpers'; import { env } from '$env/dynamic/private'; // matchId → lowercase usernames of all expected players