diff --git a/server/routes/games.js b/server/routes/games.js index 3320811..e7272fc 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -205,7 +205,7 @@ const processGameOrder = (game, player, dice) => { game.playerOrder = players.map(player => getPlayerColor(game, player)); game.state = 'initial-placement'; message = `Initial settlement placement has started!`; - + game.direction = 'forward'; game.turn = { actions: ['place-settlement'], limits: { corners: getValidCorners(game) }, @@ -252,7 +252,7 @@ const roll = (game, session) => { processGameOrder(game, player, game.dice[0]); break; - case "active": + case "normal": game.dice = [ Math.ceil(Math.random() * 6), Math.ceil(Math.random() * 6) ]; message = `${name} rolled ${game.dice[0]}, ${game.dice[1]}.`; break; @@ -564,6 +564,26 @@ const getColorFromName = (game, name) => { return ''; }; +const getLastPlayerName = (game) => { + let index = game.playerOrder.length - 1; + for (let id in game.sessions) { + if (game.sessions[id].color === game.playerOrder[index]) { + return game.sessions[id].name; + } + } + return ''; +} + +const getFirstPlayerName = (game) => { + let index = 0; + for (let id in game.sessions) { + if (game.sessions[id].color === game.playerOrder[index]) { + return game.sessions[id].name; + } + } + return ''; +} + const getNextPlayer = (game, name) => { let color; for (let id in game.sessions) { @@ -585,6 +605,27 @@ const getNextPlayer = (game, name) => { return name; } +const getPrevPlayer = (game, name) => { + let color; + for (let id in game.sessions) { + if (game.sessions[id].name === name) { + color = game.sessions[id].color; + break; + } + } + if (!color) { + return name; + } + let index = game.playerOrder.indexOf(color); + index = (index - 1) % game.playerOrder.length; + for (let id in game.sessions) { + if (game.sessions[id].color === game.playerOrder[index]) { + return game.sessions[id].name; + } + } + return name; +} + const getValidCorners = (game) => { const limits = []; @@ -706,11 +747,16 @@ router.put("/:id/:action/:value?", async (req, res) => { error = `It is not your turn! It is ${game.turn.name}'s turn.`; break; } - index = value; + index = parseInt(value); if (game.placements.corners[index] === undefined) { error = `You have requested to place a settlement illegally!`; break; } + /* If this is not a valid road in the turn limits, discard it */ + if (game.turn && game.turn.limits && game.turn.limits.corners && game.turn.limits.corners.indexOf(index) === -1) { + error = `You tried to cheat! You should not try to break the rules.`; + break; + } const corner = game.placements.corners[index]; if (corner.color) { error = `This location already has a settlement belonging to ${playerNameFromColor(game, corner.color)}!`; @@ -736,35 +782,64 @@ router.put("/:id/:action/:value?", async (req, res) => { error = `It is not your turn! It is ${game.turn.name}'s turn.`; break; } - index = value; + index = parseInt(value); if (game.placements.roads[index] === undefined) { error = `You have requested to place a road illegally!`; break; } + /* If this is not a valid road in the turn limits, discard it */ + if (game.turn && game.turn.limits && game.turn.limits.roads && game.turn.limits.roads.indexOf(index) === -1) { + error = `You tried to cheat! You should not try to break the rules.`; + break; + } const road = game.placements.roads[index]; if (road.color) { error = `This location already has a road belonging to ${playerNameFromColor(game, road.color)}!`; break; } if (game.state === 'initial-placement') { - console.log('TODO: Make sure this road is connected to the settlement!'); road.color = session.color; - const next = getNextPlayer(game, name); - game.turn = { - actions: ['place-settlement'], - limits: { corners: getValidCorners(game) }, - name: next, - color: getColorFromName(game, next) - }; addChatMessage(game, session, `${name} placed a road.`); - addChatMessage(game, null, `It is ${next}'s turn. Place a settlement.`); + let next; + if (game.direction === 'forward' && getLastPlayerName(game) === name) { + game.direction = 'backward'; + next = name; + } else if (game.direction === 'backward' && getFirstPlayerName(game) === name) { + /* Done! */ + delete game.direction; + } else { + if (game.direction === 'forward') { + next = getNextPlayer(game, name); + } else { + next = getPrevPlayer(game, name); + } + } + if (next) { + game.turn = { + actions: ['place-settlement'], + limits: { corners: getValidCorners(game) }, + name: next, + color: getColorFromName(game, next) + }; + addChatMessage(game, null, `It is ${next}'s turn. Place a settlement.`); + } else { + game.turn = { + actions: [], + limits: { }, + name: name, + color: getColorFromName(game, name) + }; + + addChatMessage(game, null, `Everyone has placed their two settlements!`); + addChatMessage(game, null, `TODO: Give players resources from their second placement.`); + addChatMessage(game, null, `It is ${name}'s turn.`); + game.state = 'normal'; + } } else { error = `Road placement not enabled for normal game play.`; break; } - break; - error = `Road placement not yet implemented!`; - break; + break; case 'place-city': error = `City placement not yet implemented!`; break;