Cleanup public_lobbies endpoint

This commit is contained in:
Scott Anderson
2025-06-24 15:40:31 -04:00
parent 4680b04656
commit 55cba1bdac
3 changed files with 35 additions and 18 deletions
+10 -5
View File
@@ -3,7 +3,12 @@ import { customElement, state } from "lit/decorators.js";
import { translateText } from "../client/Utils";
import { GameMapType, GameMode } from "../core/game/Game";
import { terrainMapFileLoader } from "../core/game/TerrainMapFileLoader";
import { GameID, GameInfo } from "../core/Schemas";
import {
GameID,
GameInfo,
PublicLobbies,
PublicLobbiesSchema,
} from "../core/Schemas";
import { generateID } from "../core/Util";
import { JoinLobbyEvent } from "./Main";
@@ -41,7 +46,8 @@ export class PublicLobby extends LitElement {
private async fetchAndUpdateLobbies(): Promise<void> {
try {
this.lobbies = await this.fetchLobbies();
const publicLobbies = await this.fetchPublicLobbies();
this.lobbies = publicLobbies.lobbies;
this.lobbies.forEach((l) => {
// Store the start time on first fetch because endpoint is cached, causing
// the time to appear irregular.
@@ -72,13 +78,12 @@ export class PublicLobby extends LitElement {
}
}
async fetchLobbies(): Promise<GameInfo[]> {
async fetchPublicLobbies(): Promise<PublicLobbies> {
try {
const response = await fetch(`/api/public_lobbies`);
if (!response.ok)
throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return data.lobbies;
return response.json().then(PublicLobbiesSchema.parse);
} catch (error) {
console.error("Error fetching lobbies:", error);
throw error;
+23 -11
View File
@@ -106,17 +106,6 @@ export type Player = z.infer<typeof PlayerSchema>;
export type GameStartInfo = z.infer<typeof GameStartInfoSchema>;
const PlayerTypeSchema = z.enum(PlayerType);
export interface GameInfo {
gameID: GameID;
clients?: ClientInfo[];
numClients?: number;
msUntilStart?: number;
gameConfig?: GameConfig;
}
export interface ClientInfo {
clientID: ClientID;
username: string;
}
export enum LogSeverity {
Debug = "DEBUG",
Info = "INFO",
@@ -205,6 +194,21 @@ export const QuickChatKeySchema = z.enum(
) as [string, ...string[]],
);
export const ClientInfoSchema = z.object({
clientID: ID,
username: UsernameSchema,
});
export type ClientInfo = z.infer<typeof ClientInfoSchema>;
export const GameInfoSchema = z.object({
gameID: ID,
clients: ClientInfoSchema.array().optional(),
numClients: z.number().optional(),
msUntilStart: z.number().optional(),
gameConfig: GameConfigSchema.optional(),
});
export type GameInfo = z.infer<typeof GameInfoSchema>;
//
// Intents
//
@@ -521,3 +525,11 @@ export const GameRecordSchema = AnalyticsRecordSchema.extend({
turns: TurnSchema.array(),
});
export type GameRecord = z.infer<typeof GameRecordSchema>;
//
// Game server API calls
//
export const PublicLobbiesSchema = z.object({
lobbies: GameInfoSchema.array(),
});
export type PublicLobbies = z.infer<typeof PublicLobbiesSchema>;
+2 -2
View File
@@ -5,7 +5,7 @@ import http from "http";
import path from "path";
import { fileURLToPath } from "url";
import { getServerConfigFromServer } from "../core/configuration/ConfigLoader";
import { GameInfo } from "../core/Schemas";
import { GameInfo, PublicLobbies } from "../core/Schemas";
import { generateID } from "../core/Util";
import { gatekeeper, LimiterType } from "./Gatekeeper";
import { logger } from "./Logger";
@@ -260,7 +260,7 @@ async function fetchLobbies(): Promise<number> {
// Update the JSON string
publicLobbiesJsonStr = JSON.stringify({
lobbies: lobbyInfos,
});
} satisfies PublicLobbies);
return publicLobbyIDs.size;
}