1
0

Initial placement phase completed!

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-02-06 14:03:37 -08:00
parent 9e8ef7ac04
commit d8878ed81f

View File

@ -205,7 +205,7 @@ const processGameOrder = (game, player, dice) => {
game.playerOrder = players.map(player => getPlayerColor(game, player)); game.playerOrder = players.map(player => getPlayerColor(game, player));
game.state = 'initial-placement'; game.state = 'initial-placement';
message = `Initial settlement placement has started!`; message = `Initial settlement placement has started!`;
game.direction = 'forward';
game.turn = { game.turn = {
actions: ['place-settlement'], actions: ['place-settlement'],
limits: { corners: getValidCorners(game) }, limits: { corners: getValidCorners(game) },
@ -252,7 +252,7 @@ const roll = (game, session) => {
processGameOrder(game, player, game.dice[0]); processGameOrder(game, player, game.dice[0]);
break; break;
case "active": case "normal":
game.dice = [ Math.ceil(Math.random() * 6), Math.ceil(Math.random() * 6) ]; game.dice = [ Math.ceil(Math.random() * 6), Math.ceil(Math.random() * 6) ];
message = `${name} rolled ${game.dice[0]}, ${game.dice[1]}.`; message = `${name} rolled ${game.dice[0]}, ${game.dice[1]}.`;
break; break;
@ -564,6 +564,26 @@ const getColorFromName = (game, name) => {
return ''; 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) => { const getNextPlayer = (game, name) => {
let color; let color;
for (let id in game.sessions) { for (let id in game.sessions) {
@ -585,6 +605,27 @@ const getNextPlayer = (game, name) => {
return 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 getValidCorners = (game) => {
const limits = []; 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.`; error = `It is not your turn! It is ${game.turn.name}'s turn.`;
break; break;
} }
index = value; index = parseInt(value);
if (game.placements.corners[index] === undefined) { if (game.placements.corners[index] === undefined) {
error = `You have requested to place a settlement illegally!`; error = `You have requested to place a settlement illegally!`;
break; 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]; const corner = game.placements.corners[index];
if (corner.color) { if (corner.color) {
error = `This location already has a settlement belonging to ${playerNameFromColor(game, 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.`; error = `It is not your turn! It is ${game.turn.name}'s turn.`;
break; break;
} }
index = value; index = parseInt(value);
if (game.placements.roads[index] === undefined) { if (game.placements.roads[index] === undefined) {
error = `You have requested to place a road illegally!`; error = `You have requested to place a road illegally!`;
break; 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]; const road = game.placements.roads[index];
if (road.color) { if (road.color) {
error = `This location already has a road belonging to ${playerNameFromColor(game, road.color)}!`; error = `This location already has a road belonging to ${playerNameFromColor(game, road.color)}!`;
break; break;
} }
if (game.state === 'initial-placement') { if (game.state === 'initial-placement') {
console.log('TODO: Make sure this road is connected to the settlement!');
road.color = session.color; road.color = session.color;
const next = getNextPlayer(game, name); addChatMessage(game, session, `${name} placed a road.`);
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 = { game.turn = {
actions: ['place-settlement'], actions: ['place-settlement'],
limits: { corners: getValidCorners(game) }, limits: { corners: getValidCorners(game) },
name: next, name: next,
color: getColorFromName(game, next) color: getColorFromName(game, next)
}; };
addChatMessage(game, session, `${name} placed a road.`);
addChatMessage(game, null, `It is ${next}'s turn. Place a settlement.`); 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 { } else {
error = `Road placement not enabled for normal game play.`; error = `Road placement not enabled for normal game play.`;
break; break;
} }
break; break;
error = `Road placement not yet implemented!`;
break;
case 'place-city': case 'place-city':
error = `City placement not yet implemented!`; error = `City placement not yet implemented!`;
break; break;