have game server store game

This commit is contained in:
Evan
2024-12-04 18:53:36 -08:00
parent 22b877e85c
commit 731536931e
7 changed files with 403 additions and 10 deletions
+1 -3
View File
@@ -74,9 +74,7 @@ export class GameManager {
active.filter(g => !g.hasStarted() && g.isPublic).forEach(g => {
g.start()
})
finished.forEach(g => {
g.endGame()
})
finished.map(g => g.endGame()); // Fire and forget
this.games = [...lobbies, ...active]
}
}
+20 -2
View File
@@ -3,7 +3,9 @@ import { Config } from "../core/configuration/Config";
import { Client } from "./Client";
import WebSocket from 'ws';
import { slog } from "./StructuredLog";
import { Storage } from '@google-cloud/storage';
const storage = new Storage();
export enum GamePhase {
Lobby = 'LOBBY',
@@ -14,7 +16,7 @@ export enum GamePhase {
export class GameServer {
private maxGameDuration = 60 * 60 * 1000 // 1 hour
private maxGameDuration = 2 * 60 * 60 * 1000 // 2 hours
private turns: Turn[] = []
private intents: Intent[] = []
@@ -135,7 +137,7 @@ export class GameServer {
})
}
endGame() {
async endGame() {
// Close all WebSocket connections
clearInterval(this.endTurnIntervalID);
this.clients.forEach(client => {
@@ -144,6 +146,22 @@ export class GameServer {
client.ws.close();
}
});
try {
if (this.turns.length > 100 && this.clients.length > 0) {
const bucket = storage.bucket(this.config.gameStorageBucketName());
const file = bucket.file(this.id);
const game = {
id: this.id,
date: new Date().toISOString().split('T')[0],
turns: this.turns
}
await file.save(JSON.stringify(game), {
contentType: 'application/json'
});
}
} catch (error) {
console.log('error writing game to gcs: ' + error)
}
}
phase(): GamePhase {