Skip to content

Commit

Permalink
Use onConnect and onDisconnect
Browse files Browse the repository at this point in the history
Try harder to make sure the server is healthy before starting a game
  • Loading branch information
Quantumplation committed Dec 14, 2024
1 parent 99550fe commit 18780e6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
35 changes: 30 additions & 5 deletions referee/referee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
},
}),
Expand Down Expand Up @@ -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 = {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/HydraMultiplayer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/utils/HydraMultiplayer/dedicated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down

0 comments on commit 18780e6

Please sign in to comment.