From db5157f22dc6d70cbc07b00f393cd3c52279b2e7 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:36:09 +0200 Subject: [PATCH] Build the lobby viewer's action gates once for the lobby page and its action --- .../lobbies/actions/lobbies.$id.server.ts | 83 +++++-------------- .../lobbies/components/LobbyParticipants.tsx | 5 +- .../lobbies/components/LobbyStartBanner.tsx | 4 +- app/features/lobbies/core/LobbyState.ts | 16 +++- .../lobbies/loaders/lobbies.$id.server.ts | 60 +++++--------- app/features/lobbies/lobbies-utils.server.ts | 59 +++++++++++++ 6 files changed, 114 insertions(+), 113 deletions(-) diff --git a/app/features/lobbies/actions/lobbies.$id.server.ts b/app/features/lobbies/actions/lobbies.$id.server.ts index ac4feaf0b..37ffb0007 100644 --- a/app/features/lobbies/actions/lobbies.$id.server.ts +++ b/app/features/lobbies/actions/lobbies.$id.server.ts @@ -33,7 +33,7 @@ import { } from "../lobbies-schemas"; import { lobbySearchParams } from "../lobbies-search-params"; import { visibilityFromForm } from "../lobbies-utils"; -import { mapsFromForm } from "../lobbies-utils.server"; +import { lobbyViewerState, mapsFromForm } from "../lobbies-utils.server"; /** Actions changing what the lobbies list shows, see `LobbyCard`. */ const LISTING_ACTIONS = new Set< @@ -77,6 +77,15 @@ export const action = defineAction( return LobbySet.sideOf(set, participant.id) ? set.id : null; }; + const viewerActions = async () => + lobbyViewerState({ + lobby, + sets, + viewer: viewer ?? null, + hasFriendCode: Boolean( + await UserRepository.findCurrentFriendCodeByUserId(user.id), + ), + }).actions; const autoAcceptedUserIds: number[] = []; const requireNoConflict = () => errorToastIfConflict({ userId: user.id, lobbyId: id }); @@ -84,18 +93,7 @@ export const action = defineAction( switch (body._action) { case "SIGN_UP": { errorToastIfFalsy(!viewer, "Already signed up"); - errorToastIfFalsy( - !lobby.isViewerKicked, - "The host kicked you from this lobby", - ); - errorToastIfFalsy( - lobby.viewerApplication !== "DENIED", - "The host denied your application to this lobby", - ); - errorToastIfFalsy( - await UserRepository.findCurrentFriendCodeByUserId(user.id), - "Set your friend code first", - ); + errorToastIfFalsy((await viewerActions()).SIGN_UP, "Can't sign up"); if (lobby.isApplicationRequired) { await LobbyRepository.insertApplication({ @@ -151,8 +149,10 @@ export const action = defineAction( } case "CHECK_IN": { errorToastIfFalsy(viewer, "Not signed up"); - const actions = viewerActions({ lobby, sets, viewer, now }); - errorToastIfFalsy(actions.CHECK_IN, "Can't check in right now"); + errorToastIfFalsy( + (await viewerActions()).CHECK_IN, + "Can't check in right now", + ); requireNoConflict(); await LobbyRepository.updateParticipant({ @@ -217,7 +217,7 @@ export const action = defineAction( case "TOGGLE_DROP_AFTER_SET": { errorToastIfFalsy(viewer, "Not signed up"); errorToastIfFalsy( - viewer.isDroppingAfterSet || unfinishedSetIdOf(viewer) !== null, + viewer.isDroppingAfterSet || (await viewerActions()).DROP_AFTER_SET, "Not playing a set", ); @@ -501,9 +501,7 @@ async function signUp({ const now = databaseTimestampNow(); const hasStarted = now >= lobby.startsAt; - const withSpotCount = lobby.participants.filter( - (participant) => participant.status !== "WAITLISTED", - ).length; + const withSpotCount = LobbyState.signedUpCount(lobby.participants); const isLate = hasStarted && LobbyState.isCheckInOpen({ @@ -512,7 +510,8 @@ async function signUp({ now, }); - let isPresentRightAway = isLate && activeCountOf(lobby) < lobby.roomSize; + let isPresentRightAway = + isLate && LobbyState.activeCount(lobby.participants) < lobby.roomSize; if (isPresentRightAway && onConflict === "REJECT") { errorToastIfConflict({ userId, lobbyId: lobby.id }); } else if (isPresentRightAway) { @@ -563,14 +562,6 @@ async function acceptApplication({ }); } -/** Participants present: checked in or in the room. */ -function activeCountOf(lobby: LobbyRepository.LobbyById) { - return lobby.participants.filter( - (participant) => - participant.status === "IN_ROOM" || participant.status === "CHECKED_IN", - ).length; -} - function sendSetEndingMessage({ chatRoomId, userId, @@ -606,39 +597,3 @@ function latestPlayedSetOf( ) { return sets.findLast((set) => LobbySet.sideOf(set, userId) !== null); } - -function viewerActions({ - lobby, - sets, - viewer, - now, -}: { - lobby: LobbyRepository.LobbyById; - sets: LobbyMatchRepository.LobbySetWithGames[]; - viewer: LobbyRepository.LobbyById["participants"][number]; - now: number; -}) { - const latestSet = sets.at(-1); - const isPlaying = latestSet !== undefined && !LobbySet.isFinished(latestSet); - - const { status, isCheckInOpen } = LobbyState.statusAndCheckIn({ - lobby, - participants: lobby.participants, - isPlaying, - now, - }); - - return LobbyState.viewerActions({ - status, - viewer, - isPlayingUnfinishedSet: false, - hasFriendCode: true, - isKicked: lobby.isViewerKicked, - application: lobby.viewerApplication, - isCheckInOpen, - activeCount: activeCountOf(lobby), - roomSize: lobby.roomSize, - now, - startsAt: lobby.startsAt, - }); -} diff --git a/app/features/lobbies/components/LobbyParticipants.tsx b/app/features/lobbies/components/LobbyParticipants.tsx index 185c7a44b..057811b0c 100644 --- a/app/features/lobbies/components/LobbyParticipants.tsx +++ b/app/features/lobbies/components/LobbyParticipants.tsx @@ -1,5 +1,6 @@ import { useTranslation } from "react-i18next"; import { WeaponPool } from "~/components/match-page/WeaponPool"; +import * as LobbyState from "../core/LobbyState"; import type { LobbyParticipantStatus } from "../lobbies-constants"; import { useLobbyView } from "../lobbies-hooks"; import styles from "./LobbyParticipants.module.css"; @@ -19,9 +20,7 @@ export function LobbyParticipants() { (participant) => participant.status === status, ); - const spotsTaken = data.lobby.participants.filter( - (participant) => participant.status !== "WAITLISTED", - ).length; + const spotsTaken = LobbyState.signedUpCount(data.lobby.participants); return (
diff --git a/app/features/lobbies/components/LobbyStartBanner.tsx b/app/features/lobbies/components/LobbyStartBanner.tsx index 8bc617131..93608dcee 100644 --- a/app/features/lobbies/components/LobbyStartBanner.tsx +++ b/app/features/lobbies/components/LobbyStartBanner.tsx @@ -42,9 +42,7 @@ export function LobbyStartBanner() { const checkedInCount = data.lobby.participants.filter( (participant) => participant.status === "CHECKED_IN", ).length; - const signedUpCount = data.lobby.participants.filter( - (participant) => participant.status !== "WAITLISTED", - ).length; + const signedUpCount = LobbyState.signedUpCount(data.lobby.participants); return ( diff --git a/app/features/lobbies/core/LobbyState.ts b/app/features/lobbies/core/LobbyState.ts index 8c64194c7..30d4338c0 100644 --- a/app/features/lobbies/core/LobbyState.ts +++ b/app/features/lobbies/core/LobbyState.ts @@ -180,7 +180,7 @@ export function viewerActions({ isKicked, application, isCheckInOpen: checkInOpen, - activeCount, + activeCount: presentCount, roomSize, now, startsAt, @@ -225,7 +225,7 @@ export function viewerActions({ (hasStarted ? viewer.status !== "CHECKED_IN" && viewer.status !== "IN_ROOM" && - activeCount < roomSize + presentCount < roomSize : viewer.status === "REGISTERED"); return { @@ -236,7 +236,17 @@ export function viewerActions({ }; } -function signedUpCount( +/** Participants present: checked in or in the room. */ +export function activeCount( + participants: Array<{ status: LobbyParticipantStatus }>, +) { + return participants.filter( + (p) => p.status === "IN_ROOM" || p.status === "CHECKED_IN", + ).length; +} + +/** Participants holding a spot, i.e. everyone but the waitlisted. */ +export function signedUpCount( participants: Array<{ status: LobbyParticipantStatus }>, ) { return participants.filter((p) => p.status !== "WAITLISTED").length; diff --git a/app/features/lobbies/loaders/lobbies.$id.server.ts b/app/features/lobbies/loaders/lobbies.$id.server.ts index da8743a82..7551c3d0c 100644 --- a/app/features/lobbies/loaders/lobbies.$id.server.ts +++ b/app/features/lobbies/loaders/lobbies.$id.server.ts @@ -9,14 +9,12 @@ import * as ReportedWeaponRepository from "~/features/sendouq-match/ReportedWeap import * as UserCardRepository from "~/features/user-card/UserCardRepository.server"; import * as UserRepository from "~/features/user-page/UserRepository.server"; import * as SearchParams from "~/modules/search-params/search-params"; -import { databaseTimestampNow } from "~/utils/dates"; import type { SerializeFrom } from "~/utils/remix"; import { notFound, parseParams } from "~/utils/remix.server"; import { idObject } from "~/utils/schema"; import * as Leaderboard from "../core/Leaderboard"; import * as LobbyMapList from "../core/LobbyMapList.server"; import * as LobbySet from "../core/LobbySet"; -import * as LobbyState from "../core/LobbyState"; import * as ScoreboardAttribution from "../core/ScoreboardAttribution"; import * as LobbyMatchRepository from "../LobbyMatchRepository.server"; import * as LobbyRepository from "../LobbyRepository.server"; @@ -25,6 +23,7 @@ import { LOBBY_SIDE_TEAM_ID, } from "../lobbies-constants"; import { lobbySearchParams } from "../lobbies-search-params"; +import { lobbyViewerState } from "../lobbies-utils.server"; export type LobbyLoaderData = SerializeFrom; @@ -78,36 +77,29 @@ export const loader = async ({ params, request, url }: LoaderFunctionArgs) => { } } - const now = databaseTimestampNow(); const latestSet = sets.at(-1) ?? null; - const isPlaying = latestSet !== null && !LobbySet.isFinished(latestSet); - const { status, isCheckInOpen } = LobbyState.statusAndCheckIn({ - lobby, - participants: lobby.participants, - isPlaying, - now, - }); - - const viewerSide = - latestSet && viewer?.status === "IN_ROOM" - ? LobbySet.sideOf(latestSet, viewer.id) - : null; - const isPlayingUnfinishedSet = isPlaying && viewerSide !== null; - const hasFriendCode = user ? Boolean(await UserRepository.findCurrentFriendCodeByUserId(user.id)) : false; + const { + status, + isCheckInOpen, + side: viewerSide, + isPlayingUnfinishedSet, + actions: viewerActions, + } = lobbyViewerState({ + lobby, + sets, + viewer, + hasFriendCode, + }); - const activeCount = lobby.participants.filter( - (participant) => - participant.status === "IN_ROOM" || participant.status === "CHECKED_IN", - ).length; - - const viewerTeamChatRoomId = isPlayingUnfinishedSet - ? viewerSide === "ALPHA" - ? latestSet.alphaChatRoomId - : latestSet.bravoChatRoomId - : null; + const viewerTeamChatRoomId = + latestSet && isPlayingUnfinishedSet + ? viewerSide === "ALPHA" + ? latestSet.alphaChatRoomId + : latestSet.bravoChatRoomId + : null; const userIds = R.unique([ ...lobby.participants.map((participant) => participant.id), @@ -145,19 +137,7 @@ export const loader = async ({ params, request, url }: LoaderFunctionArgs) => { isPlayingUnfinishedSet, } : null, - viewerActions: LobbyState.viewerActions({ - status, - viewer, - isPlayingUnfinishedSet, - hasFriendCode, - isKicked: lobby.isViewerKicked, - application: lobby.viewerApplication, - isCheckInOpen, - activeCount, - roomSize: lobby.roomSize, - now, - startsAt: lobby.startsAt, - }), + viewerActions, hasFriendCode, /** The viewer's team chat while they play a set, a voice call in their previous one follows them into it. */ teamChatRoomId: viewerTeamChatRoomId, diff --git a/app/features/lobbies/lobbies-utils.server.ts b/app/features/lobbies/lobbies-utils.server.ts index ee1dc728c..512b06b5c 100644 --- a/app/features/lobbies/lobbies-utils.server.ts +++ b/app/features/lobbies/lobbies-utils.server.ts @@ -1,7 +1,12 @@ import { MapPool } from "~/features/map-list-generator/core/map-pool"; import { tournamentSharedCached } from "~/features/tournament-bracket/core/Tournament.server"; import type { ModeWithStage } from "~/modules/in-game-lists/types"; +import { dateToDatabaseTimestamp } from "~/utils/dates"; import { errorToastIfFalsy } from "~/utils/remix.server"; +import * as LobbySet from "./core/LobbySet"; +import * as LobbyState from "./core/LobbyState"; +import type * as LobbyMatchRepository from "./LobbyMatchRepository.server"; +import type * as LobbyRepository from "./LobbyRepository.server"; import type { LobbyMaps } from "./lobbies-constants"; import { modesOfMaps } from "./lobbies-utils"; @@ -41,6 +46,60 @@ export async function mapsFromForm(values: { }); } +/** + * The lobby's status and what the viewer may do in it right now, the one place the lobby page and its action decide + * the viewer's gates from. `side` is the viewer's side of the set being played while in the room. + */ +export function lobbyViewerState({ + lobby, + sets, + viewer, + now: nowDate = new Date(), + hasFriendCode, +}: { + lobby: LobbyRepository.LobbyById; + sets: LobbyMatchRepository.LobbySetWithGames[]; + viewer: LobbyRepository.LobbyById["participants"][number] | null; + now?: Date; + hasFriendCode: boolean; +}) { + const now = dateToDatabaseTimestamp(nowDate); + const latestSet = sets.at(-1) ?? null; + const isPlaying = latestSet !== null && !LobbySet.isFinished(latestSet); + const { status, isCheckInOpen } = LobbyState.statusAndCheckIn({ + lobby, + participants: lobby.participants, + isPlaying, + now, + }); + + const side = + latestSet && viewer?.status === "IN_ROOM" + ? LobbySet.sideOf(latestSet, viewer.id) + : null; + const isPlayingUnfinishedSet = isPlaying && side !== null; + + return { + status, + isCheckInOpen, + side, + isPlayingUnfinishedSet, + actions: LobbyState.viewerActions({ + status, + viewer, + isPlayingUnfinishedSet, + hasFriendCode, + isKicked: lobby.isViewerKicked, + application: lobby.viewerApplication, + isCheckInOpen, + activeCount: LobbyState.activeCount(lobby.participants), + roomSize: lobby.roomSize, + now, + startsAt: lobby.startsAt, + }), + }; +} + function withPoolModes(maps: T) { return { ...maps, modes: new MapPool(maps.mapPool).modes }; }