summaryrefslogtreecommitdiff
path: root/server.py
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-11-27 21:19:08 +0100
committernea <romangraef@gmail.com>2022-11-27 21:19:08 +0100
commita091c4415d5153902f28e4bbbfeac3e5764b9019 (patch)
treef16c3bb9f598bd802fd57e9716a46b84be7e98b0 /server.py
parenta8ba3505473224c0696724a56898ed40ae19787f (diff)
downloadchess-a091c4415d5153902f28e4bbbfeac3e5764b9019.tar.gz
chess-a091c4415d5153902f28e4bbbfeac3e5764b9019.tar.bz2
chess-a091c4415d5153902f28e4bbbfeac3e5764b9019.zip
Lastmove highlighting
Diffstat (limited to 'server.py')
-rw-r--r--server.py26
1 files changed, 11 insertions, 15 deletions
diff --git a/server.py b/server.py
index f320bc4..8a6b896 100644
--- a/server.py
+++ b/server.py
@@ -35,6 +35,12 @@ async def handle_socket(request: web.Request):
board = chess.Board()
transport, engine = await chess.engine.popen_uci('stockfish')
await ws.send_json(dict(event="ready", player_color='white', board=board.fen()))
+
+ async def send_to_user(message: dict):
+ message['board'] = board.fen()
+ message['legalmoves'] = [m.uci() for m in board.legal_moves]
+ await ws.send_json(message)
+
async for msg in ws:
msg: aiohttp.WSMessage
if msg.type == aiohttp.WSMsgType.TEXT:
@@ -44,24 +50,14 @@ async def handle_socket(request: web.Request):
try:
user_move = chess.Move.from_uci(req['params']['move'])
except ValueError:
- await ws.send_json(dict(
- event="reject_move",
- board=board.fen(),
- ))
+ await send_to_user(dict(event="reject_move"))
continue
if user_move not in board.legal_moves:
- await ws.send_json(dict(
- event="reject_move",
- board=board.fen(),
- ))
+ await send_to_user(dict(event="reject_move"))
continue
board.push(user_move)
- await ws.send_json(dict(
- event='accept_move',
- board=board.fen(),
- lastmove=user_move.uci(),
- ))
+ await send_to_user(dict(event="accept_move", lastmove=user_move.uci()))
candidates: typing.List[chess.engine.InfoDict] = await engine.analyse(
board, chess.engine.Limit(time=1), multipv=100)
@@ -73,8 +69,8 @@ async def handle_socket(request: web.Request):
most_drawy_move: chess.engine.InfoDict = min(candidates, key=appraise)
my_move: chess.Move = (most_drawy_move['pv'][0])
board.push(my_move)
- await ws.send_json(dict(
- event="computer_moved", board=board.fen(),
+ await send_to_user(dict(
+ event="computer_moved",
lastmove=my_move.uci(),
))