record games in gcs

This commit is contained in:
Evan
2024-12-07 18:30:04 -08:00
parent 2d2df14ae3
commit 7669105a1b
17 changed files with 131 additions and 132 deletions
+12 -6
View File
@@ -30,8 +30,19 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
const playerID = uuidv4()
const eventBus = new EventBus()
const config = getConfig()
let gameConfig: GameConfig = null
if (lobbyConfig.gameType == GameType.Singleplayer) {
gameConfig = {
gameType: GameType.Singleplayer,
gameMap: lobbyConfig.map,
difficulty: lobbyConfig.difficulty,
}
}
const transport = new Transport(
lobbyConfig.gameType == GameType.Singleplayer,
gameConfig,
eventBus,
lobbyConfig.gameID,
lobbyConfig.ip,
@@ -49,12 +60,7 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
if (message.type == "start") {
console.log('lobby: game started')
onjoin()
const gameConfig = {
gameMap: message.config?.gameMap || lobbyConfig.map,
difficulty: message.config?.difficulty || lobbyConfig.difficulty,
gameType: lobbyConfig.gameType
}
createClientGame(gameConfig, eventBus, transport, lobbyConfig.gameID, clientID).then(r => r.start())
createClientGame(message.config, eventBus, transport, lobbyConfig.gameID, clientID).then(r => r.start())
};
}
transport.connect(onconnect, onmessage)
+23 -7
View File
@@ -1,26 +1,31 @@
import {Config} from "../core/configuration/Config";
import {ClientMessage, ClientMessageSchema, Intent, ServerMessage, ServerTurnMessageSchema, Turn} from "../core/Schemas";
import { Config } from "../core/configuration/Config";
import { ClientMessage, ClientMessageSchema, GameConfig, GameID, GameRecordSchema, Intent, ServerMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
import { CreateGameRecord, generateGameID } from "../core/Util";
export class LocalServer {
private gameID = "LOCAL"
private turns: Turn[] = []
private intents: Intent[] = []
private startedAt: number
private endTurnIntervalID
constructor(private config: Config, private clientConnect: () => void, private clientMessage: (message: ServerMessage) => void) {
private gameID: GameID
constructor(private config: Config, private gameConfig: GameConfig, private clientConnect: () => void, private clientMessage: (message: ServerMessage) => void) {
this.gameID = generateGameID()
}
start() {
this.startedAt = Date.now()
this.endTurnIntervalID = setInterval(() => this.endTurn(), this.config.turnIntervalMs());
this.clientConnect()
this.clientMessage({
this.clientMessage(ServerStartGameMessageSchema.parse({
type: "start",
config: this.gameConfig,
turns: [],
})
}))
}
onMessage(message: string) {
@@ -43,4 +48,15 @@ export class LocalServer {
turn: pastTurn
})
}
public endGame() {
console.log('local server ending game')
clearInterval(this.endTurnIntervalID)
const record = CreateGameRecord(this.gameID, this.gameConfig, this.turns, this.startedAt, Date.now())
// For unload events, sendBeacon is the only reliable method
const blob = new Blob([JSON.stringify(GameRecordSchema.parse(record))], {
type: 'application/json'
});
navigator.sendBeacon('/archive_singleplayer_game', blob);
}
}
+3 -4
View File
@@ -29,11 +29,10 @@ class Client {
if (!this.usernameInput) {
console.warn('Username input element not found');
}
const s = this.stopGame
window.addEventListener('beforeunload', function (event) {
window.addEventListener('beforeunload', (event) => {
console.log('Browser is closing');
if (s != null) {
s()
if (this.gameStop != null) {
this.gameStop()
}
});
+1 -1
View File
@@ -97,7 +97,7 @@ export class PublicLobby extends LitElement {
@click=${() => this.lobbyClicked(lobby)}
class="lobby-button ${this.isLobbyHighlighted ? 'highlighted' : ''}"
>
<div class="lobby-name">Game ${lobby.id.substring(0, 3)}</div>
<div class="lobby-name">Game ${lobby.id}</div>
<div class="lobby-timer">Starts in: ${timeRemaining}s</div>
<div class="player-count">Players: ${lobby.numClients}</div>
</button>
+2 -1
View File
@@ -1,6 +1,7 @@
import { LitElement, html, css } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { Difficulty, GameMap, GameType } from '../core/game/Game';
import { generateGameID as generateGameID } from '../core/Util';
@customElement('single-player-modal')
export class SinglePlayerModal extends LitElement {
@@ -127,7 +128,7 @@ export class SinglePlayerModal extends LitElement {
detail: {
gameType: GameType.Singleplayer,
lobby: {
id: "LOCAL",
id: generateGameID(),
},
map: this.selectedMap,
difficulty: this.selectedDifficulty
+4 -2
View File
@@ -1,7 +1,7 @@
import { Config } from "../core/configuration/Config"
import { EventBus, GameEvent } from "../core/EventBus"
import { AllianceRequest, AllPlayers, Cell, Player, PlayerID, PlayerType, Tile, UnitType } from "../core/game/Game"
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, GameID, Intent, ServerMessage, ServerMessageSchema, ClientPingMessageSchema } from "../core/Schemas"
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, GameID, Intent, ServerMessage, ServerMessageSchema, ClientPingMessageSchema, GameConfig } from "../core/Schemas"
import { LocalServer } from "./LocalServer"
@@ -99,6 +99,7 @@ export class Transport {
constructor(
private isLocal: boolean,
private gameConfig: GameConfig | null,
private eventBus: EventBus,
private gameID: GameID,
private clientIP: string | null,
@@ -150,7 +151,7 @@ export class Transport {
}
private connectLocal(onconnect: () => void, onmessage: (message: ServerMessage) => void) {
this.localServer = new LocalServer(this.config, onconnect, onmessage)
this.localServer = new LocalServer(this.config, this.gameConfig, onconnect, onmessage)
this.localServer.start()
}
@@ -208,6 +209,7 @@ export class Transport {
leaveGame() {
if (this.isLocal) {
this.localServer.endGame()
return
}
this.stopPing()