Skip to content

Commit

Permalink
Add find_matching_user_for_seek()
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Dec 25, 2024
1 parent 9b84a00 commit a22ab8e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
19 changes: 19 additions & 0 deletions server/auto_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,22 @@ def find_matching_seek(app_state, user, variant_tc):
),
None,
)


def find_matching_user_for_seek(app_state, seek, variant_tc):
"""Return first compatible user from app_state.auto_pairing_users if there is any, else None"""

return next(
(
user_candidate
for user_candidate in (
auto_pairing_user
for auto_pairing_user in app_state.auto_pairings[variant_tc]
if auto_pairing_user in app_state.auto_pairing_users
and auto_pairing_user.ready_for_auto_pairing
and auto_pairing_user != seek.creator
)
if user_candidate.auto_compatible_with_seek(seek)
),
None,
)
4 changes: 2 additions & 2 deletions server/wsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from auto_pair import (
auto_pair,
add_to_auto_pairings,
find_matching_user,
find_matching_user_for_seek,
)
from chat import chat_response
from const import ANON_PREFIX, STARTED
Expand Down Expand Up @@ -157,7 +157,7 @@ async def handle_create_seek(app_state, ws, user, data):
if not user.anon:
variant_tc = (seek.variant, seek.chess960, seek.base, seek.inc, seek.byoyomi_period)
if variant_tc in app_state.auto_pairings:
matching_user = find_matching_user(app_state, user, variant_tc)
matching_user = find_matching_user_for_seek(app_state, seek, variant_tc)

auto_paired = False
if matching_user is not None:
Expand Down
22 changes: 21 additions & 1 deletion tests/test_auto_pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
from pychess_global_app_state_utils import get_app_state
from const import VARIANTS
from glicko2.glicko2 import DEFAULT_PERF
from auto_pair import add_to_auto_pairings, find_matching_seek, find_matching_user
from auto_pair import (
add_to_auto_pairings,
find_matching_seek,
find_matching_user,
find_matching_user_for_seek,
)

ONE_TEST_ONLY = False

Expand Down Expand Up @@ -281,6 +286,21 @@ def test_find_matching_user(self):
result = find_matching_user(app_state, self.lplayer, variant_tc)
self.assertEqual(result, self.aplayer)

@unittest.skipIf(ONE_TEST_ONLY, "1 test only")
def test_find_matching_user_for_seek(self):
"""Test find_matching_user_for_seek() called by wsl.py handle_create_seek()"""

app_state = get_app_state(self.app)

variant_tc = ("chess", False, 5, 3, 0) # chess 5+3
add_to_auto_pairings(app_state, self.aplayer, DATA["chess"])

seek = Seek("id", self.bplayer, "chess", rated=True)
app_state.seeks[seek.id] = seek

result = find_matching_user_for_seek(app_state, seek, variant_tc)
self.assertEqual(result, self.aplayer)


if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit a22ab8e

Please sign in to comment.