diff --git a/cardGen.js b/cardGen.js index c0690d5..160cb1c 100644 --- a/cardGen.js +++ b/cardGen.js @@ -429,6 +429,9 @@ function requestDeck(itemData = null){ deckData[itemCount] = {'deckId':deck.deckId, 'playerId':deck.playerId, 'deckName':deck.deckName, 'maxLength': 0}; // Max length for listPositioning later in cardBuild + + itemData.component.cardCount.deck[forPlayer] = 0; + boardElement[itemCount] = 'realDeck'; cardFace[itemCount] = 0; // Face down for deck @@ -471,7 +474,24 @@ function requestDeck(itemData = null){ // ^ not using item.length incase anything in future gets deleted // from item for instance cardData[itemCount] = builtCards[deckListItem.cardId]; // builtCards id set to cardId from DB, so adding the builtCard object is based on the deckList's cardId (from DB) + + // The data to be passed (for basic stat/inspect basically) + // atk, mana, cost, etc. will be their own components that alter game by game + itemData.component.cardData[itemCount] = { + 'id': builtCards[deckListItem.cardId].id, + 'name': builtCards[deckListItem.cardId].name, + //'effect': builtCards[deckListItem.cardId].effect[0].description, + 'cost': builtCards[deckListItem.cardId].cost, + }; + // For above 'builtCards' should be changed I think + boardElement[itemCount] = 'deck'; // Add all cards to deck at match start + + // 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]; + // Associate the card with the deck // TODO: Change deckIn to something more sensical deckIn[itemCount] = deckItem; @@ -482,7 +502,8 @@ function requestDeck(itemData = null){ deckData[deckItem].maxLength++; // TODO: everything like below, using the new component.XYZ - itemData.component.deck[deckItem].deckSize++; + //itemData.component.deck[deckItem].deckSize++; + itemData.component.cardCount.deck[forPlayer]++; // // Adding to get everything sorted in one! @@ -524,6 +545,9 @@ function requestDeck(itemData = null){ } } + // Set the handSize to 0, will need moving somewhere else + //itemData.component.deck[deckItem].handSize = 0; + itemData.component.cardCount.hand[forPlayer] = 0; itemCount++; // Increment item to not overwrite } diff --git a/components.js b/components.js index c02513f..6d789fe 100644 --- a/components.js +++ b/components.js @@ -11,6 +11,11 @@ const component = { turn : 0, playerTurn : 0, + cardCount : { + deck : {}, + hand : {}, + }, + // Card Stuff cardData : {}, cardFace : {}, diff --git a/gameMod.js b/gameMod.js index 8a60805..611f375 100644 --- a/gameMod.js +++ b/gameMod.js @@ -23,7 +23,7 @@ function passTurn(roomId, playerId){ // 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; + 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 @@ -52,6 +52,57 @@ function passTurn(roomId, playerId){ // 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 + } diff --git a/public/js/canvas/draw.js b/public/js/canvas/draw.js index b47ba21..33c0767 100644 --- a/public/js/canvas/draw.js +++ b/public/js/canvas/draw.js @@ -106,8 +106,8 @@ function drawDeck(entity){ shape: 'circle', x: gameData.position[entity][0], y: gameData.position[entity][1], - width: gameData.size[entity][0]*.375, - height: gameData.size[entity][1]*.375, + width: gameData.size[entity][0]*.25, + height: gameData.size[entity][1]*.25, fillStyle: '#DCDCDC' }); deckCounterSprite.draw(); @@ -115,11 +115,12 @@ function drawDeck(entity){ // Draw deck count text ctx.fillStyle = '#000'; - let deckSize=gameData.deck[entity].deckSize; - let textX=gameData.position[entity][0]-(ctx.measureText(deckSize).width/2); - let textY=gameData.position[entity][1]+(ctx.measureText(deckSize).width/2); + // Deck count for the deck belonging to player + let deckCount=gameData.cardCount.deck[gameData.player[entity]]; + let textX=gameData.position[entity][0];//-(ctx.measureText(deckCount).width); + let textY=gameData.position[entity][1]+(ctx.measureText(deckCount).width/2); - printText(deckSize, textX, textY, 'center', 'middle', 'normal', 'bold', '10', 'Arial', '#000'); + printText(deckCount, textX, textY, 'center', 'bottom', 'normal', 'bold', '10', 'Arial', '#000'); } diff --git a/public/js/game/components.js b/public/js/game/components.js index b298f9b..df92a49 100644 --- a/public/js/game/components.js +++ b/public/js/game/components.js @@ -5,7 +5,6 @@ let gameData = { item : null, itemCount : null, - playerId : null, opponentId : null, roomId : null, @@ -13,6 +12,11 @@ let gameData = { playerTurn : 0, players : null, + cardCount : { + deck : {}, + hand : {}, + }, + // Real components from here? player : {}, diff --git a/public/js/game/dataUpdate.js b/public/js/game/dataUpdate.js index 3cfba6b..8aabd47 100644 --- a/public/js/game/dataUpdate.js +++ b/public/js/game/dataUpdate.js @@ -13,6 +13,7 @@ function updateGameData(data){ updateItems(data.component.item, data.component.itemCount); updatePlayerComponent(data.player); updateDecks(data.component.deck); + updateCardCount(data.component.cardCount); console.log(gameData); } @@ -46,4 +47,8 @@ function updateDecks(deck = null){ function updatePlayerComponent(player = null){ gameData.player = player; } +function updateCardCount(cardCount = null){ + console.log(cardCount); + gameData.cardCount = cardCount; +} diff --git a/public/js/game/socket.js b/public/js/game/socket.js index 799aaea..bda521b 100644 --- a/public/js/game/socket.js +++ b/public/js/game/socket.js @@ -18,3 +18,10 @@ function passTurn(){ requestPassTurn(); } +// Draw Card +socket.on('responseDrawCard', function (data) { + console.log('<< drawCard'); + updateCardCount(data); + drawGameBoard(); +}); + diff --git a/rooms.js b/rooms.js index 07179b6..2c5b4a0 100644 --- a/rooms.js +++ b/rooms.js @@ -4,6 +4,7 @@ const roomMod = require('./roomMod'); let numRooms = 0; const maxRooms = 3; const maxPlayersPerRoom = 2; +global.maxPlayersPerRoom = maxPlayersPerRoom; const maxSpectatorsPerRoom = 0; function requestRooms(socket, filter){