From f9e45fbdaae40018d37662868b98dff417a8786e Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Mon, 10 Jun 2024 19:21:18 +0300 Subject: [PATCH] Sub counts clean up --- app/db/tables.ts | 2 +- .../tournament-bracket/core/tests/mocks.ts | 3 ++ .../core/tests/test-utils.ts | 1 + .../tournament/TournamentRepository.server.ts | 10 ++++ app/features/tournament/routes/to.$id.tsx | 51 +++++++++---------- 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/app/db/tables.ts b/app/db/tables.ts index 19facd764..d0d3f927a 100644 --- a/app/db/tables.ts +++ b/app/db/tables.ts @@ -581,7 +581,7 @@ export interface TournamentSub { okWeapons: string | null; tournamentId: number; userId: number; - visibility: string; + visibility: "+1" | "+2" | "+3" | "ALL"; } export interface TournamentStaff { diff --git a/app/features/tournament-bracket/core/tests/mocks.ts b/app/features/tournament-bracket/core/tests/mocks.ts index 10bb4487c..cc33f8d65 100644 --- a/app/features/tournament-bracket/core/tests/mocks.ts +++ b/app/features/tournament-bracket/core/tests/mocks.ts @@ -2029,6 +2029,7 @@ export const PADDLING_POOL_257 = () => logoUrl: null, logoSrc: "/test.png", avatarImgId: null, + subCounts: [], startTime: 1709748000, author: { id: 860, @@ -7733,6 +7734,7 @@ export const PADDLING_POOL_255 = () => logoUrl: null, logoSrc: "/test.png", avatarImgId: null, + subCounts: [], startTime: 1708538400, author: { id: 860, @@ -13759,6 +13761,7 @@ export const IN_THE_ZONE_32 = () => logoUrl: null, logoSrc: "/test.png", avatarImgId: null, + subCounts: [], startTime: 1707588000, author: { id: 274, diff --git a/app/features/tournament-bracket/core/tests/test-utils.ts b/app/features/tournament-bracket/core/tests/test-utils.ts index e67dece8d..6795f4f85 100644 --- a/app/features/tournament-bracket/core/tests/test-utils.ts +++ b/app/features/tournament-bracket/core/tests/test-utils.ts @@ -59,6 +59,7 @@ export const testTournament = ( isFinalized: 0, name: "test", castTwitchAccounts: [], + subCounts: [], staff: [], tieBreakerMapPool: [], participatedUsers: [], diff --git a/app/features/tournament/TournamentRepository.server.ts b/app/features/tournament/TournamentRepository.server.ts index d5d877e40..0ac7a11c4 100644 --- a/app/features/tournament/TournamentRepository.server.ts +++ b/app/features/tournament/TournamentRepository.server.ts @@ -56,6 +56,16 @@ export async function findById(id: number) { ]) .where("TournamentStaff.tournamentId", "=", id), ).as("staff"), + jsonArrayFrom( + eb + .selectFrom("TournamentSub") + .select(({ fn }) => [ + "TournamentSub.visibility", + fn.countAll().as("count"), + ]) + .where("TournamentSub.tournamentId", "=", id) + .groupBy("TournamentSub.visibility"), + ).as("subCounts"), exists( selectFrom("TournamentResult") .where("TournamentResult.tournamentId", "=", id) diff --git a/app/features/tournament/routes/to.$id.tsx b/app/features/tournament/routes/to.$id.tsx index 8ac6fc9ba..36eab282c 100644 --- a/app/features/tournament/routes/to.$id.tsx +++ b/app/features/tournament/routes/to.$id.tsx @@ -18,7 +18,6 @@ import { useUser } from "~/features/auth/core/user"; import { getUser } from "~/features/auth/core/user.server"; import { Tournament } from "~/features/tournament-bracket/core/Tournament"; import { tournamentData } from "~/features/tournament-bracket/core/Tournament.server"; -import { findSubsByTournamentId } from "~/features/tournament-subs"; import { type SendouRouteHandle } from "~/utils/remix"; import { makeTitle } from "~/utils/strings"; import { assertUnreachable } from "~/utils/types"; @@ -114,31 +113,6 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => { const user = await getUser(request); const tournamentId = tournamentIdFromParams(params); - const subsCount = findSubsByTournamentId({ - tournamentId, - userId: user?.id, - // eslint-disable-next-line array-callback-return - }).filter((sub) => { - if (sub.visibility === "ALL") return true; - - const userPlusTier = user?.plusTier ?? 4; - - switch (sub.visibility) { - case "+1": { - return userPlusTier === 1; - } - case "+2": { - return userPlusTier <= 2; - } - case "+3": { - return userPlusTier <= 3; - } - default: { - assertUnreachable(sub.visibility); - } - } - }).length; - const tournament = await tournamentData({ tournamentId, user }); const streams = @@ -162,7 +136,6 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => { return { tournament, - subsCount, streamingParticipants: streams.flatMap((s) => (s.userId ? [s.userId] : [])), streamsCount: streams.length, toSetMapPool: @@ -202,6 +175,28 @@ export default function TournamentLayout() { const onBracketsPage = location.pathname.includes("brackets"); + const subsCount = () => + tournament.ctx.subCounts.reduce((acc, cur) => { + if (cur.visibility === "ALL") return acc + cur.count; + + const userPlusTier = user?.plusTier ?? 4; + + switch (cur.visibility) { + case "+1": { + return userPlusTier === 1 ? acc + cur.count : acc; + } + case "+2": { + return userPlusTier <= 2 ? acc + cur.count : acc; + } + case "+3": { + return userPlusTier <= 3 ? acc + cur.count : acc; + } + default: { + assertUnreachable(cur.visibility); + } + } + }, 0); + return (
@@ -216,7 +211,7 @@ export default function TournamentLayout() { {!tournament.everyBracketOver && tournament.subsFeatureEnabled && ( - {t("tournament:tabs.subs", { count: data.subsCount })} + {t("tournament:tabs.subs", { count: subsCount() })} )} {tournament.hasStarted && !tournament.everyBracketOver ? (