Lobby, Game-Order, Active states starting to work
Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
parent
30c7979871
commit
395fb5558d
@ -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;
|
||||
|
@ -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 (
|
||||
<Paper className="Action">
|
||||
<Paper class="Action">
|
||||
{ board.game.state == 'lobby' && <>
|
||||
<Button fullWidth onClick={startClick}>Start game</Button>
|
||||
<Button fullWidth onClick={newBoardClick}>New board</Button>
|
||||
<Button disabled={!board.game.color || board.game.active < 2} onClick={startClick}>Start game</Button>
|
||||
<Button disabled={!board.game.color} onClick={newBoardClick}>New board</Button>
|
||||
<Button disabled={board.game.color} onClick={() => {board.setState({ pickName: true})}}>Change name</Button>
|
||||
</> }
|
||||
{ board.game.state !== 'lobby' && <>
|
||||
<Button fullWidth onClick={rollClick}>Roll</Button>
|
||||
{ board.game.state === 'game-order' &&
|
||||
<Button disabled={board.game.order !== 0} onClick={rollClick}>Roll Dice</Button> }
|
||||
{ board.game.state === 'active' && <>
|
||||
<Button onClick={rollClick}>Roll Dice</Button>
|
||||
<Button disabled onClick={passClick}>Pass Turn</Button>
|
||||
</> }
|
||||
</Paper>
|
||||
);
|
||||
@ -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 (
|
||||
<Paper className="PlayerName">
|
||||
<TextField className="nameInput"
|
||||
fullWidth
|
||||
onChange={nameChange}
|
||||
onKeyPress={nameKeyPress}
|
||||
label="Name"
|
||||
label="Enter your name"
|
||||
variant="outlined"
|
||||
value={name}
|
||||
/>
|
||||
<Button onClick={() => sendName()}>Set</Button>
|
||||
</Paper>
|
||||
);
|
||||
};
|
||||
@ -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 <b>ENTER</b> or select <b>SET</b>.</>));
|
||||
} else {
|
||||
switch (this.game && this.game.state) {
|
||||
case 'lobby':
|
||||
message.push((<>You are in the lobby as <b>{this.game.name}</b>.</>));
|
||||
if (!this.game.color) {
|
||||
message.push((<>You need to pick your color.</>));
|
||||
} else {
|
||||
message.push((<>You have selected <b>{this.game.color}</b>.</>));
|
||||
}
|
||||
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 <b>START GAME</b>.</>));
|
||||
} else {
|
||||
message.push((<>There are enough players to start the game. Select <b>START GAME</b> 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 <b>ENTER</b> or select <b>SET</b>.</>));
|
||||
} else {
|
||||
message.push((<>You can chat with other players below as <b>{this.game.name}</b>, but cannot play unless players go back to the Lobby.</>));
|
||||
}
|
||||
} else {
|
||||
if (!player.order) {
|
||||
message.push((<>You need to roll for game order. Click <b>Roll Dice</b> below.</>));
|
||||
} else {
|
||||
message.push((<>You rolled <b>{player.order}</b> for game order. Waiting for all players.</>));
|
||||
message.push((<><br/><b>THIS IS THE END OF THE FUNCTIONALITY SO FAR</b></>));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'active':
|
||||
if (!player) {
|
||||
message.push((<>This game is no longer in the lobby.<br/><b>TODO: Override game state to allow Lobby mode while in-game</b></>));
|
||||
} else {
|
||||
message.push((<><br/><b>THIS IS THE END OF THE FUNCTIONALITY SO FAR</b></>));
|
||||
}
|
||||
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 {
|
||||
<canvas className="Display" ref={el => this.canvas = el}></canvas>
|
||||
<div className="Cards" ref={el => this.cards = el}>
|
||||
{ game &&
|
||||
<div className={'Game ' + game.state}>
|
||||
{!game.color && <PlayerName board={this}/> }
|
||||
<Players board={this} promoteGameState={this.promoteGameState}/>
|
||||
<Chat board={this} promoteGameState={this.promoteGameState}/>
|
||||
{ game.color &&
|
||||
<div className={'Game ' + game.state}>
|
||||
<Paper class="Message">{ this.state.message }</Paper>
|
||||
{(this.state.pickName || !this.game.name) && <PlayerName board={this}/> }
|
||||
{(!this.state.pickName && this.game.name) &&
|
||||
<>
|
||||
<Players board={this} promoteGameState={this.promoteGameState}/>
|
||||
<Chat board={this} promoteGameState={this.promoteGameState}/>
|
||||
<Action board={this}/>
|
||||
</>
|
||||
}
|
||||
{ this.state.message != "" && <Paper><div style={{align:"left",fontSize:"8pt"}}>{this.state.message}</div></Paper> }
|
||||
{ this.state.error && <Paper className="Error"><div>{this.state.error}</div></Paper> }
|
||||
</div>
|
||||
}
|
||||
{
|
||||
|
@ -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: [],
|
||||
|
Loading…
x
Reference in New Issue
Block a user