diff --git a/gameHelper.js b/gameHelper.js index 949a1ed..1c7e150 100644 --- a/gameHelper.js +++ b/gameHelper.js @@ -47,6 +47,7 @@ function moveElementPositions(roomId, player, direction, element, position){ } + module.exports = { setCardPosition }; diff --git a/gameMod.js b/gameMod.js index d7d3c8d..41691a0 100644 --- a/gameMod.js +++ b/gameMod.js @@ -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 // TODO: Where to put this? Kind of a helper, kind of functionality. Hmmmmm @@ -147,8 +187,9 @@ function getPlayerHandData(roomId, playerId){ module.exports = { - passTurn + passTurn ,getPlayerHandData ,drawACard + ,shuffleDeck }; diff --git a/public/index.html b/public/index.html index ea0eb6f..e3e7560 100644 --- a/public/index.html +++ b/public/index.html @@ -20,6 +20,7 @@
+


diff --git a/public/js/game/socket.js b/public/js/game/socket.js index 04eab94..057def7 100644 --- a/public/js/game/socket.js +++ b/public/js/game/socket.js @@ -1,5 +1,6 @@ // Any socket request/responses for the actual game (when in a match) +// PASS TURN function requestPassTurn(){ console.log('>> passTurn'); socket.emit('passTurn', gameData.roomId, gameData.playerId); @@ -10,18 +11,13 @@ socket.on('responsePassTurn', function (data) { updateTurn(data.turn, data.playerTurn); drawGameBoard(); }); + + +// DRAW A CARD function requestDrawACard(){ console.log('>> drawACard'); 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 // Both players get new hand + deck counts updated socket.on('responseDrawCard', function (data) { @@ -39,3 +35,29 @@ socket.on('responsePlayerDrewCard', function (data) { 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(); +} + diff --git a/roomMod.js b/roomMod.js index 384f2af..4fbff12 100644 --- a/roomMod.js +++ b/roomMod.js @@ -93,6 +93,7 @@ function roomGeneration(roomId){ roomData[roomId].playerData = playerData; roomData[roomId].playerOrder = playerOrder; + // Return the all the itemData to the client(s) // TODO: This will need to give different data for each, or at least // to differ the boardside diff --git a/server.js b/server.js index beab350..3485d85 100644 --- a/server.js +++ b/server.js @@ -59,6 +59,10 @@ function onConnection(socket){ gameMod.drawACard(roomId, playerId); }); + socket.on('shuffleDeck', function(roomId, playerId) { + gameMod.shuffleDeck(roomId, playerId); + }); + } 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] + ); + +} +