mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-16 13:12:59 +00:00
feat: subscriber-hosted public lobby listing (#4480)
Part of #4040 (v1 scope: listing + browser + per-subscriber limit; custom lobby name/description left for a follow-up). ## What Subscribers can toggle their **private lobby** to be **publicly listed**; a browsable **"Open Lobbies"** list appears in the Join Lobby modal. Hard limit of **one listed lobby per subscriber**, enforced cluster-wide. ## How **Semantics** — a listed lobby stays `GameType.Private`: the host keeps full control and starts the game manually; the toggle only controls visibility. The `listed` flag lives on `GameServer` (not `GameConfig`), so it cannot be smuggled in through `update_game_config` and never touches core/sim/records. **Distribution** — reuses the existing public-lobby pipeline end to end: a new `"hosted"` `PublicGameType` bucket flows worker → master IPC → `/lobbies` websocket → `PublicLobbySocket`. Master scheduling now iterates only `SCHEDULED_PUBLIC_GAME_TYPES` (`ffa`/`team`/`special`), so it never sets countdowns on or schedules replacements for hosted lobbies. Lobbies delist automatically when the game starts/fills/dies (phase change). The broadcast fingerprint now includes browser-visible config, so host edits (map/mode) refresh the list even though the gameID doesn't change. **Gating** — new authenticated endpoint `POST /api/game/:id/listing`: - creator-only (403), private + not-started only (409) - fresh subscription check via server-side `getUserMe` using the shared `hasActiveSubscription()` helper (`active`/`trialing`); skipped in `GameEnv.Dev` (same precedent as Turnstile) so it's testable locally - one-lobby-per-creator (409): a SHA-256 hash of the creator's persistentID rides worker↔master IPC (`PublicGameInfo.creatorID`); the master dedupes as a race backstop. The hash — and host-only config (whitelist, name reveals) — are **stripped from every client payload** (broadcast + primed snapshot). **Client** — subscriber-gated "List lobby publicly" toggle in the host modal (server rejection reverts the toggle and shows a translated message); "Open Lobbies" rows (map, mode, player count) in the Join Lobby modal that reuse the existing private-join flow. **Compat** — `PublicGames.games` is now a `partialRecord`, so newer clients tolerate servers that don't send every bucket. Note: already-open old clients will fail to parse broadcasts containing the new `hosted` key until refreshed (closed Zod enum) — same class of break as previous wire-schema changes. ## Testing - `tests/server/HostedLobbyListing.test.ts` (15 tests): listed-lobby filtering, flag not settable via config intent, master aggregation + creator dedupe + no scheduling of hosted, creatorID stripping (broadcast + primed snapshot), `creatorHasListedLobby` (broadcast + local), fingerprint refresh on config change - `hasActiveSubscription` cases in `ApiSchemas.test.ts`; hosted counts-delta patch in `LobbySocket.test.ts` - Full suite green (1723 + 141 tests), tsc/eslint/prettier clean - **E2E in the real app** (headless Chromium, two browser contexts): host lists lobby → appears in second browser's Join Lobby list (creatorID absent from payload) → join succeeds (2 players in lobby) → same creator's second lobby rejected 409 with toggle revert → unlist removes it from a fresh browser's list. Curl negatives: missing auth 400, bad token 401, non-creator 403, missing game 404, bad body 400. ## Known follow-ups - Custom lobby name/description in the browser (needs the censor pipeline) — rest of #4040 - A listed lobby whose host closes the tab stays advertised indefinitely (an empty private lobby never leaves the Lobby phase) — pre-existing lifecycle, now more visible; consider delisting on creator disconnect 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -139,6 +139,13 @@ export type UserSubscription = NonNullable<
|
||||
NonNullable<UserMeResponse["player"]["subscription"]>
|
||||
>;
|
||||
|
||||
// Whether the player currently has subscriber entitlements (e.g. may list a
|
||||
// private lobby publicly). Trialing counts; past_due/canceled do not.
|
||||
export function hasActiveSubscription(userMe: UserMeResponse): boolean {
|
||||
const status = userMe.player.subscription?.status;
|
||||
return status === "active" || status === "trialing";
|
||||
}
|
||||
|
||||
export const PlayerStatsLeafSchema = z.object({
|
||||
wins: BigIntStringSchema,
|
||||
losses: BigIntStringSchema,
|
||||
|
||||
+38
-3
@@ -150,7 +150,31 @@ export type PublicGames = z.infer<typeof PublicGamesSchema>;
|
||||
export type PublicGameInfo = z.infer<typeof PublicGameInfoSchema>;
|
||||
export type PublicGameType = z.infer<typeof PublicGameTypeSchema>;
|
||||
|
||||
export const PublicGameTypeSchema = z.enum(["ffa", "team", "special"]);
|
||||
export const PublicGameTypeSchema = z.enum([
|
||||
"ffa",
|
||||
"team",
|
||||
"special",
|
||||
"hosted",
|
||||
]);
|
||||
|
||||
// Lobby types the master schedules from the map playlist. "hosted" is
|
||||
// excluded: those are player-created private lobbies that a subscriber has
|
||||
// listed publicly, and the host (not the master) controls their lifecycle.
|
||||
// Derived from PublicGameTypeSchema so a new lobby type is scheduled by
|
||||
// default and opting out is the explicit act.
|
||||
export const ScheduledPublicGameTypeSchema = PublicGameTypeSchema.exclude([
|
||||
"hosted",
|
||||
]);
|
||||
export const SCHEDULED_PUBLIC_GAME_TYPES =
|
||||
ScheduledPublicGameTypeSchema.options;
|
||||
export type ScheduledPublicGameType = z.infer<
|
||||
typeof ScheduledPublicGameTypeSchema
|
||||
>;
|
||||
|
||||
// Cluster-wide cap on subscriber-listed (hosted) lobbies, to prevent listing
|
||||
// spam. Workers reject listings past the cap; the master caps the broadcast
|
||||
// and delists any overflow as the authoritative backstop.
|
||||
export const MAX_HOSTED_LOBBIES = 10;
|
||||
|
||||
export const UsernameSchema = z
|
||||
.string()
|
||||
@@ -178,8 +202,17 @@ export const GameInfoSchema = z.object({
|
||||
serverTime: z.number(),
|
||||
gameConfig: z.lazy(() => GameConfigSchema).optional(),
|
||||
publicGameType: PublicGameTypeSchema.optional(),
|
||||
// Private lobbies only: whether the lobby is publicly listed. Server-owned
|
||||
// (only /api/game/:id/listing sets it); carried in lobby info so the host
|
||||
// UI stays in sync when the server delists (whitelist enabled, duplicate
|
||||
// creator resolved by the master).
|
||||
listed: z.boolean().optional(),
|
||||
});
|
||||
|
||||
// Browser-facing lobby info. Master/worker-internal fields (the creator hash
|
||||
// used for the one-listed-lobby-per-creator check) live on
|
||||
// InternalGameInfoSchema in IPCBridgeSchema.ts, so client payloads cannot
|
||||
// carry them by construction.
|
||||
export const PublicGameInfoSchema = z.object({
|
||||
gameID: z.string(),
|
||||
numClients: z.number(),
|
||||
@@ -190,7 +223,9 @@ export const PublicGameInfoSchema = z.object({
|
||||
|
||||
export const PublicGamesSchema = z.object({
|
||||
serverTime: z.number(),
|
||||
games: z.record(PublicGameTypeSchema, z.array(PublicGameInfoSchema)),
|
||||
// partialRecord: every consumer already treats buckets as optional, and it
|
||||
// lets clients tolerate servers that don't send every lobby type.
|
||||
games: z.partialRecord(PublicGameTypeSchema, z.array(PublicGameInfoSchema)),
|
||||
});
|
||||
|
||||
// Wire message sent from server to lobby WebSocket clients.
|
||||
@@ -199,7 +234,7 @@ export const PublicGamesSchema = z.object({
|
||||
export const PublicLobbyFullSchema = z.object({
|
||||
type: z.literal("full"),
|
||||
serverTime: z.number(),
|
||||
games: z.record(PublicGameTypeSchema, z.array(PublicGameInfoSchema)),
|
||||
games: z.partialRecord(PublicGameTypeSchema, z.array(PublicGameInfoSchema)),
|
||||
});
|
||||
|
||||
export const PublicLobbyCountsSchema = z.object({
|
||||
|
||||
Reference in New Issue
Block a user