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