snapshot_poker_phase3
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
const {
|
||||
getPokerService
|
||||
} = require("../utils/crumbPoker");
|
||||
|
||||
const {
|
||||
refreshPokerTableMessage
|
||||
} = require("../utils/pokerDiscord");
|
||||
|
||||
|
||||
const TURN_CHECK_INTERVAL =
|
||||
5 * 1000;
|
||||
|
||||
const LOBBY_CHECK_INTERVAL =
|
||||
30 * 1000;
|
||||
|
||||
const STARTUP_RECOVERY_DELAY =
|
||||
10 * 1000;
|
||||
|
||||
|
||||
module.exports = client => {
|
||||
setTimeout(
|
||||
() => {
|
||||
recoverPoker(
|
||||
client
|
||||
);
|
||||
},
|
||||
STARTUP_RECOVERY_DELAY
|
||||
);
|
||||
|
||||
setInterval(
|
||||
() => {
|
||||
processPokerTurns(
|
||||
client
|
||||
);
|
||||
},
|
||||
TURN_CHECK_INTERVAL
|
||||
);
|
||||
|
||||
setInterval(
|
||||
() => {
|
||||
processPokerLobbies(
|
||||
client
|
||||
);
|
||||
},
|
||||
LOBBY_CHECK_INTERVAL
|
||||
);
|
||||
|
||||
console.log(
|
||||
"[Poker] Recovery and timeout watcher initialized"
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
async function refreshResultGame(
|
||||
client,
|
||||
result
|
||||
) {
|
||||
const game =
|
||||
result?.game;
|
||||
|
||||
if (!game) {
|
||||
return;
|
||||
}
|
||||
|
||||
await refreshPokerTableMessage(
|
||||
client,
|
||||
game
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
async function processPokerTurns(
|
||||
client
|
||||
) {
|
||||
if (!client.isReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const results =
|
||||
await getPokerService()
|
||||
.processExpiredTurns();
|
||||
|
||||
for (const result of results) {
|
||||
if (result.processed) {
|
||||
console.log(
|
||||
`[Poker] Auto-folded ${result.userId} after turn timeout.`
|
||||
);
|
||||
|
||||
await refreshResultGame(
|
||||
client,
|
||||
result
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch (error) {
|
||||
console.error(
|
||||
"[Poker] Turn watcher error:",
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function processPokerLobbies(
|
||||
client
|
||||
) {
|
||||
if (!client.isReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const results =
|
||||
await getPokerService()
|
||||
.processExpiredLobbies();
|
||||
|
||||
for (const result of results) {
|
||||
if (result.expired) {
|
||||
console.log(
|
||||
`[Poker] Expired waiting Poker table ${result.game?.gameId ?? "unknown"}.`
|
||||
);
|
||||
|
||||
await refreshResultGame(
|
||||
client,
|
||||
result
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch (error) {
|
||||
console.error(
|
||||
"[Poker] Lobby watcher error:",
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function recoverPoker(
|
||||
client
|
||||
) {
|
||||
if (!client.isReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const service =
|
||||
getPokerService();
|
||||
|
||||
const result =
|
||||
await service.recover();
|
||||
|
||||
for (
|
||||
const turn
|
||||
of result.turns ?? []
|
||||
) {
|
||||
if (turn.processed) {
|
||||
await refreshResultGame(
|
||||
client,
|
||||
turn
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (
|
||||
const lobby
|
||||
of result.lobbies ?? []
|
||||
) {
|
||||
if (lobby.expired) {
|
||||
await refreshResultGame(
|
||||
client,
|
||||
lobby
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (
|
||||
const closing
|
||||
of result.closingTables ?? []
|
||||
) {
|
||||
await refreshResultGame(
|
||||
client,
|
||||
closing
|
||||
);
|
||||
}
|
||||
|
||||
for (
|
||||
const cashout
|
||||
of result.cashouts ?? []
|
||||
) {
|
||||
try {
|
||||
const game =
|
||||
await service.getTable({
|
||||
gameId:
|
||||
cashout.gameId
|
||||
});
|
||||
|
||||
await refreshPokerTableMessage(
|
||||
client,
|
||||
game
|
||||
);
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[Poker] Recovery complete: ` +
|
||||
`${result.memberships.length} membership(s), ` +
|
||||
`${result.cashouts.length} cash-out(s), ` +
|
||||
`${result.closingTables.length} closing table(s), ` +
|
||||
`${result.turns.length} expired turn(s), ` +
|
||||
`${result.lobbies.length} expired lobby/lobbies.`
|
||||
);
|
||||
}
|
||||
|
||||
catch (error) {
|
||||
console.error(
|
||||
"[Poker] Startup recovery error:",
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user