Answer the question
In order to leave comments, you need to log in
Game server on node.js, store lists of players and rooms in object or array?
Suppose there is some game.js containing a list of player.js class objects responsible for communicating with connected sockets (each contains a socket and a set of utility methods), as well as a list of room.js class objects responsible for game lobbies and sessions. The toy is conditionally step-by-step with the use of active time battle (ATB) - the right to move is given to the player when his progress bar is filled.
Store the list of rooms and connected players in an object or in an array? The question is from the point of view of the speed of access to the desired instance: the object can be accessed by key (the unique id of the player) directly, but the enumeration in the array is faster. Sometimes you need to refer to a specific instance, sometimes to several. Sometimes a situation may arise when it is necessary to signal a signal to all objects in the list (hence the question about array or object), an example is in the next paragraph.
The second question follows from the first. It is required to implement a certain game loop for all game "rooms" - to fill the ATB scale with it. I suppose to enumerate the entire list of rooms every 0.1-0.2 seconds and charge them this "step". Connected to the server, but not currently in a combat session, you need to restore health indicators every 3-5 seconds. Again, it begs to iterate through the array of player.js instances with a check for the condition "not in the game session".
Is this implementation correct? The number of running "rooms" can amount to several hundred with good online, the number of players can be in the thousands.
Thanks for any tips and hints.
Answer the question
In order to leave comments, you need to log in
I'll start with the second question - don't you think that there is no need to update the indicator every hundredth of a second? You can just do on the server, like, a model of rooms, in which
there will be a flag, a certain coefficient that will be taken in the calculations. And at the beginning of this event,
send a message to the clients so that the views reduce this or that
indicator on their own. And if someone cheats their application, then this will not help him, since
the correct value is known on the server anyway by the coefficient.
And in the first place, you need to test which operations will cost more.
Otherwise, you can generally make a collection based on an array for iteration + an index hash
by key.
With such volumes - there will be practically no difference. ATB recovery can be done through events - you generate an event on a timer - addATBPoints and pass as a parameter how much to restore, each player subscribes to this event - and processes it when it occurs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question