-
Notifications
You must be signed in to change notification settings - Fork 18
PlayerList v7
- status: complete
- version: 7.x
- follows from: Matching: Roles and Partners
The player list is a special object containing the list of all the
clients currently connected to a game room. It is accessible as a property of the node.game
object.
node.game.playerList;
node.game.pl; // Shorthand alias for playerList.
For security reason, by default only the logic client type receive player list updates, while player client types have an empty player list.
To change this behavior edit the notify
property inside the channel configuration file.
The player list is dynamic in size: when a player disconnects it gets removed from the player list and it gets added again upon reconnection.
node.game.pl.size(); // 5 players.
// One player disconnects.
node.game.pl.size(); // 4 players.
// Player reconnects.
node.game.pl.size(); // 5 players again.
When a new player connects (or reconnects) the following listeners are fired:
// New connection.
node.on.pconnect(function(player) {
console.log('A new player connected to the game: ' + p.id);
});
// Reconnection.
node.on.preconnect(function(player) {
console.log('A previously disconnected player reconnected to the game: ' + p.id);
});
The player list of the logic is pre-filled with the all players dispatched by the waiting room. This means that for this initial group of players, the listener pconnect is not fired.
You can get an array of all ids using querying the id
index.
var ids = node.game.pl.id.getAllKeys();
// [ 'id1', 'id2', 'id3' ]
It is also possible to cycle through all connected players in a game, for example to send a message to every player.
node.game.pl.each(function(player) {
node.say('hello!', player.id);
});
In a two-player game, it can be handy to access the first and last player directly:
var player1 = node.game.pl.first();
var player2 = node.game.pl.last();
// Get the ids of the players.
var id1 = player1.id;
var id2 = player2.id;
The player list contains objects of type Player
, which can be
accessed if the id is of the player is known.
var player;
if (node.game.pl.exist('some-player-id') {
player = node.game.pl.get('some-player-id');
console.log(player);
// Player {
// id: "some-player-id",
// sid: "ZmjmT_A6VQhTpY2CAAAA",
// clientType: null,
// stage: { stage: 1, step: 2, round: 1 },
// stageLevel: 50,
// stateLevel: 1,
// admin: false,
// clientType: 'player',
// count: 3,
// lang: {
// name: "English",
// shortName: "en",
// nativeName: "English",
// path: "en/"
// },
// disconnected: false,
// // Properties reserved for future use.
// group: null,
// ip: null,
// name: null,
// role: null,
// __proto__: Player
// };
}
The player list object is an instance of the NDDB database, so all the NDDB methods are available to the player list as well.
Go back to the wiki Home.
Copyright (C) 2021 Stefano Balietti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.