I'm writing an express/socket.io-powered game server for a web game. I have a central map of game state objects, each representing an ongoing match, like so:
// map of gameId -> game (primary holding place of games)
const activeGames = new Map<string, ActiveGame>();
Each game is an object like so:
{
players: {
player1: {
cardsInHand: [ ... ],
cardsInPlay: [ ... ],
cardsInLibrary: [ ... ]
},
player2: { ... }
}
My plan is for the server endpoints to modify these arrays in-memory and send out updated game state to all connected clients with each change. I am wondering, however, if in the long run there is some danger here in case I ever have functionality that lets one player modify another player's cards, for example removing one from play. Then multiple connections could be modifying the same aspect of a game object at the same instant.
Is it wise or necessary to set up a queue and add state changes to this queue to ensure they are done one after another rather than potentially having multiple connections making concurrent calls like this one?
game.players.player1.cardsInHand = newCardsInHand;
If two different connections ran that at once, with different values for newCardsInHand
, my state logic could break down.
How is something like this normally handled? Is this premature optimization?