-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/#1032 working veto system #1033
base: develop
Are you sure you want to change the base?
Changes from 2 commits
b7db5ec
adcc746
5933022
5a3e05b
a2c42a5
3c77c0b
4ac5d49
5bfea4f
998f03c
dc8dc57
ec6a9f6
6f137fd
c050a3c
515125d
1b6a3b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,7 @@ | |
queue.team_size = info["team_size"] | ||
queue.rating_peak = await self.fetch_rating_peak(info["rating_type"]) | ||
queue.map_pools.clear() | ||
for map_pool_id, min_rating, max_rating in info["map_pools"]: | ||
for map_pool_id, min_rating, max_rating, veto_tokens_per_player, max_tokens_per_map, minimum_maps_after_veto in info["map_pools"]: | ||
map_pool_name, map_list = map_pool_maps[map_pool_id] | ||
if not map_list: | ||
self._logger.warning( | ||
|
@@ -112,7 +112,10 @@ | |
queue.add_map_pool( | ||
MapPool(map_pool_id, map_pool_name, map_list), | ||
min_rating, | ||
max_rating | ||
max_rating, | ||
veto_tokens_per_player, | ||
Check warning on line 116 in server/ladder_service/ladder_service.py GitHub Actions / flake8
|
||
max_tokens_per_map, | ||
Check warning on line 117 in server/ladder_service/ladder_service.py GitHub Actions / flake8
|
||
minimum_maps_after_veto | ||
) | ||
# Remove queues that don't exist anymore | ||
for queue_name in list(self.queues.keys()): | ||
|
@@ -125,6 +128,7 @@ | |
select( | ||
map_pool.c.id, | ||
map_pool.c.name, | ||
map_pool_map_version.c.id.label("map_pool_map_version_id"), | ||
map_pool_map_version.c.weight, | ||
map_pool_map_version.c.map_params, | ||
map_version.c.id.label("map_id"), | ||
|
@@ -150,6 +154,7 @@ | |
map_list.append( | ||
Map( | ||
id=row.map_id, | ||
map_pool_map_version_id=row.map_pool_map_version_id, | ||
folder_name=folder_name, | ||
ranked=row.ranked, | ||
weight=row.weight, | ||
|
@@ -191,6 +196,9 @@ | |
matchmaker_queue_map_pool.c.map_pool_id, | ||
matchmaker_queue_map_pool.c.min_rating, | ||
matchmaker_queue_map_pool.c.max_rating, | ||
matchmaker_queue_map_pool.c.veto_tokens_per_player, | ||
matchmaker_queue_map_pool.c.max_tokens_per_map, | ||
matchmaker_queue_map_pool.c.minimum_maps_after_veto, | ||
game_featuredMods.c.gamemod, | ||
leaderboard.c.technical_name.label("rating_type") | ||
) | ||
|
@@ -219,7 +227,10 @@ | |
info["map_pools"].append(( | ||
row.map_pool_id, | ||
row.min_rating, | ||
row.max_rating | ||
row.max_rating, | ||
row.veto_tokens_per_player, | ||
row.max_tokens_per_map, | ||
row.minimum_maps_after_veto | ||
)) | ||
except Exception: | ||
self._logger.warning( | ||
|
@@ -523,7 +534,25 @@ | |
pool = queue.get_map_pool_for_rating(rating) | ||
if not pool: | ||
raise RuntimeError(f"No map pool available for rating {rating}!") | ||
game_map = pool.choose_map(played_map_ids) | ||
|
||
Check warning on line 537 in server/ladder_service/ladder_service.py GitHub Actions / flake8
|
||
pool, _, _, veto_tokens_per_player, max_tokens_per_map, minimum_maps_after_veto = queue.map_pools[pool.id] | ||
|
||
vetoesMap = defaultdict(int) | ||
tokensTotalPerPlayer = defaultdict(int) | ||
|
||
for (id, map) in pool.maps.items(): | ||
Check warning on line 543 in server/ladder_service/ladder_service.py Codacy Production / Codacy Static Code Analysisserver/ladder_service/ladder_service.py#L543
|
||
for player in all_players: | ||
vetoesMap[map.map_pool_map_version_id] += player.vetoes.get(map.map_pool_map_version_id, 0) | ||
tokensTotalPerPlayer[player.id] += player.vetoes.get(map.map_pool_map_version_id, 0) | ||
|
||
for player in all_players: | ||
if (tokensTotalPerPlayer[player.id] > veto_tokens_per_player): | ||
raise RuntimeError(f"Player {player.id} has too many vetoes!") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not do validation of the number of tokens per player when starting the game. This validation and error raising should happen when the server receives the set_vetoes message. This is because as it is now a player could send the server more than the max amount of vetoes and then cause a game to never be able to launch. |
||
|
||
if (max_tokens_per_map == 0): | ||
max_tokens_per_map = self.calculate_dynamic_tokens_per_map(minimum_maps_after_veto, vetoesMap.values()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good notice |
||
|
||
game_map = pool.choose_map(played_map_ids, vetoesMap, max_tokens_per_map) | ||
|
||
game = self.game_service.create_game( | ||
game_class=LadderGame, | ||
|
@@ -673,6 +702,26 @@ | |
if player not in connected_players | ||
]) | ||
|
||
def calculate_dynamic_tokens_per_map(self, M: float, tokens: list[int]) -> float: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you trying to achieve There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. calculate_dynamic_tokens_per_map finds the minimal possible max_tokens_per_map while still respecting M There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but when do you decide that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its just some math There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll prepare some explanation how math works |
||
sorted_tokens = sorted(tokens) | ||
if (sorted_tokens.count(0) >= M): | ||
return 1 | ||
|
||
result = 1; last = 0; index = 0 | ||
Check failure on line 710 in server/ladder_service/ladder_service.py GitHub Actions / flake8
|
||
while (index < len(sorted_tokens)): | ||
Check warning on line 711 in server/ladder_service/ladder_service.py GitHub Actions / flake8
|
||
(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): | ||
Check warning on line 715 in server/ladder_service/ladder_service.py GitHub Actions / flake8
|
||
continue | ||
|
||
result = sum(sorted_tokens[:index]) / divider | ||
upperLimit = sorted_tokens[index] if index < len(sorted_tokens) else float('inf') | ||
if (result <= upperLimit): | ||
return result | ||
|
||
raise Exception("Failed to calculate dynamic tokens per map: impossible vetoes setup") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here we are throwing an exception based on user input so a user could craft a veto selection that could cause the majority of games to fail to start. It would be better to have some default value rather than throwing an exception. |
||
|
||
async def get_game_history( | ||
self, | ||
players: list[Player], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ def __init__( | |
rating_type: str, | ||
team_size: int = 1, | ||
params: Optional[dict[str, Any]] = None, | ||
map_pools: Iterable[tuple[MapPool, Optional[int], Optional[int]]] = (), | ||
map_pools: Iterable[tuple[MapPool, Optional[int], Optional[int], int, int, float]] = (), | ||
): | ||
self.game_service = game_service | ||
self.name = name | ||
|
@@ -78,12 +78,15 @@ def add_map_pool( | |
self, | ||
map_pool: MapPool, | ||
min_rating: Optional[int], | ||
max_rating: Optional[int] | ||
max_rating: Optional[int], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are all of those arguments not inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because its not in the mapPool in the database, but in matchmaker_queue_map_pool (queue bracket), so should be consistent between repos There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
veto_tokens_per_player: int, | ||
max_tokens_per_map: int, | ||
minimum_maps_after_veto: float, | ||
) -> None: | ||
self.map_pools[map_pool.id] = (map_pool, min_rating, max_rating) | ||
self.map_pools[map_pool.id] = (map_pool, min_rating, max_rating, veto_tokens_per_player, max_tokens_per_map, minimum_maps_after_veto) | ||
|
||
def get_map_pool_for_rating(self, rating: float) -> Optional[MapPool]: | ||
for map_pool, min_rating, max_rating in self.map_pools.values(): | ||
for map_pool, min_rating, max_rating, _, _, _ in self.map_pools.values(): | ||
if min_rating is not None and rating < min_rating: | ||
continue | ||
if max_rating is not None and rating > max_rating: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it suddenly in camelCase?