fix board issues

This commit is contained in:
Stefan Steininger
2024-01-28 19:12:54 +01:00
parent 70c2a1b9d4
commit 9893da5b58
2 changed files with 52 additions and 45 deletions

View File

@@ -28,10 +28,7 @@ socket.addEventListener("open", (event) => {
});
socket.addEventListener("message", (event) => {
move = event.data;
from = move.substr(0, 2);
to = move.substr(2, 2);
board1.move(from + '-' + to);
board1.position(event.data)
console.log("Message from server ", event.data);
});

View File

@@ -36,21 +36,28 @@ class Simulate:
is_white_playing = True
while not board.is_game_over():
play_result = self.white.play(board) if is_white_playing else self.black.play(board)
yield play_result.move
board.push(play_result.move)
yield board
is_white_playing = not is_white_playing
async def handle_index(request) -> web.Response:
class WebInterface:
def __init__(self, white_engine: engine.Engine.__class__, black_engine: engine.Engine.__class__):
self.white = white_engine
self.black = black_engine
async def handle_index(self, request) -> web.Response:
""" Entry point of webpage, returns the index html"""
return web.Response(text=load_index(), content_type='text/html')
async def handle_websocket(request):
async def handle_websocket(self, request):
""" Handles a websocket connection to the frontend"""
ws = web.WebSocketResponse()
await ws.prepare(request)
async def wait_msg():
""" Handles messages from client """
async for msg in ws:
@@ -60,32 +67,35 @@ async def handle_websocket(request):
elif msg.type == aiohttp.WSMsgType.ERROR:
print(f'ws connection closed with exception {ws.exception()}')
async def turns():
""" Simulates the game and sends the response to the client """
runner = Simulate().run()
runner = Simulate(self.white(chess.WHITE), self.black(chess.BLACK)).run()
def sim():
return next(runner, None)
turn = await asyncio.to_thread(sim)
while turn is not None:
await ws.send_str(turn.uci())
turn = await asyncio.to_thread(sim)
board = await asyncio.to_thread(sim)
while board is not None:
await ws.send_str(board.fen())
board = await asyncio.to_thread(sim)
async with asyncio.TaskGroup() as tg:
tg.create_task(wait_msg())
tg.create_task(turns())
print('websocket connection closed')
return ws
def run_app():
def run_app(self):
app = web.Application()
app.add_routes([
web.get('/', handle_index),
web.get('/ws', handle_websocket),
web.get('/', self.handle_index),
web.get('/ws', self.handle_websocket),
web.static('/img/chesspieces/wikipedia/', _DATA_DIR),
])
web.run_app(app)
if __name__ == '__main__':
run_app()
WebInterface(engine.ClassicMctsEngine, engine.ClassicMctsEngine).run_app()