Skip to content

Commit 6e67c2b

Browse files
authored
visibleAt (#3497)
## Description: needs prereq of openfrontio/infra#272 ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: w.o.n
1 parent e2d5838 commit 6e67c2b

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/client/ClientGameRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ export class ClientGameRunner {
331331
Date.now(),
332332
update.winner,
333333
this.lobby.gameStartInfo.lobbyCreatedAt,
334+
this.lobby.gameStartInfo.visibleAt,
334335
);
335336
endGame(record);
336337
}

src/core/Schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ export const PlayerSchema = z.object({
526526
export const GameStartInfoSchema = z.object({
527527
gameID: ID,
528528
lobbyCreatedAt: z.number(),
529+
visibleAt: z.number().optional(),
529530
config: GameConfigSchema,
530531
players: PlayerSchema.array(),
531532
});

src/core/Util.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ export function createPartialGameRecord(
251251
winner: Winner,
252252
// lobby creation time (ms). Defaults to start time for singleplayer.
253253
lobbyCreatedAt?: number,
254+
// Time the lobby became visible to players (ms).
255+
visibleAt?: number,
254256
): PartialGameRecord {
255257
const duration = Math.floor((end - start) / 1000);
256258
const num_turns = allTurns.length;
@@ -262,13 +264,14 @@ export function createPartialGameRecord(
262264
const actualLobbyCreatedAt = lobbyCreatedAt ?? start;
263265
const lobbyFillTime = Math.max(
264266
0,
265-
start - Math.min(actualLobbyCreatedAt, start),
267+
start - (visibleAt ?? actualLobbyCreatedAt),
266268
);
267269

268270
const record: PartialGameRecord = {
269271
info: {
270272
gameID,
271273
lobbyCreatedAt: actualLobbyCreatedAt,
274+
visibleAt,
272275
lobbyFillTime,
273276
config,
274277
players,

src/server/GameServer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export class GameServer {
8787

8888
private lobbyInfoIntervalId: ReturnType<typeof setInterval> | null = null;
8989

90+
private visibleAt?: number;
91+
9092
constructor(
9193
public readonly id: string,
9294
readonly log_: Logger,
@@ -98,6 +100,9 @@ export class GameServer {
98100
private publicGameType?: PublicGameType,
99101
) {
100102
this.log = log_.child({ gameID: id });
103+
if (startsAt !== undefined) {
104+
this.visibleAt = Date.now();
105+
}
101106
}
102107

103108
private get lobbyCreatorID(): ClientID | undefined {
@@ -558,6 +563,8 @@ export class GameServer {
558563

559564
public setStartsAt(startsAt: number) {
560565
this.startsAt = startsAt;
566+
// Record when the lobby first became visible to players, used to measure lobby fill time.
567+
this.visibleAt ??= Date.now();
561568
}
562569

563570
public numClients(): number {
@@ -656,6 +663,7 @@ export class GameServer {
656663
const result = GameStartInfoSchema.safeParse({
657664
gameID: this.id,
658665
lobbyCreatedAt: this.createdAt,
666+
visibleAt: this.visibleAt,
659667
config: this.gameConfig,
660668
players: this.activeClients.map((c) => ({
661669
username: c.username,
@@ -1001,6 +1009,7 @@ export class GameServer {
10011009
Date.now(),
10021010
this.winner?.winner,
10031011
this.createdAt,
1012+
this.visibleAt,
10041013
),
10051014
),
10061015
);

0 commit comments

Comments
 (0)