-
Notifications
You must be signed in to change notification settings - Fork 43
Client updates
Client updating correspond to the process of keeping the connected clients up-to-date regarding the state of the game world and the entities that inhabit it.
The main method responsible for updating the clients is GameServer.updateClients()
, which is called multiple times per second (the rate is set in the config files).
During each call of that method, an update package is sent to each client, to be consumed in the Client.socket.on('update')
callback. An update packet is simply a object mappings some keys to values in a way that is expected by the client. A very small example is the following:
{
local: {
gold: 200,
hp: 100,
items: [...]
},
global: {
animals: [...],
buildings: [...]
}
}
Each update package contains two parts:
- The local part, which contains personal information about the player receiving the update (e.g. change in amount of gold owned, new item owned, ...)
- The global part, which contains information about the world (e.g. position of some NPC, status of a region...)
Each Player
instance maintains it's own instance of a PersonalUpdatePacket
(stored in Player.updatePacket
). This packet is cleared after each update. In-between updates, it is populated by any player-specific information that needs to be sent to the player. This is done by Player.setOwnProperty()
, which is a method that makes sure that a change to the Player
object is recorded in its PersonalUpdatePacket
(note that many server-side changes don't need to be sent to the clients and therefore are not included in the update packets).
Each AOI in the game also maintains its own UpdatePacket
(which is a different class than the PersonalUpdatePacket
mentioned above). Similarly, these packets are cleared after each update. The update packet of an AOI is updated whenever something happens in that AOI or in one of the 9 neighboring AOIs. This allows a player located in a given AOI to be informed about what happens in all the surrounding AOIs as well. (It also means that all players don't get the same global packet, it depends on where they are located).
AOI update packets are populated when the GameObject.setProperty()
method is called. Similarly to the Player.setOwnProperty()
above, it makes sure that a change to some GameObject
(which is the parent class of all other game entities: players, NPC, items...) is recorded in its AOI's UpdatePacket
.
Inside GameServer.updateClients()
, a copy of the global packet is created and slightly modified and cleaned up before being combined with the local packet and sent to the player.