I have a multiplayer game lobby up where users can create private chatrooms and start private games. Each user has a health bar in the game that is suppose to slowly regenerate x points per second.
I suppose I would need to start server side game loop at the beginning of each game, which is something like that:
setInterval(update('gameID'),1000);
Where update('gameID') increment the health variables for all players in a particular game where 1000 ms = 1 second.
Question: Am I right to assume this is asynchronous? I might have 50 separate games going on, and 50 of these running. The main process is not going to be blocked right?
It's asynchronous, but you don't need 50 timers in the case you describe.
player.attackedTime = (new Date).getTime()
and calculate regeneration on each attack like player.health += x_points * ((new Date).getTime() - player.attackedTime) / 1000
, but you will have to do predictive regeneration on the client.setInterval
is certainly asynchronous. Most functions that take a callback are asynchronous. If you're ever in doubt, you can check the documentation or the source code.
It is asynchronous. But doing this that way may kill your server.
I advice making these intervals passive, i.e. hold the start of the game in memory and make client ping for data. When client pings server checks current date and compares it to the stored one (and updates the stored one at the end of request). It can evaluate current health from that.
This solution should scale better.