Turns and rounds mode
All checks were successful
gitea/minesweeper-backend/pipeline/head This commit looks good
All checks were successful
gitea/minesweeper-backend/pipeline/head This commit looks good
This commit is contained in:
parent
d2909570a9
commit
3b7894f644
42
index.js
42
index.js
@ -94,7 +94,7 @@ const handleTileClick = (socket, data) => {
|
|||||||
|
|
||||||
// Find the player and make sure they are alive and have not already selected a tile
|
// Find the player and make sure they are alive and have not already selected a tile
|
||||||
const player = room.players.find(player => player.socketId === socket.id);
|
const player = room.players.find(player => player.socketId === socket.id);
|
||||||
if (!player || player.tombstone || (room.gameMode === "turns" && player.confirmedSelection)) {
|
if (!player || player.tombstone || (room.gameMode === "rounds" && player.confirmedSelection)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ const handleTileClick = (socket, data) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (room.gameMode === "turns") {
|
if (room.gameMode === "rounds") {
|
||||||
const player = room.players.find(player => player.socketId === socket.id)
|
const player = room.players.find(player => player.socketId === socket.id)
|
||||||
if (player) {
|
if (player) {
|
||||||
player.selection = tile.id;
|
player.selection = tile.id;
|
||||||
@ -113,7 +113,18 @@ const handleTileClick = (socket, data) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (room.gameMode === "realtime") {
|
else {
|
||||||
|
if (room.gameMode === "turns") {
|
||||||
|
// Check that the current player is the one who has the turn
|
||||||
|
if (room.currentPlayer !== socket.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (room.gameMode !== "realtime") {
|
||||||
|
// Unrecognized gameMode
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (tile.bomb === true) {
|
if (tile.bomb === true) {
|
||||||
console.log('bomb clicked');
|
console.log('bomb clicked');
|
||||||
room.players.find(user => user.socketId === socket.id).tombstone = tile.id;
|
room.players.find(user => user.socketId === socket.id).tombstone = tile.id;
|
||||||
@ -130,6 +141,7 @@ const handleTileClick = (socket, data) => {
|
|||||||
else if (checkWinCondition(room.players, room.board)) {
|
else if (checkWinCondition(room.players, room.board)) {
|
||||||
room.gameState = "won";
|
room.gameState = "won";
|
||||||
}
|
}
|
||||||
|
room.currentPlayer = getNextPlayer(room.players, room.currentPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
io.to(room.id).emit('updateRoom', buildRoomView(room));
|
io.to(room.id).emit('updateRoom', buildRoomView(room));
|
||||||
@ -143,8 +155,8 @@ const handleTileSelect = (socket, data) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (room.gameMode !== "turns") {
|
if (room.gameMode !== "rounds") {
|
||||||
console.log('game mode is not turns');
|
console.log('game mode is not rounds');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,6 +223,10 @@ const handleTileFlag = (socket, data) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (room.gameMode === "turns" && room.currentPlayer !== socket.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Find the tile that was clicked and select it if it is not already selected by another player
|
// Find the tile that was clicked and select it if it is not already selected by another player
|
||||||
const tile = room.board.tiles.find(tile => tile.id === data.tileId);
|
const tile = room.board.tiles.find(tile => tile.id === data.tileId);
|
||||||
if (!tile || tile.revealed || room.players.some(player => player.selection === data.tileId)) {
|
if (!tile || tile.revealed || room.players.some(player => player.selection === data.tileId)) {
|
||||||
@ -247,6 +263,7 @@ const buildRoom = (userId, username, rows, cols, bombs, autoflag, gameMode) => {
|
|||||||
players: [user],
|
players: [user],
|
||||||
gameMode: gameMode,
|
gameMode: gameMode,
|
||||||
gameState: "playing",
|
gameState: "playing",
|
||||||
|
currentPlayer: user.socketId,
|
||||||
board: board
|
board: board
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -321,6 +338,7 @@ const buildRoomView = (room) => {
|
|||||||
players: room.players,
|
players: room.players,
|
||||||
gameMode: room.gameMode,
|
gameMode: room.gameMode,
|
||||||
gameState: room.gameState,
|
gameState: room.gameState,
|
||||||
|
currentPlayer: room.currentPlayer,
|
||||||
board: buildBoardView(room.board, room.gameState)
|
board: buildBoardView(room.board, room.gameState)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -388,3 +406,17 @@ const isSelectableTile = (tile, players, currentPlayerId) => {
|
|||||||
&& players.filter(player => player.socketId != currentPlayerId).every(player => player.selection !== tile.id)
|
&& players.filter(player => player.socketId != currentPlayerId).every(player => player.selection !== tile.id)
|
||||||
&& players.every(player => player.tombstone !== tile.id);
|
&& players.every(player => player.tombstone !== tile.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getNextPlayer = (players, currentPlayerId) => {
|
||||||
|
if (players.every(player => player.tombstone)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentPlayerIdx = players.findIndex(player => player.socketId === currentPlayerId);
|
||||||
|
let nextPlayerIdx;
|
||||||
|
do {
|
||||||
|
nextPlayerIdx = (currentPlayerIdx + 1) % players.length;
|
||||||
|
currentPlayerIdx = nextPlayerIdx;
|
||||||
|
} while (players[nextPlayerIdx].tombstone);
|
||||||
|
return players[nextPlayerIdx].socketId;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user