From fe41db49e514b8af2ecd5b65b9df8cc5ad8dce32 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 23 Oct 2024 09:59:21 +0100 Subject: [PATCH] Add 'manaColour' attribute/DB element Currently cards will only have one mana colour, in future need to extend the (in code) functionality to multiple, as DB is already set to allow multiple. --- cardGen.js | 43 +++++++++++++++++++++++++++++++++--- database.js | 17 ++++++++++++++ db/231024_0956_migration.sql | 14 ++++++++++++ public/board.js | 9 +++++--- 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 db/231024_0956_migration.sql diff --git a/cardGen.js b/cardGen.js index 17a8b98..13534e7 100644 --- a/cardGen.js +++ b/cardGen.js @@ -140,6 +140,27 @@ function getCardColourRequirement(){ }); return dPromise; } +function getCardManaColour(){ + const cPromise = new Promise((resolve, reject) => { + database.dbGetCardManaColour().then(data => { + + let manaColours = []; + + data.forEach((cardManaColour) => { + + manaColours.push({ + 'cardId': cardManaColour.cardId, + 'colourId': cardManaColour.colourId, + }); + + }); + resolve(manaColours); + + }) + .catch(err => { throw err; }) + }); + return cPromise; +} //getCardColourRequirement(); // Then effects which will have effects with parent triggers, and unit type checks // colour checks, all sorts. So will be more difficult. Basic (flight, etc) @@ -187,11 +208,12 @@ function requestDeck(itemData = null){ // Change SQL to accept for just the cards passed // Get each cards data, colourReqs, and classes - const [cards, cardClasses, cardColourRequirements] = + const [cards, cardClasses, cardColourRequirements, cardManaColours] = await Promise.all([ getCards(), getCardClasses(), getCardColourRequirement(), + getCardManaColour(), ]); // ^^^^ Classes async? Can pass the cardsIds, then loop classes, if the class cardId // matches the cardId in cards[] then push the class to cards[x].classes @@ -204,7 +226,7 @@ function requestDeck(itemData = null){ const [builtCards] = await Promise.all([ - buildCards(cards, cardClasses, cardColourRequirements), + buildCards(cards, cardClasses, cardColourRequirements, cardManaColours), // TODO: builtEffects ]); @@ -262,6 +284,7 @@ function requestDeck(itemData = null){ let cardColours = {}; // Array of req. let cardAttack = {}; // Base, Current let cardSprite = {}; // Maybe from DB? + let cardManaColour = {}; // The card colour value when played in mana zone // TODO: Set the player. For now will do this in front-end as testing currently @@ -320,6 +343,7 @@ function requestDeck(itemData = null){ cardStatus[itemCount] = null; cardFace[itemCount] = 0; // Face down by default (in deck) cardColours[itemCount] = cardData[itemCount].colourRequirements; // TODO; + cardManaColour[itemCount] = cardData[itemCount].cardManaColour; // From cardData set the base attack, and current (same when deckbuild) let atk = cardData[itemCount].atk; cardAttack[itemCount] = [atk, atk]; // Base, Current @@ -346,6 +370,7 @@ function requestDeck(itemData = null){ itemData.cardColours = cardColours; itemData.cardAttack = cardAttack; itemData.cardSprite = cardSprite; + itemData.cardManaColour = cardManaColour; // item, itemCount, deckData, cardData, boardElement //console.log(cardData); @@ -371,7 +396,7 @@ function requestDeck(itemData = null){ // point to see. For now DB, and generating is ok, as still working on it } -function buildCards(cards, cardClasses, cardColourRequirements){ +function buildCards(cards, cardClasses, cardColourRequirements, cardManaColours){ console.log(cardColourRequirements); const dPromise = new Promise((resolve, reject) => { @@ -388,6 +413,7 @@ function buildCards(cards, cardClasses, cardColourRequirements){ colour: [], cost: card.cardCost, colourRequirements: [], + cardManaColour: [], // Should probably be an object type: card.cardType, atk: card.cardAttack, rarity: card.cardRarity, @@ -436,6 +462,17 @@ function buildCards(cards, cardClasses, cardColourRequirements){ }); + // Add card mana colours (colour value when played in mana zone) + cardManaColours.forEach((manaCol) => { + + // Check the card exists (it should always, but don't want jank) + if(manaCol.cardId in builtCards){ + // Add the colours to the class array (cards can have multiple) + builtCards[manaCol.cardId].cardManaColour.push(manaCol.colourId); + } + + }); + resolve(builtCards); }); diff --git a/database.js b/database.js index 723dba7..8701abd 100644 --- a/database.js +++ b/database.js @@ -107,6 +107,22 @@ function dbGetCardColourRequirement(){ return dPromise; } +function dbGetCardManaColour(){ + // Get the classes assoc. on each card + const cPromise = new Promise((resolve, reject) => { + let sql = `SELECT + cardId + ,colourId + FROM card_mana_colour + `; + + con.query(sql, function (err, result, fields) { + if (err) { throw err; reject(new Error(err)); } + resolve(result); + }); + }); + return cPromise; +} module.exports = { connect, disconnect @@ -116,4 +132,5 @@ module.exports = { , dbGetCards , dbGetCardClasses , dbGetCardColourRequirement + , dbGetCardManaColour }; diff --git a/db/231024_0956_migration.sql b/db/231024_0956_migration.sql new file mode 100644 index 0000000..531e946 --- /dev/null +++ b/db/231024_0956_migration.sql @@ -0,0 +1,14 @@ +USE `realms_divided`; +CREATE TABLE IF NOT EXISTS `card_mana_colour` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `cardId` int(11) DEFAULT NULL, + `colourId` tinyint(4) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +INSERT INTO `card_mana_colour` (`id`, `cardId`, `colourId`) VALUES + (1, 1, 3), + (2, 2, 1), + (3, 3, 2), + (4, 4, 1), + (5, 5, 3); diff --git a/public/board.js b/public/board.js index 45e7210..481976d 100644 --- a/public/board.js +++ b/public/board.js @@ -39,6 +39,7 @@ let deckIn = {}; // NEW, may be used, for now player substitute let deckData = {}; let cardAttack = {}; // TODO: add to the logic let cardColours = {}; +let cardManaColour = {}; let inEvent = null; // To disable drawing each time something changes @@ -213,6 +214,7 @@ class Board{ // If the item is a mana, draw the mana colour within it // Temp solution, but works for UI and testing if(boardElement[itemKey] == 'mana'){ + let manaFill = this.setFillByManaColour(cardManaColour[itemKey][0]); // TODO: Seperate this into a 'drawMana' or something? let manaColour = new Shape({ shape: 'circle', @@ -221,7 +223,7 @@ class Board{ y: positionY + height/2, width: width*.75, height: height*.75, - fillStyle: fill // Fill should be the card's main colour + fillStyle: manaFill // Fill should be the card's main colour }); manaColour.draw(); } @@ -643,7 +645,7 @@ class Board{ continue; } - let colourId = this.tempGetPrimaryManaOfCard(mana); + let colourId = cardManaColour[mana][0]; console.log(JSON.stringify(manaRequired)); // Loop the requirements of the cost to pay @@ -723,7 +725,7 @@ class Board{ continue; } - let colourId = this.tempGetPrimaryManaOfCard(itemKey); + let colourId = cardManaColour[itemKey][0]; manaAvailable[colourId] += 1; } @@ -1044,6 +1046,7 @@ function loadBoard(data) { deckData = data.deckData; cardAttack = data.cardAttack; // TODO: add to the logic cardColours = data.cardColours; + cardManaColour = data.cardManaColour; // TODO: JANK IN, CHANGE CODE TO USE NEW ARRAY!! // Temp jank, set colour to first colour req.