Add build cardEffect object from DB elements

develop
Nathan Steel 1 year ago
parent e796bed764
commit 116f7af480

@ -1,6 +1,7 @@
// Need to require DB again. Apparently this is ok, as it'll // Need to require DB again. Apparently this is ok, as it'll
// still only have one require cross project (?) // still only have one require cross project (?)
const database = require('./database'); const database = require('./database');
const util = require('util')
// cardClass, cardColourRequirement // cardClass, cardColourRequirement
@ -161,6 +162,96 @@ function getCardManaColour(){
}); });
return cPromise; 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(); //getCardColourRequirement();
// Then effects which will have effects with parent triggers, and unit type checks // 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) // colour checks, all sorts. So will be more difficult. Basic (flight, etc)
@ -215,6 +306,7 @@ function requestDeck(itemData = null){
getCardColourRequirement(), getCardColourRequirement(),
getCardManaColour(), getCardManaColour(),
]); ]);
// ^^^^ Classes async? Can pass the cardsIds, then loop classes, if the class cardId // ^^^^ 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 // 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.info(cardClasses);
//console.log(cardColourRequirements); //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] = const [builtCards] =
await Promise.all([ await Promise.all([
buildCards(cards, cardClasses, cardColourRequirements, cardManaColours), buildCards(cards, cardClasses, cardColourRequirements, cardManaColours),

@ -148,7 +148,7 @@ function dbGetEffectStep(){
const ePromise = new Promise((resolve, reject) => { const ePromise = new Promise((resolve, reject) => {
let sql = `SELECT let sql = `SELECT
effectId, effectId,
effectStep, effect_step.id AS stepId,
stepOrder, stepOrder,
basicEffectId, basicEffectId,
amount, amount,
@ -175,6 +175,7 @@ function dbGetEffectStep(){
function dbGetEffectTrigger(){ function dbGetEffectTrigger(){
const ePromise = new Promise((resolve, reject) => { const ePromise = new Promise((resolve, reject) => {
let sql = `SELECT let sql = `SELECT
effect_trigger.id AS triggerId,
cardId, cardId,
effectId, effectId,
triggerTypeId, triggerTypeId,

Loading…
Cancel
Save