-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathchallenger.py
49 lines (37 loc) · 2.07 KB
/
challenger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
from api import API
from botli_dataclasses import Challenge_Request, Challenge_Response
class Challenger:
def __init__(self, api: API) -> None:
self.api = api
async def create(self, challenge_request: Challenge_Request) -> Challenge_Response:
challenge_id = None
try:
async with asyncio.timeout(challenge_request.timeout):
async for response in self.api.create_challenge(challenge_request):
if response.challenge_id:
challenge_id = response.challenge_id
if response.was_accepted:
return Challenge_Response(challenge_id=challenge_id, success=True)
if response.was_declined:
return Challenge_Response(success=False)
if response.has_reached_rate_limit:
print(f'Challenge against {challenge_request.opponent_username} '
'failed due to Lichess rate limit.')
return Challenge_Response(success=False, has_reached_rate_limit=True)
if response.invalid_initial:
print('Challenge failed due to invalid initial time.')
return Challenge_Response(success=False, is_misconfigured=True)
if response.invalid_increment:
print('Challenge failed due to invalid increment time.')
return Challenge_Response(success=False, is_misconfigured=True)
if response.error:
print(response.error)
return Challenge_Response(success=False)
except TimeoutError:
print(f'Challenge against {challenge_request.opponent_username} has timed out.')
if challenge_id is None:
print('Could not cancel challenge because the challenge_id was not set in "Challenger"!')
else:
await self.api.cancel_challenge(challenge_id)
return Challenge_Response(success=False)