From 42dde4ce44524a3f2833a4d3f1a2ac5c18546cde Mon Sep 17 00:00:00 2001 From: cloudwithax Date: Tue, 21 Dec 2021 20:34:06 -0500 Subject: [PATCH] Add new node algo, NodeAlgorithm.by_players --- pomice/enums.py | 4 ++++ pomice/pool.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/pomice/enums.py b/pomice/enums.py index b7e3d22..343139f 100644 --- a/pomice/enums.py +++ b/pomice/enums.py @@ -35,11 +35,15 @@ class NodeAlgorithm(Enum): 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. 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 by_ping = auto() by_region = auto() + by_players = auto() def __str__(self) -> str: return self.value \ No newline at end of file diff --git a/pomice/pool.py b/pomice/pool.py index d5a6e94..f388560 100644 --- a/pomice/pool.py +++ b/pomice/pool.py @@ -466,6 +466,10 @@ class NodePool: Use NodeAlgorithm.by_region if you want to get the best node based on the node's voice region. This method will only work 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] @@ -475,6 +479,10 @@ class NodePool: if algorithm == NodeAlgorithm.by_ping: tested_nodes = {node: node.latency for node in available_nodes} 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: if voice_region == None: