Skip to content

Commit

Permalink
Cleanup (#872)
Browse files Browse the repository at this point in the history
* Create inner function for getting player rating

* Consolidate similar code in download_sf()

* Fix imports in strategies
  • Loading branch information
MarkZH authored Nov 21, 2023
1 parent 3f4ae03 commit e8d6206
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
10 changes: 5 additions & 5 deletions matchmaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
36 changes: 18 additions & 18 deletions test_bot/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e8d6206

Please sign in to comment.