mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-11-18 12:46:58 -06:00
Build the lobby viewer's action gates once for the lobby page and its action
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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 (
|
||||
<div className={styles.root}>
|
||||
|
||||
@@ -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 (
|
||||
<MatchBannerContainer>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<typeof loader>;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<T extends { mapPool: ModeWithStage[] }>(maps: T) {
|
||||
return { ...maps, modes: new MapPool(maps.mapPool).modes };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user