Turns and rounds mode
All checks were successful
gitea/minesweeper-backend/pipeline/head This commit looks good

This commit is contained in:
Jose134 2025-01-22 11:55:31 +01:00
parent d2909570a9
commit 3b7894f644

View File

@ -94,7 +94,7 @@ const handleTileClick = (socket, data) => {
// 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);
if (!player || player.tombstone || (room.gameMode === "turns" && player.confirmedSelection)) {
if (!player || player.tombstone || (room.gameMode === "rounds" && player.confirmedSelection)) {
return;
}
@ -104,7 +104,7 @@ const handleTileClick = (socket, data) => {
return;
}
if (room.gameMode === "turns") {
if (room.gameMode === "rounds") {
const player = room.players.find(player => player.socketId === socket.id)
if (player) {
player.selection = tile.id;
@ -113,7 +113,18 @@ const handleTileClick = (socket, data) => {
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) {
console.log('bomb clicked');
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)) {
room.gameState = "won";
}
room.currentPlayer = getNextPlayer(room.players, room.currentPlayer);
}
io.to(room.id).emit('updateRoom', buildRoomView(room));
@ -143,8 +155,8 @@ const handleTileSelect = (socket, data) => {
return;
}
if (room.gameMode !== "turns") {
console.log('game mode is not turns');
if (room.gameMode !== "rounds") {
console.log('game mode is not rounds');
return;
}
@ -211,6 +223,10 @@ const handleTileFlag = (socket, data) => {
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
const tile = room.board.tiles.find(tile => tile.id === 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],
gameMode: gameMode,
gameState: "playing",
currentPlayer: user.socketId,
board: board
};
};
@ -321,6 +338,7 @@ const buildRoomView = (room) => {
players: room.players,
gameMode: room.gameMode,
gameState: room.gameState,
currentPlayer: room.currentPlayer,
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.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;
}