Spaces:
Sleeping
Sleeping
Lakin Wecker commited on
Commit ·
4ab541b
1
Parent(s): 2a40846
Cleanup references to config by using an engine factory.
Browse files- engine_wrapper.py +28 -0
- main.py +10 -30
engine_wrapper.py
CHANGED
|
@@ -1,7 +1,35 @@
|
|
|
|
|
| 1 |
import chess
|
| 2 |
import chess.xboard
|
| 3 |
import chess.uci
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
class EngineWrapper:
|
| 6 |
|
| 7 |
def __init__(self, board, commands):
|
|
|
|
| 1 |
+
import os
|
| 2 |
import chess
|
| 3 |
import chess.xboard
|
| 4 |
import chess.uci
|
| 5 |
|
| 6 |
+
class EngineFactory:
|
| 7 |
+
def __init__(self, config):
|
| 8 |
+
self.config = config
|
| 9 |
+
def __call__(self, board):
|
| 10 |
+
# print("Loading Engine!")
|
| 11 |
+
cfg = self.config["engine"]
|
| 12 |
+
engine_path = os.path.join(cfg["dir"], cfg["name"])
|
| 13 |
+
weights = os.path.join(cfg["dir"], cfg["weights"]) if "weights" in cfg else None
|
| 14 |
+
threads = cfg.get("threads")
|
| 15 |
+
|
| 16 |
+
# TODO: ucioptions should probably be a part of the engine subconfig
|
| 17 |
+
ucioptions = self.config.get("ucioptions")
|
| 18 |
+
engine_type = cfg.get("protocol")
|
| 19 |
+
commands = [engine_path]
|
| 20 |
+
if weights:
|
| 21 |
+
commands.append("-w")
|
| 22 |
+
commands.append(weights)
|
| 23 |
+
if threads:
|
| 24 |
+
commands.append("-t")
|
| 25 |
+
commands.append(threads)
|
| 26 |
+
|
| 27 |
+
if engine_type == "xboard":
|
| 28 |
+
return XBoardEngine(board, commands)
|
| 29 |
+
|
| 30 |
+
return UCIEngine(board, commands, ucioptions)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
class EngineWrapper:
|
| 34 |
|
| 35 |
def __init__(self, board, commands):
|
main.py
CHANGED
|
@@ -28,9 +28,8 @@ def watch_control_stream(control_queue, li):
|
|
| 28 |
event = json.loads(evnt.decode('utf-8'))
|
| 29 |
control_queue.put_nowait(event)
|
| 30 |
|
| 31 |
-
def start(li, user_profile,
|
| 32 |
# init
|
| 33 |
-
max_games = CONFIG['max_concurrent_games']
|
| 34 |
username = user_profile.get("username")
|
| 35 |
print("Welcome {}!".format(username))
|
| 36 |
manager = multiprocessing.Manager()
|
|
@@ -52,7 +51,7 @@ def start(li, user_profile, engine_path, weights=None, threads=None):
|
|
| 52 |
print("+++ Process Free. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 53 |
elif event["type"] == "challenge":
|
| 54 |
chlng = model.Challenge(event["challenge"])
|
| 55 |
-
if len(challenge_queue) <
|
| 56 |
challenge_queue.append(chlng)
|
| 57 |
print(" Queue {}".format(chlng.show()))
|
| 58 |
else:
|
|
@@ -64,9 +63,7 @@ def start(li, user_profile, engine_path, weights=None, threads=None):
|
|
| 64 |
else:
|
| 65 |
queued_processes -= 1
|
| 66 |
game_id = event["game"]["id"]
|
| 67 |
-
|
| 68 |
-
engine_type = CONFIG["engine"].get("protocol")
|
| 69 |
-
pool.apply_async(play_game, [li, game_id, engine_path, engine_type, weights, threads, control_queue, uci_options])
|
| 70 |
busy_processes += 1
|
| 71 |
print("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 72 |
|
|
@@ -83,14 +80,14 @@ def start(li, user_profile, engine_path, weights=None, threads=None):
|
|
| 83 |
control_stream.join()
|
| 84 |
|
| 85 |
|
| 86 |
-
def play_game(li, game_id,
|
| 87 |
username = li.get_profile()["username"]
|
| 88 |
updates = li.get_game_stream(game_id).iter_lines()
|
| 89 |
|
| 90 |
#Initial response of stream will be the full game info. Store it
|
| 91 |
game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl)
|
| 92 |
board = setup_board(game.state)
|
| 93 |
-
engine =
|
| 94 |
conversation = Conversation(game, engine, li)
|
| 95 |
|
| 96 |
print("+++ {}".format(game.show()))
|
|
@@ -120,8 +117,8 @@ def play_game(li, game_id, engine_path, engine_type, weights, threads, control_q
|
|
| 120 |
control_queue.put_nowait({"type": "local_game_done"})
|
| 121 |
|
| 122 |
|
| 123 |
-
def can_accept_challenge(chlng):
|
| 124 |
-
return chlng.is_supported(
|
| 125 |
|
| 126 |
|
| 127 |
def play_first_move(game, engine, board, li):
|
|
@@ -143,22 +140,6 @@ def setup_board(state):
|
|
| 143 |
return board
|
| 144 |
|
| 145 |
|
| 146 |
-
def setup_engine(engine_path, engine_type, board, weights=None, threads=None, ucioptions=None):
|
| 147 |
-
# print("Loading Engine!")
|
| 148 |
-
commands = [engine_path]
|
| 149 |
-
if weights:
|
| 150 |
-
commands.append("-w")
|
| 151 |
-
commands.append(weights)
|
| 152 |
-
if threads:
|
| 153 |
-
commands.append("-t")
|
| 154 |
-
commands.append(threads)
|
| 155 |
-
|
| 156 |
-
if engine_type == "xboard":
|
| 157 |
-
return engine_wrapper.XBoardEngine(board, commands)
|
| 158 |
-
|
| 159 |
-
return engine_wrapper.UCIEngine(board, commands, ucioptions)
|
| 160 |
-
|
| 161 |
-
|
| 162 |
def is_white_to_move(moves):
|
| 163 |
return (len(moves) % 2) == 0
|
| 164 |
|
|
@@ -190,9 +171,8 @@ if __name__ == "__main__":
|
|
| 190 |
is_bot = upgrade_account(li)
|
| 191 |
|
| 192 |
if is_bot:
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
start(li, user_profile, engine_path, weights_path, cfg.get("threads"))
|
| 197 |
else:
|
| 198 |
print("{} is not a bot account. Please upgrade your it to a bot account!".format(user_profile["username"]))
|
|
|
|
| 28 |
event = json.loads(evnt.decode('utf-8'))
|
| 29 |
control_queue.put_nowait(event)
|
| 30 |
|
| 31 |
+
def start(li, user_profile, max_games, max_queued, engine_factory, config):
|
| 32 |
# init
|
|
|
|
| 33 |
username = user_profile.get("username")
|
| 34 |
print("Welcome {}!".format(username))
|
| 35 |
manager = multiprocessing.Manager()
|
|
|
|
| 51 |
print("+++ Process Free. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 52 |
elif event["type"] == "challenge":
|
| 53 |
chlng = model.Challenge(event["challenge"])
|
| 54 |
+
if len(challenge_queue) < max_queued and can_accept_challenge(chlng, config):
|
| 55 |
challenge_queue.append(chlng)
|
| 56 |
print(" Queue {}".format(chlng.show()))
|
| 57 |
else:
|
|
|
|
| 63 |
else:
|
| 64 |
queued_processes -= 1
|
| 65 |
game_id = event["game"]["id"]
|
| 66 |
+
pool.apply_async(play_game, [li, game_id, control_queue, engine_factory])
|
|
|
|
|
|
|
| 67 |
busy_processes += 1
|
| 68 |
print("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 69 |
|
|
|
|
| 80 |
control_stream.join()
|
| 81 |
|
| 82 |
|
| 83 |
+
def play_game(li, game_id, control_queue, engine_factory):
|
| 84 |
username = li.get_profile()["username"]
|
| 85 |
updates = li.get_game_stream(game_id).iter_lines()
|
| 86 |
|
| 87 |
#Initial response of stream will be the full game info. Store it
|
| 88 |
game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl)
|
| 89 |
board = setup_board(game.state)
|
| 90 |
+
engine = engine_factory(board)
|
| 91 |
conversation = Conversation(game, engine, li)
|
| 92 |
|
| 93 |
print("+++ {}".format(game.show()))
|
|
|
|
| 117 |
control_queue.put_nowait({"type": "local_game_done"})
|
| 118 |
|
| 119 |
|
| 120 |
+
def can_accept_challenge(chlng, config):
|
| 121 |
+
return chlng.is_supported(config)
|
| 122 |
|
| 123 |
|
| 124 |
def play_first_move(game, engine, board, li):
|
|
|
|
| 140 |
return board
|
| 141 |
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
def is_white_to_move(moves):
|
| 144 |
return (len(moves) % 2) == 0
|
| 145 |
|
|
|
|
| 171 |
is_bot = upgrade_account(li)
|
| 172 |
|
| 173 |
if is_bot:
|
| 174 |
+
max_games = CONFIG["max_concurrent_games"]
|
| 175 |
+
max_queued = CONFIG["max_queued_challenges"]
|
| 176 |
+
start(li, user_profile, max_games, max_queued, engine_wrapper.EngineFactory(CONFIG), CONFIG)
|
|
|
|
| 177 |
else:
|
| 178 |
print("{} is not a bot account. Please upgrade your it to a bot account!".format(user_profile["username"]))
|