-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathchallenge_validator.py
More file actions
110 lines (87 loc) · 4.94 KB
/
challenge_validator.py
File metadata and controls
110 lines (87 loc) · 4.94 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from typing import Any
from config import Config
from enums import DeclineReason
from game_manager import GameManager
from utils import parse_time_control
class ChallengeValidator:
def __init__(self, config: Config, game_manager: GameManager) -> None:
self.config = config
self.game_manager = game_manager
self.bot_time_controls = self._get_time_controls(self.config.challenge.bot_time_controls)
self.human_time_controls = self._get_time_controls(self.config.challenge.human_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 = 10800 if config.challenge.max_initial is None else config.challenge.max_initial
def get_decline_reason(self, challenge_event: dict[str, Any]) -> DeclineReason | None:
speed: str = challenge_event["speed"]
if speed == "ultraBullet":
print('Time control "UltraBullet" is not allowed for bots.')
return DeclineReason.TIME_CONTROL
if speed == "correspondence":
print('Time control "Correspondence" is not supported by BotLi.')
return DeclineReason.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 DeclineReason.VARIANT
if (
len(self.game_manager.tournaments) + len(self.game_manager.tournaments_to_join)
) >= self.config.challenge.concurrency:
print("Concurrency exhausted due to tournaments.")
return DeclineReason.LATER
if challenge_event["challenger"]["id"] in self.config.whitelist:
return
if challenge_event["challenger"]["id"] in self.config.blacklist:
print("Challenger is blacklisted.")
return DeclineReason.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 DeclineReason.GENERIC
is_bot: bool = challenge_event["challenger"].get("title") == "BOT"
modes = self.config.challenge.bot_modes if is_bot else self.config.challenge.human_modes
if not modes:
if is_bot:
print("Bots are not allowed according to config.")
return DeclineReason.NO_BOT
print("Only bots are allowed according to config.")
return DeclineReason.ONLY_BOT
speeds = self.config.challenge.bot_time_controls if is_bot else self.config.challenge.human_time_controls
if not speeds:
if is_bot:
print("Bots are not allowed according to config.")
return DeclineReason.NO_BOT
print("Only bots are allowed according to config.")
return DeclineReason.ONLY_BOT
initial: int = challenge_event["timeControl"]["limit"]
increment: int = challenge_event["timeControl"]["increment"]
time_controls = self.bot_time_controls if is_bot else self.human_time_controls
if speed not in speeds and (initial, increment) not in time_controls:
print(f'Time control "{speed}" is not allowed according to config.')
return DeclineReason.TIME_CONTROL
if increment < self.min_increment:
print(f"Increment {increment} is too short according to config.")
return DeclineReason.TOO_FAST
if increment > self.max_increment:
print(f"Increment {increment} is too long according to config.")
return DeclineReason.TOO_SLOW
if initial < self.min_initial:
print(f"Initial time {initial} is too short according to config.")
return DeclineReason.TOO_FAST
if initial > self.max_initial:
print(f"Initial time {initial} is too long according to config.")
return DeclineReason.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 DeclineReason.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 DeclineReason.CASUAL
if is_casual and "casual" not in modes:
print("Casual is not allowed according to config.")
return DeclineReason.RATED
@staticmethod
def _get_time_controls(speeds: list[str]) -> list[tuple[int, int]]:
return [parse_time_control(speed) for speed in speeds if "+" in speed]