// 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)%maxPlayersPerRoom; 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'); // Start of the new players turn, draw a card drawACard(roomId, newPlayerTurn); } function drawACard(roomId, playerId){ if(global.roomData[roomId].itemData.component.cardCount.hand[playerId] >= 2){ global.socketAlert(roomData[roomId].playerData[playerId].socketId, 'Hand full; cannot draw card', 'alert'); return false; } // Change position to last position available in hand let fromPosition = global.roomData[roomId].itemData.component.cardCount.deck[playerId]; // 'Bottom' of deck let toPosition = global.roomData[roomId].itemData.component.cardCount.hand[playerId]; // Rightmost hand pos // from inDeck to hand // change the listPositions of both (only return listPosition of hand ids?) // Reduce deckSize by 1 for the player that drew global.roomData[roomId].itemData.component.cardCount.deck[playerId]--; // And increase the hand size by 1 global.roomData[roomId].itemData.component.cardCount.hand[playerId]++; // TODO: Move card from deck to hand for the player. Dupe what I've got in board for // this, but tidy it up //global.roomData[roomId].itemData.component.inDeck = newPlayerTurn; //global.roomData[roomId].itemData.component.hand = newPlayerTurn; // Then emit the deckSize and hand size to all the player's sockets 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+' drew card'); // Send the data back global.io.to(clientSocket.id).emit( 'responseDrawCard' ,global.roomData[roomId].itemData.component.cardCount ); } // Emit the 'hand' and related cardData for cards in the players hand } module.exports = { passTurn };