From 116f7af480b82a417cf8b43cf4c6076682f96cc4 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 24 Oct 2024 22:53:08 +0100 Subject: [PATCH] Add build cardEffect object from DB elements --- cardGen.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ database.js | 3 +- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/cardGen.js b/cardGen.js index 13534e7..6dca172 100644 --- a/cardGen.js +++ b/cardGen.js @@ -1,6 +1,7 @@ // Need to require DB again. Apparently this is ok, as it'll // still only have one require cross project (?) const database = require('./database'); +const util = require('util') // cardClass, cardColourRequirement @@ -161,6 +162,96 @@ function getCardManaColour(){ }); return cPromise; } +// Build card effects +// TODO: Accept card Ids (array) +function buildCardEffects(effects, effectSteps, effectTriggers){ + const cPromise = new Promise((resolve, reject) => { + + let cardEffects = {}; + let effectData = {}; + + effects.forEach((effect) => { + // If card doesn't currently have an effect + if(cardEffects[effect.cardId] === undefined){ + cardEffects[effect.cardId] = []; + } + // Add the effectId to the temp cardId + cardEffects[effect.cardId].push(effect.effectId); + + // Now actually start the effect + effectData[effect.effectId]={}; + + // Add the description to new effect + effectData[effect.effectId]['description'] = effect.description; + effectData[effect.effectId]['step'] = {}; + effectData[effect.effectId]['trigger'] = {}; + }); + //console.log(cardEffects); + //console.log(effectData); + + effectSteps.forEach((step) => { + // If step doesn't exist, add step (id of DB item) + if(effectData[step.effectId]['step'][step.stepId] === undefined){ + effectData[step.effectId]['step'][step.stepId] = {}; + + // Add the step effect details + effectData[step.effectId]['step'][step.stepId] = { + // Order steps occur in, two same = 'simultaneous' + 'stepOrder': step.stepOrder, + // The hardcoded effect that will occur at step + 'basicEffectId': step.basicEffectId, + // How much/many the effect will do/trigger + 'amount': step.amount, + 'target': [], + }; + } + + // At this point there should be data in effectStep + // Add a new 'target' (each step can have many) + effectData[step.effectId]['step'][step.stepId]['target'] + .push({ + 'colourId': step.colourId, + 'typeId': step.typeId, + 'classId': step.classId, + 'passiveId': step.passiveId, + 'itemFromStep': step.itemFromStep, + }); + + }); + //console.log(util.inspect(effectData, true, 4, true)) + + effectTriggers.forEach((trigger) => { + // If trigger doesn't exist, add trigger (id of DB item) + if(effectData[trigger.effectId]['trigger'][trigger.triggerId] === undefined){ + effectData[trigger.effectId]['trigger'][trigger.triggerId] = {}; + + // Add the trigger effect details + effectData[trigger.effectId]['trigger'][trigger.triggerId] = { + 'triggerTypeId': trigger.triggerTypeId, + 'amount': trigger.amount, + 'target': [], + }; + } + + // Add a new 'target' (each trigger can have many) + effectData[trigger.effectId]['trigger'][trigger.triggerId]['target'] + .push({ + 'colourId': trigger.colourId, + 'typeId': trigger.typeId, + 'classId': trigger.classId, + 'passiveId': trigger.passiveId, + }); + }); + //console.log(util.inspect(effectData, true, 5, true)) + + resolve([cardEffects, effectData]); + + }); + 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) @@ -215,6 +306,7 @@ function requestDeck(itemData = null){ 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 @@ -224,6 +316,22 @@ function requestDeck(itemData = null){ //console.info(cardClasses); //console.log(cardColourRequirements); + // Return all effect data from DB + const [effects, effectSteps, effectTriggers] = + await Promise.all([ + database.dbGetEffect(), + database.dbGetEffectStep(), + database.dbGetEffectTrigger(), + ]); + + // Build Effects + const [cardEffects] = + await Promise.all([ + buildCardEffects(effects, effectSteps, effectTriggers), + ]); + //console.log(cardEffects); + + // Build the cardData (maybe do all the components here too) const [builtCards] = await Promise.all([ buildCards(cards, cardClasses, cardColourRequirements, cardManaColours), diff --git a/database.js b/database.js index 7051662..54a99d2 100644 --- a/database.js +++ b/database.js @@ -148,7 +148,7 @@ function dbGetEffectStep(){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT effectId, - effectStep, + effect_step.id AS stepId, stepOrder, basicEffectId, amount, @@ -175,6 +175,7 @@ function dbGetEffectStep(){ function dbGetEffectTrigger(){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT + effect_trigger.id AS triggerId, cardId, effectId, triggerTypeId,