Fix unseeded teams sorting at the top

This commit is contained in:
Kalle
2023-05-18 14:03:44 +03:00
parent 9ba072b3c3
commit c6835ffa21
2 changed files with 58 additions and 53 deletions

View File

@@ -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<FindTeamsByTournamentIdItem>;
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<FindTeamsByTournamentId>,
teamB: Unpacked<FindTeamsByTournamentId>
) {
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;
}

View File

@@ -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<TournamentLoaderData>();
return (
<div className="stack lg">
{data.teams
.slice()
.sort(fullTeamAndHigherPlusStatusOnTop)
.map((team) => {
return <TeamWithRoster key={team.id} team={team} />;
})}
{data.teams.map((team) => {
return <TeamWithRoster key={team.id} team={team} />;
})}
</div>
);
}
@@ -47,39 +43,3 @@ function TeamWithRoster({
</div>
);
}
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;
}