TheYoBots commited on
Commit
bc06f7d
·
1 Parent(s): 317d1d8

Rewrite certain functions in lishogi-bot.py

Browse files

- Delete unneeded ‘ponder_thread = None’ line.
- Delete redundant else case.
- Combine ping code for black and white.
- Create function for fake thinking.
- Group exception handlers in main game loop.

Files changed (1) hide show
  1. lishogi-bot.py +18 -20
lishogi-bot.py CHANGED
@@ -186,10 +186,8 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
186
  btime = game.state["btime"]
187
  start_time = time.perf_counter_ns()
188
 
189
- if board.turn == shogi.BLACK:
190
- wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
191
- else:
192
- btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
193
  logger.info("Searching for wtime {} btime {}".format(wtime, btime))
194
  best_move, ponder_move = engine.search_with_ponder(board, wtime, btime, game.state["winc"], game.state["binc"])
195
  engine.print_stats()
@@ -199,10 +197,9 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
199
  ponder_board.push(shogi.Move.from_usi(best_move))
200
  ponder_board.push(shogi.Move.from_usi(ponder_move))
201
  ponder_usi = ponder_move
202
- if board.turn == shogi.BLACK:
203
- wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + game.state["winc"])
204
- else:
205
- btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + game.state["binc"])
206
  logger.info("Pondering for wtime {} btime {}".format(wtime, btime))
207
  ponder_thread = threading.Thread(target=ponder_thread_func, args=(game, engine, ponder_board, wtime, btime, game.state["winc"], game.state["binc"]))
208
  ponder_thread.start()
@@ -211,9 +208,7 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
211
  while not terminated:
212
  try:
213
  binary_chunk = next(lines)
214
- except(StopIteration):
215
- break
216
- try:
217
  upd = json.loads(binary_chunk.decode('utf-8')) if binary_chunk else None
218
  u_type = upd["type"] if upd else "ping"
219
  if u_type == "chatLine":
@@ -224,11 +219,7 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
224
  if len(moves) > 0 and len(moves) != len(board.move_stack):
225
  board = update_board(board, moves[-1])
226
  if not is_game_over(game) and is_engine_move(game, moves):
227
- if config.get("fake_think_time") and len(moves) > 9:
228
- delay = min(game.clock_initial, game.my_remaining_seconds()) * 0.015
229
- accel = 1 - max(0, min(100, len(moves) - 20)) / 150
230
- sleep = min(5, delay * accel)
231
- time.sleep(sleep)
232
 
233
  best_move = None
234
  ponder_move = None
@@ -278,16 +269,15 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
278
  li.abort(game.id)
279
  break
280
  except (HTTPError, ReadTimeout, RemoteDisconnected, ChunkedEncodingError, ConnectionError, ProtocolError):
281
- if game.id in (ongoing_game["gameId"] for ongoing_game in li.get_ongoing_games()):
282
- continue
283
- else:
284
  break
 
 
285
 
286
  logger.info("--- {} Game over".format(game.url()))
287
  engine.stop()
288
  if not ( ponder_thread is None ):
289
  ponder_thread.join()
290
- ponder_thread = None
291
 
292
  # This can raise queue.NoFull, but that should only happen if we're not processing
293
  # events fast enough and in this case I believe the exception should be raised
@@ -313,6 +303,14 @@ def get_book_move(board, config):
313
  pass
314
 
315
 
 
 
 
 
 
 
 
 
316
  def setup_board(game):
317
  if game.variant_name == "From Position":
318
  board = shogi.Board(makesfenfromfen(game.initial_fen))
 
186
  btime = game.state["btime"]
187
  start_time = time.perf_counter_ns()
188
 
189
+ wb = 'w' if board.turn == shogi.BLACK else 'b'
190
+ game.ping(config.get("abort_time", 30), (upd[f"{wb}time"] + upd[f"{wb}inc"]) / 1000 + 60)
 
 
191
  logger.info("Searching for wtime {} btime {}".format(wtime, btime))
192
  best_move, ponder_move = engine.search_with_ponder(board, wtime, btime, game.state["winc"], game.state["binc"])
193
  engine.print_stats()
 
197
  ponder_board.push(shogi.Move.from_usi(best_move))
198
  ponder_board.push(shogi.Move.from_usi(ponder_move))
199
  ponder_usi = ponder_move
200
+
201
+ wb = 'w' if board.turn == shogi.BLACK else 'b'
202
+ game.ping(config.get("abort_time", 30), (upd[f"{wb}time"] + upd[f"{wb}inc"]) / 1000 + 60)
 
203
  logger.info("Pondering for wtime {} btime {}".format(wtime, btime))
204
  ponder_thread = threading.Thread(target=ponder_thread_func, args=(game, engine, ponder_board, wtime, btime, game.state["winc"], game.state["binc"]))
205
  ponder_thread.start()
 
208
  while not terminated:
209
  try:
210
  binary_chunk = next(lines)
211
+
 
 
212
  upd = json.loads(binary_chunk.decode('utf-8')) if binary_chunk else None
213
  u_type = upd["type"] if upd else "ping"
214
  if u_type == "chatLine":
 
219
  if len(moves) > 0 and len(moves) != len(board.move_stack):
220
  board = update_board(board, moves[-1])
221
  if not is_game_over(game) and is_engine_move(game, moves):
222
+ fake_thinking(config, board, game)
 
 
 
 
223
 
224
  best_move = None
225
  ponder_move = None
 
269
  li.abort(game.id)
270
  break
271
  except (HTTPError, ReadTimeout, RemoteDisconnected, ChunkedEncodingError, ConnectionError, ProtocolError):
272
+ if game.id not in (ongoing_game["gameId"] for ongoing_game in li.get_ongoing_games()):
 
 
273
  break
274
+ except StopIteration:
275
+ break
276
 
277
  logger.info("--- {} Game over".format(game.url()))
278
  engine.stop()
279
  if not ( ponder_thread is None ):
280
  ponder_thread.join()
 
281
 
282
  # This can raise queue.NoFull, but that should only happen if we're not processing
283
  # events fast enough and in this case I believe the exception should be raised
 
303
  pass
304
 
305
 
306
+ def fake_thinking(config, board, game):
307
+ if config.get("fake_think_time") and len(board.move_stack) > 9:
308
+ delay = min(game.clock_initial, game.my_remaining_seconds()) * 0.015
309
+ accel = 1 - max(0, min(100, len(board.move_stack) - 20)) / 150
310
+ sleep = min(5, delay * accel)
311
+ time.sleep(sleep)
312
+
313
+
314
  def setup_board(game):
315
  if game.variant_name == "From Position":
316
  board = shogi.Board(makesfenfromfen(game.initial_fen))