diff --git a/server.js b/server.js index 2ef7d10..c0be258 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,8 @@ -const {Board, Piece} = require('./src/backend'); +const { Board } = require('./src/backend'); -const express = require("express"); -const http = require("http"); -const socketIo = require("socket.io"); +const express = require('express'); +const http = require('http'); +const socketIo = require('socket.io'); const port = process.env.PORT || 4001; @@ -18,20 +18,15 @@ const io = socketIo(server, { let interval; -let board = new Board(); - -let sockets = []; let games = new Map(); io.on("connection", (socket) => { - sockets.push(socket); console.log("New client connected"); if (interval) { clearInterval(interval); } //interval = setInterval(() => getApiAndEmit(socket), 5000); socket.on("disconnect", () => { - // TODO sockets.remove(socket) console.log("Client disconnected"); clearInterval(interval); }); @@ -58,9 +53,9 @@ io.on("connection", (socket) => { socket.on("move", (key, move) => { console.log(`Received move at '${key}'`); let game = games.get(key); - if(move && game.board.makeMove(move.from, move.to) == 0) { + if(move && game.board.makeMove(move.from, move.to) === 0) { games.set(key, game); - for (s of game.sockets) { + for (const s of game.sockets) { getApiAndEmit(s, game.board); } } diff --git a/src/backend.js b/src/backend.js index 6421a48..b7eb41e 100644 --- a/src/backend.js +++ b/src/backend.js @@ -1,3 +1,6 @@ +import { Piece } from './logic' + + const BLACK = 0; const WHITE = 1; const PAWN = 0; @@ -36,71 +39,6 @@ function imageFromPiece(piece) { return null; } -class Piece { - constructor(color, type) { - this.color = color; - this.type = type; - this.passantable = false; - this.moves = 0; - } - - setType(type) { - this.type = type; - } - - getInfoText() { - if(this.moves === 1) { - return "Has made 1 move" - } else { - return "Has made " + this.moves + " moves" - } - } - - isEmpty() { - return this.type === EMPTY; - } - - isFull() { - return !this.isEmpty(); - } - - isBlack() { - return this.color === BLACK; - } - - isWhite() { - return this.color === WHITE; - } - - isEnemyOf(piece) { - if (this.color === EMPTY || piece.color === EMPTY) { - return false; - } else { - return this.color !== piece.color; - } - } - - isFriendOf(piece) { - if (this.color === EMPTY || piece.color === EMPTY) { - return false; - } else { - return this.color === piece.color; - } - } - - is(type) { - return this.type === type; - } - - hasMoved() { - return this.moves !== 0; - } - - hasntMoved() { - return !this.hasMoved(); - } -} - class Board { constructor(props) { this.state = (props && props.text) ? @@ -190,7 +128,7 @@ class Board { default: return '_'; } - }).join('');; + }).join(''); } doReset() { @@ -330,12 +268,12 @@ class Board { } else if (piece.is(KING)) { [[1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0]] .forEach(delta => tryAddMove(x + delta[0], y + delta[1])); - if (piece.hasntMoved()) { + if (piece.hasNotMoved()) { const kingIndex = this.findIndex(piece); const [x, y] = this.getXandY(kingIndex); let leftRook = this.pieceAt(0, y); - if(leftRook.is(ROOK) && leftRook.hasntMoved()) { + if(leftRook.is(ROOK) && leftRook.hasNotMoved()) { // Check if spaces between rook and king are empty if(this.pieceAt(1, y).isEmpty() && this.pieceAt(2, y).isEmpty() && @@ -351,7 +289,7 @@ class Board { } let rightRook = this.pieceAt(7, y); - if(rightRook.is(ROOK) && rightRook.hasntMoved()) { + if(rightRook.is(ROOK) && rightRook.hasNotMoved()) { // Check if spaces between rook and king are empty if(this.pieceAt(5, y).isEmpty() && this.pieceAt(6, y).isEmpty()) { diff --git a/src/backend.test.js b/src/backend.test.js index d88f185..21a0d68 100644 --- a/src/backend.test.js +++ b/src/backend.test.js @@ -48,7 +48,7 @@ it('is created correctly', () => { ]; expect(board.textFromState()).toBe(rows.join('')); for (const square of board.state.squares) { - expect(square.hasntMoved()).toBe(true); + expect(square.hasNotMoved()).toBe(true); } }); diff --git a/src/board.test.js b/src/board.test.js index ff1bff2..dbb9883 100644 --- a/src/board.test.js +++ b/src/board.test.js @@ -52,7 +52,7 @@ it('is created correctly', () => { ]; expect(board.textFromState()).toBe(rows.join('')); for (const square of board.state.squares) { - expect(square.hasntMoved()).toBe(true); + expect(square.hasNotMoved()).toBe(true); } }); diff --git a/src/components/Board.js b/src/components/Board.js index 0108a6f..0b13a19 100644 --- a/src/components/Board.js +++ b/src/components/Board.js @@ -2,6 +2,7 @@ import React from 'react'; import Popup from './Popup'; import '../index.css'; +import { Piece } from '../logic'; const BLACK = 0; @@ -75,71 +76,6 @@ const Square = ({ bgColor, onClick, piece, className = "" }) => { ); } -class Piece { - constructor(color, type) { - this.color = color; - this.type = type; - this.passantable = false; - this.moves = 0; - } - - setType(type) { - this.type = type; - } - - getInfoText() { - if(this.moves === 1) { - return "Has made 1 move" - } else { - return "Has made " + this.moves + " moves" - } - } - - isEmpty() { - return this.type === EMPTY; - } - - isFull() { - return !this.isEmpty(); - } - - isBlack() { - return this.color === BLACK; - } - - isWhite() { - return this.color === WHITE; - } - - isEnemyOf(piece) { - if (this.color === EMPTY || piece.color === EMPTY) { - return false; - } else { - return this.color !== piece.color; - } - } - - isFriendOf(piece) { - if (this.color === EMPTY || piece.color === EMPTY) { - return false; - } else { - return this.color === piece.color; - } - } - - is(type) { - return this.type === type; - } - - hasMoved() { - return this.moves !== 0; - } - - hasntMoved() { - return !this.hasMoved(); - } -} - class Board extends React.Component { constructor(props) { super(props); @@ -380,12 +316,12 @@ class Board extends React.Component { } else if (piece.is(KING)) { [[1, 1], [1, -1], [-1, 1], [-1, -1], [0, 1], [0, -1], [1, 0], [-1, 0]] .forEach(delta => tryAddMove(x + delta[0], y + delta[1])); - if (piece.hasntMoved()) { + if (piece.hasNotMoved()) { const kingIndex = this.findIndex(piece); const [x, y] = this.getXandY(kingIndex); let leftRook = this.pieceAt(0, y); - if(leftRook.is(ROOK) && leftRook.hasntMoved()) { + if(leftRook.is(ROOK) && leftRook.hasNotMoved()) { // Check if spaces between rook and king are empty if(this.pieceAt(1, y).isEmpty() && this.pieceAt(2, y).isEmpty() && @@ -401,7 +337,7 @@ class Board extends React.Component { } let rightRook = this.pieceAt(7, y); - if(rightRook.is(ROOK) && rightRook.hasntMoved()) { + if(rightRook.is(ROOK) && rightRook.hasNotMoved()) { // Check if spaces between rook and king are empty if(this.pieceAt(5, y).isEmpty() && this.pieceAt(6, y).isEmpty()) { diff --git a/src/logic.js b/src/logic.js new file mode 100644 index 0000000..7721b04 --- /dev/null +++ b/src/logic.js @@ -0,0 +1,66 @@ +class Piece { + constructor(color, type) { + this.color = color; + this.type = type; + this.passantable = false; + this.moves = 0; + } + + setType(type) { + this.type = type; + } + + getInfoText() { + if(this.moves === 1) { + return "Has made 1 move" + } else { + return "Has made " + this.moves + " moves" + } + } + + isEmpty() { + return this.type === EMPTY; + } + + isFull() { + return !this.isEmpty(); + } + + isBlack() { + return this.color === BLACK; + } + + isWhite() { + return this.color === WHITE; + } + + isEnemyOf(piece) { + if (this.color === EMPTY || piece.color === EMPTY) { + return false; + } else { + return this.color !== piece.color; + } + } + + isFriendOf(piece) { + if (this.color === EMPTY || piece.color === EMPTY) { + return false; + } else { + return this.color === piece.color; + } + } + + is(type) { + return this.type === type; + } + + hasMoved() { + return this.moves !== 0; + } + + hasNotMoved() { + return !this.hasMoved(); + } +} + +export { Piece } \ No newline at end of file