Add shuffle to server + button to frontend

feature/clientSideSimplify
Nathan Steel 1 year ago
parent 2041108973
commit fc2784fec8

@ -47,6 +47,7 @@ function moveElementPositions(roomId, player, direction, element, position){
} }
module.exports = { module.exports = {
setCardPosition setCardPosition
}; };

@ -103,6 +103,46 @@ function drawACard(roomId, playerId){
} }
// Shuffle the deck 'randomly' for a certain player
function shuffleDeck(roomId, playerId){
// Create a tempDeck array of same length of the player deck
let deckLength = global.roomData[roomId].itemData.component.cardCount.deck[playerId];
let tempDeck = Array.from(Array(deckLength).keys());
// Loop the tempDeck and shuffle
// https://stackoverflow.com/a/73603221
for(let i = 0; i < deckLength; i++){
// picks the random number between 0 and length of the deck (-1 so 0..34)
let shuffle = Math.floor(Math.random() * (tempDeck.length - 1));
// swap the current listPosition with a random one with the deck count
[ tempDeck[i], tempDeck[shuffle] ] = [ tempDeck[shuffle], tempDeck[i] ];
}
let tempDeckItem = 0;
// Now change the related inDeck entities listPositions to match the random number from tempDeck
for (const [key, value] of Object.entries(global.roomData[roomId].itemData.component.inDeck)) {
if(global.roomData[roomId].itemData.component.player[key] != playerId){
continue;
}
// Add the tempDeckItems number as the listPosition for the inDeck item
// + 1 as listPositions start at 1
global.roomData[roomId].itemData.component.listPosition[key] = tempDeck[tempDeckItem] + 1;
// Move to next tempDeckItem
tempDeckItem++;
}
global.socketResponseShuffleDeck(roomId, playerId, true);
}
// DATA RETURNER DUDES // DATA RETURNER DUDES
// TODO: Where to put this? Kind of a helper, kind of functionality. Hmmmmm // TODO: Where to put this? Kind of a helper, kind of functionality. Hmmmmm
@ -147,8 +187,9 @@ function getPlayerHandData(roomId, playerId){
module.exports = { module.exports = {
passTurn passTurn
,getPlayerHandData ,getPlayerHandData
,drawACard ,drawACard
,shuffleDeck
}; };

@ -20,6 +20,7 @@
<hr> <hr>
<button onclick="passTurn()">Pass Turn</button> <button onclick="passTurn()">Pass Turn</button>
<button onclick="requestDrawACard()">Draw Card</button> <button onclick="requestDrawACard()">Draw Card</button>
<button onclick="requestShuffleDeck()">Shuffle Deck</button>
<br><br> <br><br>
<hr> <hr>

@ -1,5 +1,6 @@
// Any socket request/responses for the actual game (when in a match) // Any socket request/responses for the actual game (when in a match)
// PASS TURN
function requestPassTurn(){ function requestPassTurn(){
console.log('>> passTurn'); console.log('>> passTurn');
socket.emit('passTurn', gameData.roomId, gameData.playerId); socket.emit('passTurn', gameData.roomId, gameData.playerId);
@ -10,18 +11,13 @@ socket.on('responsePassTurn', function (data) {
updateTurn(data.turn, data.playerTurn); updateTurn(data.turn, data.playerTurn);
drawGameBoard(); drawGameBoard();
}); });
// DRAW A CARD
function requestDrawACard(){ function requestDrawACard(){
console.log('>> drawACard'); console.log('>> drawACard');
socket.emit('drawACard', gameData.roomId, gameData.playerId); socket.emit('drawACard', gameData.roomId, gameData.playerId);
} }
// Functions like this would be elsewhere, do client-side
// validation THEN request stuff from the server?
// This is here for now, as it's used by the button
function passTurn(){
requestPassTurn();
}
// Draw Card // Draw Card
// Both players get new hand + deck counts updated // Both players get new hand + deck counts updated
socket.on('responseDrawCard', function (data) { socket.on('responseDrawCard', function (data) {
@ -39,3 +35,29 @@ socket.on('responsePlayerDrewCard', function (data) {
drawGameBoard(); drawGameBoard();
}); });
// SHUFFLE DECK
function requestShuffleDeck(){
console.log('>> shuffleDeck');
socket.emit('shuffleDeck', gameData.roomId, gameData.playerId);
}
// Both players get an emit that the deck has been shuffled
// but do not recieve any other data (not needed)
// Only need to know a deck has been shuffled to perform animation in future
socket.on('responseShuffleDeck', function (data) {
console.log('<< shuffleDeck');
alert(data[0] + ' shuffled');
// TODO
// animateDeckShuffle(playerX from data);
// drawGameBoard(); // From this point drawGameBoard should be a 60FPS loop
// not a one off draw after emits
});
// Functions like this would be elsewhere, do client-side
// validation THEN request stuff from the server?
// This is here for now, as it's used by the button
function passTurn(){
requestPassTurn();
}

@ -93,6 +93,7 @@ function roomGeneration(roomId){
roomData[roomId].playerData = playerData; roomData[roomId].playerData = playerData;
roomData[roomId].playerOrder = playerOrder; roomData[roomId].playerOrder = playerOrder;
// Return the all the itemData to the client(s) // Return the all the itemData to the client(s)
// TODO: This will need to give different data for each, or at least // TODO: This will need to give different data for each, or at least
// to differ the boardside // to differ the boardside

@ -59,6 +59,10 @@ function onConnection(socket){
gameMod.drawACard(roomId, playerId); gameMod.drawACard(roomId, playerId);
}); });
socket.on('shuffleDeck', function(roomId, playerId) {
gameMod.shuffleDeck(roomId, playerId);
});
} }
global.getPlayerSocketFromRoom = function(playerId, roomId){ global.getPlayerSocketFromRoom = function(playerId, roomId){
@ -129,3 +133,12 @@ global.socketResponsePlayerDrewCard = function(roomId, playerId){
} }
global.socketResponseShuffleDeck = function(roomId, playerId, hasShuffled){
global.io.to(global.getPlayerSocketFromRoom(playerId, roomId)).emit(
'responseShuffleDeck'
,[playerId, hasShuffled]
);
}

Loading…
Cancel
Save