bestNode property

Node bestNode

Get the best available node, it is recommended to use getOrCreatePlayerNode instead as this won't create the player itself if it doesn't exists

Implementation

Node get bestNode {
  if (this._nodes.isEmpty) {
    throw ClusterException._new("No available nodes");
  }
  if (this._nodes.length == 1) {
    return this._nodes.values.first;
  }

  /// Node id of the node who has fewer players
  int? minNodeId;
  /// Number of players the node has
  int? minNodePlayers;

  this._nodes.forEach((id, node) {
    if (minNodeId == null && minNodePlayers == null) {
      minNodeId = id;
      minNodePlayers = node._players.length;
    } else {
      if (node._players.length < minNodePlayers!) {
        minNodeId = id;
        minNodePlayers = node._players.length;
      }
    }
  });

  return this._nodes[minNodeId]!;
}