-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathchallenge_validator.py
95 lines (74 loc) · 4.24 KB
/
challenge_validator.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from typing import Any
from config import Config
from enums import Decline_Reason
class Challenge_Validator:
def __init__(self, config: Config) -> None:
self.config = config
self.time_controls = self._get_time_controls(self.config.challenge.time_controls)
self.min_increment = 0 if config.challenge.min_increment is None else config.challenge.min_increment
self.max_increment = 180 if config.challenge.max_increment is None else config.challenge.max_increment
self.min_initial = 0 if config.challenge.min_initial is None else config.challenge.min_initial
self.max_initial = 315360000 if config.challenge.max_initial is None else config.challenge.max_initial
def get_decline_reason(self, challenge_event: dict[str, Any]) -> Decline_Reason | None:
speed: str = challenge_event['speed']
if speed == 'correspondence':
print('Time control "Correspondence" is not supported by BotLi.')
return Decline_Reason.TIME_CONTROL
variant: str = challenge_event['variant']['key']
if variant not in self.config.challenge.variants:
print(f'Variant "{variant}" is not allowed according to config.')
return Decline_Reason.VARIANT
if challenge_event['challenger']['id'] in self.config.whitelist:
return
if challenge_event['challenger']['id'] in self.config.blacklist:
print('Challenger is blacklisted.')
return Decline_Reason.GENERIC
if not (self.config.challenge.bot_modes or self.config.challenge.human_modes):
print('Neither bots nor humans are allowed according to config.')
return Decline_Reason.GENERIC
is_bot: bool = challenge_event['challenger']['title'] == 'BOT'
modes = self.config.challenge.bot_modes if is_bot else self.config.challenge.human_modes
if modes is None:
if is_bot:
print('Bots are not allowed according to config.')
return Decline_Reason.NO_BOT
print('Only bots are allowed according to config.')
return Decline_Reason.ONLY_BOT
increment: int = challenge_event['timeControl']['increment']
initial: int = challenge_event['timeControl']['limit']
if not self.config.challenge.time_controls:
print('No time control is allowed according to config.')
return Decline_Reason.GENERIC
if speed not in self.config.challenge.time_controls and (initial, increment) not in self.time_controls:
print(f'Time control "{speed}" is not allowed according to config.')
return Decline_Reason.TIME_CONTROL
if increment < self.min_increment:
print(f'Increment {increment} is too short according to config.')
return Decline_Reason.TOO_FAST
if increment > self.max_increment:
print(f'Increment {increment} is too long according to config.')
return Decline_Reason.TOO_SLOW
if initial < self.min_initial:
print(f'Initial time {initial} is too short according to config.')
return Decline_Reason.TOO_FAST
if initial > self.max_initial:
print(f'Initial time {initial} is too long according to config.')
return Decline_Reason.TOO_SLOW
if is_bot and speed == 'bullet' and increment == 0 and self.config.challenge.bullet_with_increment_only:
print('Bullet against bots is only allowed with increment according to config.')
return Decline_Reason.TOO_FAST
is_rated: bool = challenge_event['rated']
is_casual = not is_rated
if is_rated and 'rated' not in modes:
print('Rated is not allowed according to config.')
return Decline_Reason.CASUAL
if is_casual and 'casual' not in modes:
print('Casual is not allowed according to config.')
return Decline_Reason.RATED
def _get_time_controls(self, speeds: list[str]) -> list[tuple[int, int]]:
time_controls: list[tuple[int, int]] = []
for speed in speeds:
if '+' in speed:
initial_str, increment_str = speed.split('+')
time_controls.append((int(initial_str) * 60, int(increment_str)))
return time_controls