Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
1-alex98 committed Nov 19, 2022
1 parent fb1aeec commit 5e45de1
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 46 deletions.
21 changes: 1 addition & 20 deletions server/ladder_service/ladder_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,11 @@ async def _queue_pop_timer(self) -> None:

async def _queue_pop_iteration(self):
possible_games = list()
for queue in self._queues_without1v1():
for queue in self.queues.values():
possible_games += await queue.find_matches()
matches_tmm = self.matchmaker.pick_noncolliding_games(possible_games)
matches_tmm = await self._add_matches_from1v1(matches_tmm)
await self._found_matches(matches_tmm)

async def _add_matches_from1v1(self, matches_tmm):
for queue in self._1v1queues():
matches_1v1 = await queue.find_matches1v1()
self._logger.debug("Suggested the following matches %s", matches_1v1)
matches_tmm_searches = set(search for match in matches_tmm
for team in match
for search in team.get_original_searches())
matches_1v1 = [match for match in matches_1v1 if has_no_overlap(match, matches_tmm_searches)]
self._logger.debug("Found the following 1v1 matches %s", matches_1v1)
matches_tmm += matches_1v1
return matches_tmm

def _queues_without1v1(self) -> list[MatchmakerQueue]:
return [queue for queue in self.queues.values() if queue.team_size != 1]

def _1v1queues(self) -> list[MatchmakerQueue]:
return [queue for queue in self.queues.values() if queue.team_size == 1]

async def _found_matches(self, matches: list[Match]):
for queue in self.queues.values():
await queue.found_matches([match for match in matches if match[0].queue == queue])
Expand Down
2 changes: 1 addition & 1 deletion server/matchmaker/algorithm/random_newbies.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Iterable

from ..search import Match, Search
from ..search import Search
from .matchmaker import MatchmakingPolicy1v1


Expand Down
2 changes: 1 addition & 1 deletion server/matchmaker/algorithm/stable_marriage.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class StableMarriageMatchmaker(Matchmaker):
"""

def find(
self, searches: Iterable[Search], team_size: int = 1,, rating_peak: float
self, searches: Iterable[Search], team_size: int, rating_peak: float
) -> list[Match]:
if team_size != 1:
self._logger.error(
Expand Down
10 changes: 5 additions & 5 deletions server/matchmaker/algorithm/team_matchmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NotEnoughPlayersException(Exception):
@with_logger
class TeamMatchMaker(Matchmaker):
"""
Matchmaker for teams of varied size. Untested for higher than 4v4 but it should work
Matchmaker for teams of varied size. Untested for higher than 4v4 but it should work.
Overview of the algorithm:
1. list all the parties in queue by their average rating.
Expand All @@ -46,9 +46,6 @@ class TeamMatchMaker(Matchmaker):
4. add this game to a games list
5. repeat 2. to 4. for every party.
6. you now have a list of potential games with minimal rating variation and minimal rating imbalance.
7. remove all games with match quality below threshold then sort by quality descending
8. pick the first game from the game list and remove all other games that contain the same players
9. repeat 8. until the list is empty
"""

def find(
Expand All @@ -57,10 +54,13 @@ def find(
if not searches:
return []

if team_size == 1:
return [GameCandidate(match, config.MINIMUM_GAME_QUALITY)
for match in StableMarriageMatchmaker().find(searches, team_size, rating_peak)]

searches = SortedList(searches, key=lambda s: s.average_rating)
possible_games = []

self._logger.debug("========= starting matching algorithm =========")
self._logger.debug("Searches in queue: %s", list(searches))

for index, search in enumerate(searches):
Expand Down
18 changes: 0 additions & 18 deletions server/matchmaker/matchmaker_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from ..decorators import with_logger
from ..players import PlayerState
from .algorithm.stable_marriage import StableMarriageMatchmaker
from .algorithm.team_matchmaker import GameCandidate, TeamMatchMaker
from .map_pool import MapPool
from .search import Match, Search
Expand Down Expand Up @@ -139,23 +138,6 @@ async def find_matches(self) -> list[GameCandidate]:
self.rating_peak,
)

async def find_matches1v1(self) -> list[Match]:
self._logger.info("Searching for 1v1 matches: %s", self.name)

searches = list(self._queue.keys())

if self.num_players < 2 * self.team_size:
return []
matchmaker = StableMarriageMatchmaker()
# Call self.match on all matches and filter out the ones that were cancelled
loop = asyncio.get_running_loop()
return await loop.run_in_executor(
None,
matchmaker.find,
searches,
self.team_size,
)

def get_unmatched_searches(self, proposed_matches: list[Match]) -> list[Search]:
searches = list(self._queue.keys())
for match in proposed_matches:
Expand Down
1 change: 0 additions & 1 deletion tests/unit_tests/test_matchmaker_queue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import functools
import time
from unittest import mock

import pytest
Expand Down

0 comments on commit 5e45de1

Please sign in to comment.