summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--index.html19
-rw-r--r--server.py26
2 files changed, 27 insertions, 18 deletions
diff --git a/index.html b/index.html
index ea4fdbc..7673b63 100644
--- a/index.html
+++ b/index.html
@@ -11,6 +11,7 @@
* {
padding: 0;
margin: 0;
+ box-sizing: border-box;
}
#app {
@@ -35,9 +36,14 @@
.board-field {
text-align: center;
width: 1.5em;
+ border: 1px transparent;
height: 1.5em;
}
+ .lastmove {
+ border: 1px solid green;
+ }
+
#warn {
display: block;
position: fixed;
@@ -73,6 +79,7 @@
this.socket = new WebSocket(`${location.protocol.includes('s') ? 'wss' : 'ws'}://${window.location.host}/socket`)
this.exiting = false
this.boardState = {}
+ this.lastMove = ''
window.addEventListener('beforeunload', () => {
this.exiting = true
})
@@ -81,6 +88,7 @@
console.log(message)
this.playerColor = message.player_color || this.playerColor
this.boardState = parseFEN(message.board)
+ this.lastMove = message.lastmove || ''
this.synchronizeBoard()
})
this.socket.addEventListener('close', () => {
@@ -119,7 +127,7 @@
let uci = fromField + toField
if (((toField[1] === '8' && this.playerColor === 'white')
|| (toField[2] === '1' && this.playerColor === 'black'))
- && (this.boardState[toField].toUpperCase() === 'P')) {
+ && (this.boardState[fromField].toUpperCase() === 'P')) {
uci += window.prompt('promote to what')
}
this.socket.send(JSON.stringify({
@@ -137,7 +145,8 @@
synchronizeBoard() {
this.turnIndicator.innerHTML = `<b>${capitalize(this.boardState.turn)}</b>s turn`
for (let field in this.fields) {
- this.fields[field].innerHTML = ""
+ const fieldDOM = this.fields[field]
+ fieldDOM.innerHTML = ""
if (this.boardState[field]) {
let piece = document.createElement("span")
piece.innerText = notationToPieceUnicode(this.boardState[field])
@@ -145,7 +154,11 @@
ev.dataTransfer.setData("text", field)
})
piece.draggable = this.isPlayerTurn
- this.fields[field].appendChild(piece)
+ fieldDOM.appendChild(piece)
+ }
+ fieldDOM.classList.remove('lastmove')
+ if (this.lastMove.includes(field)) {
+ fieldDOM.classList.add('lastmove')
}
}
}
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(),
))