sendou.ink/app/features/tournament-bracket/core/tests/test-utils.ts
Kalle eae3d529e2
Bracket component rewrite (#1653)
* Remove old code

* Add prefetching

* Elim bracket initial

* Hide rounds with only byes

* Round hiding logic

* Align stuff

* Add TODO

* Adjustments

* Deadline

* Compactify button

* Simulations

* Round robin bracket initial

* eventId -> tournamentId

* seedByTeamId removed

* Couple more TODOs

* RR placements table

* Locking matches

* Extract TournamentStream component

* Bracket streams

* Remove extras for tournament-manager, misc

* Fix E2E tests

* Fix SKALOP_SYSTEM_MESSAGE_URL in env.example

* TODOs

* TODO moved to GitHub

* Handle team changing in match cache invalidation

* Fix streamer seeing undo last score button

* Show "Sub" badge on team roster page

* Show who didn't play yet on match teams preview

* Ranked/unranked badge

* Bracket hover show roster

* Add lock/unlock match test

* Fix score reporting
2024-02-11 10:49:12 +02:00

129 lines
3.4 KiB
TypeScript

import { BRACKET_NAMES } from "~/features/tournament/tournament-constants";
import { Tournament } from "../Tournament";
import type { TournamentData } from "../Tournament.server";
import type { DataTypes, ValueToArray } from "~/modules/brackets-manager/types";
const tournamentCtxTeam = (
teamId: number,
partial?: Partial<TournamentData["ctx"]["teams"][0]>,
): TournamentData["ctx"]["teams"][0] => {
return {
checkIns: [{ checkedInAt: 1705858841, bracketIdx: null }],
createdAt: 0,
id: teamId,
inviteCode: null,
mapPool: [],
members: [],
name: "Team " + teamId,
prefersNotToHost: 0,
seed: teamId + 1,
...partial,
};
};
const nTeams = (n: number, startingId: number) => {
const teams = [];
for (let i = 0; i < n; i++) {
teams.push(tournamentCtxTeam(i, tournamentCtxTeam(i + startingId)));
}
return teams;
};
export const testTournament = (
data: ValueToArray<DataTypes>,
partialCtx?: Partial<TournamentData["ctx"]>,
) => {
return new Tournament({
data,
ctx: {
eventId: 1,
id: 1,
description: null,
startTime: 1705858842,
isFinalized: 0,
name: "test",
showMapListGenerator: 0,
castTwitchAccounts: [],
staff: [],
tieBreakerMapPool: [],
participatedUsers: [],
mapPickingStyle: "AUTO_SZ",
settings: {
bracketProgression: [
{ name: BRACKET_NAMES.MAIN, type: "double_elimination" },
],
},
inProgressBrackets: data.stage.map((stage) => ({
id: stage.id,
name: stage.name,
type: stage.type,
createdAt: 0,
})),
castedMatchesInfo: null,
bestOfs: data.round.map((round) => ({ bestOf: 3, roundId: round.id })),
teams: nTeams(
data.participant.length,
Math.min(...data.participant.map((p) => p.id)),
),
author: {
chatNameColor: null,
customUrl: null,
discordAvatar: null,
discordId: "123",
discordName: "test",
id: 1,
},
...partialCtx,
},
});
};
export const adjustResults = (
data: ValueToArray<DataTypes>,
adjustedArr: Array<{
ids: [number, number];
score: [number, number];
points?: [number, number];
}>,
): ValueToArray<DataTypes> => {
return {
...data,
match: data.match.map((match, idx) => {
const adjusted = adjustedArr[idx];
if (!adjusted) throw new Error("No adjusted result for match " + idx);
if (adjusted.ids[0] !== match.opponent1!.id) {
throw new Error("Adjusted match opponent1 id does not match");
}
if (adjusted.ids[1] !== match.opponent2!.id) {
throw new Error("Adjusted match opponent2 id does not match");
}
return {
...match,
opponent1: {
...match.opponent1!,
score: adjusted.score[0],
result: adjusted.score[0] > adjusted.score[1] ? "win" : "loss",
totalPoints: adjusted.points
? adjusted.points[0]
: adjusted.score[0] > adjusted.score[1]
? 100
: 0,
},
opponent2: {
...match.opponent2!,
score: adjusted.score[1],
result: adjusted.score[1] > adjusted.score[0] ? "win" : "loss",
totalPoints: adjusted.points
? adjusted.points[1]
: adjusted.score[1] > adjusted.score[0]
? 100
: 0,
},
};
}),
};
};