Spaces:
Sleeping
Sleeping
TheYoBots commited on
Commit ·
f01cd90
1
Parent(s): 0998d20
New Additions/Corrections
Browse files- Updated few md links that were outdated.
- Corrected explaination of docs after config.yml to config.yml.default change, which had not been done.
Corrected USI_Hash to simply Hash.
- Added capitalisation and punctuations to config.yml.default file.
- Notify engine of final endgame state (depends on python-shogi).
( taken from https://github.com/ShailChoksi/lichess-bot/pull/282 )
- Replace print() with logging.
( taken from https://github.com/ShailChoksi/lichess-bot/pull/281 )
- README.md +5 -3
- config.py +4 -1
- config.yml.default +32 -32
- conversation.py +6 -1
- engine_ctrl.py +10 -7
- engine_wrapper.py +10 -1
- lishogi-bot.py +4 -2
README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# Lishogi Bot
|
| 2 |
|
| 3 |
[](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/python-build.yml)
|
| 4 |
-
[ and Lishogi USI Bots. In case you don't know English, view the [Japanese Translation here (日本語翻訳)](https://github.com/TheYoBots/Lishogi-Bot/wiki/Japanese-Translation).
|
| 7 |
|
|
@@ -20,7 +20,8 @@ virtualenv .venv -p python3 (if this fails you probably need to add Python3 to
|
|
| 20 |
source ./venv/bin/activate
|
| 21 |
python3 -m pip install -r requirements.txt
|
| 22 |
```
|
| 23 |
-
-
|
|
|
|
| 24 |
|
| 25 |
### Windows:
|
| 26 |
- **NOTE:** Only Python 3.7 or later is supported!
|
|
@@ -37,7 +38,8 @@ python3 -m pip install -r requirements.txt
|
|
| 37 |
`./.venv/Scripts/activate` (`.\.venv\Scripts\activate` should work in cmd in administator mode) (This may not work on Windows, and in this case you need to execute "Set-ExecutionPolicy RemoteSigned" first and choose "Y" there (you may need to run Powershell as administrator). After you executed the script, change execution policy back with "Set-ExecutionPolicy Restricted" and pressing "Y")
|
| 38 |
|
| 39 |
`pip install -r requirements.txt`
|
| 40 |
-
-
|
|
|
|
| 41 |
|
| 42 |
## Lishogi OAuth
|
| 43 |
- Create an account for your bot on [Lishogi.org](https://lishogi.org/signup).
|
|
|
|
| 1 |
# Lishogi Bot
|
| 2 |
|
| 3 |
[](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/python-build.yml)
|
| 4 |
+
[](https://lishogi.org/player/bots)
|
| 5 |
|
| 6 |
A bridge between [Lichess API](https://lichess.org/api#tag/Bot) and Lishogi USI Bots. In case you don't know English, view the [Japanese Translation here (日本語翻訳)](https://github.com/TheYoBots/Lishogi-Bot/wiki/Japanese-Translation).
|
| 7 |
|
|
|
|
| 20 |
source ./venv/bin/activate
|
| 21 |
python3 -m pip install -r requirements.txt
|
| 22 |
```
|
| 23 |
+
- Copy `config.yml.default` to `config.yml`.
|
| 24 |
+
- Edit the `config.yml` file to your liking by changing the supported [variants](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L36-L38), [timings](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L39-L45), [challenge modes](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L46-L48) and [incoming challenges](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L25-L35), so that it plays shogi the way you want it to.
|
| 25 |
|
| 26 |
### Windows:
|
| 27 |
- **NOTE:** Only Python 3.7 or later is supported!
|
|
|
|
| 38 |
`./.venv/Scripts/activate` (`.\.venv\Scripts\activate` should work in cmd in administator mode) (This may not work on Windows, and in this case you need to execute "Set-ExecutionPolicy RemoteSigned" first and choose "Y" there (you may need to run Powershell as administrator). After you executed the script, change execution policy back with "Set-ExecutionPolicy Restricted" and pressing "Y")
|
| 39 |
|
| 40 |
`pip install -r requirements.txt`
|
| 41 |
+
- Copy `config.yml.default` to `config.yml`.
|
| 42 |
+
- Edit the `config.yml` file to your liking by changing the supported [variants](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L36-L38), [timings](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L39-L45), [challenge modes](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L46-L48) and [incoming challenges](https://github.com/TheYoBots/Lishogi-Bot/blob/c746023ce8bc95ab7c0f3810921675a8b96610e8/config.yml#L25-35), so that it plays shogi the way you want it to.
|
| 43 |
|
| 44 |
## Lishogi OAuth
|
| 45 |
- Create an account for your bot on [Lishogi.org](https://lishogi.org/signup).
|
config.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
import yaml
|
| 2 |
import os
|
| 3 |
import os.path
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def load_config(config_file):
|
|
@@ -8,7 +11,7 @@ def load_config(config_file):
|
|
| 8 |
try:
|
| 9 |
CONFIG = yaml.safe_load(stream)
|
| 10 |
except Exception as e:
|
| 11 |
-
|
| 12 |
raise e
|
| 13 |
|
| 14 |
# [section, type, error message]
|
|
|
|
| 1 |
import yaml
|
| 2 |
import os
|
| 3 |
import os.path
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
|
| 8 |
|
| 9 |
def load_config(config_file):
|
|
|
|
| 11 |
try:
|
| 12 |
CONFIG = yaml.safe_load(stream)
|
| 13 |
except Exception as e:
|
| 14 |
+
logger.error("There appears to be a syntax problem with your config.yml")
|
| 15 |
raise e
|
| 16 |
|
| 17 |
# [section, type, error message]
|
config.yml.default
CHANGED
|
@@ -1,48 +1,48 @@
|
|
| 1 |
-
token: "xxxxxxxxxxxxxxxx" #
|
| 2 |
-
url: "https://lishogi.org/" #
|
| 3 |
|
| 4 |
-
engine: #
|
| 5 |
-
dir: "./engines/" # dir containing engines, relative to this project
|
| 6 |
-
name: "engine_name" #
|
| 7 |
protocol: "usi" # "usi" protocol. No other options will be accepted here.
|
| 8 |
-
ponder: false #
|
| 9 |
-
# engine_options: #
|
| 10 |
# cpuct: 3.1
|
| 11 |
-
usi_options: #
|
| 12 |
-
Move Overhead: 5000 #
|
| 13 |
-
Threads: 1 #
|
| 14 |
-
|
| 15 |
-
# go_commands: #
|
| 16 |
# nodes: 1 # Search so many nodes only.
|
| 17 |
# depth: 5 # Search depth ply only.
|
| 18 |
# movetime: 1000 # Integer. Search exactly movetime milliseconds.
|
| 19 |
-
silence_stderr: false #
|
| 20 |
|
| 21 |
-
abort_time: 30 #
|
| 22 |
-
fake_think_time: false #
|
| 23 |
-
move_overhead: 5000 #
|
| 24 |
|
| 25 |
-
challenge: #
|
| 26 |
-
concurrency: 1 # Number of games to play simultaneously.
|
| 27 |
-
sort_by: "best" #
|
| 28 |
-
accept_bot: true #
|
| 29 |
only_bot: false # Enable this to accept challenges only from bots. This will disable accepting challenges from human.
|
| 30 |
-
max_increment: 180 #
|
| 31 |
-
min_increment: 0 #
|
| 32 |
-
max_byoyomi: 180 #
|
| 33 |
-
min_byoyomi: 0 #
|
| 34 |
-
max_base: 315360000 #
|
| 35 |
-
min_base: 0 #
|
| 36 |
-
variants: #
|
| 37 |
- standard
|
| 38 |
# - fromPosition
|
| 39 |
-
time_controls: #
|
| 40 |
- ultraBullet
|
| 41 |
- bullet
|
| 42 |
- blitz
|
| 43 |
- rapid
|
| 44 |
- classical
|
| 45 |
-
# - correspondence
|
| 46 |
-
modes: #
|
| 47 |
-
- casual #
|
| 48 |
-
- rated #
|
|
|
|
| 1 |
+
token: "xxxxxxxxxxxxxxxx" # Lishogi OAuth Token with 'play as a bot' enabled.
|
| 2 |
+
url: "https://lishogi.org/" # Lishogi base URL. Do not change this unless locally hosted to an external URL.
|
| 3 |
|
| 4 |
+
engine: # Engine Settings.
|
| 5 |
+
dir: "./engines/" # dir containing engines, relative to this project.
|
| 6 |
+
name: "engine_name" # Binary name of the engine to use. Make sure the engine you use is running under the USI protocol.
|
| 7 |
protocol: "usi" # "usi" protocol. No other options will be accepted here.
|
| 8 |
+
ponder: false # Not supported. Setting it to 'true' will not work.
|
| 9 |
+
# engine_options: # Any custom command line params to pass to the engine.
|
| 10 |
# cpuct: 3.1
|
| 11 |
+
usi_options: # Arbitrary USI options passed to the engine.
|
| 12 |
+
Move Overhead: 5000 # Increase if your bot flags games too often.
|
| 13 |
+
Threads: 1 # Max CPU threads the engine can use.
|
| 14 |
+
Hash: 256 # Max memory (in megabytes) the engine can allocate.
|
| 15 |
+
# go_commands: # Additional options to pass to the USI go command.
|
| 16 |
# nodes: 1 # Search so many nodes only.
|
| 17 |
# depth: 5 # Search depth ply only.
|
| 18 |
# movetime: 1000 # Integer. Search exactly movetime milliseconds.
|
| 19 |
+
silence_stderr: false # Some engines are very noisy.
|
| 20 |
|
| 21 |
+
abort_time: 30 # Time in seconds after which the engine will abort the game if there is no activity.
|
| 22 |
+
fake_think_time: false # Artificially slow down the bot to pretend like it's thinking.
|
| 23 |
+
move_overhead: 5000 # Increase if your bot flags games too often.
|
| 24 |
|
| 25 |
+
challenge: # Incoming challenges
|
| 26 |
+
concurrency: 1 # Number of games to play simultaneously.
|
| 27 |
+
sort_by: "best" # Possible values here are "best", "first"
|
| 28 |
+
accept_bot: true # Accepts challenges coming from other bots. Setting this to 'true' will allow games from both human and bot, but setting it to false will allow games only from human.
|
| 29 |
only_bot: false # Enable this to accept challenges only from bots. This will disable accepting challenges from human.
|
| 30 |
+
max_increment: 180 # Maximum amount of increment to accaept a challenge. The max is 180. Set to 0 for no increment.
|
| 31 |
+
min_increment: 0 # Minimum amount of increment to accept a challenge.
|
| 32 |
+
max_byoyomi: 180 # Maximum amount of increment to accept a challenge. The max is 180. Set to 0 for no byoyomi.
|
| 33 |
+
min_byoyomi: 0 # Minimum amount of increment to accept a challenge.
|
| 34 |
+
max_base: 315360000 # Maximum amount of base time to accept a challenge. The max is 315360000 (10 years).
|
| 35 |
+
min_base: 0 # Minimum amount of base time to accept a challenge.
|
| 36 |
+
variants: # Shogi variants to accept. Currently lishogi only supports 'standard and 'from position' games.
|
| 37 |
- standard
|
| 38 |
# - fromPosition
|
| 39 |
+
time_controls: # Time controls to accept.
|
| 40 |
- ultraBullet
|
| 41 |
- bullet
|
| 42 |
- blitz
|
| 43 |
- rapid
|
| 44 |
- classical
|
| 45 |
+
# - correspondence
|
| 46 |
+
modes: # Game modes to accept.
|
| 47 |
+
- casual # Unrated/Casual games.
|
| 48 |
+
- rated # Rated games.
|
conversation.py
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
class Conversation:
|
| 2 |
def __init__(self, game, engine, xhr, version, challenge_queue):
|
| 3 |
self.game = game
|
|
@@ -9,7 +14,7 @@ class Conversation:
|
|
| 9 |
command_prefix = "!"
|
| 10 |
|
| 11 |
def react(self, line, game):
|
| 12 |
-
|
| 13 |
if (line.text[0] == self.command_prefix):
|
| 14 |
self.command(line, game, line.text[1:].lower())
|
| 15 |
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
|
| 3 |
+
logger = logging.getLogger(__name__)
|
| 4 |
+
|
| 5 |
+
|
| 6 |
class Conversation:
|
| 7 |
def __init__(self, game, engine, xhr, version, challenge_queue):
|
| 8 |
self.game = game
|
|
|
|
| 14 |
command_prefix = "!"
|
| 15 |
|
| 16 |
def react(self, line, game):
|
| 17 |
+
logger.info("*** {} [{}] {}: {}".format(self.game.url(), line.room, line.username, line.text.encode("utf-8")))
|
| 18 |
if (line.text[0] == self.command_prefix):
|
| 19 |
self.command(line, game, line.text[1:].lower())
|
| 20 |
|
engine_ctrl.py
CHANGED
|
@@ -2,6 +2,9 @@ import threading
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import signal
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from util import *
|
| 7 |
|
|
@@ -13,7 +16,7 @@ class Engine:
|
|
| 13 |
|
| 14 |
def set_go_commands(self, go_comm):
|
| 15 |
self.go_commands = go_comm
|
| 16 |
-
|
| 17 |
|
| 18 |
def open_process(self, command, shell=True, _popen_lock=threading.Lock()):
|
| 19 |
kwargs = {
|
|
@@ -82,7 +85,7 @@ class Engine:
|
|
| 82 |
elif command == "option" or command.startswith("Fairy-Stockfish"):
|
| 83 |
pass
|
| 84 |
else:
|
| 85 |
-
|
| 86 |
|
| 87 |
def isready(self):
|
| 88 |
self.send("isready")
|
|
@@ -91,11 +94,11 @@ class Engine:
|
|
| 91 |
if command == "readyok":
|
| 92 |
break
|
| 93 |
elif command == "info" and arg.startswith("string Error! "):
|
| 94 |
-
|
| 95 |
elif command == "info" and arg.startswith("string "):
|
| 96 |
pass
|
| 97 |
else:
|
| 98 |
-
|
| 99 |
|
| 100 |
def setoption(self, name, value):
|
| 101 |
if value is True:
|
|
@@ -111,7 +114,7 @@ class Engine:
|
|
| 111 |
if position != "startpos":
|
| 112 |
position = "sfen " + position
|
| 113 |
self.send("position %s moves %s" % (position, " ".join(moves)))
|
| 114 |
-
|
| 115 |
|
| 116 |
builder = []
|
| 117 |
builder.append("go")
|
|
@@ -142,7 +145,7 @@ class Engine:
|
|
| 142 |
builder.append(str(binc))
|
| 143 |
|
| 144 |
self.send(" ".join(builder))
|
| 145 |
-
|
| 146 |
|
| 147 |
info = {}
|
| 148 |
info["bestmove"] = None
|
|
@@ -213,4 +216,4 @@ class Engine:
|
|
| 213 |
if upperbound:
|
| 214 |
info["score"]["upperbound"] = upperbound
|
| 215 |
else:
|
| 216 |
-
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
import signal
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
from util import *
|
| 10 |
|
|
|
|
| 16 |
|
| 17 |
def set_go_commands(self, go_comm):
|
| 18 |
self.go_commands = go_comm
|
| 19 |
+
logger.info(self.go_commands)
|
| 20 |
|
| 21 |
def open_process(self, command, shell=True, _popen_lock=threading.Lock()):
|
| 22 |
kwargs = {
|
|
|
|
| 85 |
elif command == "option" or command.startswith("Fairy-Stockfish"):
|
| 86 |
pass
|
| 87 |
else:
|
| 88 |
+
logger.error("Unexpected engine response to usi: %s %s" % (command, arg))
|
| 89 |
|
| 90 |
def isready(self):
|
| 91 |
self.send("isready")
|
|
|
|
| 94 |
if command == "readyok":
|
| 95 |
break
|
| 96 |
elif command == "info" and arg.startswith("string Error! "):
|
| 97 |
+
logger.error("Unexpected engine response to isready: %s %s" % (command, arg))
|
| 98 |
elif command == "info" and arg.startswith("string "):
|
| 99 |
pass
|
| 100 |
else:
|
| 101 |
+
logger.error("Unexpected engine response to isready: %s %s" % (command, arg))
|
| 102 |
|
| 103 |
def setoption(self, name, value):
|
| 104 |
if value is True:
|
|
|
|
| 114 |
if position != "startpos":
|
| 115 |
position = "sfen " + position
|
| 116 |
self.send("position %s moves %s" % (position, " ".join(moves)))
|
| 117 |
+
logger.info("position %s moves %s" % (position, " ".join(moves)))
|
| 118 |
|
| 119 |
builder = []
|
| 120 |
builder.append("go")
|
|
|
|
| 145 |
builder.append(str(binc))
|
| 146 |
|
| 147 |
self.send(" ".join(builder))
|
| 148 |
+
logger.info(" ".join(builder))
|
| 149 |
|
| 150 |
info = {}
|
| 151 |
info["bestmove"] = None
|
|
|
|
| 216 |
if upperbound:
|
| 217 |
info["score"]["upperbound"] = upperbound
|
| 218 |
else:
|
| 219 |
+
logger.error("Unexpected engine response to go: %s %s" % (command, arg))
|
engine_wrapper.py
CHANGED
|
@@ -3,6 +3,9 @@ import shogi
|
|
| 3 |
import backoff
|
| 4 |
import subprocess
|
| 5 |
from util import *
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
import engine_ctrl
|
| 8 |
|
|
@@ -45,13 +48,16 @@ class EngineWrapper:
|
|
| 45 |
def name(self):
|
| 46 |
return self.engine.name
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
def quit(self):
|
| 49 |
self.engine.kill_process()
|
| 50 |
|
| 51 |
def print_handler_stats(self, info, stats):
|
| 52 |
for stat in stats:
|
| 53 |
if stat in info:
|
| 54 |
-
|
| 55 |
|
| 56 |
def get_handler_stats(self, info, stats):
|
| 57 |
stats_str = []
|
|
@@ -134,3 +140,6 @@ class USIEngine(EngineWrapper):
|
|
| 134 |
rating = game.opponent.rating if game.opponent.rating is not None else "none"
|
| 135 |
title = game.opponent.title if game.opponent.title else "none"
|
| 136 |
player_type = "computer" if title == "BOT" else "human"
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import backoff
|
| 4 |
import subprocess
|
| 5 |
from util import *
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
import engine_ctrl
|
| 11 |
|
|
|
|
| 48 |
def name(self):
|
| 49 |
return self.engine.name
|
| 50 |
|
| 51 |
+
def report_game_result(self, game, board):
|
| 52 |
+
pass
|
| 53 |
+
|
| 54 |
def quit(self):
|
| 55 |
self.engine.kill_process()
|
| 56 |
|
| 57 |
def print_handler_stats(self, info, stats):
|
| 58 |
for stat in stats:
|
| 59 |
if stat in info:
|
| 60 |
+
logger.info(" {}: {}".format(stat, info[stat]))
|
| 61 |
|
| 62 |
def get_handler_stats(self, info, stats):
|
| 63 |
stats_str = []
|
|
|
|
| 140 |
rating = game.opponent.rating if game.opponent.rating is not None else "none"
|
| 141 |
title = game.opponent.title if game.opponent.title else "none"
|
| 142 |
player_type = "computer" if title == "BOT" else "human"
|
| 143 |
+
def report_game_result(self, game, board):
|
| 144 |
+
self.engine.protocol._position(board)
|
| 145 |
+
|
lishogi-bot.py
CHANGED
|
@@ -201,6 +201,8 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 201 |
ponder_thread = threading.Thread(target=ponder_thread_func, args=(game, engine, ponder_board, wtime, btime, game.state["winc"], game.state["binc"], game.state["byo"]))
|
| 202 |
ponder_thread.start()
|
| 203 |
li.make_move(game.id, best_move)
|
|
|
|
|
|
|
| 204 |
|
| 205 |
while not terminated:
|
| 206 |
try:
|
|
@@ -218,9 +220,9 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 218 |
start_time = time.perf_counter_ns()
|
| 219 |
fake_thinking(config, board, game)
|
| 220 |
if board.turn == shogi.BLACK:
|
| 221 |
-
|
| 222 |
else:
|
| 223 |
-
|
| 224 |
|
| 225 |
best_move = None
|
| 226 |
ponder_move = None
|
|
|
|
| 201 |
ponder_thread = threading.Thread(target=ponder_thread_func, args=(game, engine, ponder_board, wtime, btime, game.state["winc"], game.state["binc"], game.state["byo"]))
|
| 202 |
ponder_thread.start()
|
| 203 |
li.make_move(game.id, best_move)
|
| 204 |
+
elif is_game_over(game):
|
| 205 |
+
engine.report_game_result(game, board)
|
| 206 |
|
| 207 |
while not terminated:
|
| 208 |
try:
|
|
|
|
| 220 |
start_time = time.perf_counter_ns()
|
| 221 |
fake_thinking(config, board, game)
|
| 222 |
if board.turn == shogi.BLACK:
|
| 223 |
+
logger.info('move: {}'.format(int(len(board.move_stack) / 1 + 1)))
|
| 224 |
else:
|
| 225 |
+
logger.info('move: {}'.format(int(len(board.move_stack) / 1 + 0.5)))
|
| 226 |
|
| 227 |
best_move = None
|
| 228 |
ponder_move = None
|