Skip to content

Commit

Permalink
Make search expansion grow through time instead of shrink (#448)
Browse files Browse the repository at this point in the history
* Make expansion grow through time instead of shrink

* Use list evaluation, primarily to re-trigger travis build

* Add config parameters for search expansion

* Adjust default config values
  • Loading branch information
Askaholic authored and Brutus5000 committed Jun 24, 2019
1 parent 51fc291 commit 56c667f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
3 changes: 3 additions & 0 deletions server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@
CONTROL_SERVER_PORT = int(os.getenv('CONTROL_SERVER_PORT', 4000))

LADDER_ANTI_REPETITION_LIMIT = int(os.getenv('LADDER_ANTI_REPETITION_LIMIT', 3))
LADDER_SEARCH_EXPANSION_START = int(os.getenv('LADDER_SEARCH_EXPANSION_START', 60 * 2))
LADDER_SEARCH_EXPANSION_END = int(os.getenv('LADDER_SEARCH_EXPANSION_END', 60 * 20))
LADDER_SEARCH_EXPANSION_MAX = float(os.getenv('LADDER_SEARCH_EXPANSION_MAX', 0.25))
2 changes: 1 addition & 1 deletion server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ async def command_avatar(self, message):
avatar = {"url": row["url"], "tooltip": row["tooltip"]}
avatarList.append(avatar)

if len(avatarList) > 0:
if avatarList:
self.sendJSON({"command": "avatar", "avatarlist": avatarList})

elif action == "select":
Expand Down
37 changes: 29 additions & 8 deletions server/matchmaker/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import time
from typing import List, Optional

import server.config as config
from server.decorators import with_logger
from server.players import Player
from trueskill import Rating, quality

from .. import config
from ..decorators import with_logger
from ..players import Player


@with_logger
class Search():
Expand Down Expand Up @@ -97,14 +98,34 @@ def boundary_75(self):
return rounded_mu - 100, rounded_mu + 100

@property
def search_expansion(self):
def search_expansion(self) -> float:
"""
Defines how much to expand the search range of game quality due to waiting time
Defines how much to expand the search range of game quality due to waiting
time.
The graph of this function over time looks essentially like this:
END (x)-> ___ <- MAX (y)
/
___/ <- START (x)
The search threshold will not expand until a certain time START has been
reached. Then it will expand linearly with time until time END, at which
point it will have reached it's maximum value and will not expand
further.
"""
return 0.25 * min(1 / ((time.time() - self.start_time) / 300), 1)
elapsed = time.time() - self.start_time
MAX = config.LADDER_SEARCH_EXPANSION_MAX
START = config.LADDER_SEARCH_EXPANSION_START
END = config.LADDER_SEARCH_EXPANSION_END

if elapsed < START:
return 0.0
if elapsed > END:
return MAX

return (MAX / (END - START)) * (elapsed - START)

@property
def match_threshold(self):
def match_threshold(self) -> float:
"""
Defines the threshold for game quality
Expand All @@ -119,7 +140,7 @@ def match_threshold(self):
thresholds.append(max(q - self.search_expansion, 0))
return min(thresholds)

def quality_with(self, other: 'Search'):
def quality_with(self, other: 'Search') -> float:
assert all(other.raw_ratings)
assert other.players

Expand Down
20 changes: 18 additions & 2 deletions tests/unit_tests/test_matchmaker_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import random
from collections import deque
from concurrent.futures import CancelledError, TimeoutError
from unittest.mock import Mock

import pytest
import server.config as config
from mock import Mock
from server.matchmaker import MatchmakerQueue, Search
from server.players import Player

Expand Down Expand Up @@ -89,14 +89,30 @@ def test_search_no_match_wrong_type(matchmaker_players):


def test_search_boundaries(matchmaker_players):
p1, _, _, _, _, _ = matchmaker_players
p1 = matchmaker_players[0]
s1 = Search([p1])
assert p1.ladder_rating[0] > s1.boundary_80[0]
assert p1.ladder_rating[0] < s1.boundary_80[1]
assert p1.ladder_rating[0] > s1.boundary_75[0]
assert p1.ladder_rating[0] < s1.boundary_75[1]


def test_search_expansion(matchmaker_players, mocker):
p1 = matchmaker_players[0]
mocker.patch('time.time', return_value=0)
s1 = Search([p1])

assert s1.search_expansion == 0.0
mocker.patch('time.time', return_value=500)
assert s1.search_expansion > 0.0

# Make sure that the expansion stops at some point
mocker.patch('time.time', return_value=500_000)
e1 = s1.search_expansion
mocker.patch('time.time', return_value=500_300)
assert e1 == s1.search_expansion


async def test_search_await(mocker, loop, matchmaker_players):
p1, p2, _, _, _, _ = matchmaker_players
s1, s2 = Search([p1]), Search([p2])
Expand Down

0 comments on commit 56c667f

Please sign in to comment.