Extract function in repotScore

This commit is contained in:
Kalle (Sendou) 2022-01-07 14:54:09 +02:00
parent eb19399984
commit b60bde2444
2 changed files with 45 additions and 27 deletions

View File

@ -63,9 +63,11 @@ export function deleteResult(id: string) {
return db.tournamentMatchGameResult.delete({ where: { id } });
}
export function createParticipants(
data: ({ matchId: string; order: TeamOrder; teamId: string } | undefined)[]
) {
export type CreateParticipantsData = (
| { matchId: string; order: TeamOrder; teamId: string }
| undefined
)[];
export function createParticipants(data: CreateParticipantsData) {
return db.tournamentMatchParticipant.createMany({
data: data.flatMap((result) => result ?? []),
});

View File

@ -650,30 +650,9 @@ export async function reportScore({
winner: winnerTeam.order,
}),
// todo: bracket reset
TournamentMatch.createParticipants([
match.winnerDestinationMatchId
? {
matchId: match.winnerDestinationMatchId,
order: resolveNewOrder({
bracket,
oldMatch: match,
newMatchId: match.winnerDestinationMatchId,
}),
teamId: winnerTeam.teamId,
}
: undefined,
match.loserDestinationMatchId
? {
matchId: match.loserDestinationMatchId,
order: resolveNewOrder({
bracket,
oldMatch: match,
newMatchId: match.loserDestinationMatchId,
}),
teamId: loserTeam?.teamId,
}
: undefined,
]),
TournamentMatch.createParticipants(
newParticipantsForMatches({ bracket, match, loserTeam, winnerTeam })
),
]);
// otherwise if set is not over simply create result and return
} else {
@ -727,6 +706,43 @@ function resolveNewOrder({
return "LOWER";
}
function newParticipantsForMatches({
bracket,
match,
winnerTeam,
loserTeam,
}: {
bracket: NonNullable<TournamentBracket.FindById>;
match: NonNullable<TournamentMatch.FindById>;
winnerTeam: Unpacked<NonNullable<TournamentMatch.FindById>["participants"]>;
loserTeam: Unpacked<NonNullable<TournamentMatch.FindById>["participants"]>;
}): TournamentMatch.CreateParticipantsData {
return [
match.winnerDestinationMatchId
? {
matchId: match.winnerDestinationMatchId,
order: resolveNewOrder({
bracket,
oldMatch: match,
newMatchId: match.winnerDestinationMatchId,
}),
teamId: winnerTeam.teamId,
}
: undefined,
match.loserDestinationMatchId
? {
matchId: match.loserDestinationMatchId,
order: resolveNewOrder({
bracket,
oldMatch: match,
newMatchId: match.loserDestinationMatchId,
}),
teamId: loserTeam.teamId,
}
: undefined,
];
}
export async function undoLastScore({
matchId,
position,