Is there a proper way to end a game using a custom endpoint? #959
-
I have a case where the game has to end if a player runs out of time for a move. In case the player disconnects from the game and then times out, I want the game to end. Should I just setState and add 'gameover' to ctx? Is there a better way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@kumarajith Do you mean call One pattern that might work would be something like this: import { INVALID_MOVE } from 'boardgame.io/core';
const timeoutDuration = 60_000; // or whatever makes sense for the game
const game = {
turn: {
onMove: (G, ctx) => {
// When a player moves, store the time of the move.
// You could also reset this when the turn starts or wherever it makes sense.
G.timeAtLastMove = Date.now();
},
},
moves: {
// Move that checks if the last move was too far in the past and ends the game if so.
requestTimeout: (G, ctx) => {
const timeSinceLastMove = Date.now() - G.timeAtLastMove;
if (timeSinceLastMove > timeoutDuration) {
ctx.endGame(/* value to set as ctx.gameover */);
} else {
// If the game hasn’t timed out, declare this move invalid, so state remains unchanged.
return INVALID_MOVE;
}
},
},
}; The tricky part is how you call the |
Beta Was this translation helpful? Give feedback.
@kumarajith Do you mean call
setState
directly on the database? The drawback with that is that it won’t update clients — database state isn’t reactive by itself, it only gets broadcast to clients when changed by moves/events/etc. in the game.One pattern that might work would be something like this: