Centralize some Piece logic.

Using new logic.js
Also a bit of server.js cleanup.
This commit is contained in:
Sage Vaillancourt 2022-10-12 23:12:43 -04:00
parent 7fd455fe6d
commit 3b8d679cac
6 changed files with 85 additions and 150 deletions

View File

@ -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);
}
}

View File

@ -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()) {

View File

@ -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);
}
});

View File

@ -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);
}
});

View File

@ -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()) {

66
src/logic.js Normal file
View File

@ -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 }