Skip to content

Commit

Permalink
linter fixes + players.py bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
K-ETFreeman committed Dec 25, 2024
1 parent 5933022 commit 5a3e05b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
26 changes: 14 additions & 12 deletions server/ladder_service/ladder_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -544,11 +544,11 @@ def get_displayed_rating(player: Player) -> float:
if not pool:
raise RuntimeError(f"No map pool available for rating {rating}!")

Check warning on line 546 in server/ladder_service/ladder_service.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace

Check notice on line 546 in server/ladder_service/ladder_service.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

server/ladder_service/ladder_service.py#L546

Trailing whitespace
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():

Check warning on line 551 in server/ladder_service/ladder_service.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

server/ladder_service/ladder_service.py#L551

Redefining built-in 'map'
for player in all_players:
vetoesMap[map.map_pool_map_version_id] += player.vetoes.get(map.map_pool_map_version_id, 0)

Expand Down Expand Up @@ -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)):

Check warning on line 716 in server/ladder_service/ladder_service.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
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

Check warning on line 726 in server/ladder_service/ladder_service.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
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

Check warning on line 731 in server/ladder_service/ladder_service.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
return 0

Check warning on line 733 in server/ladder_service/ladder_service.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
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(
Expand Down
2 changes: 1 addition & 1 deletion server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 3 additions & 5 deletions server/matchmaker/map_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Check failure on line 24 in server/matchmaker/map_pool.py

View workflow job for this annotation

GitHub Actions / flake8

missing whitespace around parameter equals

Check failure on line 24 in server/matchmaker/map_pool.py

View workflow job for this annotation

GitHub Actions / flake8

missing whitespace around parameter equals
if vetoesMap is None:
vetoesMap = {}
"""
Expand Down Expand Up @@ -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}

Check failure on line 56 in server/matchmaker/map_pool.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)

Check warning on line 56 in server/matchmaker/map_pool.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace

Check notice on line 56 in server/matchmaker/map_pool.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

server/matchmaker/map_pool.py#L56

Trailing whitespace

Check warning on line 57 in server/matchmaker/map_pool.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace

Check notice on line 57 in server/matchmaker/map_pool.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

server/matchmaker/map_pool.py#L57

Trailing whitespace
#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())

Check failure on line 59 in server/matchmaker/map_pool.py

View workflow job for this annotation

GitHub Actions / flake8

missing whitespace after ','

Check warning on line 60 in server/matchmaker/map_pool.py

View workflow job for this annotation

GitHub Actions / flake8

blank line contains whitespace
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]

Check warning on line 62 in server/matchmaker/map_pool.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

server/matchmaker/map_pool.py#L62

Standard pseudo-random generators are not suitable for security/cryptographic purposes.

def __repr__(self) -> str:
return f"MapPool({self.id}, {self.name}, {list(self.maps.values())})"
22 changes: 12 additions & 10 deletions server/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 113 in server/players.py

View workflow job for this annotation

GitHub Actions / flake8

missing whitespace after ','
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
Expand Down

0 comments on commit 5a3e05b

Please sign in to comment.