AttackingOrDefending commited on
Commit
a21bca3
·
1 Parent(s): 83ec02f

Test bot with yaneuraou (#66)

Browse files
.github/workflows/lishogi-bot-build.yml ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This workflow will install Python dependencies, run lishogi-bot directly in the workflow
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3
+
4
+ name: Lishogi Bot Build
5
+
6
+ on:
7
+ push:
8
+ branches: [ master ]
9
+ pull_request:
10
+ branches: [ master ]
11
+
12
+ jobs:
13
+ build:
14
+
15
+ runs-on: windows-latest
16
+
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - name: Set up Python 3.9
20
+ uses: actions/setup-python@v2
21
+ with:
22
+ python-version: 3.9
23
+ - name: Install dependencies
24
+ run: |
25
+ python -m pip install --upgrade pip
26
+ pip install pytest pytest-timeout
27
+ pip install -r requirements.txt
28
+ - name: Test with pytest
29
+ run: |
30
+ pytest tests.py -s
.github/workflows/python-build.yml CHANGED
@@ -35,6 +35,3 @@ jobs:
35
  flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
36
  # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
37
  flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
38
- - name: Test with pytest
39
- run: |
40
- pytest
 
35
  flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
36
  # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
37
  flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
 
 
 
README.md CHANGED
@@ -1,4 +1,5 @@
1
  # Lishogi Bot
 
2
  [![Python Build](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/python-build.yml/badge.svg)](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/python-build.yml)
3
  [![Lishogi Bots](https://img.shields.io/badge/Lishogi_Bots-%40Bot-blue.svg)](https://lishogi.org/player/bots)
4
 
 
1
  # Lishogi Bot
2
+ [![Lishogi Bot Build](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/lishogi-bot-build.yml/badge.svg)](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/lishogi-bot-build.yml)
3
  [![Python Build](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/python-build.yml/badge.svg)](https://github.com/TheYoBots/Lishogi-Bot/actions/workflows/python-build.yml)
4
  [![Lishogi Bots](https://img.shields.io/badge/Lishogi_Bots-%40Bot-blue.svg)](https://lishogi.org/player/bots)
5
 
test_nothing.py DELETED
@@ -1,5 +0,0 @@
1
- import pytest
2
-
3
-
4
- def test_nothing():
5
- assert True
 
 
 
 
 
 
tests.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import pytest_timeout
3
+ import requests
4
+ import time
5
+ import zipfile
6
+ import yaml
7
+ import shogi
8
+ import shogi.KIF as kif
9
+ from shutil import copyfile
10
+ import importlib
11
+ lishogi_bot = importlib.import_module("lishogi-bot")
12
+
13
+ TOKEN = 'Ao7zQxDqoBhE7tpG'
14
+
15
+
16
+ def test_nothing():
17
+ assert True
18
+
19
+
20
+ def download_yo():
21
+ response = requests.get('https://github.com/mizar/YaneuraOu/releases/download/v6.5.0/YaneuraOu-v6.5.0-windows.zip', allow_redirects=True)
22
+ with open('yo.zip', 'wb') as file:
23
+ file.write(response.content)
24
+ with zipfile.ZipFile('yo.zip', 'r') as zip_ref:
25
+ zip_ref.extractall('.')
26
+ copyfile('./windows/NNUE/YaneuraOu_NNUE-normal-clang++-avx2.exe', 'yo.exe')
27
+
28
+
29
+ def download_suisho():
30
+ response = requests.get('https://github.com/Tama4649/Kristallweizen/blob/master/Kristallweizen_kaiV0.4.zip?raw=true', allow_redirects=True)
31
+ with open('shogi.zip', 'wb') as file:
32
+ file.write(response.content)
33
+ with zipfile.ZipFile('shogi.zip', 'r') as zip_ref:
34
+ zip_ref.extractall('.')
35
+ copyfile('./eval/nn.bin', 'shogi.bin')
36
+
37
+
38
+ def run_bot(CONFIG, logging_level):
39
+ lishogi_bot.logger.info(lishogi_bot.intro())
40
+ li = lishogi_bot.lishogi.Lishogi(CONFIG["token"], CONFIG["url"], lishogi_bot.__version__)
41
+
42
+ user_profile = li.get_profile()
43
+ username = user_profile["username"]
44
+ is_bot = user_profile.get("title") == "BOT"
45
+ lishogi_bot.logger.info("Welcome BOT {}!".format(username))
46
+
47
+ if not is_bot:
48
+ is_bot = lishogi_bot.upgrade_account(li)
49
+
50
+ if is_bot:
51
+ engine_factory = lishogi_bot.partial(lishogi_bot.engine_wrapper.create_engine, CONFIG)
52
+ games = li.get_ongoing_games()
53
+ game_ids = list(map(lambda game: game['gameId'], games))
54
+ for game in game_ids:
55
+ try:
56
+ li.abort(game)
57
+ except:
58
+ pass
59
+ time.sleep(2)
60
+ while li.get_ongoing_games():
61
+ time.sleep(60)
62
+ game_id = li.challenge_ai()['id']
63
+ time.sleep(2)
64
+ games = li.get_ongoing_games()
65
+ game_ids = list(map(lambda game: game['gameId'], games))
66
+ for game in game_ids:
67
+ if game != game_id:
68
+ try:
69
+ li.abort(game)
70
+ except:
71
+ pass
72
+ time.sleep(2)
73
+
74
+ @pytest.mark.timeout(300)
75
+ def run_test():
76
+ lishogi_bot.start(li, user_profile, engine_factory, CONFIG, logging_level, None, one_game=True)
77
+ response = requests.get('https://lishogi.org/game/export/{}'.format(game_id))
78
+ response = response.content.decode()
79
+ parser = kif.Parser()
80
+ summary = parser.parse_str(response)[0]
81
+ starting = 'lishogi' in summary['names'][1]
82
+ board = shogi.Board()
83
+ for move in summary['moves']:
84
+ board.push_usi(move)
85
+ win = board.turn == (shogi.WHITE if starting else shogi.BLACK) and board.is_game_over()
86
+ assert win
87
+
88
+ run_test()
89
+ else:
90
+ lishogi_bot.logger.error("{} is not a bot account. Please upgrade it to a bot account!".format(user_profile["username"]))
91
+ games = li.get_ongoing_games()
92
+ game_ids = list(map(lambda game: game['gameId'], games))
93
+ for game in game_ids:
94
+ try:
95
+ li.abort(game)
96
+ except:
97
+ pass
98
+ time.sleep(2)
99
+
100
+
101
+ def test_bot():
102
+ logging_level = lishogi_bot.logging.INFO # lishogi_bot.logging_level.DEBUG
103
+ lishogi_bot.logging.basicConfig(level=logging_level, filename=None, format="%(asctime)-15s: %(message)s")
104
+ lishogi_bot.enable_color_logging(debug_lvl=logging_level)
105
+ download_suisho()
106
+ lishogi_bot.logger.info("Downloaded Suisho")
107
+ download_yo()
108
+ lishogi_bot.logger.info("Downloaded YaneuraOu")
109
+ with open("./config.yml.default") as file:
110
+ CONFIG = yaml.safe_load(file)
111
+ CONFIG['token'] = TOKEN
112
+ CONFIG['engine']['dir'] = './'
113
+ CONFIG['engine']['name'] = 'yo.exe'
114
+ CONFIG['engine']['usi_options']['EvalFile'] = 'shogi.bin'
115
+ run_bot(CONFIG, logging_level)
116
+
117
+
118
+ if __name__ == '__main__':
119
+ test_bot()