Spaces:
Sleeping
Sleeping
Thibault Duplessis commited on
Commit ·
71114f0
1
Parent(s): da19602
implement !queue command
Browse filesbased on https://github.com/careless25/lichess-bot/commit/5f81e74b7a5b9de8a378eca48ab5eccb84c34f96
- conversation.py +9 -2
- lichess-bot.py +7 -5
conversation.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
from time import time
|
| 2 |
|
| 3 |
class Conversation():
|
| 4 |
-
def __init__(self, game, engine, xhr, version):
|
| 5 |
self.game = game
|
| 6 |
self.engine = engine
|
| 7 |
self.xhr = xhr
|
| 8 |
self.version = version
|
|
|
|
| 9 |
|
| 10 |
command_prefix = "!"
|
| 11 |
|
|
@@ -17,7 +18,7 @@ class Conversation():
|
|
| 17 |
|
| 18 |
def command(self, line, game, cmd):
|
| 19 |
if cmd == "commands" or cmd == "help":
|
| 20 |
-
self.send_reply(line, "Supported commands: !name, !howto,
|
| 21 |
if cmd == "wait" and game.is_abortable():
|
| 22 |
game.abort_in(60)
|
| 23 |
self.send_reply(line, "Waiting 60 seconds...")
|
|
@@ -30,6 +31,12 @@ class Conversation():
|
|
| 30 |
self.send_reply(line, ", ".join(stats))
|
| 31 |
elif cmd == "eval":
|
| 32 |
self.send_reply(line, "I don't tell that to my opponent, sorry.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def send_reply(self, line, reply):
|
| 35 |
self.xhr.chat(self.game.id, line.room, reply)
|
|
|
|
| 1 |
from time import time
|
| 2 |
|
| 3 |
class Conversation():
|
| 4 |
+
def __init__(self, game, engine, xhr, version, challenge_queue):
|
| 5 |
self.game = game
|
| 6 |
self.engine = engine
|
| 7 |
self.xhr = xhr
|
| 8 |
self.version = version
|
| 9 |
+
self.challengers = challenge_queue
|
| 10 |
|
| 11 |
command_prefix = "!"
|
| 12 |
|
|
|
|
| 18 |
|
| 19 |
def command(self, line, game, cmd):
|
| 20 |
if cmd == "commands" or cmd == "help":
|
| 21 |
+
self.send_reply(line, "Supported commands: !name, !howto, !eval, !queue")
|
| 22 |
if cmd == "wait" and game.is_abortable():
|
| 23 |
game.abort_in(60)
|
| 24 |
self.send_reply(line, "Waiting 60 seconds...")
|
|
|
|
| 31 |
self.send_reply(line, ", ".join(stats))
|
| 32 |
elif cmd == "eval":
|
| 33 |
self.send_reply(line, "I don't tell that to my opponent, sorry.")
|
| 34 |
+
elif cmd == "queue":
|
| 35 |
+
if self.challengers:
|
| 36 |
+
challengers = ", ".join(["@" + challenger.challenger_name for challenger in reversed(self.challengers)])
|
| 37 |
+
self.send_reply(line, "Challenge queue: {}".format(challengers))
|
| 38 |
+
else:
|
| 39 |
+
self.send_reply(line, "No challenges queued.")
|
| 40 |
|
| 41 |
def send_reply(self, line, reply):
|
| 42 |
self.xhr.chat(self.game.id, line.room, reply)
|
lichess-bot.py
CHANGED
|
@@ -72,7 +72,7 @@ def start(li, user_profile, engine_factory, config):
|
|
| 72 |
max_games = challenge_config.get("concurrency", 1)
|
| 73 |
logger.info("You're now connected to {} and awaiting challenges.".format(config["url"]))
|
| 74 |
manager = multiprocessing.Manager()
|
| 75 |
-
challenge_queue =
|
| 76 |
control_queue = manager.Queue()
|
| 77 |
control_stream = multiprocessing.Process(target=watch_control_stream, args=[control_queue, li])
|
| 78 |
control_stream.start()
|
|
@@ -90,7 +90,9 @@ def start(li, user_profile, engine_factory, config):
|
|
| 90 |
if chlng.is_supported(challenge_config):
|
| 91 |
challenge_queue.append(chlng)
|
| 92 |
if (challenge_config.get("sort_by", "best") == "best"):
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
else:
|
| 95 |
try:
|
| 96 |
li.decline_challenge(chlng.id)
|
|
@@ -104,7 +106,7 @@ def start(li, user_profile, engine_factory, config):
|
|
| 104 |
else:
|
| 105 |
queued_processes -= 1
|
| 106 |
game_id = event["game"]["id"]
|
| 107 |
-
pool.apply_async(play_game, [li, game_id, control_queue, engine_factory, user_profile, config])
|
| 108 |
busy_processes += 1
|
| 109 |
logger.info("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 110 |
while ((queued_processes + busy_processes) < max_games and challenge_queue): # keep processing the queue until empty or max_games is reached
|
|
@@ -124,7 +126,7 @@ def start(li, user_profile, engine_factory, config):
|
|
| 124 |
control_stream.join()
|
| 125 |
|
| 126 |
@backoff.on_exception(backoff.expo, BaseException, max_time=600, giveup=is_final)
|
| 127 |
-
def play_game(li, game_id, control_queue, engine_factory, user_profile, config):
|
| 128 |
response = li.get_game_stream(game_id)
|
| 129 |
lines = response.iter_lines()
|
| 130 |
|
|
@@ -132,7 +134,7 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config):
|
|
| 132 |
game = model.Game(json.loads(next(lines).decode('utf-8')), user_profile["username"], li.baseUrl, config.get("abort_time", 20))
|
| 133 |
board = setup_board(game)
|
| 134 |
engine = engine_factory(board)
|
| 135 |
-
conversation = Conversation(game, engine, li, __version__)
|
| 136 |
|
| 137 |
logger.info("+++ {}".format(game))
|
| 138 |
|
|
|
|
| 72 |
max_games = challenge_config.get("concurrency", 1)
|
| 73 |
logger.info("You're now connected to {} and awaiting challenges.".format(config["url"]))
|
| 74 |
manager = multiprocessing.Manager()
|
| 75 |
+
challenge_queue = manager.list()
|
| 76 |
control_queue = manager.Queue()
|
| 77 |
control_stream = multiprocessing.Process(target=watch_control_stream, args=[control_queue, li])
|
| 78 |
control_stream.start()
|
|
|
|
| 90 |
if chlng.is_supported(challenge_config):
|
| 91 |
challenge_queue.append(chlng)
|
| 92 |
if (challenge_config.get("sort_by", "best") == "best"):
|
| 93 |
+
list_c = list(challenge_queue)
|
| 94 |
+
list_c.sort(key=lambda c: -c.score())
|
| 95 |
+
challenge_queue = list_c
|
| 96 |
else:
|
| 97 |
try:
|
| 98 |
li.decline_challenge(chlng.id)
|
|
|
|
| 106 |
else:
|
| 107 |
queued_processes -= 1
|
| 108 |
game_id = event["game"]["id"]
|
| 109 |
+
pool.apply_async(play_game, [li, game_id, control_queue, engine_factory, user_profile, config, challenge_queue])
|
| 110 |
busy_processes += 1
|
| 111 |
logger.info("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 112 |
while ((queued_processes + busy_processes) < max_games and challenge_queue): # keep processing the queue until empty or max_games is reached
|
|
|
|
| 126 |
control_stream.join()
|
| 127 |
|
| 128 |
@backoff.on_exception(backoff.expo, BaseException, max_time=600, giveup=is_final)
|
| 129 |
+
def play_game(li, game_id, control_queue, engine_factory, user_profile, config, challenge_queue):
|
| 130 |
response = li.get_game_stream(game_id)
|
| 131 |
lines = response.iter_lines()
|
| 132 |
|
|
|
|
| 134 |
game = model.Game(json.loads(next(lines).decode('utf-8')), user_profile["username"], li.baseUrl, config.get("abort_time", 20))
|
| 135 |
board = setup_board(game)
|
| 136 |
engine = engine_factory(board)
|
| 137 |
+
conversation = Conversation(game, engine, li, __version__, challenge_queue)
|
| 138 |
|
| 139 |
logger.info("+++ {}".format(game))
|
| 140 |
|