AttackingOrDefending commited on
Commit
fc6e666
·
1 Parent(s): 129a717

GitHub Actions Tests by playing a game (#15)

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 --timeout=300
.github/workflows/python-build.yml CHANGED
@@ -35,7 +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
41
-
 
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,5 +1,6 @@
1
  # Lishogi Bot
2
 
 
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
 
 
1
  # Lishogi Bot
2
 
3
+ [![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)
4
  [![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)
5
  [![Lishogi Bots](https://img.shields.io/badge/Lishogi_Bots-%40Bot-blue.svg)](https://lishogi.org/player/bots)
6
 
config.yml.default CHANGED
@@ -9,7 +9,7 @@ engine: # Engine Settings.
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.
@@ -21,7 +21,7 @@ engine: # Engine Settings.
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
  rate_limiting_delay: 0 # Time (in ms) to delay after sending a move to prevent "Too Many Requests" errors.
24
- move_overhead: 5000 # Increase if your bot flags games too often.
25
 
26
  correspondence:
27
  move_time: 60 # Time in seconds to search in correspondence games.
 
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: 500 # 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.
 
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
  rate_limiting_delay: 0 # Time (in ms) to delay after sending a move to prevent "Too Many Requests" errors.
24
+ move_overhead: 2000 # Increase if your bot flags games too often.
25
 
26
  correspondence:
27
  move_time: 60 # Time in seconds to search in correspondence games.
lishogi-bot.py CHANGED
@@ -97,7 +97,7 @@ def game_logging_configurer(queue, level):
97
  root.setLevel(level)
98
 
99
 
100
- def start(li, user_profile, engine_factory, config, logging_level, log_filename):
101
  challenge_config = config["challenge"]
102
  max_games = challenge_config.get("concurrency", 1)
103
  logger.info("You're now connected to {} and awaiting challenges.".format(config["url"]))
@@ -133,6 +133,8 @@ def start(li, user_profile, engine_factory, config, logging_level, log_filename)
133
  elif event["type"] == "local_game_done":
134
  busy_processes -= 1
135
  logger.info("+++ Process Free. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
 
 
136
  elif event["type"] == "challenge":
137
  chlng = model.Challenge(event["challenge"])
138
  if chlng.is_supported(challenge_config):
 
97
  root.setLevel(level)
98
 
99
 
100
+ def start(li, user_profile, engine_factory, config, logging_level, log_filename, one_game=False):
101
  challenge_config = config["challenge"]
102
  max_games = challenge_config.get("concurrency", 1)
103
  logger.info("You're now connected to {} and awaiting challenges.".format(config["url"]))
 
133
  elif event["type"] == "local_game_done":
134
  busy_processes -= 1
135
  logger.info("+++ Process Free. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
136
+ if one_game:
137
+ break
138
  elif event["type"] == "challenge":
139
  chlng = model.Challenge(event["challenge"])
140
  if chlng.is_supported(challenge_config):
lishogi.py CHANGED
@@ -18,7 +18,8 @@ ENDPOINTS = {
18
  "accept": "/api/challenge/{}/accept",
19
  "decline": "/api/challenge/{}/decline",
20
  "upgrade": "/api/bot/account/upgrade",
21
- "resign": "/api/bot/game/{}/resign"
 
22
  }
23
 
24
 
@@ -104,3 +105,7 @@ class Lishogi:
104
  def set_user_agent(self, username):
105
  self.header.update({"User-Agent": "lishogi-bot/{} user:{}".format(self.version, username)})
106
  self.session.headers.update(self.header)
 
 
 
 
 
18
  "accept": "/api/challenge/{}/accept",
19
  "decline": "/api/challenge/{}/decline",
20
  "upgrade": "/api/bot/account/upgrade",
21
+ "resign": "/api/bot/game/{}/resign",
22
+ "challenge_ai": "/api/challenge/ai"
23
  }
24
 
25
 
 
105
  def set_user_agent(self, username):
106
  self.header.update({"User-Agent": "lishogi-bot/{} user:{}".format(self.version, username)})
107
  self.session.headers.update(self.header)
108
+
109
+ def challenge_ai(self):
110
+ data = {"level": 1, "clock.limit": 60, "clock.increment": 0, "clock.byoyomi": 2, "clock.periods": 1}
111
+ return self.api_post(ENDPOINTS["challenge_ai"], data=data)
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,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import requests
3
+ import time
4
+ import yaml
5
+ from shutil import copyfile
6
+ import importlib
7
+ lishogi_bot = importlib.import_module("lishogi-bot")
8
+
9
+
10
+ def test_nothing():
11
+ assert True
12
+
13
+
14
+ def download_fsf():
15
+ response = requests.get('https://github.com/ianfab/Fairy-Stockfish/releases/download/fairy_sf_13_1/fairy-stockfish-largeboard_x86-64.exe', allow_redirects=True)
16
+ with open('fsf.exe', 'wb') as file:
17
+ file.write(response.content)
18
+
19
+
20
+ def run_bot(CONFIG, logging_level):
21
+ lishogi_bot.logger.info(lishogi_bot.intro())
22
+ li = lishogi_bot.lishogi.Lishogi(CONFIG["token"], CONFIG["url"], lishogi_bot.__version__)
23
+
24
+ user_profile = li.get_profile()
25
+ username = user_profile["username"]
26
+ is_bot = user_profile.get("title") == "BOT"
27
+ lishogi_bot.logger.info("Welcome {}!".format(username))
28
+
29
+ if not is_bot:
30
+ is_bot = lishogi_bot.upgrade_account(li)
31
+
32
+ if is_bot:
33
+ engine_factory = lishogi_bot.partial(lishogi_bot.engine_wrapper.create_engine, CONFIG)
34
+ games = li.get_ongoing_games()
35
+ game_ids = list(map(lambda game: game['gameId'], games))
36
+ for game in game_ids:
37
+ try:
38
+ li.abort(game)
39
+ except:
40
+ pass
41
+ time.sleep(2)
42
+ game_id = li.challenge_ai()['id']
43
+ time.sleep(2)
44
+ games = li.get_ongoing_games()
45
+ game_ids = list(map(lambda game: game['gameId'], games))
46
+ for game in game_ids:
47
+ if game != game_id:
48
+ try:
49
+ li.abort(game)
50
+ except:
51
+ pass
52
+ time.sleep(2)
53
+ lishogi_bot.start(li, user_profile, engine_factory, CONFIG, logging_level, None, one_game=True)
54
+ response = requests.get('https://lishogi.org/game/export/{}'.format(game_id))
55
+ response = response.text
56
+ response = response.lower()
57
+ response = response.split('\n')
58
+ result = list(filter(lambda line: 'result' in line, response))
59
+ result = result[0][9:-2]
60
+ color = list(filter(lambda line: 'sente' in line, response))
61
+ color = 'w' if username.lower() in color[0] else 'b'
62
+ win = result == '1-0' and color == 'w' or result == '0-1' and color == 'b'
63
+ assert win
64
+ else:
65
+ lishogi_bot.logger.error("{} is not a bot account. Please upgrade it to a bot account!".format(user_profile["username"]))
66
+ games = li.get_ongoing_games()
67
+ game_ids = list(map(lambda game: game['gameId'], games))
68
+ for game in game_ids:
69
+ try:
70
+ li.abort(game)
71
+ except:
72
+ pass
73
+ time.sleep(2)
74
+
75
+
76
+ def test_bot():
77
+ logging_level = lishogi_bot.logging.INFO # lichess_bot.logging_level.DEBUG
78
+ lishogi_bot.logging.basicConfig(level=logging_level, filename=None, format="%(asctime)-15s: %(message)s")
79
+ lishogi_bot.enable_color_logging(debug_lvl=logging_level)
80
+ download_fsf()
81
+ lishogi_bot.logger.info("Downloaded Fairy Stockfish")
82
+ with open("./config.yml.default") as file:
83
+ CONFIG = yaml.safe_load(file)
84
+ CONFIG['token'] = 'Ao7zQxDqoBhE7tpG'
85
+ CONFIG['engine']['dir'] = './'
86
+ CONFIG['engine']['name'] = 'fsf.exe'
87
+ run_bot(CONFIG, logging_level)
88
+
89
+
90
+ if __name__ == '__main__':
91
+ test_bot()