Compare commits

..

No commits in common. "5e21219d005563258161fbd30d36918d7ba88a21" and "b4d75a78558e9c7045b9021845cee711607ec28f" have entirely different histories.

10 changed files with 13899 additions and 2151 deletions

View File

@ -1,7 +1,8 @@
# QuickChess # QuickChess
This is a simple implementation of Chess in React. It supports all moves, including castling and en passant. A live build can be seen here: This is a simple implementation of Chess in React. It supports nearly all moves,
[https://quickchess.win/](https://quickchess.win/). Assets with castling still to be added. A live build can be seen here:
[https://sagev.space/quickchess/](https://sagev.space/quickchess/). Assets
borrowed from Wikipedia. borrowed from Wikipedia.
# Building # Building

15898
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,14 @@
{ {
"name": "quickchess", "name": "chess",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"type": "module",
"dependencies": { "dependencies": {
"@testing-library/jest-dom": "^5.11.4", "@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^14.0.0", "@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10", "@testing-library/user-event": "^12.1.10",
"express": "^4.17.1", "express": "^4.17.1",
"react": "^18.2.0", "react": "^17.0.1",
"react-dom": "^18.2.0", "react-dom": "^17.0.1",
"react-scripts": "5.0.1", "react-scripts": "5.0.1",
"socket.io": "^3.0.5", "socket.io": "^3.0.5",
"socket.io-client": "^3.0.5", "socket.io-client": "^3.0.5",
@ -17,10 +16,9 @@
}, },
"scripts": { "scripts": {
"start": "react-scripts start", "start": "react-scripts start",
"build": "npm ci && react-scripts build", "build": "react-scripts build",
"test": "react-scripts test", "test": "react-scripts test",
"eject": "react-scripts eject", "eject": "react-scripts eject"
"server": "node server.js"
}, },
"eslintConfig": { "eslintConfig": {
"extends": [ "extends": [
@ -40,8 +38,8 @@
"last 1 safari version" "last 1 safari version"
] ]
}, },
"homepage": "https://quickchess.sage.boats", "homepage": "./",
"devDependencies": { "devDependencies": {
"react-test-renderer": "^18.2.0" "react-test-renderer": "^17.0.1"
} }
} }

View File

@ -1,9 +1,9 @@
import { textFromBoard } from './src/logic.js' const { textFromBoard } = require('./src/logic')
import { Board } from './src/backend.js'; const { Board } = require('./src/backend');
import express from 'express'; const express = require('express');
import http from 'http'; const http = require('http');
import { Server } from 'socket.io'; const socketIo = require('socket.io');
const port = process.env.PORT || 4001; const port = process.env.PORT || 4001;
@ -11,7 +11,7 @@ const app = express();
const server = http.createServer(app); const server = http.createServer(app);
const io = new Server(server, { const io = socketIo(server, {
cors: { cors: {
cors: true, cors: true,
} }
@ -28,9 +28,7 @@ io.on("connection", (socket) => {
} }
//interval = setInterval(() => getApiAndEmit(socket), 5000); //interval = setInterval(() => getApiAndEmit(socket), 5000);
socket.on("disconnect", () => { socket.on("disconnect", () => {
const game = socket.connectedGame console.log("Client disconnected");
const gameMessage = game ? `from game '${game.key}'` : "and was not attached to a game"
console.log("Client disconnected " + gameMessage);
clearInterval(interval); clearInterval(interval);
}); });
@ -43,14 +41,12 @@ io.on("connection", (socket) => {
game.sockets.push(socket); game.sockets.push(socket);
} else { } else {
game = { game = {
key,
sockets: [socket], sockets: [socket],
board: new Board(), board: new Board(),
moveDate: null, moveDate: null,
}; };
console.log(`Created game '${key}'`); console.log(`Created game '${key}'`);
} }
socket.connectedGame = game;
games.set(key, game); games.set(key, game);
getApiAndEmit(socket, game.board); getApiAndEmit(socket, game.board);
}); });

View File

@ -1,4 +1,16 @@
import { Piece, startingBoard, BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY } from './logic.js' import { Piece } from './logic'
const BLACK = 0; const WHITE = 1;
const PAWN = 0;
const ROOK = 1;
const KNIGHT = 2;
const BISHOP = 3;
const QUEEN = 4;
const KING = 5;
const EMPTY = -1;
const SHUFFLING_ENABLED = 0; const SHUFFLING_ENABLED = 0;
@ -15,10 +27,22 @@ function settingText(setting) {
} }
} }
export class Board { function range(n) {
return Array.from(Array(n).keys());
}
function imageFromPiece(piece) {
if (piece && piece.type >= 0) {
const image = piece.color === WHITE ? piece.type : piece.type + 6;
return Images[image];
}
return null;
}
class Board {
constructor(props) { constructor(props) {
this.state = props?.text ? this.state = props?.text ?
this.stateFromText(props.text) : startingBoard(); this.stateFromText(props.text) : this.originalState();
} }
setHand(hand) { setHand(hand) {
@ -428,6 +452,8 @@ export class Board {
} }
} }
module.exports = { Board }
// export default Board; // export default Board;
// export {Board, Piece}; // export {Board, Piece};
// export { BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY }; // export { BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY };

View File

@ -1,8 +1,9 @@
import React from 'react'; import React from 'react';
import Popup from './Popup.js'; import Popup from './Popup';
import '../index.css'; import '../index.css';
import { Piece, startingBoard, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY, BLACK, WHITE } from '../logic.js'; import { Piece, startingBoard, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY, BLACK, WHITE } from '../logic';
const Images = [ const Images = [
'./white_pawn.svg', './white_pawn.svg',
@ -619,4 +620,5 @@ class Board extends React.Component {
} }
export default Board; export default Board;
export { Piece, BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY }; export {Piece};
export { BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY };

View File

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import './style.css'; import './style.css';
class Popup extends React.Component { class Popup extends React.Component {
render() { render() {
return ( return (
<div className='popup'> <div className='popup'>

View File

@ -10,13 +10,6 @@ body {
justify-content: center; justify-content: center;
} }
#root {
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
}
ol, ul { ol, ul {
padding-left: 30px; padding-left: 30px;
} }
@ -61,11 +54,6 @@ ol, ul {
} }
} }
.game {
height: 100%;
display: flex;
}
.square { .square {
background: #fff; background: #fff;
border: none; border: none;
@ -106,7 +94,6 @@ ol, ul {
.game-board { .game-board {
display: flex; display: flex;
flex-direction: column;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
} }

View File

@ -2,11 +2,12 @@ import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import io from 'socket.io-client'; import io from 'socket.io-client';
import Board from './components/Board.js'; import Board from './components/Board';
import './index.css'; import './index.css';
const ENDPOINT = process.env.REACT_APP_QUICKCHESS_API || "https://api.quickchess.win";
const ENDPOINT = "https://quickchess.undercover.cafe";
class NameForm extends React.Component { class NameForm extends React.Component {
constructor(props) { constructor(props) {
@ -34,7 +35,7 @@ class NameForm extends React.Component {
</label> </label>
<div style={{ textAlign: 'center', width: '80%' }}> <div style={{ textAlign: 'center', width: '80%' }}>
<input className='big-input' style={{ width: '50%' }} name='lobby-name' type="text" value={this.state.value} onChange={this.handleChange} /> <input className='big-input' style={{ width: '50%' }} name='lobby-name' type="text" value={this.state.value} onChange={this.handleChange} />
<input className='big-input' style={{ marginLeft: '8px' }} type="submit" value="Join" /> <input className='big-input' style={{ marginLeft: '8px' }} type="submit" value="Submit" />
</div> </div>
</form> </form>
); );
@ -77,14 +78,11 @@ class Game extends React.Component {
render() { render() {
return ( return (
<>
<h2 className="white-on-light">QuickChess</h2>
<div className="game"> <div className="game">
<div className="game-board"> <div className="game-board">
{this.state.gameKey ? this.state.board : <NameForm onSubmit={this.setGameKey.bind(this)} />} {this.state.gameKey ? this.state.board : <NameForm onSubmit={this.setGameKey.bind(this)} />}
</div> </div>
</div> </div>
</>
); );
} }
} }

View File

@ -1,16 +1,16 @@
export const BLACK = 0; const BLACK = 0;
export const WHITE = 1; const WHITE = 1;
export const PAWN = 0; const PAWN = 0;
export const ROOK = 1; const ROOK = 1;
export const KNIGHT = 2; const KNIGHT = 2;
export const BISHOP = 3; const BISHOP = 3;
export const QUEEN = 4; const QUEEN = 4;
export const KING = 5; const KING = 5;
export const EMPTY = -1; const EMPTY = -1;
export class Piece { class Piece {
constructor(color, type) { constructor(color, type) {
this.color = color; this.color = color;
this.type = type; this.type = type;
@ -75,7 +75,7 @@ export class Piece {
} }
} }
export const startingBoard = () => { const startingBoard = () => {
let squares = []; let squares = [];
const mainRow = [ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK]; const mainRow = [ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK];
function add(num, color, type) { function add(num, color, type) {
@ -101,9 +101,9 @@ export const startingBoard = () => {
}); });
} }
export const textFromBoard = ({ state }) => { const textFromBoard = ({ state }) => {
const turn = state?.blackIsNext ? 'B' : 'W'; const turn = (this.state.blackIsNext? 'B' : 'W');
return turn + state?.squares.map(square => { return turn + this.state.squares.map(square => {
if (!square) { if (!square) {
return '_'; return '_';
} }
@ -128,3 +128,5 @@ export const textFromBoard = ({ state }) => {
} }
}).join(''); }).join('');
} }
export { Piece, startingBoard, textFromBoard, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY, BLACK, WHITE }