Skip to content

Commit

Permalink
Don't save ponder task
Browse files Browse the repository at this point in the history
  • Loading branch information
Torom committed Aug 5, 2024
1 parent dc924c0 commit bebd25d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
16 changes: 7 additions & 9 deletions engine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import subprocess
from asyncio import SubprocessTransport, Task, create_task
from asyncio import SubprocessTransport

import chess
from chess.engine import INFO_ALL, InfoDict, Limit, Opponent, Option, UciProtocol, popen_uci
Expand All @@ -13,13 +13,11 @@ def __init__(self,
transport: SubprocessTransport,
engine: UciProtocol,
ponder: bool,
opponent: Opponent,
ponder_task: Task | None) -> None:
opponent: Opponent) -> None:
self.transport = transport
self.engine = engine
self.ponder = ponder
self.opponent = opponent
self.ponder_task = ponder_task

@classmethod
async def from_config(cls,
Expand All @@ -34,7 +32,7 @@ async def from_config(cls,
await cls._configure_engine(engine, uci_options)
await engine.send_opponent_information(opponent=opponent)

return cls(transport, engine, engine_config.ponder, opponent, None)
return cls(transport, engine, engine_config.ponder, opponent)

@classmethod
async def test(cls, engine_config: Engine_Config, syzygy_config: Syzygy_Config) -> None:
Expand Down Expand Up @@ -98,14 +96,14 @@ async def make_move(self,

return result.move, result.info

def start_pondering(self, board: chess.Board) -> None:
async def start_pondering(self, board: chess.Board) -> None:
if self.ponder:
self.ponder_task = create_task(self.engine.analysis(board))
await self.engine.analysis(board)

def stop_pondering(self) -> None:
async def stop_pondering(self) -> None:
if self.ponder:
self.ponder = False
self.ponder_task = create_task(self.engine.analysis(chess.Board(), Limit(time=0.001)))
await self.engine.analysis(chess.Board(), Limit(time=0.001))

async def close(self) -> None:
try:
Expand Down
4 changes: 2 additions & 2 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def run(self) -> None:
if self.lichess_game.is_our_turn:
await self._make_move()
else:
self.lichess_game.start_pondering()
await self.lichess_game.start_pondering()

opponent_title = self.info.black_title if self.lichess_game.is_white else self.info.white_title
abortion_seconds = 30.0 if opponent_title == 'BOT' else 60.0
Expand All @@ -80,7 +80,7 @@ async def run(self) -> None:
if self.lichess_game.is_our_turn:
await self._make_move()
else:
self.lichess_game.start_pondering()
await self.lichess_game.start_pondering()
elif event['type'] == 'gameState':
self.lichess_game.update(event)

Expand Down
10 changes: 5 additions & 5 deletions lichess_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def make_move(self) -> Lichess_Move:

self.board.push(move_response.move)
if not move_response.is_engine_move:
self.engine.start_pondering(self.board)
await self.engine.start_pondering(self.board)

print(f'{move_response.public_message} {move_response.private_message}'.strip())
self.last_message = move_response.public_message
Expand Down Expand Up @@ -120,8 +120,8 @@ def engine_times(self) -> tuple[float, float, float]:

return self.white_time, black_time, self.increment

def start_pondering(self) -> None:
self.engine.start_pondering(self.board)
async def start_pondering(self) -> None:
await self.engine.start_pondering(self.board)

async def end_game(self) -> None:
await self.engine.close()
Expand Down Expand Up @@ -492,7 +492,7 @@ async def _make_gaviota_move(self) -> Move_Response | None:
else:
return

self.engine.stop_pondering()
await self.engine.stop_pondering()
move = random.choice(best_moves)
message = f'Gaviota: {self._format_move(move):14} {egtb_info}'
return Move_Response(move, message, is_drawish=offer_draw, is_resignable=resign)
Expand Down Expand Up @@ -565,7 +565,7 @@ async def _make_syzygy_move(self) -> Move_Response | None:
offer_draw = False
resign = True

self.engine.stop_pondering()
await self.engine.stop_pondering()
move = random.choice(best_moves)
message = f'Syzygy: {self._format_move(move):14} {egtb_info}'
return Move_Response(move, message, is_drawish=offer_draw, is_resignable=resign)
Expand Down

0 comments on commit bebd25d

Please sign in to comment.