mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-11-01 16:20:47 -06:00
75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
import { db } from "~/db/sql";
|
|
import type { Tables } from "~/db/tables";
|
|
import { MATCHES_COUNT_NEEDED_FOR_LEADERBOARD } from "~/features/leaderboards/leaderboards-constants";
|
|
import { commonUserSelect, peakXpOverallSql } from "~/utils/kysely.server";
|
|
import * as StreamRanking from "../sidebar/core/StreamRanking";
|
|
|
|
export function replaceAll(streams: Omit<Tables["LiveStream"], "id">[]) {
|
|
return db.transaction().execute(async (trx) => {
|
|
await trx.deleteFrom("LiveStream").execute();
|
|
|
|
await trx.insertInto("LiveStream").values(streams).execute();
|
|
});
|
|
}
|
|
|
|
/** Adds the accounts as streamers of their tournament, returning the inserted ids in order; an account already streaming it is skipped. */
|
|
export function insertTournamentStreamers(
|
|
rows: Omit<Tables["TournamentStreamer"], "id">[],
|
|
) {
|
|
return db
|
|
.insertInto("TournamentStreamer")
|
|
.values(rows)
|
|
.onConflict((oc) =>
|
|
oc.columns(["twitchAccount", "tournamentId"]).doNothing(),
|
|
)
|
|
.returning("id")
|
|
.execute();
|
|
}
|
|
|
|
/** The user's ongoing Splatoon 3 Twitch stream, `undefined` when they are not live. */
|
|
export function findByUserId(userId: number) {
|
|
return db
|
|
.selectFrom("LiveStream")
|
|
.select([
|
|
"LiveStream.twitch",
|
|
"LiveStream.viewerCount",
|
|
"LiveStream.thumbnailUrl",
|
|
])
|
|
.where("LiveStream.userId", "=", userId)
|
|
.where("LiveStream.twitch", "is not", null)
|
|
.$narrowType<{ twitch: string }>()
|
|
.executeTakeFirst();
|
|
}
|
|
|
|
/** Live streams of high X Power players with a calculated skill (enough sets for the leaderboard) in season `season` or the one before it. */
|
|
export function findXRankStreams(season: number) {
|
|
return db
|
|
.selectFrom("LiveStream")
|
|
.innerJoin("User", "User.twitch", "LiveStream.twitch")
|
|
.innerJoin("SplatoonPlayer", "SplatoonPlayer.userId", "User.id")
|
|
.where(peakXpOverallSql(), ">=", StreamRanking.minXpForStreamToBeShown())
|
|
.where("LiveStream.twitch", "is not", null)
|
|
.where((eb) =>
|
|
eb.exists(
|
|
eb
|
|
.selectFrom("Skill")
|
|
.select("Skill.id")
|
|
.whereRef("Skill.userId", "=", "User.id")
|
|
.where("Skill.season", "in", [season - 1, season])
|
|
.where(
|
|
"Skill.matchesCount",
|
|
">=",
|
|
MATCHES_COUNT_NEEDED_FOR_LEADERBOARD,
|
|
),
|
|
),
|
|
)
|
|
.select((eb) => [
|
|
...commonUserSelect(eb),
|
|
peakXpOverallSql<number>().as("peakXp"),
|
|
"LiveStream.viewerCount",
|
|
"LiveStream.thumbnailUrl",
|
|
"LiveStream.twitch as twitchUsername",
|
|
])
|
|
.execute();
|
|
}
|