Skip to content

Commit

Permalink
Match votes
Browse files Browse the repository at this point in the history
  • Loading branch information
fp12 committed Jan 2, 2017
1 parent 7f8e28f commit 9fee89b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
38 changes: 38 additions & 0 deletions challonge/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@ async def report_tie(self, scores_csv: str):
"""
await self._report(scores_csv, 'tie')

async def change_votes(self, player1_votes: int = None, player2_votes: int = None, add: bool = False):
""" change the votes for either player
|methcoro|
The votes will be overriden by default,
If `add` is set to True, another API request call will be made to ensure the local is up to date with
the Challonge server. Then the votes given in argument will be added to those on the server
Args:
player1_votes: if set, the player 1 votes will be changed to this value, or added to the current value if `add` is set
player1_votes: if set, the player 2 votes will be changed to this value, or added to the current value if `add` is set
add: if set, votes in parameters will be added instead of overriden
Raises:
ChallongeException
"""
assert player1_votes is not None or player2_votes is not None
if add:
# order a fresh update of this match
res = await self.connection('GET', 'tournaments/{}/matches/{}'.format(self._tournament_id, self._id))
self._refresh_from_json(res)
if player1_votes is not None:
player1_votes += self._player1_votes or 0
if player2_votes is not None:
player2_votes += self._player2_votes or 0

params = {}
if player1_votes is not None:
params.update({'player1_votes': player1_votes})
if player2_votes is not None:
params.update({'player2_votes': player2_votes})
res = await self.connection('PUT',
'tournaments/{}/matches/{}'.format(self._tournament_id, self._id),
'match',
**params)
self._refresh_from_json(res)

async def _attach(self, url: str = None, description: str = None):
assert (url is not None or description is not None), 'url:{} - description:{}'.format(url, description)
params = {'description': description or ''}
Expand Down
15 changes: 15 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ def test_b_report_winner(self):
self.assertEqual(m[0].winner_id, p1.id)
yield from self.user.destroy_tournament(t)

# @unittest.skip('')
@async_test
def test_c_votes(self):
random_name = get_random_name()
t = yield from self.user.create_tournament(random_name, random_name)
yield from t.add_participants('p1', 'p2', 'p3', 'p4')
yield from t.start()
m = yield from t.get_matches()
yield from m[0].change_votes(player1_votes=3)
self.assertEqual(m[0].player1_votes, 3)
yield from m[0].change_votes(player1_votes=1, player2_votes=5, add=True)
self.assertEqual(m[0].player1_votes, 4)
self.assertEqual(m[0].player2_votes, 5)
yield from self.user.destroy_tournament(t)


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

0 comments on commit 9fee89b

Please sign in to comment.