diff --git a/client/src/Board.css b/client/src/Board.css index 2481621..49e53c8 100755 --- a/client/src/Board.css +++ b/client/src/Board.css @@ -143,6 +143,60 @@ margin: 0.25em; } +.Action { + display: flex; + align-items: center; + justify-content: center; + background-color: rgba(16, 16, 16, 0.25); +} + +.Action Button { + margin: 0.25em; + background-color: white; +} + +.Error { + display: flex; + position: absolute; + top: calc(50vh - 1.5em); + left: 0px; + right: 0px; + align-items: center; + justify-content: center; + background-color: yellow; + text-align: left; + font-size: 12pt; + padding: 1em; +} + +.Message { + display: inline-block; + align-items: left; + justify-content: left; + background-color: rgba(224, 224, 224); + text-align: left; + font-size: 12pt; + padding: 1em; +} + + +.PlayerName { + display: flex; + align-items: center; + justify-content: center; + flex-direction: row; +} + +.PlayerName > .nameInput { + margin-right: 1em; + flex: 1; + max-width: 30em; +} + +.PlayerName > Button { + background: lightblue; +} + .Statistics > div:nth-child(2) { display: flex; flex-direction: row; diff --git a/client/src/Board.js b/client/src/Board.js index 7c895a3..b9b2553 100755 --- a/client/src/Board.js +++ b/client/src/Board.js @@ -254,7 +254,7 @@ const Chat = ({ board, promoteGameState }) => { const Action = ({ board }) => { const startClick = (event) => { - board.setGameState("active").then((state) => { + board.setGameState("game-order").then((state) => { board.game.state = state; }); }; @@ -267,14 +267,21 @@ const Action = ({ board }) => { board.throwDice(); } + const passClick = (event) => { + } + return ( - + { board.game.state == 'lobby' && <> - - + + + } - { board.game.state !== 'lobby' && <> - + { board.game.state === 'game-order' && + } + { board.game.state === 'active' && <> + + } ); @@ -287,25 +294,31 @@ const PlayerName = ({board}) => { setName(event.target.value); } + const sendName = () => { + console.log(`Send: ${name}`); + if (name != board.game.name) { + board.setPlayerName(name); + } else { + board.setState({ pickName: false, error: "" }); + } + } + const nameKeyPress = (event) => { if (event.key === "Enter") { - console.log(`Send: ${name}`); - if (name != board.game.name) { - board.setPlayerName(name); - } + sendName(); } } return ( + ); }; @@ -330,7 +343,6 @@ const Players = ({ board }) => { const classes = useStyles(); const players = []; - console.log(`Player ${board.game.name} is ${board.game.color}`); for (let color in board.game.players) { const item = board.game.players[color]; if (board.game.state !== "lobby" && item.status === 'Not active') { @@ -377,7 +389,8 @@ class Board extends React.Component { stone: 0, wheat: 0, game: null, - message: "" + message: "", + error: "" }; this.componentDidMount = this.componentDidMount.bind(this); this.updateDimensions = this.updateDimensions.bind(this); @@ -399,6 +412,7 @@ class Board extends React.Component { this.imageLoaded = this.imageLoaded.bind(this); this.setPlayerName = this.setPlayerName.bind(this); this.setSelected = this.setSelected.bind(this); + this.updateMessage = this.updateMessage.bind(this); this.mouse = { x: 0, y: 0 }; this.radius = 0.317; @@ -456,16 +470,13 @@ class Board extends React.Component { } return res.json(); }).then((game) => { - let message; - if (game.status !== 'success') { - message = game.status; - } - + const error = (game.status !== 'success') ? game.status : undefined; this.updateGame(game); - this.setState({ game: game, message: message }); + this.updateMessage(); + this.setState({ game: game, error: error }); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); window.requestAnimationFrame(this.drawFrame); @@ -494,12 +505,16 @@ class Board extends React.Component { let message; if (game.status !== 'success') { message = game.status; + } else { + this.setState({ pickName: false }); } this.updateGame(game); - this.setState({ game: game, message: message}); + this.updateMessage(); + + this.setState({ game: game, error: message}); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); window.requestAnimationFrame(this.drawFrame); @@ -527,10 +542,11 @@ class Board extends React.Component { }).then((game) => { console.log (`Board shuffled!`); this.updateGame(game); - this.setState({ game: game, message: "Board shuffled!" }); + this.updateMessage(); + this.setState({ game: game, error: "Board shuffled!" }); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); window.requestAnimationFrame(this.drawFrame); @@ -557,16 +573,16 @@ class Board extends React.Component { } return res.json(); }).then((game) => { - let message; - if (game.status !== "success") { - game.dice = []; - message = game.status; + const error = (game.status !== 'success') ? game.status : undefined; + if (error) { + game.dice = [ game.order ]; } this.updateGame(game); - this.setState({ game: { ...this.state.game, dice: game.dice }, message: message } ); + this.updateMessage(); + this.setState({ game: { ...this.state.game, dice: game.dice }, error: error } ); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); return this.game.dice; @@ -598,12 +614,16 @@ class Board extends React.Component { } return res.json(); }).then((game) => { + const error = (game.status !== 'success') ? game.status : undefined; + console.log (`Game ${game.id} loaded ${moment().format()}.`); + this.updateGame(game); - this.setState({ game: game, message: "" }); + this.updateMessage(); + this.setState({ game: game, error: error }); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); }); @@ -642,10 +662,10 @@ class Board extends React.Component { }).then((game) => { console.log (`Game state changed.`); this.updateGame(game); - this.setState({ game: game, message: "" }); + this.setState({ game: game, error: "" }); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); }); @@ -676,10 +696,11 @@ class Board extends React.Component { }).then((game) => { console.log (`Game state set to ${game.state}!`); this.updateGame(game); - this.setState({ game: { ...this.state.game, state: game.state }, message: `Game state now ${game.state}.` }); + this.updateMessage(); + this.setState({ game: { ...this.state.game, state: game.state }, error: `Game state now ${game.state}.` }); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); return this.game.state; @@ -700,7 +721,7 @@ class Board extends React.Component { window.requestAnimationFrame(this.drawFrame); - if (this.game.state == 'lobby') { + if (this.game.state !== 'active') { return; } @@ -1254,6 +1275,65 @@ class Board extends React.Component { window.requestAnimationFrame(this.drawFrame); } + updateMessage() { + const player = (this.game && this.game.color) ? this.game.players[this.game.color] : undefined; + + let message = []; + if (this.state.pickName) { + message.push((<>Enter the name you would like to be known by, then press ENTER or select  SET.)); + } else { + switch (this.game && this.game.state) { + case 'lobby': + message.push((<>You are in the lobby as {this.game.name}.)); + if (!this.game.color) { + message.push((<>You need to pick your color.)); + } else { + message.push((<>You have selected {this.game.color}.)); + } + message.push((<>You can chat with other players below.)); + if (this.game.active < 2) { + message.push((<>Once there are two or more players, you can select START GAME.)); + } else { + message.push((<>There are enough players to start the game. Select START GAME when ready.)); + } + break; + case 'game-order': + if (!player) { + message.push((<>This game is already active.)); + if (!this.game.name) { + message.push((<>Enter the name you would like to be known by, then press ENTER or select  SET.)); + } else { + message.push((<>You can chat with other players below as {this.game.name}, but cannot play unless players go back to the Lobby.)); + } + } else { + if (!player.order) { + message.push((<>You need to roll for game order. Click Roll Dice below.)); + } else { + message.push((<>You rolled {player.order} for game order. Waiting for all players.)); + message.push((<>
THIS IS THE END OF THE FUNCTIONALITY SO FAR)); + } + } + break; + case 'active': + if (!player) { + message.push((<>This game is no longer in the lobby.
TODO: Override game state to allow Lobby mode while in-game)); + } else { + message.push((<>
THIS IS THE END OF THE FUNCTIONALITY SO FAR)); + } + break; + case null: + case undefined: + case '': + message.push((<>The game is in a wonky state. Sorry :()); + break; + default: + message.push((<>Game state is: {this.game.state})); + break; + } + } + this.setState({ message: message }); + } + componentDidMount() { this.start = new Date(); @@ -1285,15 +1365,15 @@ class Board extends React.Component { if (res.status < 400) { return res; } - let message; + let error; if (!this.id) { - message = `Unable to create new game.`; - throw new Error(message); + error = `Unable to create new game.`; + throw new Error(error); } - message = `Unable to find game ${this.id}. Starting new game.` - console.log(message); - this.setState({ message: message }); + error = `Unable to find game ${this.id}. Starting new game.` + console.log(error); + this.setState({ error: error }); params.url = `${base}/api/v1/games/${this.id}`; params.method = "POST"; @@ -1316,10 +1396,12 @@ class Board extends React.Component { } this.updateGame(game); - this.setState({ game: game, message: "" }); + this.updateMessage(); + + this.setState({ game: game, error: "" }); }).catch((error) => { console.error(error); - this.setState({message: error.message}); + this.setState({error: error.message}); }).then(() => { this.resetGameLoad(); }); @@ -1349,14 +1431,17 @@ class Board extends React.Component { this.canvas = el}>
this.cards = el}> { game && -
- {!game.color && } - - - { game.color && +
+ { this.state.message } + {(this.state.pickName || !this.game.name) && } + {(!this.state.pickName && this.game.name) && + <> + + + } - { this.state.message != "" &&
{this.state.message}
} + { this.state.error &&
{this.state.error}
}
} { diff --git a/server/routes/games.js b/server/routes/games.js index 61b433b..6251aee 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -108,14 +108,15 @@ const roll = (game, session) => { case "game-order": if (player.order) { - retrun = `Player ${name} already rolled for order.`; + return `Player ${name} already rolled for order.`; } + game.dice = [ Math.ceil(Math.random() * 6) ]; player.order = game.dice[0]; message = `${name} rolled ${game.dice[0]} for play order.`; game.chat.push({ date: Date.now(), message: message }); break; - case "in-game": + case "active": game.dice = [ Math.ceil(Math.random() * 6), Math.ceil(Math.random() * 6) ]; message = `${name} rolled ${game.dice[0] + game.dice[1]}.`; game.chat.push({ date: Date.now(), message: message }); @@ -460,6 +461,7 @@ const sendGame = async (req, res, game, error) => { console.log(message); game.state = 'lobby'; } + game.active = active; let session; if (req.session) { @@ -498,6 +500,7 @@ const sendGame = async (req, res, game, error) => { status: error ? error : "success", name: session.name, color: session.color, + order: (session.color in game.players) ? game.players[session.color].order : 0, sessions: reducedSessions }); @@ -509,7 +512,7 @@ const createGame = (id) => { const game = { startTime: Date.now(), turns: 0, - state: "lobby", /* lobby, in-game, finished */ + state: "lobby", /* lobby, active, finished */ tiles: [], pips: [], borders: [],