diff options
author | Thomas Voss <thomasvoss@live.com> | 2022-01-13 00:57:03 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2022-01-13 00:57:03 +0100 |
commit | b5f1e33139d1095aec2f12d751052ce19d26eb50 (patch) | |
tree | 26c0322729c8f1c9d017ef4c76164aa60ee72d45 | |
parent | 783e74876b324244270721deda2df217ea9ba4f6 (diff) |
Remove duplicate code
Instead of duplicating code for removing captured pieces, we can use a
general function that both removes pieces *and* updates the piece delta.
-rw-r--r-- | draughts/public/javascripts/game.js | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/draughts/public/javascripts/game.js b/draughts/public/javascripts/game.js index ffaf246..a13fc36 100644 --- a/draughts/public/javascripts/game.js +++ b/draughts/public/javascripts/game.js @@ -34,7 +34,7 @@ const addMarker = ({ x, y, captures }) => { /* Get the ID of the selected piece so we can include it in the message to the server */ let id = selectedPiece.id.slice(1) - captures.forEach(p => document.getElementById(opponentPrefix + p.id).remove()) + removeCaptures(captures, opponentPrefix) /* Move the selected piece, unselect it, remove the markers, and end our turn */ selectedPiece.style.transform = img.style.transform @@ -57,10 +57,16 @@ const addMarker = ({ x, y, captures }) => { }) } +const removeCaptures = (captures, color) => { + captures.forEach(p => document.getElementById(color + p.id).remove()) + let node = document.getElementById(color == "b" ? "delta-red" : "delta-blue") + node.innerHTML = `+${Number(node.innerHTML) + captures.length}` +} + const movePiece = ({ id, position, captures }) => { document.getElementById(opponentPrefix + id).style.transform = `translate(${position.x * 100}%, ${position.y * 100}%)` - captures.forEach(p => document.getElementById(colorPrefix + p.id).remove()) + removeCaptures(captures, colorPrefix) } const setupPieceEventListeners = () => { |