Add new node algo, NodeAlgorithm.by_players

This commit is contained in:
cloudwithax 2021-12-21 20:34:06 -05:00
parent 3ef98104ce
commit 42dde4ce44
2 changed files with 12 additions and 0 deletions

View File

@ -35,11 +35,15 @@ class NodeAlgorithm(Enum):
NodeAlgorithm.by_region returns a node based on its voice region, NodeAlgorithm.by_region returns a node based on its voice region,
which the region is specified by the user in the method as an arg. which the region is specified by the user in the method as an arg.
This method will only work if you set a voice region when you create a node. This method will only work if you set a voice region when you create a node.
NodeAlgorithm.by_players return a nodes based on how many players it has.
This algorithm prefers nodes with the least amount of players.
""" """
# We don't have to define anything special for these, since these just serve as flags # We don't have to define anything special for these, since these just serve as flags
by_ping = auto() by_ping = auto()
by_region = auto() by_region = auto()
by_players = auto()
def __str__(self) -> str: def __str__(self) -> str:
return self.value return self.value

View File

@ -466,6 +466,10 @@ class NodePool:
Use NodeAlgorithm.by_region if you want to get the best node Use NodeAlgorithm.by_region if you want to get the best node
based on the node's voice region. This method will only work based on the node's voice region. This method will only work
if you set a voice region when you create a node. if you set a voice region when you create a node.
Use NodeAlgorithm.by_players if you want to get the best node
based on how players it has. This method will return a node with
the least amount of players
""" """
available_nodes = [node for node in cls._nodes.values() if node._available] available_nodes = [node for node in cls._nodes.values() if node._available]
@ -476,6 +480,10 @@ class NodePool:
tested_nodes = {node: node.latency for node in available_nodes} tested_nodes = {node: node.latency for node in available_nodes}
return min(tested_nodes, key=tested_nodes.get) return min(tested_nodes, key=tested_nodes.get)
elif algorithm == NodeAlgorithm.by_players:
tested_nodes = {node: len(node.players.keys()) for node in available_nodes}
return min(tested_nodes, key=tested_nodes.get)
else: else:
if voice_region == None: if voice_region == None:
raise NodeException("You must specify a VoiceRegion in order to use this functionality.") raise NodeException("You must specify a VoiceRegion in order to use this functionality.")