From f3dd68cad9824fd02b0b435efa2aded7dba61993 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 18 May 2023 11:57:15 -0400 Subject: [PATCH] Add npm run server to start up the backend. Pull ENDPOINT value from REACT_APP_QUICKCHESS_API. Switch to ES Modules. --- package.json | 6 ++++-- server.js | 18 +++++++++++------- src/backend.js | 6 ++---- src/index.js | 2 +- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index a24132c..8dbd9b0 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "quickchess", "version": "0.1.0", "private": true, + "type": "module", "dependencies": { "@testing-library/jest-dom": "^5.11.4", "@testing-library/react": "^11.1.0", @@ -18,7 +19,8 @@ "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", - "eject": "react-scripts eject" + "eject": "react-scripts eject", + "server": "node server.js" }, "eslintConfig": { "extends": [ @@ -38,7 +40,7 @@ "last 1 safari version" ] }, - "homepage": "./", + "homepage": "https://quickchess.sage.boats", "devDependencies": { "react-test-renderer": "^17.0.1" } diff --git a/server.js b/server.js index 38d34d4..4f9f015 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,9 @@ -const { textFromBoard } = require('./src/logic') -const { Board } = require('./src/backend'); +import { textFromBoard } from './src/logic.js' +import { Board } from './src/backend.js'; -const express = require('express'); -const http = require('http'); -const socketIo = require('socket.io'); +import express from 'express'; +import http from 'http'; +import { Server } from 'socket.io'; const port = process.env.PORT || 4001; @@ -11,7 +11,7 @@ const app = express(); const server = http.createServer(app); -const io = socketIo(server, { +const io = new Server(server, { cors: { cors: true, } @@ -28,7 +28,9 @@ io.on("connection", (socket) => { } //interval = setInterval(() => getApiAndEmit(socket), 5000); socket.on("disconnect", () => { - console.log("Client disconnected"); + const game = socket.connectedGame + const gameMessage = game ? `from game '${game.key}'` : "and was not attached to a game" + console.log("Client disconnected " + gameMessage); clearInterval(interval); }); @@ -41,12 +43,14 @@ io.on("connection", (socket) => { game.sockets.push(socket); } else { game = { + key, sockets: [socket], board: new Board(), moveDate: null, }; console.log(`Created game '${key}'`); } + socket.connectedGame = game; games.set(key, game); getApiAndEmit(socket, game.board); }); diff --git a/src/backend.js b/src/backend.js index 0bd3616..dbc6026 100644 --- a/src/backend.js +++ b/src/backend.js @@ -1,4 +1,4 @@ -const { Piece, startingBoard, BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY } = require('./logic') +import { Piece, startingBoard, BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY } from './logic.js' const SHUFFLING_ENABLED = 0; @@ -15,7 +15,7 @@ function settingText(setting) { } } -class Board { +export class Board { constructor(props) { this.state = props?.text ? this.stateFromText(props.text) : startingBoard(); @@ -428,8 +428,6 @@ class Board { } } -module.exports = { Board } - // export default Board; // export {Board, Piece}; // export { BLACK, WHITE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING, EMPTY }; diff --git a/src/index.js b/src/index.js index 442f48b..d87f2f9 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,7 @@ import Board from './components/Board'; import './index.css'; -const ENDPOINT = "https://quickchess.undercover.cafe"; +const ENDPOINT = process.env.REACT_APP_QUICKCHESS_API || "https://quickchess.undercover.cafe"; class NameForm extends React.Component { constructor(props) {