Database – Notify players every x seconds in a multiplayer game

databasedjangogame developmentpython

I'm working on a realtime multiplayer game using Django and gevent-socketio, I'm facing some issues:

I need to send an update of the game state to connected players every X seconds (~4 seconds), so basically I need to execute a function every x seconds which does some calculations and sends the connected players the new game state. What is the easiest way to achieve this? is using cron a good choice here or should I look for other tools like celery or …?

Also, the function needs to operate on some data. querying the database every time the function is executed does not seems to be a good idea, where can I store the game data (which is updated frequently during the game)?

Best Answer

Regarding your first question:
If the type of game allows it, I would advice a round-robin strategy. You send the game state in equal intervals you obtain by dividing the maximum time between updates by the number of players that are connected, and send the state as a series of updates to the single connected players.
Like that you avoid performance issues due to CPU spikes, as mentioned in some of the comments to @Kolyunya's answer.

As an aside this strategy allows the following optimisation:
Adapting the maximum time between updates to the number of players that are connected at a given time:
If the number of players is low you can set the maximum update time low as well, so the few players can have faster updates.

For the implementation I second @Brian's tip to see if this strategy suits your needs.

Regarding your second question:
You should keep your game state in memory, redis would be a obvious choice.

Related Topic