diff --git a/matchmaking.py b/matchmaking.py index 2b1542356..725889ed9 100644 --- a/matchmaking.py +++ b/matchmaking.py @@ -149,17 +149,17 @@ def update_user_profile(self) -> None: def get_weights(self, online_bots: list[USER_PROFILE_TYPE], rating_preference: str, min_rating: int, max_rating: int, game_type: str) -> list[int]: """Get the weight for each bot. A higher weights means the bot is more likely to get challenged.""" + def rating(bot: USER_PROFILE_TYPE) -> int: + return int(bot.get("perfs", {}).get(game_type, {}).get("rating", 0)) + if rating_preference == "high": # A bot with max_rating rating will be twice as likely to get picked than a bot with min_rating rating. reduce_ratings_by = min(min_rating - (max_rating - min_rating), min_rating - 1) - # or, reduce_ratings_by = min(2 * min_rating - max_rating, min_rating - 1) - weights = [bot.get("perfs", {}).get(game_type, {}).get("rating", 0) - reduce_ratings_by for bot in online_bots] + weights = [rating(bot) - reduce_ratings_by for bot in online_bots] elif rating_preference == "low": # A bot with min_rating rating will be twice as likely to get picked than a bot with max_rating rating. reduce_ratings_by = max(max_rating - (min_rating - max_rating), max_rating + 1) - # or, reduce_ratings_by = max(2 * max_rating - min_rating, max_rating + 1) - weights = [(reduce_ratings_by - bot.get("perfs", {}).get(game_type, {}).get("rating", 0)) - for bot in online_bots] + weights = [reduce_ratings_by - rating(bot) for bot in online_bots] else: weights = [1] * len(online_bots) return weights diff --git a/strategies.py b/strategies.py index 44b00b5c1..4f567c98f 100644 --- a/strategies.py +++ b/strategies.py @@ -6,12 +6,12 @@ from __future__ import annotations import chess -from chess.engine import PlayResult +from chess.engine import PlayResult, Limit import random from engine_wrapper import MinimalEngine from typing import Any, Union import logging -MOVE = Union[chess.engine.PlayResult, list[chess.Move]] +MOVE = Union[PlayResult, list[chess.Move]] # Use this logger variable to print messages to the console or log files. @@ -63,8 +63,7 @@ class ComboEngine(ExampleEngine): This engine demonstrates how one can use `time_limit`, `draw_offered`, and `root_moves`. """ - def search(self, board: chess.Board, time_limit: chess.engine.Limit, ponder: bool, draw_offered: bool, - root_moves: MOVE) -> chess.engine.PlayResult: + def search(self, board: chess.Board, time_limit: Limit, ponder: bool, draw_offered: bool, root_moves: MOVE) -> PlayResult: """ Choose a move using multiple different methods. diff --git a/test_bot/test_bot.py b/test_bot/test_bot.py index 8ac7105d7..03dcf2910 100644 --- a/test_bot/test_bot.py +++ b/test_bot/test_bot.py @@ -31,25 +31,25 @@ def download_sf() -> None: """Download Stockfish 15.""" if os.path.exists(stockfish_path): return + windows_or_linux = "windows" if platform == "win32" else "ubuntu" - if windows_or_linux == "windows": - archive_link = ("https://github.com/official-stockfish/Stockfish/releases/download/sf_16/" - "stockfish-windows-x86-64-modern.zip") - response = requests.get(archive_link, allow_redirects=True) - with open("./TEMP/sf_zip.zip", "wb") as file: - file.write(response.content) - with zipfile.ZipFile("./TEMP/sf_zip.zip", "r") as archive_ref: - archive_ref.extractall("./TEMP/") - shutil.copyfile("./TEMP/stockfish/stockfish-windows-x86-64-modern.exe", stockfish_path) - else: - archive_link = ("https://github.com/official-stockfish/Stockfish/releases/download/sf_16/" - "stockfish-ubuntu-x86-64-modern.tar") - response = requests.get(archive_link, allow_redirects=True) - with open("./TEMP/sf_zip.tar", "wb") as file: - file.write(response.content) - with tarfile.TarFile("./TEMP/sf_zip.tar", "r") as archive_ref: - archive_ref.extractall("./TEMP/") - shutil.copyfile("./TEMP/stockfish/stockfish-ubuntu-x86-64-modern", stockfish_path) + sf_base = f"stockfish-{windows_or_linux}-x86-64-modern" + archive_ext = "zip" if platform == "win32" else "tar" + archive_link = f"https://github.com/official-stockfish/Stockfish/releases/download/sf_16/{sf_base}.{archive_ext}" + + response = requests.get(archive_link, allow_redirects=True) + archive_name = f"./TEMP/sf_zip.{archive_ext}" + with open(archive_name, "wb") as file: + file.write(response.content) + + archive_open = zipfile.ZipFile if archive_ext == "zip" else tarfile.TarFile + with archive_open(archive_name, "r") as archive_ref: + archive_ref.extractall("./TEMP/") + + exe_ext = ".exe" if platform == "win32" else "" + shutil.copyfile(f"./TEMP/stockfish/{sf_base}{exe_ext}", stockfish_path) + + if windows_or_linux == "ubuntu": st = os.stat(stockfish_path) os.chmod(stockfish_path, st.st_mode | stat.S_IEXEC)