diff --git a/cardGen.js b/cardGen.js index 72a903c..f69e03e 100644 --- a/cardGen.js +++ b/cardGen.js @@ -225,6 +225,8 @@ function requestDeck(itemData = null){ // To continue from previous item/itemCount from other funcs, something like this let item = []; let itemCount = 0; + let player = {}; + let players = []; // Jank check to allow current client call, and new roomMod.roomGeneration call to // both work (will replace client call with roomGeneration when closer) @@ -235,28 +237,53 @@ function requestDeck(itemData = null){ if(itemData['itemCount'] !== undefined){ itemCount = itemData['itemCount']; } + if(itemData['player'] !== undefined){ + player = itemData['player']; + } + if(itemData['players'] !== undefined){ + players = itemData['players']; + } + }else{ + itemData = {}; } + // TODO: Deck data/deckIn should be in deckGen, other stuff probably in a cardGen + // function let deckData = {}; // New but may be useful let deckIn = {}; // Which deck the item is in? Kinda the player/boardelement thing? let cardData = {}; let boardElement = {}; + // New(in here) + let cardStatus = {}; // Tapped, etc + // player + let listPosition = {}; + let cardFace = {}; + // REALLY NEW + let cardColours = {}; // Array of req. + let cardAttack = {}; // Base, Current + let cardSprite = {}; // Maybe from DB? + // TODO: Set the player. For now will do this in front-end as testing currently + let forPlayer = 0; // TODO: Change to actually have each player select a deck // Loop and create the deck first - //console.log(decks); decks.forEach((deck) => { item.push(itemCount); // Add new item to add stuff for - deckData[itemCount] = {'deckId':deck.deckId, 'playerId':deck.playerId, 'deckName':deck.deckName}; + deckData[itemCount] = {'deckId':deck.deckId, 'playerId':deck.playerId, 'deckName':deck.deckName, 'maxLength': 0}; // Max length for listPositioning later in cardBuild boardElement[itemCount] = 'realDeck'; - itemCount++; + cardFace[itemCount] = 0; // Face down for deck + player[itemCount] = forPlayer; + itemCount++; // Needed/good + forPlayer++; // Jank/bad }) - console.log(deckData); - console.log(deckList); + //console.log(deckData); + //console.log(deckList); // Loop each item in the deckList // Loop inside it X times where X is cardCount // Add the builtCard with same cardId as deckList item X times + + // and load that deck for them. This just sets first deck to player0 to players.length deckList.forEach((deckListItem) => { let deckItem = null; @@ -266,9 +293,12 @@ function requestDeck(itemData = null){ // Needs to check deck AND player id, as that's the primary key (together) if(deckData[key].deckId == deckListItem.deckId && deckData[key].playerId == deckListItem.playerId){ deckItem = key; // Key is the `item` key + // Now add cards to the player that this deck belongs to + forPlayer = player[key]; } }; + // For each new card, loop to the cardCount (how many cards in deck) // and add to the deck for(let i = 0; i < deckListItem.cardCount; i++){ @@ -280,25 +310,49 @@ function requestDeck(itemData = null){ // Associate the card with the deck // TODO: Change deckIn to something more sensical deckIn[itemCount] = deckItem; + + // Attempt to set the listPosition of each card in related deck + // Increase the length of the deck in deckItem + listPosition[itemCount] = deckData[deckItem].maxLength; // TODO: better + deckData[deckItem].maxLength++; + + // Adding to get everything sorted in one! + cardStatus[itemCount] = null; + cardFace[itemCount] = 0; // Face down by default (in deck) + cardColours[itemCount] = []; // TODO; + // From cardData set the base attack, and current (same when deckbuild) + let atk = cardData[itemCount].atk; + cardAttack[itemCount] = [atk, atk]; // Base, Current + player[itemCount] = forPlayer; // Jank TODO: actually set to correct player + + itemCount++; // Increment item to not overwrite } }); + // ADD all new elements, and updated data into itemData + itemData.item = item; + itemData.itemCount = itemCount; + itemData.player = player; + itemData.players = players; + itemData.deckData = deckData; + itemData.deckIn = deckIn; + itemData.cardData = cardData; + itemData.boardElement = boardElement; + itemData.cardStatus = cardStatus; + itemData.listPosition = listPosition; + itemData.cardFace = cardFace; + itemData.cardColours = cardColours; + //itemData.cardSprite = cardSprite; + // item, itemCount, deckData, cardData, boardElement //console.log(cardData); // Returning everything to be looped in the front-end // This won't be looped as it will at final, instead just for deck generation // Returned as object over array, as easier to disect when gets to front-end - let dataReturn = { - item: item, - itemCount: itemCount, - deckData: deckData, - deckIn: deckIn, - cardData: cardData, - boardElement: boardElement, - }; + let dataReturn = itemData; return resolve(dataReturn); //return resolve(cardData); diff --git a/public/board.js b/public/board.js index 76a6151..505b91d 100644 --- a/public/board.js +++ b/public/board.js @@ -825,22 +825,50 @@ function loadBoard(data) { cardData = data.cardData; // position; // Not to do, as calculated per client (and will be per screen-size) // size; // Like position, calculated per client. - //cardStatus; // TODO: ? Statuses on load?, TODO: OR if player DCs and needs to reload! - player = {}; // TODO: Set each item to correct player - listPosition = {}; // TODO: - cardFace = {}; // TODO: Like status, for DCs and reloads + cardStatus = data.cardStatus; + player = data.player; // Set each item to correct player + listPosition = data.listPosition; + cardFace = data.cardFace; // Like status, for DCs and reloads + cardSprite = {}; // TODO: ? Maybe, or this could be done clientside based // ^ on card id? - cardFace = {}; // TODO: For DB/Reconnect more-so let deckIn = data.deckIn; // NEW, may be used, for now player substitute let deckData = data.deckData; + let cardAttack = data.cardAttack; // TODO: add to the logic + let cardColours = data.cardColours; // TODO: add to the logic - + // TODO: JANK IN, CHANGE CODE TO USE NEW ARRAY!! + // Temp jank, set colour to first colour req. + for(let i = 0; i < itemCount; i++){ + if(cardData[i] !== undefined){ // i may not have carddata, as realDeck + cardData[i].colour = 0;//cardData[itemCount].colour[0]; + // Set the artwork (this would be done front-end I think) + cardSprite[i] = [0,0]; + // Temp sprite set based on colour TODO: Change to set correct sprite from DB + switch (cardData[i].colour){ + case 0: // White + cardSprite[i] = [0,0]; + break; + case 1: // Blue + cardSprite[i] = [0,1]; + break; + case 2: // Red + cardSprite[i] = [1,0]; + break; + case 3: // Green + cardSprite[i] = [1,1]; + break; + default: + break; + } + } + } // Stuff not currently in the getDecks TODO: To be stripped down and // rewritten/made into generateItems or something, for each match // Decks can be as-is for getting deckLists for deckselection, but // TODO: for matches need something to generate and return every item, and attribute +if(false){ // Temp solution // Loop all items, and set their related missing attributes // TODO: some more jank to get it 'playable' with DB entries @@ -901,6 +929,8 @@ function loadBoard(data) { } } + +} // END FALSE/TEMP DISABLE shuffleDeck(0); // Shuffle player 0 deck shuffleDeck(1); // Shuffle player 1 deck diff --git a/public/index.html b/public/index.html index 72d0112..d0ccafc 100644 --- a/public/index.html +++ b/public/index.html @@ -61,6 +61,7 @@
+
diff --git a/public/main.js b/public/main.js index c30d765..413e56d 100644 --- a/public/main.js +++ b/public/main.js @@ -186,3 +186,25 @@ socket.on('responseStartGame', function (data) { loadBoard(data.message); }); +// More experimenty +function requestRoomGeneration(){ + console.log('+ requestRoomGeneration'); + socket.emit('requestRoomGeneration'); +} +function responseRoomGeneration(data){ + if(!data.success){ + alert(data.message); + return false; + } + return data; +} +socket.on('responseRoomGeneration', function (data) { + console.log('<< responseRoomGeneration'); + responseStartGame(data); + if(data.success !== true){ + alert('Err with responseRoomGeneration. '+data.message); + } + // Pass only the data to loadBoard + loadBoard(data.message); +}); + diff --git a/roomMod.js b/roomMod.js index 11a88d7..61ac1b0 100644 --- a/roomMod.js +++ b/roomMod.js @@ -15,17 +15,20 @@ function setPlayers(playerCount = 2, itemData){ // Add new item attribute for 'players' // TODO: Maybe check if exists, and add to in that case (for replacing people?) // Doubt that would ever make it into the game, but could still be worth - itemData.player = {}; //let player = {}; + itemData.player = {}; //let player = {}; // Player item belongs to + itemData.players = []; // List of the players (an associated playerItem?) + //itemData. // Can be done with just referring to itemData[x], but less readable // Will likely redefine vars each new function. For now will keep this as-is let playerNo = 0 + itemData['itemCount']; // Start loop from current itemCount playerCount = playerCount + itemData['itemCount']; // End at playerCount diff from itemCount - for(playerNo; playerNo <= playerCount; playerNo++){ + for(playerNo; playerNo < playerCount; playerNo++){ itemData['item'].push(playerNo); itemData['player'][itemData['itemCount']] = playerNo; // The player belongs to itself itemData['itemCount']++; + itemData['players'].push(playerNo); // Add player no to array so can be looped } // Return related item and item attributes //returns = {'item': item, 'itemCount': itemCount, 'player': player}; @@ -49,11 +52,11 @@ function roomGeneration(playerCount, teamCount = null, playerTeams = null){ // TODO: Get their selected decks (will need to pass somewhere) // Generate the decks, and card within the deck cardLists - const [deckData] = await Promise.all([ cardGen.requestDeck(itemData) ]); + [itemData] = await Promise.all([ cardGen.requestDeck(itemData) ]); - console.log('deckData'); - console.log(deckData); - return resolve(deckData); + //console.log('deckData'); + //console.log(deckData); + return resolve(itemData); })() }); } diff --git a/server.js b/server.js index 0adafe0..70889cb 100644 --- a/server.js +++ b/server.js @@ -58,6 +58,19 @@ function requestStartGame(socket){ io.to(socket.id).emit('responseStartGame', err); }); } +function requestRoomGeneration(socket){ + response = {success: false, message: 'Failed requestRoomGeneration() server.js'}; + roomMod.roomGeneration().then(data => { + response.success = true; + response.message = data; + io.to(socket.id).emit('responseRoomGeneration', response); + }) + .catch(err => { + response.message = err; + io.to(socket.id).emit('responseRoomGeneration', err); + }); + +} function onConnection(socket){ @@ -70,6 +83,9 @@ function onConnection(socket){ socket.on('requestStartGame', function() { requestStartGame(socket); }); + socket.on('requestRoomGeneration', function() { + requestRoomGeneration(socket); + }); socket.on('requestRooms', function(filter) { requestRooms(socket, filter);