From 18780e63161ecf2ef9ede08cf174fe07d4f23323 Mon Sep 17 00:00:00 2001 From: Pi Lanningham Date: Sat, 14 Dec 2024 03:10:26 -0500 Subject: [PATCH] Use onConnect and onDisconnect Try harder to make sure the server is healthy before starting a game --- referee/referee.ts | 35 +++++++++++++++++++++---- src/utils/HydraMultiplayer/base.ts | 6 ++++- src/utils/HydraMultiplayer/dedicated.ts | 6 ++++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/referee/referee.ts b/referee/referee.ts index ce8471f..c1b1013 100644 --- a/referee/referee.ts +++ b/referee/referee.ts @@ -55,18 +55,19 @@ async function sendEvent(gameId, data) { } async function reportResults(gameId, results) { + console.log( + `Reporting results for game ${gameId}\n`, + JSON.stringify(results, null, 2), + ); + let now = Date.now(); for (let i = 0; i < 10; i++) { - console.log( - `Reporting results for game ${gameId}\n`, - JSON.stringify(results, null, 2), - ); try { console.log("Sending to dynamodb"); await dynamo.send( new PutItemCommand({ TableName: "doom-game-results", Item: { - pk: { S: `${gameId}-${Date.now()}` }, + pk: { S: `${gameId}-${now}` }, results: { S: JSON.stringify(results) }, }, }), @@ -202,15 +203,39 @@ const module = await createModule({ }); global.Module = module; +let connected = false; const hydra = new HydraMultiplayerDedicated({ key: keys, address: keys.address, url: HYDRA_NODE, module, networkId: NETWORK_ID, + onConnect: () => { + console.log("Connected to Hydra"); + connected = true; + }, + onDisconnect: async () => { + console.log("Abruptly disconnected from Hydra, restarting"); + if (hydra.gameId) { + try { + await reportResults(hydra.gameId, { + gameId: hydra.gameId, + result: "error", + }) + } catch(e) { + + } + } + connected = false; + process.exit(1); + } }); global.HydraMultiplayer = hydra; +while(!connected) { + await new Promise((resolve) => setTimeout(resolve, 500)); +} + let expectedHumans = 0; let gameId = ""; type Player = { diff --git a/src/utils/HydraMultiplayer/base.ts b/src/utils/HydraMultiplayer/base.ts index 166e61c..62870a0 100644 --- a/src/utils/HydraMultiplayer/base.ts +++ b/src/utils/HydraMultiplayer/base.ts @@ -37,18 +37,22 @@ export abstract class HydraMultiplayer { filterAddress, module, networkId = 0, + onConnect, + onDisconnect, }: { key: Keys; url: string; module: EmscriptenModule; filterAddress?: string; networkId?: number; + onConnect?: () => void; + onDisconnect?: () => void; }) { this.key = key; this.module = module; this.networkId = networkId; - this.hydra = new Hydra(url, filterAddress, 100); + this.hydra = new Hydra(url, filterAddress, onConnect, onDisconnect, 100); this.hydra.onTxSeen = this.observeTx.bind(this); this.SendPacket = this.SendPacket.bind(this); diff --git a/src/utils/HydraMultiplayer/dedicated.ts b/src/utils/HydraMultiplayer/dedicated.ts index 2a53ed5..7a3f104 100644 --- a/src/utils/HydraMultiplayer/dedicated.ts +++ b/src/utils/HydraMultiplayer/dedicated.ts @@ -21,14 +21,18 @@ export class HydraMultiplayerDedicated extends HydraMultiplayer { url, module, networkId, + onConnect, + onDisconnect, }: { key: Keys; address: string; url: string; module: EmscriptenModule; networkId?: number; + onConnect?: () => void; + onDisconnect?: () => void; }) { - super({ key, url, module, networkId }); + super({ key, url, module, networkId, onConnect, onDisconnect }); this.address = address; this.onPacket = this.trackKills.bind(this); this.clients = {};