fix board issues
This commit is contained in:
@@ -28,10 +28,7 @@ socket.addEventListener("open", (event) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.addEventListener("message", (event) => {
|
socket.addEventListener("message", (event) => {
|
||||||
move = event.data;
|
board1.position(event.data)
|
||||||
from = move.substr(0, 2);
|
|
||||||
to = move.substr(2, 2);
|
|
||||||
board1.move(from + '-' + to);
|
|
||||||
console.log("Message from server ", event.data);
|
console.log("Message from server ", event.data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -36,21 +36,28 @@ class Simulate:
|
|||||||
is_white_playing = True
|
is_white_playing = True
|
||||||
while not board.is_game_over():
|
while not board.is_game_over():
|
||||||
play_result = self.white.play(board) if is_white_playing else self.black.play(board)
|
play_result = self.white.play(board) if is_white_playing else self.black.play(board)
|
||||||
yield play_result.move
|
|
||||||
board.push(play_result.move)
|
board.push(play_result.move)
|
||||||
|
yield board
|
||||||
is_white_playing = not is_white_playing
|
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"""
|
""" Entry point of webpage, returns the index html"""
|
||||||
return web.Response(text=load_index(), content_type='text/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"""
|
""" Handles a websocket connection to the frontend"""
|
||||||
ws = web.WebSocketResponse()
|
ws = web.WebSocketResponse()
|
||||||
await ws.prepare(request)
|
await ws.prepare(request)
|
||||||
|
|
||||||
|
|
||||||
async def wait_msg():
|
async def wait_msg():
|
||||||
""" Handles messages from client """
|
""" Handles messages from client """
|
||||||
async for msg in ws:
|
async for msg in ws:
|
||||||
@@ -60,32 +67,35 @@ async def handle_websocket(request):
|
|||||||
elif msg.type == aiohttp.WSMsgType.ERROR:
|
elif msg.type == aiohttp.WSMsgType.ERROR:
|
||||||
print(f'ws connection closed with exception {ws.exception()}')
|
print(f'ws connection closed with exception {ws.exception()}')
|
||||||
|
|
||||||
|
|
||||||
async def turns():
|
async def turns():
|
||||||
""" Simulates the game and sends the response to the client """
|
""" 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():
|
def sim():
|
||||||
return next(runner, None)
|
return next(runner, None)
|
||||||
|
|
||||||
turn = await asyncio.to_thread(sim)
|
board = await asyncio.to_thread(sim)
|
||||||
while turn is not None:
|
while board is not None:
|
||||||
await ws.send_str(turn.uci())
|
await ws.send_str(board.fen())
|
||||||
turn = await asyncio.to_thread(sim)
|
board = await asyncio.to_thread(sim)
|
||||||
|
|
||||||
|
|
||||||
async with asyncio.TaskGroup() as tg:
|
async with asyncio.TaskGroup() as tg:
|
||||||
tg.create_task(wait_msg())
|
tg.create_task(wait_msg())
|
||||||
tg.create_task(turns())
|
tg.create_task(turns())
|
||||||
|
|
||||||
|
|
||||||
print('websocket connection closed')
|
print('websocket connection closed')
|
||||||
return ws
|
return ws
|
||||||
|
|
||||||
def run_app():
|
def run_app(self):
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
app.add_routes([
|
app.add_routes([
|
||||||
web.get('/', handle_index),
|
web.get('/', self.handle_index),
|
||||||
web.get('/ws', handle_websocket),
|
web.get('/ws', self.handle_websocket),
|
||||||
web.static('/img/chesspieces/wikipedia/', _DATA_DIR),
|
web.static('/img/chesspieces/wikipedia/', _DATA_DIR),
|
||||||
])
|
])
|
||||||
web.run_app(app)
|
web.run_app(app)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run_app()
|
WebInterface(engine.ClassicMctsEngine, engine.ClassicMctsEngine).run_app()
|
||||||
|
|||||||
Reference in New Issue
Block a user