sendou.ink/scripts/unlock-league-matches.ts
Kalle c42c2f6adc
Some checks failed
E2E Tests / e2e (push) Has been cancelled
Tests and checks on push / run-checks-and-tests (push) Has been cancelled
Updates translation progress / update-translation-progress-issue (push) Has been cancelled
Add fix LUTI script
2026-03-05 19:37:10 +02:00

71 lines
1.9 KiB
TypeScript

// usage: npx tsx ./scripts/unlock-league-matches.ts <parentTournamentId>
import "dotenv/config";
import { sql } from "~/db/sql";
import invariant from "~/utils/invariant";
import { logger } from "~/utils/logger";
const parentTournamentId = Number(process.argv[2]?.trim());
invariant(
parentTournamentId && !Number.isNaN(parentTournamentId),
"parent tournament id is required (argument 1)",
);
const divisions = sql
.prepare(
'SELECT id FROM "Tournament" WHERE parentTournamentId = @parentTournamentId',
)
.all({ parentTournamentId }) as { id: number }[];
invariant(
divisions.length > 0,
`No divisions found for tournament ${parentTournamentId}`,
);
logger.info(`Found ${divisions.length} divisions`);
const updateMatch = sql.prepare(
'UPDATE "TournamentMatch" SET status = 2 WHERE id = @id',
);
const unlockAll = sql.transaction(() => {
let totalUnlocked = 0;
for (const division of divisions) {
const stages = sql
.prepare(
'SELECT id FROM "TournamentStage" WHERE tournamentId = @tournamentId',
)
.all({ tournamentId: division.id }) as { id: number }[];
if (stages.length === 0) continue;
const stageIds = stages.map((s) => s.id);
const lockedMatches = sql
.prepare(
`SELECT id, opponentOne, opponentTwo FROM "TournamentMatch"
WHERE stageId IN (${stageIds.map(() => "?").join(",")})
AND status = 0`,
)
.all(...stageIds) as {
id: number;
opponentOne: string;
opponentTwo: string;
}[];
let divUnlocked = 0;
for (const match of lockedMatches) {
const o1 = JSON.parse(match.opponentOne);
const o2 = JSON.parse(match.opponentTwo);
if (o1?.id == null || o2?.id == null) continue;
updateMatch.run({ id: match.id });
divUnlocked++;
}
logger.info(`Division ${division.id}: unlocked ${divUnlocked} match(es)`);
totalUnlocked += divUnlocked;
}
logger.info(`Total unlocked: ${totalUnlocked} match(es)`);
});
unlockAll();