diff options
-rw-r--r-- | draughts/app.js | 5 | ||||
-rw-r--r-- | draughts/color.js | 7 | ||||
-rw-r--r-- | draughts/game.js | 2 | ||||
-rw-r--r-- | draughts/public/game.html | 1 | ||||
-rw-r--r-- | draughts/public/javascripts/color.js | 5 | ||||
-rw-r--r-- | draughts/public/javascripts/game.js | 12 |
6 files changed, 16 insertions, 16 deletions
diff --git a/draughts/app.js b/draughts/app.js index 88b6b61..c736a4a 100644 --- a/draughts/app.js +++ b/draughts/app.js @@ -3,6 +3,7 @@ const WebSocket = require("ws") const Express = require("express") const Game = require("./game") +const Color = require("./public/javascripts/color") const Messages = require("./public/javascripts/messages") const Environment = require("./environment") const indexRouter = require("./routes/index") @@ -37,11 +38,11 @@ wss.on("connection", ws => { game = new Game(env.games.length) /* The number of games servers as a unique game ID */ env.games.push(game) game.bluePlayer = ws - game.messageClient({ head: Messages.WELCOME, body: "b" }, ws) + game.messageClient({ head: Messages.WELCOME, body: Color.BLUE }, ws) } else { game.redPlayer = ws game.ongoing = true - game.messageClient({ head: Messages.WELCOME, body: "r" }, ws) + game.messageClient({ head: Messages.WELCOME, body: Color.RED }, ws) game.nextTurn() } diff --git a/draughts/color.js b/draughts/color.js deleted file mode 100644 index 72361e8..0000000 --- a/draughts/color.js +++ /dev/null @@ -1,7 +0,0 @@ -/* A basic enumeration representing the two possible piece colors */ -const Color = { - BLUE: 0, - RED: 1 -} - -module.exports = Color diff --git a/draughts/game.js b/draughts/game.js index db9795a..d034746 100644 --- a/draughts/game.js +++ b/draughts/game.js @@ -1,7 +1,7 @@ const WebSocket = require("ws") -const Color = require("./color") const Piece = require("./piece") +const Color = require("./public/javascripts/color") const Messages = require("./public/javascripts/messages") /* diff --git a/draughts/public/game.html b/draughts/public/game.html index f49df7d..42c52c2 100644 --- a/draughts/public/game.html +++ b/draughts/public/game.html @@ -6,6 +6,7 @@ <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="stylesheets/game.css" /> + <script src="javascripts/color.js" defer></script> <script src="javascripts/messages.js" defer></script> <script src="javascripts/game.js" defer></script> <title>Draughts</title> diff --git a/draughts/public/javascripts/color.js b/draughts/public/javascripts/color.js new file mode 100644 index 0000000..0449161 --- /dev/null +++ b/draughts/public/javascripts/color.js @@ -0,0 +1,5 @@ +/* A basic enumeration for the two different player colors */ +(function (exports) { + exports.BLUE = "b" + exports.RED = "r" +})(typeof(exports) == "undefined" ? (this.Color == {}) : exports) diff --git a/draughts/public/javascripts/game.js b/draughts/public/javascripts/game.js index a13fc36..20c9935 100644 --- a/draughts/public/javascripts/game.js +++ b/draughts/public/javascripts/game.js @@ -5,8 +5,9 @@ const ws = new WebSocket("ws://localhost:3000") let legalMoves, // Array of moves :: Moves that each piece can legally make ourTurn, // Boolean :: Is it currently our turn? selectedPiece, // Piece :: The current HTML piece we have selected - colorPrefix, // Character :: Either 'b' or 'r' depending on the clients side - opponentPrefix // Character :: The inverse of `colorPrefix' + colorPrefix // Color :: Either Color.BLUE or Color.RED depending on the clients side + +const opponentPrefix = () => colorPrefix == Color.BLUE ? Color.RED : Color.BLUE /* Remove all the position markers from the board */ const removeMarkers = () => Array @@ -34,7 +35,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) - removeCaptures(captures, opponentPrefix) + removeCaptures(captures, opponentPrefix()) /* Move the selected piece, unselect it, remove the markers, and end our turn */ selectedPiece.style.transform = img.style.transform @@ -59,12 +60,12 @@ 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") + let node = document.getElementById(color == Color.BLUE ? "delta-red" : "delta-blue") node.innerHTML = `+${Number(node.innerHTML) + captures.length}` } const movePiece = ({ id, position, captures }) => { - document.getElementById(opponentPrefix + id).style.transform = + document.getElementById(opponentPrefix() + id).style.transform = `translate(${position.x * 100}%, ${position.y * 100}%)` removeCaptures(captures, colorPrefix) } @@ -101,7 +102,6 @@ ws.addEventListener("message", ({ data }) => { switch (data.head) { case Messages.WELCOME: colorPrefix = data.body - opponentPrefix = colorPrefix == "b" ? "r" : "b" setupPieceEventListeners() break case Messages.RESIGN: |