Search code examples
node.jssocketsredisgame-loop

What is the proper way to check game state and send sockets to connected clients?


I'am building a social multi-player real time game and I am considering what architecture to implement in order to keep track of state game and send sockets with correct one to connected players.

My game has 4 states round start, check players, matching state, round result. These states have timeout 10 sec and are repeatedly loops while room has players.

My state looks like the following:

var stateDoc = {
  type: 'round start',
  uid: uid,
  timeout: 10000
};

My question is how should I store states and update these ones?


Solution

    1. you can have a json data called game. You can store it as binary or as json in a redis list. If you pass it as json, you can perform a toJson before and fromJson after.

      //number of games LLEN games

      //read game #0
      LINDEX games 0

      //write game at the end of list
      RPUSH games "{...}" RPUSH games.roundstart 1

    2. you should better use 5 lists : one for every state & one with all games The list for a status (games.roundstart for example) will only store the key to a game of this status.

    3. The update logic should be stored server side (and not in redis) On the update process, you will lookup for every list and perform the actions depending of this lists.