diff options
author | nea <romangraef@gmail.com> | 2022-11-27 21:32:34 +0100 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-11-27 21:32:34 +0100 |
commit | a75c2ec5addc65acd8bcb288b5fb89507bf8884c (patch) | |
tree | f29abf6605db36f5a18247cfd711eb9e6faed25b /index.html | |
parent | a091c4415d5153902f28e4bbbfeac3e5764b9019 (diff) | |
download | chess-a75c2ec5addc65acd8bcb288b5fb89507bf8884c.tar.gz chess-a75c2ec5addc65acd8bcb288b5fb89507bf8884c.tar.bz2 chess-a75c2ec5addc65acd8bcb288b5fb89507bf8884c.zip |
allow ending games
Diffstat (limited to 'index.html')
-rw-r--r-- | index.html | 17 |
1 files changed, 13 insertions, 4 deletions
@@ -77,11 +77,11 @@ class Board { constructor(elem) { this.socket = new WebSocket(`${location.protocol.includes('s') ? 'wss' : 'ws'}://${window.location.host}/socket`) - this.exiting = false + this.ended = false this.boardState = {} this.lastMove = '' window.addEventListener('beforeunload', () => { - this.exiting = true + this.ended = false }) this.socket.addEventListener('message', ev => { const message = JSON.parse(ev.data) @@ -89,10 +89,12 @@ this.playerColor = message.player_color || this.playerColor this.boardState = parseFEN(message.board) this.lastMove = message.lastmove || '' + this.ended ||= message.event === 'game_over' + this.result = message.result this.synchronizeBoard() }) this.socket.addEventListener('close', () => { - if (!this.exiting) + if (!this.ended) document.getElementById('warn').classList.remove('invisible') }) let board = document.createElement("table") @@ -138,12 +140,19 @@ })) } + get canPlay() { + return this.isPlayerTurn && !this.ended + } + get isPlayerTurn() { return this.playerColor === this.boardState.turn } synchronizeBoard() { this.turnIndicator.innerHTML = `<b>${capitalize(this.boardState.turn)}</b>s turn` + if (this.ended) { + this.turnIndicator.innerHTML += `<p><b>${this.result}</b></p>` + } for (let field in this.fields) { const fieldDOM = this.fields[field] fieldDOM.innerHTML = "" @@ -153,7 +162,7 @@ piece.addEventListener("dragstart", ev => { ev.dataTransfer.setData("text", field) }) - piece.draggable = this.isPlayerTurn + piece.draggable = this.canPlay fieldDOM.appendChild(piece) } fieldDOM.classList.remove('lastmove') |