record game metadata to gcs

This commit is contained in:
Evan
2024-12-06 14:39:31 -08:00
parent b8db74247f
commit cd09c0a1d6
6 changed files with 92 additions and 12 deletions
+23 -3
View File
@@ -3,7 +3,7 @@ import { ClientID, GameConfig, GameID } from "../core/Schemas";
import { v4 as uuidv4 } from 'uuid';
import { Client } from "./Client";
import { GamePhase, GameServer } from "./GameServer";
import { Difficulty, GameMap } from "../core/game/Game";
import { Difficulty, GameMap, GameType } from "../core/game/Game";
@@ -39,7 +39,17 @@ export class GameManager {
createPrivateGame(): string {
const id = genSmallGameID()
this.games.push(new GameServer(id, Date.now(), false, this.config, { gameMap: GameMap.World, difficulty: Difficulty.Medium }))
this.games.push(new GameServer(
id,
Date.now(),
false,
this.config,
{
gameMap: GameMap.World,
gameType: GameType.Private,
difficulty: Difficulty.Medium
}
))
return id
}
@@ -68,7 +78,17 @@ export class GameManager {
if (now > this.lastNewLobby + this.config.gameCreationRate()) {
this.lastNewLobby = now
const id = uuidv4()
lobbies.push(new GameServer(id, now, true, this.config, { gameMap: GameMap.World, difficulty: Difficulty.Medium }))
lobbies.push(new GameServer(
id,
now,
true,
this.config,
{
gameMap: GameMap.World,
gameType: GameType.Public,
difficulty: Difficulty.Medium
}
))
}
active.filter(g => !g.hasStarted() && g.isPublic).forEach(g => {
+12 -4
View File
@@ -1,9 +1,10 @@
import { ClientMessage, ClientMessageSchema, GameConfig, Intent, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
import { ClientMessage, ClientMessageSchema, GameConfig, GameRecordSchema, Intent, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
import { Config } from "../core/configuration/Config";
import { Client } from "./Client";
import WebSocket from 'ws';
import { slog } from "./StructuredLog";
import { Storage } from '@google-cloud/storage';
import { ProcessGameRecord as ProcessRecord } from "../core/Util";
const storage = new Storage();
@@ -22,6 +23,7 @@ export class GameServer {
private intents: Intent[] = []
private clients: Client[] = []
private _hasStarted = false
private _startTime: number = null
private endTurnIntervalID
@@ -88,11 +90,13 @@ export class GameServer {
}
public startTime(): number {
return this.createdAt + this.config.lobbyLifetime()
return this._startTime
}
public start() {
this._hasStarted = true
this._startTime = Date.now()
this.clients.forEach(c => {
console.log(`game ${this.id} sending start message to ${c.id}`)
this.sendStartGameMsg(c.ws, 0)
@@ -138,7 +142,7 @@ export class GameServer {
// Close all WebSocket connections
clearInterval(this.endTurnIntervalID);
this.clients.forEach(client => {
client.ws.removeAllListeners('message');
client.ws.removeAllListeners('message'); // TODO: remove this?
if (client.ws.readyState === WebSocket.OPEN) {
client.ws.close(1000, "game has ended");
}
@@ -151,10 +155,14 @@ export class GameServer {
const file = bucket.file(this.id);
const game = {
id: this.id,
gameConfig: this.gameConfig,
startTimestampMS: this._startTime,
endTimestampMS: Date.now(),
date: new Date().toISOString().split('T')[0],
turns: this.turns
}
await file.save(JSON.stringify(game), {
const processed = ProcessRecord(game)
await file.save(JSON.stringify(GameRecordSchema.parse(processed)), {
contentType: 'application/json'
});
}