Skip to content

Commit

Permalink
chore: add timestamp to pk in dynamo
Browse files Browse the repository at this point in the history
  • Loading branch information
yHSJ committed Dec 12, 2024
1 parent c7fa64a commit 8836589
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions referee/referee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,34 @@ async function sendEvent(gameId, data) {
}

async function reportResults(gameId, results) {
for(let i = 0; i < 10; i++) {
console.log(`Reporting results for game ${gameId}\n`, JSON.stringify(results, null, 2));
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 },
results: { S: JSON.stringify(results) },
},
}));
await dynamo.send(
new PutItemCommand({
TableName: "doom-game-results",
Item: {
pk: { S: `${gameId}-${Date.now()}` },
results: { S: JSON.stringify(results) },
},
}),
);
console.log("Sending to discord bot");
let resp = await fetch(DISCORD_BOT, {
method: "POST",
body: JSON.stringify(results),
});
if (resp.status !== 200) {
throw new Error(resp.statusText + ": " + await resp.text());
throw new Error(resp.statusText + ": " + (await resp.text()));
} else {
console.log("Success!");
break;
}
} catch(e) {
} catch (e) {
console.warn("Failed to report results, retrying: ", e);
}
}
Expand Down Expand Up @@ -110,9 +115,8 @@ const keys = {
};
console.log("Address: ", keys.address);


// check for extraneous state utxos and cleanup
while(true) {
while (true) {
try {
console.log("Checking head's utxo set for stale games...");
const response = await fetch(`${HYDRA_NODE}snapshot/utxo`);
Expand Down Expand Up @@ -238,8 +242,10 @@ global.playerConnected = async (addr: number, player: number) => {
};
global.playerDisconnected = async (addr: number, player: number) => {
console.log(`Someone disconnected, ending the game`);
let [player1Key, player1] = Object.entries(players).find(([_, p]) => p.playerNumber === 0) ?? [];
let [player2Key, player2] = Object.entries(players).find(([_, p]) => p.playerNumber === 1) ?? [];
let [player1Key, player1] =
Object.entries(players).find(([_, p]) => p.playerNumber === 0) ?? [];
let [player2Key, player2] =
Object.entries(players).find(([_, p]) => p.playerNumber === 1) ?? [];
await reportResults(gameId, {
gameId: gameId,
result: "disconnect",
Expand All @@ -250,8 +256,8 @@ global.playerDisconnected = async (addr: number, player: number) => {
playerTwo: {
pkh: player2?.ephemeralKey,
kills: hydra.clients[player2Key]?.kills[1],
}
})
},
});
done = true;
if (!RECORD_STATS) return;
try {
Expand All @@ -261,7 +267,6 @@ global.playerDisconnected = async (addr: number, player: number) => {
}
};


// Log a new game or player joined transaction if we see it
let timeout = 60_000;
let timer = 15 * 60 * 1000; // 15 minute timer
Expand All @@ -284,9 +289,7 @@ hydra.onPacket = (_tx: any, packet: Packet) => {
}
};
hydra.onNewGame = async (newGameId, humanCount, _botCount, ephemeralKey) => {
console.log(
`Observed new game: ${newGameId}, by ${ephemeralKey}`,
);
console.log(`Observed new game: ${newGameId}, by ${ephemeralKey}`);
gameId = newGameId;
expectedHumans = humanCount;
await sendEvent(gameId, {
Expand Down Expand Up @@ -358,8 +361,10 @@ while (!done) {
timer -= 1000;
if (timer <= 0) {
console.log("Game ended.");
let [player1Key, player1] = Object.entries(players).find(([_, p]) => p.playerNumber === 0) ?? [];
let [player2Key, player2] = Object.entries(players).find(([_, p]) => p.playerNumber === 1) ?? [];
let [player1Key, player1] =
Object.entries(players).find(([_, p]) => p.playerNumber === 0) ?? [];
let [player2Key, player2] =
Object.entries(players).find(([_, p]) => p.playerNumber === 1) ?? [];
await reportResults(gameId, {
gameId: gameId,
result: "finished",
Expand All @@ -370,14 +375,16 @@ while (!done) {
playerTwo: {
pkh: player2?.ephemeralKey,
kills: hydra.clients[player2Key]?.kills[1],
}
},
});
done = true;
}
if (timeout <= 0) {
console.log("Game timed out.");
let [player1Key, player1] = Object.entries(players).find(([_, p]) => p.playerNumber === 0) ?? [];
let [player2Key, player2] = Object.entries(players).find(([_, p]) => p.playerNumber === 1) ?? [];
let [player1Key, player1] =
Object.entries(players).find(([_, p]) => p.playerNumber === 0) ?? [];
let [player2Key, player2] =
Object.entries(players).find(([_, p]) => p.playerNumber === 1) ?? [];
await reportResults(gameId, {
gameId: gameId,
result: "timeout",
Expand All @@ -388,7 +395,7 @@ while (!done) {
playerTwo: {
pkh: player2?.ephemeralKey,
kills: hydra.clients[player2Key]?.kills[1],
}
},
});
done = true;
}
Expand Down

0 comments on commit 8836589

Please sign in to comment.