From 144f3142e14f697536d0370e04c9d925e40b5782 Mon Sep 17 00:00:00 2001 From: Nathan Date: Sat, 19 Oct 2024 11:18:26 +0100 Subject: [PATCH] JANK in the server/db data into frontend! --- public/board.js | 100 +++++++++++++++++++++++++++++++++++++++++++++++- public/main.js | 6 ++- server.js | 52 ++++++++++++++++++++----- 3 files changed, 146 insertions(+), 12 deletions(-) diff --git a/public/board.js b/public/board.js index 1144aa9..30dcc15 100644 --- a/public/board.js +++ b/public/board.js @@ -812,10 +812,104 @@ requestDeck(); // public/main.js // socket.on('responseGetDeck', function (data) { -function loadBoard() { +function loadBoard(data) { + + console.log('DATA'); + console.log(data); + + // Built the original boardstate using the data passed from server + // This is only for the first time recieve (TODO: ALL) game data at match start + // Then ideally only what's needed to be passed to/from client/server sent after + item = data.item; + itemCount = data.itemCount; + boardElement = data.boardElement; + 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 + 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; + + + // 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 + + console.log(cardData[0]); + + // Temp solution + // Loop all items, and set their related missing attributes + // TODO: some more jank to get it 'playable' with DB entries + let player0DeckCount = 0; + let player1DeckCount = 0; + for(let i = 0; i < itemCount; i++){ + // Set player for decks (to just be their array index for now) TODO: Properly + if(boardElement[i] == 'realDeck'){ + player[i] = i; // Jank... + //player[i] = deckData[i]; + //console.log(deckData[i]); + }else{ + // Everything else missing in a jank fashion + player[i] = deckIn[i]; // 1-1 here, but likely not in future + } + + cardStatus[i] = null; // Just building on first load, as deck gen + // substite at the mo, so all cards are free of status + cardFace[i] = 0; // As above, deck gen sub. Everything starts facedown + + // Set position for cards in each deck... + // TODO: JANK, tidy, remove, make good!! + // using the new player set just above, which in itself is jank + if(player[i] == 0 && boardElement[i] != 'realDeck'){ + listPosition[i] = player0DeckCount+1; + player0DeckCount++; + } + // Just an else, as in DB/for this test only 2 decks + // TODO: MAKE BETTER! + else{ + listPosition[i] = player0DeckCount+1; + player0DeckCount++; + } + + // TODO: JANK IN, CHANGE CODE TO USE NEW ARRAY!! + // Temp jank, set colour to first colour req. + 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; + } + } + + } + shuffleDeck(0); // Shuffle player 0 deck + shuffleDeck(1); // Shuffle player 1 deck + //return false; // Fill each players deck with their cards - createDecks(); + //createDecks(); // Play shield from top of each players deck to the maximum allowed (4 typically) for(let currentPlayer = 0; currentPlayer <= players-1; currentPlayer++){ @@ -1183,6 +1277,7 @@ function calculateItemSizePosition(itemKey){ positionY = 60; width = cardWidth*1.5; height = cardHeight*1.5; + console.log('HELLO 1'); } if(itemPlayer == 0 && itemElement == 'realDeck'){ @@ -1190,6 +1285,7 @@ function calculateItemSizePosition(itemKey){ positionY = canvas.height-cardHeight*1.5-60; width = cardWidth*1.5; height = cardHeight*1.5; + console.log('HELLO 0'); } if(itemPlayer == 1 && itemElement == 'board'){ diff --git a/public/main.js b/public/main.js index 527211d..433553f 100644 --- a/public/main.js +++ b/public/main.js @@ -181,6 +181,10 @@ socket.on('responseGetDeck', function (data) { console.log('<< responseGetDeck'); responseGetDeck(data); console.log('Load board?'); - loadBoard(data); + if(data.success !== true){ + alert('Err with responseGetDeck. '+data.message); + } + // Pass only the data to loadBoard + loadBoard(data.message); }); diff --git a/server.js b/server.js index e46005d..cf3e629 100644 --- a/server.js +++ b/server.js @@ -271,7 +271,7 @@ function requestDeck(socket, playerId, deckId){ // Add the cards (x of how many cardObjects with cardId are in count in decklist) // to a deck array to pass through. Or decks array with deckId specified? - console.log(deckList); + //console.log(deckList); // These are the four basic fellas for this // from public/board.js where current game testing/logic is applied // So creating as a test for a 'room' to see if it's simple enough to @@ -280,22 +280,37 @@ function requestDeck(socket, playerId, deckId){ let item = []; let itemCount = 0; let deckData = {}; // New but may be useful + let deckIn = {}; // Which deck the item is in? Kinda the player/boardelement thing? let cardData = {}; let boardElement = {}; // TODO: Set the player. For now will do this in front-end as testing currently + // 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}; + boardElement[itemCount] = 'realDeck'; + itemCount++; + }) + 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 deckList.forEach((deckListItem) => { - // Check if it's a new deck (not sure how) - if(false){ // TODO: Logic check - item.push(itemCount); // Add new item to add stuff for - deckData[itemCount] = [deckListItem.deckId, deckListItem.playerId]; - boardElement[itemCount] = 'realDeck'; - itemCount++; - } + let deckItem = null; + // Loop each deck, if the deck/playerIds match, add association + for(key in deckData){ + //Object.keys(deckData).forEach(function(key) { // Less efficient than for + // 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 + } + + }; // 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++){ @@ -304,13 +319,30 @@ function requestDeck(socket, playerId, deckId){ // 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) boardElement[itemCount] = 'deck'; // Add all cards to deck at match start + // Associate the card with the deck + // TODO: Change deckIn to something more sensical + deckIn[itemCount] = deckItem; itemCount++; // Increment item to not overwrite } }); + // item, itemCount, deckData, cardData, boardElement //console.log(cardData); - return resolve(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, + }; + return resolve(dataReturn); + //return resolve(cardData); // Promise stuff testing })() @@ -325,6 +357,8 @@ function requestDeck(socket, playerId, deckId){ // things, but outside of testing that won't occur(?), may need to test this at some // point to see. For now DB, and generating is ok, as still working on it } +// For testing +requestDeck(); function requestDeckStart(socket){ // For future: