diff --git a/package-lock.json b/package-lock.json index 80b388c..4beea13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "react-dom": "^17.0.2", "react-scripts": "4.0.3", "typescript": "^4.3.5", + "uuid": "^8.3.2", "web-vitals": "^1.1.2" }, "devDependencies": { @@ -19905,7 +19906,6 @@ "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "optional": true, "bin": { "uuid": "dist/bin/uuid" } @@ -37026,8 +37026,7 @@ "uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "optional": true + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" }, "v8-compile-cache": { "version": "2.3.0", diff --git a/package.json b/package.json index 627e1bd..82d5096 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "react-dom": "^17.0.2", "react-scripts": "4.0.3", "typescript": "^4.3.5", + "uuid": "^8.3.2", "web-vitals": "^1.1.2" }, "scripts": { diff --git a/src/App.tsx b/src/App.tsx index 57fb6c8..8ea8895 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,14 +1,17 @@ import React from 'react'; import './App.css'; -import {Simulate} from "react-dom/test-utils"; +import {v4 as uuidv4} from 'uuid'; +import ChessSwap from "./ChessSwap"; +import Color from "./Color"; interface IAppProps { } interface IAppState { - text: string, - chat: string, - lobby: string + lobby: string, + joinLobbyText: string, + playerColor: Color | undefined, + turnsToSwap: number } class App extends React.Component { @@ -19,16 +22,32 @@ class App extends React.Component { super(props); this.state = { - text: "", - chat: "", - lobby: "" + lobby: "", + joinLobbyText: "", + playerColor: undefined, + turnsToSwap: 6 } - - this.joinLobby("default"); } + createLobby(){ + let newLobby: string = uuidv4(); + this.setState({lobby: newLobby}) + this.connectToLobby(newLobby); + } + + updateTurnsToSwap(turnsToSwap: number){ + this.setState({ + turnsToSwap: turnsToSwap + }) + } joinLobby(lobbyName: string){ + // TODO: Do announcement here + this.setState({lobby: lobbyName}) + this.connectToLobby(lobbyName); + } + + connectToLobby(lobbyName){ if (this.ws?.OPEN){ this.ws.close(); @@ -40,33 +59,51 @@ class App extends React.Component { this.ws.onopen = () => { console.log('connected to lobby: ' + lobbyName); + this.ws?.send("isThereOpponent") } this.ws.onmessage = evt => { //const message = JSON.parse(evt.data); const message = evt.data; - this.setState({chat: this.state.chat + "\n" + message}) console.log(message); + + switch (message) { + case "isThereOpponent": + this.ws?.send("OpponentReady"); + console.log("I am white"); + this.setState({playerColor: Color.white}) + break; + case "OpponentReady": + console.log("I am black"); + this.setState({playerColor: Color.black}) + } + } } render(){ - return
-

Chat

-
{this.state.chat}
- this.setState({ - text: e.target.value - })}/> -

Current Lobby:

- this.setState({ - lobby: e.target.value - })}/> - - -
+ if (this.state.lobby === ""){ + return
+

Welcome to Chess Swap!

+

A game by Lucas Winther

+

This was made using open source tools. See the backend or frontend respotistory for more information

+ + this.setState({joinLobbyText: e.target.value})} placeholder={"lobby name"}/> +
+ } + else if (this.state.playerColor !== undefined){ + return
+

Swap in {this.state.turnsToSwap} turns

+ +
+ } + else{ + return
+

You are in lobby

{this.state.lobby}

+

Waiting for opponent

+
+ } + } } diff --git a/src/ChessSwap.tsx b/src/ChessSwap.tsx index 351360a..636450f 100644 --- a/src/ChessSwap.tsx +++ b/src/ChessSwap.tsx @@ -2,9 +2,12 @@ import React, { Component } from "react"; import PropTypes from "prop-types"; import Chess, {Piece} from "chess.js"; // import Chess from "chess.js"(default) if recieving an error about new Chess() not being a constructor import Chessboard from "chessboardjsx"; +import Color from "./Color"; interface IChessSwapProps { - + orientation: Color, + updateTurnsToSwapOnGui: (number) => void, + turnsToSwap: number } interface IChessSwapState { fen: string, @@ -12,17 +15,18 @@ interface IChessSwapState { squareStyles: object, pieceSquare: string, square: string, - history: [] + history: [], + currentOrientation: Color, + turnsToNextSwap: number } - interface IOnDrop { sourceSquare: string, targetSquare: string, piece: Piece } -class HumanVsHuman extends Component { +export default class ChessSwap extends Component { // @ts-ignore // It obviously does, rtfm typescript! @@ -42,7 +46,11 @@ class HumanVsHuman extends Component { // currently clicked square square: "", // array of past game moves - history: [] + history: [], + // Which color the player is + currentOrientation: this.props.orientation, + // When to swap + turnsToNextSwap: this.props.turnsToSwap }; } @@ -82,7 +90,6 @@ class HumanVsHuman extends Component { })); }; - onDrop = (info: IOnDrop ) => { let sourceSquare = info.sourceSquare; let targetSquare = info.targetSquare; @@ -100,6 +107,9 @@ class HumanVsHuman extends Component { history: this.game.history({ verbose: true }), squareStyles: squareStyling({ pieceSquare, history }) })); + + this.setState({turnsToNextSwap: this.state.turnsToNextSwap - 1}) + this.props.updateTurnsToSwapOnGui(this.state.turnsToNextSwap) }; onMouseOverSquare = (square: string) => { @@ -160,61 +170,29 @@ class HumanVsHuman extends Component { }); render() { - const { fen, dropSquareStyle, squareStyles } = this.state; - // @ts-ignore - return this.props.children({ - squareStyles, - position: fen, - onMouseOverSquare: this.onMouseOverSquare, - onMouseOutSquare: this.onMouseOutSquare, - onDrop: this.onDrop, - dropSquareStyle, - onDragOverSquare: this.onDragOverSquare, - onSquareClick: this.onSquareClick, - onSquareRightClick: this.onSquareRightClick - }); + const { fen, dropSquareStyle, squareStyles } = this.state; + return } } - -export default function ChessSwap() { - return ( -
- - {({ - position, - onDrop, - onMouseOverSquare, - onMouseOutSquare, - squareStyles, - dropSquareStyle, - onDragOverSquare, - onSquareClick, - onSquareRightClick - }) => ( - - )} - -
- ); -} - // @ts-ignore const squareStyling = ({ pieceSquare, history }) => { const sourceSquare = history.length && history[history.length - 1].from; diff --git a/src/Color.tsx b/src/Color.tsx new file mode 100644 index 0000000..5cce748 --- /dev/null +++ b/src/Color.tsx @@ -0,0 +1,5 @@ +enum Color { + white = "white", + black = "black" +} +export default Color; \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 7f2d334..cd64579 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -3,7 +3,6 @@ import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; -import ChessSwap from "./ChessSwap"; // Chat Example /*ReactDOM.render( @@ -16,7 +15,7 @@ import ChessSwap from "./ChessSwap"; ReactDOM.render( - + , document.getElementById('root') );