diff --git a/app/features/tournament/queries/findTeamsByTournamentId.server.ts b/app/features/tournament/queries/findTeamsByTournamentId.server.ts index 8074450a0..042031922 100644 --- a/app/features/tournament/queries/findTeamsByTournamentId.server.ts +++ b/app/features/tournament/queries/findTeamsByTournamentId.server.ts @@ -8,6 +8,8 @@ import type { UserWithPlusTier, } from "~/db/types"; import { parseDBJsonArray } from "~/utils/sql"; +import { TOURNAMENT } from "../tournament-constants"; +import type { Unpacked } from "~/utils/types"; const stm = sql.prepare(/*sql*/ ` with "TeamWithMembers" as ( @@ -63,8 +65,6 @@ const stm = sql.prepare(/*sql*/ ` left join "MapPoolMap" on "MapPoolMap"."tournamentTeamId" = "TeamWithMembers"."id" group by "TeamWithMembers"."id" - order by - "TeamWithMembers"."seed" asc `); export interface FindTeamsByTournamentIdItem { @@ -92,11 +92,56 @@ export type FindTeamsByTournamentId = Array; export function findTeamsByTournamentId(tournamentId: Tournament["id"]) { const rows = stm.all({ tournamentId }); - return rows.map((row) => { - return { - ...row, - members: parseDBJsonArray(row.members), - mapPool: parseDBJsonArray(row.mapPool), - }; - }) as FindTeamsByTournamentId; + return ( + rows.map((row) => { + return { + ...row, + members: parseDBJsonArray(row.members), + mapPool: parseDBJsonArray(row.mapPool), + }; + }) as FindTeamsByTournamentId + ).sort(teamSorter); +} + +function teamSorter( + teamA: Unpacked, + teamB: Unpacked +) { + if ( + teamA.members.length >= TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL && + teamB.members.length < TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL + ) { + return -1; + } + + if ( + teamA.members.length < TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL && + teamB.members.length >= TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL + ) { + return 1; + } + + if (teamA.seed || teamB.seed) { + const teamASeed = teamA.seed ?? Infinity; + const teamBSeed = teamB.seed ?? Infinity; + + return teamASeed - teamBSeed; + } + + const lowestATeamPlusTier = Math.min( + ...teamA.members.map((m) => m.plusTier ?? Infinity) + ); + const lowestBTeamPlusTier = Math.min( + ...teamB.members.map((m) => m.plusTier ?? Infinity) + ); + + if (lowestATeamPlusTier > lowestBTeamPlusTier) { + return 1; + } + + if (lowestATeamPlusTier < lowestBTeamPlusTier) { + return -1; + } + + return 0; } diff --git a/app/features/tournament/routes/to.$id.teams.tsx b/app/features/tournament/routes/to.$id.teams.tsx index e3597b6c2..5268f7007 100644 --- a/app/features/tournament/routes/to.$id.teams.tsx +++ b/app/features/tournament/routes/to.$id.teams.tsx @@ -2,20 +2,16 @@ import { Link, useOutletContext } from "@remix-run/react"; import { Avatar } from "~/components/Avatar"; import { userPage } from "~/utils/urls"; import type { FindTeamsByTournamentIdItem } from "../queries/findTeamsByTournamentId.server"; -import { TOURNAMENT } from "../tournament-constants"; -import type { TournamentLoaderData, TournamentLoaderTeam } from "./to.$id"; +import type { TournamentLoaderData } from "./to.$id"; export default function TournamentTeamsPage() { const data = useOutletContext(); return (
- {data.teams - .slice() - .sort(fullTeamAndHigherPlusStatusOnTop) - .map((team) => { - return ; - })} + {data.teams.map((team) => { + return ; + })}
); } @@ -47,39 +43,3 @@ function TeamWithRoster({ ); } - -function fullTeamAndHigherPlusStatusOnTop( - teamA: TournamentLoaderTeam, - teamB: TournamentLoaderTeam -) { - if ( - teamA.members.length >= TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL && - teamB.members.length < TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL - ) { - return -1; - } - - if ( - teamA.members.length < TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL && - teamB.members.length >= TOURNAMENT.TEAM_MIN_MEMBERS_FOR_FULL - ) { - return 1; - } - - const lowestATeamPlusTier = Math.min( - ...teamA.members.map((m) => m.plusTier ?? Infinity) - ); - const lowestBTeamPlusTier = Math.min( - ...teamB.members.map((m) => m.plusTier ?? Infinity) - ); - - if (lowestATeamPlusTier > lowestBTeamPlusTier) { - return 1; - } - - if (lowestATeamPlusTier < lowestBTeamPlusTier) { - return -1; - } - - return 0; -}