Send back to lobby if < 2 players
Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
parent
7885c094e1
commit
b57e771679
@ -1 +1,3 @@
|
||||
|
||||
PUBLIC_URL=/ketr.ketran
|
||||
HOST=nuc.ketrenos.com
|
||||
DANGEROUSLY_DISABLE_HOST_CHECK='true'
|
||||
|
@ -18,7 +18,7 @@
|
||||
"web-vitals": "^2.1.2"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "export $(cat ../.env | xargs) && react-scripts start",
|
||||
"start": "react-scripts start",
|
||||
"build": "export $(cat ../.env | xargs) && react-scripts build",
|
||||
"test": "export $(cat ../.env | xargs) && react-scripts test",
|
||||
"eject": "export $(cat ../.env | xargs) && react-scripts eject"
|
||||
|
@ -227,13 +227,12 @@ const Chat = ({ board, promoteGameState }) => {
|
||||
//const timeDelta = game.timestamp - Date.now();
|
||||
|
||||
const messages = board.game.chat.map((item, index) => {
|
||||
//const timestamp = moment(item.date - timeDelta).fromNow();
|
||||
return (
|
||||
<ListItem key={`msg-${index}`}>
|
||||
<ListItemAvatar>
|
||||
<Avatar className={classes[item.from]}>{item.from}</Avatar>
|
||||
</ListItemAvatar>
|
||||
<ListItemText primary={item.message} secondary={(<Moment fromNow interval={1000}/>)} />
|
||||
<ListItemText primary={item.message} secondary={(<Moment fromNow date={item.date} interval={1000}/>)} />
|
||||
</ListItem>
|
||||
);
|
||||
});
|
||||
@ -344,6 +343,10 @@ const Players = ({ board, promoteGameState }) => {
|
||||
const players = [];
|
||||
for (let key in board.game.players) {
|
||||
const item = board.game.players[key];
|
||||
console.log(item);
|
||||
if (board.game.state != "lobby" && item.status == 'Not active') {
|
||||
continue;
|
||||
}
|
||||
players.push((
|
||||
<ListItem key={`player-${key}`}>
|
||||
<ListItemAvatar>
|
||||
@ -492,10 +495,12 @@ class Board extends React.Component {
|
||||
}
|
||||
return res.json();
|
||||
}).then((game) => {
|
||||
console.log (`Dice rolled!`);
|
||||
console.log(game.dice);
|
||||
let message = game.status != "success" ? game.status : "Dice rolled!"
|
||||
if (game.status != "success") {
|
||||
game.dice = [];
|
||||
}
|
||||
this.updateGame(game);
|
||||
this.setState({ game: { ...this.state.game, dice: game.dice }, message: "Dice rolled!"} );
|
||||
this.setState({ game: { ...this.state.game, dice: game.dice }, message: message } );
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
this.setState({message: error.message});
|
||||
@ -1141,6 +1146,10 @@ class Board extends React.Component {
|
||||
updateGame(game) {
|
||||
this.game = game;
|
||||
|
||||
if (game.state === "invalid") {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pips = Pips(this);
|
||||
this.tiles = Tiles(this);
|
||||
this.table = Table(this);
|
||||
@ -1251,7 +1260,7 @@ class Board extends React.Component {
|
||||
return (
|
||||
<div className="Board" ref={el => this.el = el}>
|
||||
<canvas className="Display" ref={el => this.canvas = el}></canvas>
|
||||
<div className="Cards" ref={el => this.cards = el}>
|
||||
<div className="Cards" ref={el => this.cards = el}>
|
||||
{ game &&
|
||||
<div className="Game">
|
||||
<Players board={this} promoteGameState={this.promoteGameState}/>
|
||||
@ -1260,7 +1269,10 @@ class Board extends React.Component {
|
||||
{ this.state.message != "" && <Paper><div style={{align:"left",fontSize:"8pt"}}>{this.state.message}</div></Paper> }
|
||||
</div>
|
||||
}
|
||||
<div>In hand</div>
|
||||
{
|
||||
game && game.state == "active" &&
|
||||
<>
|
||||
<div>In hand</div>
|
||||
<div className="Hand">
|
||||
<Resource type="wood" count={this.state.wood}/>
|
||||
<Resource type="wheat" count={this.state.wheat}/>
|
||||
@ -1300,6 +1312,7 @@ class Board extends React.Component {
|
||||
<div>Settlements remaining: 5</div>
|
||||
</div>
|
||||
</div>
|
||||
</> }
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
@ -1,33 +0,0 @@
|
||||
const path = require("path");
|
||||
const webpack = require("webpack");
|
||||
|
||||
module.exports = {
|
||||
entry: "./src/index.js",
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.(js|jsx)$/,
|
||||
exclude: /(node_modules|bower_components)/,
|
||||
loader: "babel-loader",
|
||||
options: { presets: ["@babel/env"] }
|
||||
},
|
||||
{
|
||||
test: /\.css$/,
|
||||
use: ["style-loader", "css-loader"]
|
||||
},
|
||||
{
|
||||
test: /\.(png|svg|jpg|md)$/,
|
||||
use: [ "file-loader" ]
|
||||
}
|
||||
]
|
||||
},
|
||||
resolve: {
|
||||
extensions: ["*", ".js", ".jsx"]
|
||||
},
|
||||
output: {
|
||||
path: path.resolve(__dirname, "dist/"),
|
||||
publicPath: "./dist/",
|
||||
filename: "bundle.js"
|
||||
}
|
||||
};
|
||||
|
@ -1,41 +0,0 @@
|
||||
const path = require("path");
|
||||
const merge = require('webpack-merge')
|
||||
const common = require('./webpack.common.js');
|
||||
const webpack = require('webpack');
|
||||
const config = require("config");
|
||||
const base = config.get("basePath");
|
||||
|
||||
const proxy = {
|
||||
}
|
||||
|
||||
console.log(`Using base: ${base}`);
|
||||
|
||||
proxy[`${base}/`] = {
|
||||
target: "http://localhost:8930",
|
||||
bypass: function(req, res, proxyOptions) {
|
||||
if ((req.url.indexOf(`${base}/assets`) == 0) ||
|
||||
(req.url.indexOf(`${base}/dist`) == 0)) {
|
||||
return req.url.replace(base, "");
|
||||
}
|
||||
console.log(`Proxying to backend server: ${req.url}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/* https://webpack.js.org/configuration/dev-server/ */
|
||||
|
||||
module.exports = merge(common, {
|
||||
mode: "development",
|
||||
devServer: {
|
||||
contentBase: path.join(__dirname, "/"),
|
||||
port: 8930,
|
||||
publicPath: `http://localhost:8930${base}/dist/`,
|
||||
hotOnly: true,
|
||||
disableHostCheck: true,
|
||||
historyApiFallback: true,
|
||||
proxy: proxy
|
||||
},
|
||||
plugins: [new webpack.HotModuleReplacementPlugin()]
|
||||
});
|
||||
|
||||
|
@ -1,8 +0,0 @@
|
||||
const merge = require('webpack-merge')
|
||||
const common = require('./webpack.common.js');
|
||||
|
||||
module.exports = merge(common, {
|
||||
mode: "production",
|
||||
devtool: 'source-map'
|
||||
});
|
||||
|
@ -101,93 +101,109 @@ for (let i = 0; i < 5; i++) {
|
||||
const games = {};
|
||||
|
||||
const roll = (game, player) => {
|
||||
if (player == 0) {
|
||||
console.log("No player active; roll has no action");
|
||||
return;
|
||||
let error;
|
||||
if (!player) {
|
||||
error = "No player active; roll has no action";
|
||||
console.log(error);
|
||||
return error;
|
||||
}
|
||||
|
||||
const name = player.name;
|
||||
|
||||
switch (game.state) {
|
||||
case "lobby":
|
||||
if (game.players[player].order) {
|
||||
console.log(`Player ${player} already rolled for order.`);
|
||||
return;
|
||||
if (player.order) {
|
||||
error = `Player ${name} already rolled for order.`;
|
||||
console.log(error);
|
||||
return error;
|
||||
}
|
||||
game.dice = [ Math.ceil(Math.random() * 6) ];
|
||||
game.players[player].order = game.dice[0];
|
||||
const message = `${player} rolled ${game.dice[0]} for play order.`;
|
||||
player.order = game.dice[0];
|
||||
const message = `${name} rolled ${game.dice[0]} for play order.`;
|
||||
game.chat.push({ date: Date.now(), message: message });
|
||||
console.log(message);
|
||||
return;
|
||||
}
|
||||
|
||||
error = `Invalid game state (${game.state}) in roll.`;
|
||||
return error;
|
||||
}
|
||||
|
||||
router.put("/:id/:action/:value?", (req, res) => {
|
||||
console.log(`PUT games/${req.params.id}/${req.params.action}`);
|
||||
if (!req.params.action in games) {
|
||||
if (!(req.params.id in games)) {
|
||||
const error = `Game not found: ${req.params.id}`;
|
||||
return res.status(404).send(error);
|
||||
}
|
||||
|
||||
const game = games[req.params.id];
|
||||
|
||||
if (!req.session.activePlayer || !req.session.activePlayer in game.players) {
|
||||
const error = `Invalid player: ${req.session.activePlayer}`;
|
||||
return res.status(404).send(error);
|
||||
let error;
|
||||
|
||||
if (!req.session.activePlayer || !(req.session.activePlayer in game.players)) {
|
||||
error = `Invalid player: ${req.session.activePlayer}`;
|
||||
return sendGame(res, req, game, error);
|
||||
}
|
||||
|
||||
const player = game.players[req.session.activePlayer].name;
|
||||
const player = game.players[req.session.activePlayer],
|
||||
name = player.name;
|
||||
|
||||
switch (req.params.action) {
|
||||
case "roll":
|
||||
roll(game, player);
|
||||
error = roll(game, player);
|
||||
break;
|
||||
case "shuffle":
|
||||
if (game.state === "lobby") {
|
||||
shuffleBoard(game);
|
||||
const message = `${player} requested a new board.`;
|
||||
const message = `${name} requested a new board.`;
|
||||
game.chat.push({ date: Date.now(), message: message });
|
||||
console.log(message);
|
||||
return sendGame(res, req, game);
|
||||
} else {
|
||||
const error = `Game no longer in lobby (${game.state}). Can not shuffle board.`;
|
||||
return res.status(400).send(error)
|
||||
error = `Game no longer in lobby (${game.state}). Can not shuffle board.`;
|
||||
}
|
||||
case "state":
|
||||
const state = req.params.value ? req.params.value : "active";
|
||||
if (state != game.state) {
|
||||
game.state = state;
|
||||
const message = `${player} set game state to ${state}.`;
|
||||
const message = `${name} set game state to ${state}.`;
|
||||
game.chat.push({ date: Date.now(), message: message });
|
||||
}
|
||||
return sendGame(res, req, game);
|
||||
}
|
||||
|
||||
return sendGame(res, req, game);
|
||||
return sendGame(res, req, game, error);
|
||||
})
|
||||
|
||||
router.get("/:id", async (req, res/*, next*/) => {
|
||||
console.log("GET games/" + req.params.id);
|
||||
let error;
|
||||
if (req.params.id in games) {
|
||||
const game = games[req.params.id];
|
||||
const id = req.params.id;
|
||||
console.log("GET games/" + id);
|
||||
let error, game;
|
||||
|
||||
if (id in games) {
|
||||
game = games[id];
|
||||
return sendGame(res, req, game)
|
||||
}
|
||||
|
||||
if (/^.|\//.exec(req.params.id)) {
|
||||
if (/^\.|\//.exec(id)) {
|
||||
error = `Requested game ID is invalid`;
|
||||
console.log(error, id);
|
||||
return res.status(400).send(error);
|
||||
}
|
||||
const game = await readFile(`games/${req.params.id}`)
|
||||
|
||||
game = await readFile(`games/${id}`)
|
||||
.catch(() => {
|
||||
return null;
|
||||
return;
|
||||
});
|
||||
|
||||
if (!game) {
|
||||
error = `Unable to load game: ${req.params.id}`;
|
||||
res.status(404).send(error);
|
||||
error = `Game ${id} not found -- returning invalid game state.`;
|
||||
console.warn(error);
|
||||
game = { id: id, state: 'invalid' };
|
||||
} else {
|
||||
games[req.params.id] = game;
|
||||
return sendGame(game);
|
||||
game = JSON.parse(game);
|
||||
}
|
||||
|
||||
games[id] = game;
|
||||
return sendGame(res, req, game, error);
|
||||
});
|
||||
|
||||
router.put("/:id", (req, res/*, next*/) => {
|
||||
@ -237,7 +253,18 @@ router.put("/:id", (req, res/*, next*/) => {
|
||||
}
|
||||
});
|
||||
|
||||
const sendGame = async (res, req, game) => {
|
||||
const sendGame = async (res, req, game, error) => {
|
||||
let active = 0;
|
||||
for (let player in game.players) {
|
||||
player = game.players[player];
|
||||
active += ((player.status && player.status != 'Not active') ? 1 : 0);
|
||||
}
|
||||
if (active < 2 && game.state != 'lobby' && game.state != 'invalid') {
|
||||
let message = "Insufficient players in game. Setting back to lobby."
|
||||
game.chat.push({ date: Date.now(), message: message });
|
||||
console.log(message);
|
||||
game.state = 'lobby';
|
||||
}
|
||||
await writeFile(`games/${game.id}`, JSON.stringify(game, null, 2))
|
||||
.catch((error) => {
|
||||
console.error(`Unable to write to games/${games.id}`);
|
||||
@ -246,6 +273,7 @@ const sendGame = async (res, req, game) => {
|
||||
|
||||
return res.status(200).send(Object.assign({}, game, {
|
||||
timestamp: Date.now(),
|
||||
status: error ? error : "success",
|
||||
activePlayer: (req.session && req.session.activePlayer) ?
|
||||
req.session.activePlayer : null
|
||||
}));
|
||||
|
Loading…
x
Reference in New Issue
Block a user