mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-08-23 19:46:28 -05:00
Streamline tournament streams
This commit is contained in:
@@ -96,6 +96,7 @@ export async function findById(id: number) {
|
||||
"User.discordAvatar",
|
||||
"User.customUrl",
|
||||
"User.country",
|
||||
"User.twitch",
|
||||
"PlusTier.tier as plusTier",
|
||||
"TournamentTeamMember.isOwner",
|
||||
"TournamentTeamMember.createdAt",
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
import { getStreams } from "~/modules/twitch";
|
||||
import { participantTwitchUsersByTournamentId } from "../queries/participantTwitchUsersByTournamentId.server";
|
||||
import type { TournamentData } from "~/features/tournament-bracket/core/Tournament.server";
|
||||
|
||||
export async function streamsByTournamentId({
|
||||
tournamentId,
|
||||
castTwitchAccounts,
|
||||
}: {
|
||||
tournamentId: number;
|
||||
castTwitchAccounts: string[] | null;
|
||||
}) {
|
||||
export async function streamsByTournamentId(tournament: TournamentData["ctx"]) {
|
||||
// prevent error logs in development
|
||||
if (
|
||||
process.env.NODE_ENV === "development" &&
|
||||
@@ -15,24 +9,26 @@ export async function streamsByTournamentId({
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
const twitchUsersOfTournament =
|
||||
participantTwitchUsersByTournamentId(tournamentId);
|
||||
const twitchUsersOfTournament = tournament.teams
|
||||
.filter((team) => team.checkIns.length > 0)
|
||||
.flatMap((team) => team.members)
|
||||
.filter((member) => member.twitch);
|
||||
|
||||
const streams = await getStreams();
|
||||
|
||||
const tournamentStreams = streams.flatMap((stream) => {
|
||||
const user = twitchUsersOfTournament.find(
|
||||
(u) => u.twitch === stream.twitchUserName,
|
||||
const member = twitchUsersOfTournament.find(
|
||||
(member) => member.twitch === stream.twitchUserName,
|
||||
);
|
||||
|
||||
if (user) {
|
||||
if (member) {
|
||||
return {
|
||||
...stream,
|
||||
userId: user.id,
|
||||
userId: member.userId,
|
||||
};
|
||||
}
|
||||
|
||||
if (castTwitchAccounts?.includes(stream.twitchUserName)) {
|
||||
if (tournament.castTwitchAccounts?.includes(stream.twitchUserName)) {
|
||||
return {
|
||||
...stream,
|
||||
userId: null,
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import { sql } from "~/db/sql";
|
||||
import type { User } from "~/db/types";
|
||||
|
||||
const stm = sql.prepare(/* sql */ `
|
||||
select
|
||||
"User"."id",
|
||||
"User"."twitch"
|
||||
from "User"
|
||||
left join "TournamentTeamMember" on "TournamentTeamMember"."userId" = "User"."id"
|
||||
left join "TournamentTeam" on "TournamentTeam"."id" = "TournamentTeamMember"."tournamentTeamId"
|
||||
left join "TournamentTeamCheckIn" on "TournamentTeamCheckIn"."tournamentTeamId" = "TournamentTeam"."id"
|
||||
where "TournamentTeam"."tournamentId" = @tournamentId
|
||||
and "TournamentTeamCheckIn"."checkedInAt" is not null
|
||||
and "User"."twitch" is not null
|
||||
`);
|
||||
|
||||
// const testStm = sql.prepare(/* sql */ `
|
||||
// select
|
||||
// "User"."id",
|
||||
// "User"."twitch"
|
||||
// from "User"
|
||||
// where "User"."twitch" is not null
|
||||
// `);
|
||||
|
||||
export function participantTwitchUsersByTournamentId(tournamentId: number) {
|
||||
return stm.all({ tournamentId }) as Array<Pick<User, "id" | "twitch">>;
|
||||
}
|
||||
@@ -3,25 +3,20 @@ import { useLoaderData } from "@remix-run/react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Redirect } from "~/components/Redirect";
|
||||
import { tournamentRegisterPage } from "~/utils/urls";
|
||||
import * as TournamentRepository from "../TournamentRepository.server";
|
||||
import { streamsByTournamentId } from "../core/streams.server";
|
||||
import { tournamentIdFromParams } from "../tournament-utils";
|
||||
import { useTournament } from "./to.$id";
|
||||
import { TournamentStream } from "../components/TournamentStream";
|
||||
import { tournamentData } from "~/features/tournament-bracket/core/Tournament.server";
|
||||
|
||||
export type TournamentStreamsLoader = typeof loader;
|
||||
|
||||
export const loader = async ({ params }: LoaderFunctionArgs) => {
|
||||
const tournamentId = tournamentIdFromParams(params);
|
||||
const tournament = await tournamentData({ tournamentId });
|
||||
|
||||
return {
|
||||
streams: await streamsByTournamentId({
|
||||
tournamentId,
|
||||
castTwitchAccounts:
|
||||
await TournamentRepository.findCastTwitchAccountsByTournamentId(
|
||||
tournamentId,
|
||||
),
|
||||
}),
|
||||
streams: await streamsByTournamentId(tournament.ctx),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -117,10 +117,7 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
||||
|
||||
const streams =
|
||||
tournament.data.stage.length > 0
|
||||
? await streamsByTournamentId({
|
||||
tournamentId,
|
||||
castTwitchAccounts: tournament.ctx.castTwitchAccounts,
|
||||
})
|
||||
? await streamsByTournamentId(tournament.ctx)
|
||||
: [];
|
||||
|
||||
const tournamentStartedInTheLastMonth =
|
||||
|
||||
Reference in New Issue
Block a user