// For anything related to the actual game itself (kinda) // this will be split into different bits, but should be what manages a rooms // game states, and alladat // Basically here to prevent circular dependencies (where I can) // PlayerId is using array 0,1,2 for now, not the actual id // actual Id would be better, but the player should be passed correctly // from the client. They can edit data, but server-side validation SHOULD prevent // in the future function passTurn(roomId, playerId){ // TODO:Check playerId and roomId before doing the stuff, to verify the user // IS the user, and in the room let playerTurn = global.roomData[roomId].itemData.component.playerTurn; //global.socketAlert(roomData[roomId].playerData[playerId].socketId, playerTurn, 'log'); if(playerTurn != playerId){ global.socketAlert(roomData[roomId].playerData[playerId].socketId, 'Not your turn', 'alert'); return false; }; // Turns are 0,1,0,1 at the mo, no coinflip or re-order, etc. so this JANK is ok for now // %2 as 2 players and Ids are 0 and 1 so it works let newPlayerTurn = (playerTurn + 1)%2; global.roomData[roomId].itemData.component.playerTurn = newPlayerTurn; // If it's back to the player that went first, the turn count increased too if(playerTurn == 0){ global.roomData[roomId].itemData.component.turn++; } // Send turn data to each player // Probably via the sockets in room (as could be spectators in future) let clients = global.io.sockets.adapter.rooms.get(roomId); for (const clientId of clients) { const clientSocket = global.io.sockets.sockets.get(clientId); console.log('>> '+clientSocket.playerId+' passed turn'); // Send the data back let turnData = { 'playerTurn': newPlayerTurn, 'turn': global.roomData[roomId].itemData.component.turn, }; global.io.to(clientSocket.id).emit('responsePassTurn', turnData); } // Let the player know it's their turn via alert too (in case tabbed out) global.socketAlert(roomData[roomId].playerData[newPlayerTurn].socketId, 'Your turn', 'alert'); } module.exports = { passTurn };