From b5f1e33139d1095aec2f12d751052ce19d26eb50 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 13 Jan 2022 00:57:03 +0100 Subject: 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. --- draughts/public/javascripts/game.js | 10 ++++++++-- 1 file 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 = () => { -- cgit v1.2.3