diff --git a/cardGen.js b/cardGen.js index 160cb1c..2eccb69 100644 --- a/cardGen.js +++ b/cardGen.js @@ -490,7 +490,7 @@ function requestDeck(itemData = null){ // Add the entity into 'inDeck' component // also add a listPosition based on current deckLength itemData.component.inDeck[itemCount] = true; // Not sure what to store here - itemData.component.listPosition[itemCount] = itemData.component.cardCount.deck[forPlayer]; + itemData.component.listPosition[itemCount] = itemData.component.cardCount.deck[forPlayer] + 1; // Associate the card with the deck // TODO: Change deckIn to something more sensical diff --git a/components.js b/components.js index 6d789fe..8b59d6a 100644 --- a/components.js +++ b/components.js @@ -10,6 +10,7 @@ const component = { roomId : null, turn : 0, playerTurn : 0, + player : {}, cardCount : { deck : {}, diff --git a/gameHelper.js b/gameHelper.js new file mode 100644 index 0000000..949a1ed --- /dev/null +++ b/gameHelper.js @@ -0,0 +1,53 @@ +// Moves card positions down/up for the old/new element for player in room +function setCardPosition(roomId, player, card, newPosition, newElement, oldPosition, oldElement){ + + // Move anything in the old boardElement after the old listPosition down by one + moveElementPositions(roomId, player, 0, oldElement, oldPosition); + + // Move anything in the new boardElement after (including) the new listPosition up by one + moveElementPositions(roomId, player, 1, newElement, newPosition); + + // Then fit the card into the new gap that's opened up + listPosition[card] = newPosition; + + // Remove from oldElement + delete oldElement[card] + // Add to newElement + newElement[card] = newPosition; + +} + +// direction 0 up, 1 down +function moveElementPositions(roomId, player, direction, element, position){ + + position = position; + listPosition = global.roomData[roomId].itemData.component.listPosition; + + for (const [key, value] of Object.entries(element)) { + + if(global.roomData[roomId].itemData.component.player[key] != player){ + continue; + } + + + // Move down from (not including) the position passed + if(direction == 0 && listPosition[key] > position){ + listPosition[key]--; + } + + // Move everything from (including) the new position up + if(direction == 1 && listPosition[key] >= position){ + console.log('hit'); + console.log(listPosition[key]); + console.log(position); + listPosition[key]++; + } + + } + +} + +module.exports = { + setCardPosition +}; + diff --git a/gameMod.js b/gameMod.js index 611f375..d7d3c8d 100644 --- a/gameMod.js +++ b/gameMod.js @@ -1,3 +1,5 @@ +const gameHelper = require('./gameHelper'); + // 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 @@ -17,7 +19,7 @@ function passTurn(roomId, playerId){ //global.socketAlert(roomData[roomId].playerData[playerId].socketId, playerTurn, 'log'); if(playerTurn != playerId){ - global.socketAlert(roomData[roomId].playerData[playerId].socketId, 'Not your turn', 'alert'); + global.socketAlert(global.getPlayerSocketFromRoom(playerId, roomId), 'Not your turn', 'alert'); return false; }; @@ -32,30 +34,14 @@ function passTurn(roomId, playerId){ } // 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); - - } + global.socketResponsePassTurn(roomId); // Let the player know it's their turn via alert too (in case tabbed out) + // TODO: This could probably be done front-end from the newPlayerTurn in socketResponsePassTurn global.socketAlert(roomData[roomId].playerData[newPlayerTurn].socketId, 'Your turn', 'alert'); - // Start of the new players turn, draw a card drawACard(roomId, newPlayerTurn); - } @@ -65,49 +51,104 @@ function drawACard(roomId, playerId){ global.socketAlert(roomData[roomId].playerData[playerId].socketId, 'Hand full; cannot draw card', 'alert'); return false; } + if(global.roomData[roomId].itemData.component.cardCount.deck[playerId] <= 0){ + global.socketAlert(roomData[roomId].playerData[playerId].socketId, 'Deck empty; cannot draw card', 'alert'); + return false; + } + + // TODO: Check no card event/trigger occured that prevents/change draw card + // 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 + let fromPosition = global.roomData[roomId].itemData.component.cardCount.deck[playerId]; // 'top' of deck + let toPosition = global.roomData[roomId].itemData.component.cardCount.hand[playerId] + 1; // Rightmost hand pos (starting at 1) + + // ECSey att2, there's surely a better way of getting playerX top card within inDeck? + // Tried unions but it messes up the object data. Maybe need to have no data in each object + // and have it literally just be keys? + + // Get each card from the deck + for (const [key, value] of Object.entries(global.roomData[roomId].itemData.component.inDeck)) { + // Key is the entity here + + // If the card inDeck does not belongs to the player, skip over it + if(global.roomData[roomId].itemData.component.player[key] != playerId){ + continue; + } + + // If the card isn't the last (bottom) card of deck, skip over it + // TODO: -1 is jank, sort so listPositions all start from 1..x + if(global.roomData[roomId].itemData.component.listPosition[key] != fromPosition){ + continue; + } + + // The main man + // Move positions in hand/deck, and put the item from the deck into the hand + gameHelper.setCardPosition(roomId, playerId, key, toPosition, global.roomData[roomId].itemData.component.hand, fromPosition, global.roomData[roomId].itemData.component.inDeck); + + } - - // 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]++; + + // Then emit the deckSize and hand size to all the player's sockets + global.socketResponseDrawCard(roomId, 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; + // Emit the 'hand' and related cardData for cards in the players hand + // Could merge this with the top? + global.socketResponsePlayerDrewCard(roomId, playerId); +} - // 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'); +// DATA RETURNER DUDES +// TODO: Where to put this? Kind of a helper, kind of functionality. Hmmmmm +// maybe do a dataHelper? then anything to return data can be included there? +// TODO: May get all the data from hand, board, grave, etc. in functions +// like this, then union all the data and return that in one swoomp +// Probably better to just get all the keys from the boardlemenets and do +// the loop once though... +function getPlayerHandData(roomId, playerId){ + + let handEntities = {}; + let handPositions = {}; + let handCardData = {}; + + for (const [key, value] of Object.entries(global.roomData[roomId].itemData.component.hand)) { + + // Key is entity ID here + + // If the entity in hand belongs to the player, then they are allowed its data + if(global.roomData[roomId].itemData.component.player[key] == playerId){ - // Send the data back - global.io.to(clientSocket.id).emit( - 'responseDrawCard' - ,global.roomData[roomId].itemData.component.cardCount - ); + // Get entity of items in the hand + handEntities[key] = global.roomData[roomId].itemData.component.hand[key]; + // Get listPosition of just items in the hand + handPositions[key] = global.roomData[roomId].itemData.component.listPosition[key]; + // Same for cardData + handCardData[key] = global.roomData[roomId].itemData.component.cardData[key]; // TODO: Nothing on client side? + + // Leaving other bits for now + + } } - - // Emit the 'hand' and related cardData for cards in the players hand - - + + return { + 'handEntities': handEntities, + 'handPositions': handPositions, + 'handCardData': handCardData + }; + } module.exports = { passTurn + ,getPlayerHandData + ,drawACard }; diff --git a/public/index.html b/public/index.html index dfa5155..ea0eb6f 100644 --- a/public/index.html +++ b/public/index.html @@ -19,6 +19,7 @@