mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-08-23 03:26:16 -05:00
Fix unseeded teams sorting at the top
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user