From 5a3e05b1c09ebfd7b2f1f9c518eb6976135cfda5 Mon Sep 17 00:00:00 2001 From: Kirill Riman <55841348+K-ETFreeman@users.noreply.github.com> Date: Wed, 25 Dec 2024 16:43:28 +0300 Subject: [PATCH] linter fixes + players.py bugfix --- server/ladder_service/ladder_service.py | 26 +++++++++++++------------ server/lobbyconnection.py | 2 +- server/matchmaker/map_pool.py | 8 +++----- server/players.py | 22 +++++++++++---------- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/server/ladder_service/ladder_service.py b/server/ladder_service/ladder_service.py index efce323d9..7f3b5d7ed 100644 --- a/server/ladder_service/ladder_service.py +++ b/server/ladder_service/ladder_service.py @@ -117,8 +117,8 @@ async def update_data(self) -> None: MapPool(map_pool_id, map_pool_name, map_list), min_rating, max_rating, - veto_tokens_per_player, - max_tokens_per_map, + veto_tokens_per_player, + max_tokens_per_map, minimum_maps_after_veto ) # Remove queues that don't exist anymore @@ -544,11 +544,11 @@ def get_displayed_rating(player: Player) -> float: if not pool: raise RuntimeError(f"No map pool available for rating {rating}!") - pool, _, _, veto_tokens_per_player, max_tokens_per_map, minimum_maps_after_veto = queue.map_pools[pool.id] + pool, _, _, _, max_tokens_per_map, minimum_maps_after_veto = queue.map_pools[pool.id] vetoesMap = defaultdict(int) - for (id, map) in pool.maps.items(): + for map in pool.maps.values(): for player in all_players: vetoesMap[map.map_pool_map_version_id] += player.vetoes.get(map.map_pool_map_version_id, 0) @@ -713,27 +713,29 @@ def calculate_dynamic_tokens_per_map(self, M: float, tokens: list[int]) -> float sorted_tokens = sorted(tokens) if (sorted_tokens.count(0) >= M): return 1 - - result = 1; last = 0; index = 0 - while (index < len(sorted_tokens)): + + result = 1 + last = 0 + index = 0 + while (index < len(sorted_tokens)): (index, last) = next(((i, el) for i, el in enumerate(sorted_tokens) if el > last), (len(sorted_tokens) - 1, sorted_tokens[-1])) index += 1 divider = index - M - if (divider <= 0): + if (divider <= 0): continue result = sum(sorted_tokens[:index]) / divider - upperLimit = sorted_tokens[index] if index < len(sorted_tokens) else float('inf') + upperLimit = sorted_tokens[index] if index < len(sorted_tokens) else float("inf") if (result <= upperLimit): return result - + return 0 def get_pools_veto_data(self) -> list[tuple[list[int], int, int]]: result = [] for queue in self.queues.values(): - for map_pool, min_rating, max_rating, veto_tokens_per_player, max_tokens_per_map, minimum_maps_after_veto in queue.map_pools.values(): - result.append(([map.map_pool_map_version_id for map in map_pool.maps.values()], veto_tokens_per_player, max_tokens_per_map)) + for pool, _, _, veto_tokens_per_player, max_tokens_per_map, _ in queue.map_pools.values(): + result.append(([map.map_pool_map_version_id for map in pool.maps.values()], veto_tokens_per_player, max_tokens_per_map)) return result async def get_game_history( diff --git a/server/lobbyconnection.py b/server/lobbyconnection.py index 4bb299ad2..e0919211c 100644 --- a/server/lobbyconnection.py +++ b/server/lobbyconnection.py @@ -1316,7 +1316,7 @@ async def command_set_party_factions(self, message): self.party_service.set_factions(self.player, list(factions)) async def command_set_player_vetoes(self, message): - converted = {v['map_pool_map_version_id']: v['veto_tokens_applied'] for v in message["vetoes"]} + converted = {v["map_pool_map_version_id"]: v["veto_tokens_applied"] for v in message["vetoes"]} await self.player.update_vetoes(self.ladder_service.get_pools_veto_data(), converted) async def send_warning(self, message: str, fatal: bool = False): diff --git a/server/matchmaker/map_pool.py b/server/matchmaker/map_pool.py index 31c857064..b34641eff 100644 --- a/server/matchmaker/map_pool.py +++ b/server/matchmaker/map_pool.py @@ -21,7 +21,7 @@ def __init__( def set_maps(self, maps: Iterable[MapPoolMap]) -> None: self.maps = {map_.id: map_ for map_ in maps} - def choose_map(self, played_map_ids: Iterable[int] = (), vetoesMap = None, max_tokens_per_map = 1) -> Map: + def choose_map(self, played_map_ids: Iterable[int]=(), vetoesMap=None, max_tokens_per_map=1) -> Map: if vetoesMap is None: vetoesMap = {} """ @@ -53,15 +53,13 @@ def choose_map(self, played_map_ids: Iterable[int] = (), vetoesMap = None, max_t break - least_common_ids = {id_ for id_, _ in least_common} - #multiply weight by 2 if map is least common and not vetoed by anyone + # Multiply weight by 2 if map is least common and not vetoed by anyone mapList = list((map.map_pool_map_version_id, map, 2 if (map.id in least_common_ids) and (vetoesMap.get(map.map_pool_map_version_id,0) == 0) else 1) for id, map in self.maps.items()) weights = [max(0, (1 - vetoesMap.get(id, 0) / max_tokens_per_map) * map.weight * least_common_multiplier) for id, map, least_common_multiplier in mapList] - map = random.choices(mapList, weights=weights, k=1)[0][1] - return map + return random.choices(mapList, weights=weights, k=1)[0][1] def __repr__(self) -> str: return f"MapPool({self.id}, {self.name}, {list(self.maps.values())})" diff --git a/server/players.py b/server/players.py index 3f686a654..5c9406798 100644 --- a/server/players.py +++ b/server/players.py @@ -100,24 +100,26 @@ def vetoes(self, value: dict[int, int]) -> None: raise ValueError("Vetoes must be a dictionary") if not all(isinstance(key, int) and isinstance(val, int) and val >= 0 for key, val in value.items()): raise ValueError("Incorrect vetoes dictonary") - self._vetoes = value + self._vetoes = value async def update_vetoes(self, pools_vetodata: list[tuple[list[int], int, int]], current: dict = None) -> None: if current is None: current = self.vetoes fixedVetoes = {} - vetoDatas = [] + vetoDatas = [] for (map_pool_map_version_ids, veto_tokens_per_player, max_tokens_per_map) in pools_vetodata: - sum = 0 - for id in map_pool_map_version_ids: - new_tokens_applied = max(current.get(id, 0),0) - if (sum + new_tokens_applied > veto_tokens_per_player or (max_tokens_per_map > 0 and new_tokens_applied > max_tokens_per_map)): - new_tokens_applied = min(veto_tokens_per_player - sum, max_tokens_per_map) + tokens_sum = 0 + for map_id in map_pool_map_version_ids: + new_tokens_applied = max(current.get(map_id, 0),0) + if (tokens_sum + new_tokens_applied > veto_tokens_per_player): + new_tokens_applied = veto_tokens_per_player - tokens_sum + if (max_tokens_per_map > 0 and new_tokens_applied > max_tokens_per_map): + new_tokens_applied = max_tokens_per_map if (new_tokens_applied == 0): continue - vetoDatas.append({"map_pool_map_version_id": id, "veto_tokens_applied": new_tokens_applied}) - fixedVetoes[id] = new_tokens_applied - sum += new_tokens_applied + vetoDatas.append({"map_pool_map_version_id": map_id, "veto_tokens_applied": new_tokens_applied}) + fixedVetoes[map_id] = new_tokens_applied + tokens_sum += new_tokens_applied if fixedVetoes == self.vetoes == current: return self.vetoes = fixedVetoes