1
0

New game launching works!

Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
James Ketr 2022-01-28 18:27:13 -08:00
parent 7f813ff2bd
commit ddfefef8b1

View File

@ -36,7 +36,7 @@ function shuffle(array) {
}
const assetData = {
tiles: [
tiles: [
{ type: "wood", y: 0. / 4. },
{ type: "wood", y: 1. / 4. },
{ type: "wood", y: 2. / 4. },
@ -124,7 +124,7 @@ const roll = (game, player) => {
console.log(message);
return;
}
error = `Invalid game state (${game.state}) in roll.`;
return error;
}
@ -144,14 +144,14 @@ const loadGame = async (id) => {
});
if (!game) {
return undefined;
}
try {
games[id] = JSON.parse(game);
} catch (error) {
console.error(error, game);
return null;
games[id] = createGame(id);
} else {
try {
games[id] = JSON.parse(game);
} catch (error) {
console.error(error, game);
return null;
}
}
return games[id];
@ -172,7 +172,7 @@ router.put("/:id/:action/:value?", async (req, res) => {
let player;
let error;
if ('private-token' in req.headers) {
if (req.headers['private-token'] !== req.app.get('admin')) {
error = `Invalid admin credentials.`;
@ -201,8 +201,8 @@ router.put("/:id/:action/:value?", async (req, res) => {
}
return sendGame(res, req, game, error);
}
}
if (action == "player-name") {
const name = value ? value : "";
if (color) {
@ -267,7 +267,7 @@ router.put("/:id/:action/:value?", async (req, res) => {
if (!name) {
error = `You may only select a player when you have set your name.`;
}
/* Verify this player's name is not already active in the game */
if (!error) {
for (let key in game.players) {
@ -278,7 +278,7 @@ router.put("/:id/:action/:value?", async (req, res) => {
}
}
}
/* Verify selection is valid */
if (!error && !(selected in game.players)) {
error = `An invalid player selection was attempted.`;
@ -291,7 +291,7 @@ router.put("/:id/:action/:value?", async (req, res) => {
error = `${player.name} already has ${selected}`;
}
}
/* All good -- set this player to requested selection */
if (!error) {
player.session = req.session.id;
@ -351,20 +351,16 @@ router.put("/:id/:action/:value?", async (req, res) => {
router.get("/:id", async (req, res/*, next*/) => {
const { id } = req.params;
console.log("GET games/" + id);
let error;
const game = await loadGame(id);
let game = await loadGame(id);
if (game) {
return sendGame(res, req, game)
}
error = `Game ${id} not found -- returning invalid game state.`;
const invalid = {
id: id,
players: {},
state: 'invalid'
};
return sendGame(res, req, invalid, error);
game = createGame(id);
req.session.playerColor = null;
return sendGame(res, req, game);
});
router.put("/:id", (req, res/*, next*/) => {
@ -435,15 +431,8 @@ const sendGame = async (res, req, game, error) => {
return res.status(200).send(playerGame);
}
router.post("/:id?", (req, res/*, next*/) => {
console.log("POST games/");
const { id } = req.params;
if (id && id in games) {
const error = `Can not create new game for ${id} -- it already exists.`
console.error(error);
return res.status(400).send(error);
}
const createGame = (id) => {
id = id ? id : crypto.randomBytes(8).toString('hex');
const game = {
startTime: Date.now(),
turns: 0,
@ -467,14 +456,27 @@ router.post("/:id?", (req, res/*, next*/) => {
wheat: 19,
longestRoad: null,
largestArmy: null,
chat: [ { from: "R", date: Date.now(), message: "Server initialized!" } ],
id: id ? id : crypto.randomBytes(8).toString('hex')
chat: [ { date: Date.now(), message: `New game started for ${id}` } ],
id: id
};
games[game.id] = game;
req.session.playerColor = null;
shuffleBoard(game);
console.log(`New game created: ${game.id}`);
return game;
};
router.post("/:id?", (req, res/*, next*/) => {
console.log("POST games/");
const { id } = req.params;
if (id && id in games) {
const error = `Can not create new game for ${id} -- it already exists.`
console.error(error);
return res.status(400).send(error);
}
const game = createGame(id);
req.session.playerColor = null;
return sendGame(res, req, game);
});