Skip to content

Commit

Permalink
Get Next Match/Opponent
Browse files Browse the repository at this point in the history
  • Loading branch information
fp12 committed Apr 6, 2017
1 parent af52f58 commit 3d67a53
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
60 changes: 53 additions & 7 deletions challonge/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class Participant(metaclass=FieldHolder):
'can_check_in', 'checked_in', 'reactivatable',
'display_name', 'group_player_ids']

def __init__(self, connection, json_def, **kwargs):
def __init__(self, connection, json_def, tournament, **kwargs):
self.connection = connection
self._tournament = tournament
self._refresh_from_json(json_def)

def _refresh_from_json(self, json_def):
Expand Down Expand Up @@ -47,7 +48,7 @@ async def change_display_name(self, new_name: str):
await self._change(name=new_name)

async def change_username(self, username: str):
""" will invite the Challonge user to the tournament
""" Will invite the Challonge user to the tournament
|methcoro|
Expand All @@ -61,7 +62,7 @@ async def change_username(self, username: str):
await self._change(challonge_username=username)

async def change_email(self, email: str):
""" set / update the email associated to the participant
""" Set / update the email associated to the participant
|methcoro|
Expand All @@ -79,7 +80,7 @@ async def change_email(self, email: str):
await self._change(email=email)

async def change_seed(self, new_seed: int) -> int:
"""
""" Change the seed of the participant
|methcoro|
Expand All @@ -99,7 +100,8 @@ async def change_seed(self, new_seed: int) -> int:
await self._change(seed=new_seed)

async def change_misc(self, misc: str) -> str:
"""
""" Change the `misc` field
|methcoro|
Note:
Expand All @@ -115,7 +117,7 @@ async def change_misc(self, misc: str) -> str:
await self._change(misc=misc)

async def check_in(self):
"""
""" Checks this participant in
|methcoro|
Expand All @@ -130,7 +132,8 @@ async def check_in(self):
self._refresh_from_json(res)

async def undo_check_in(self):
"""
""" Undo the check in for this participant
|methcoro|
Warning:
Expand All @@ -142,3 +145,46 @@ async def undo_check_in(self):
"""
res = await self.connection('POST', 'tournaments/{}/participants/{}/undo_check_in'.format(self._tournament_id, self._id))
self._refresh_from_json(res)

async def get_next_match(self):
""" Return the first open match found, or if none, the first pending match found
|methcoro|
Raises:
APIException
"""
if self._final_rank is not None:
return None

open_matches = await self.connection('GET',
'tournaments/{}/matches'.format(self._tournament_id),
state='open',
participant_id=self._id)
if len(open_matches) > 0:
return await self._tournament.get_match(open_matches[0]['match']['id'])

pending_matches = await self.connection('GET',
'tournaments/{}/matches'.format(self._tournament_id),
state='pending',
participant_id=self._id)
if len(pending_matches) > 0:
return await self._tournament.get_match(pending_matches[0]['match']['id'])

return None

async def get_next_opponent(self):
""" Get the opponent of the potential next match. See :func:`get_next_match`
|methcoro|
Raises:
APIException
"""
next_match = await self.get_next_match()
if next_match is not None:
opponent_id = next_match.player1_id if next_match.player2_id == self._id else next_match.player2_id
return await self._tournament.get_participant(opponent_id)
return None
2 changes: 1 addition & 1 deletion challonge/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(self, connection, json_def, **kwargs):
self.connection = connection

self.participants = None
self._create_participant = lambda p: self._create_holder(Participant, p)
self._create_participant = lambda p: self._create_holder(Participant, p, tournament=self)
self._find_participant = lambda p_id: self._find_holder(self.participants, p_id)

self.matches = None
Expand Down
20 changes: 18 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_credentials():


def get_random_name():
return "pychallonge_" + "".join(random.choice(string.ascii_lowercase) for _ in range(0, 15))
return "achallonge_" + "".join(random.choice(string.ascii_lowercase) for _ in range(0, 15))


def async_test(f):
Expand Down Expand Up @@ -359,7 +359,7 @@ def test_g_multi_tournaments(self):

t1_ref = yield from self.user.get_tournament(t1.id)
self.assertEqual(t1, t1_ref)
self.assertTrue(t1 is t1_ref)
self.assertIs(t1, t1_ref)
self.assertNotEqual(t1, t2)

yield from self.user.destroy_tournament(t1)
Expand Down Expand Up @@ -545,6 +545,22 @@ def test_c_votes(self):
self.assertEqual(m[0].player2_votes, 5)
yield from self.user.destroy_tournament(t)

# @unittest.skip('')
@async_test
def test_d_next_match_and_opponent(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
p1 = yield from t.add_participant('p1')
p2 = yield from t.add_participant('p2')
yield from t.start()
m = yield from p1.get_next_match()
self.assertIn(m, t.matches)

p2_ref = yield from p1.get_next_opponent()
self.assertIs(p2, p2_ref)

yield from self.user.destroy_tournament(t)


# @unittest.skip('')
class AttachmentsTestCase(unittest.TestCase):
Expand Down

0 comments on commit 3d67a53

Please sign in to comment.