New game launching works!
Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
parent
7f813ff2bd
commit
ddfefef8b1
@ -144,15 +144,15 @@ const loadGame = async (id) => {
|
||||
});
|
||||
|
||||
if (!game) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
games[id] = createGame(id);
|
||||
} else {
|
||||
try {
|
||||
games[id] = JSON.parse(game);
|
||||
} catch (error) {
|
||||
console.error(error, game);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return games[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);
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user