diff --git a/cardGen.js b/cardGen.js index 9adaf11..80ff642 100644 --- a/cardGen.js +++ b/cardGen.js @@ -4,22 +4,12 @@ const database = require('./database'); const util = require('util') -// cardClass, cardColourRequirement -// may want to be seperate too (as well as in cardItem), so that -// during match they can be altered by effects while keeping the OG card -// for inspecting (and compare against new stats,reqs,etc.) -// same with attack, cost, etc. things that will can be visually shown as -// changed in game - -// Just grabbing everything from DB for now, as it's quite small at current -// then will rejig when needed. -// Should all cards, effects, classes etc. be loaded in on server start -// then just load decks and decklists when needed? +// Get the decks requested function getDecks(deckIds = false){ // Await promise, once it's done get the data, if errors send err const dPromise = new Promise((resolve, reject) => { - database.dbGetDecks().then(data => { + database.dbGetDecks(deckIds).then(data => { let decks = []; @@ -48,10 +38,10 @@ function getDecks(deckIds = false){ return dPromise; } //getDecks(); -function getDeckList(){ +function getDeckList(deckIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetDeckList().then(data => { + database.dbGetDeckList(deckIds).then(data => { let deckList = []; @@ -70,11 +60,10 @@ function getDeckList(){ }); return dPromise; } -//getDeckList(); -function getCards(){ +function getCards(cardIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetCards().then(data => { + database.dbGetCards(cardIds).then(data => { let cards = []; @@ -97,10 +86,9 @@ function getCards(){ }); return dPromise; } -//getCards(); -function getCardClasses(){ +function getCardClasses(cardIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetCardClasses().then(data => { + database.dbGetCardClasses(cardIds).then(data => { let cardClasses = []; @@ -118,10 +106,9 @@ function getCardClasses(){ }); return dPromise; } -//getCardClasses(); -function getCardColourRequirement(){ +function getCardColourRequirement(cardIds = false){ const dPromise = new Promise((resolve, reject) => { - database.dbGetCardColourRequirement().then(data => { + database.dbGetCardColourRequirement(cardIds).then(data => { let colourRequirements = []; @@ -141,9 +128,9 @@ function getCardColourRequirement(){ }); return dPromise; } -function getCardManaColour(){ +function getCardManaColour(cardIds = false){ const cPromise = new Promise((resolve, reject) => { - database.dbGetCardManaColour().then(data => { + database.dbGetCardManaColour(cardIds).then(data => { let manaColours = []; @@ -274,25 +261,9 @@ function getCardPassive(){ 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) -// shouldn't be too bad -// something like effect_basic, card_effect, effect_trigger, effect_requirement, effect_option -// effect_stage, effect_advanced, with advanced being an id with x triggers, triggers have -// x req, advanced as with x options, x stages to be able to fine-tune fancy stuff -// combining all other effects, units, cards, colours, etc. Will be a lot of though, -// but better than hard coding anything more than basic effects and effect check logic -// TODO: effect (as above) - -// request a deck in the format CURRENTLY used in the game -// decks will likely be changed around // https://www.geeksforgeeks.org/how-to-wait-for-multiple-promises-in-javascript/ // https://medium.com/@nikolozz/using-socket-io-with-async-await-13fa8c2dc9d9 -// using last example -// TODO: When this is functionally working, need to split up into other func/modules -// such as: playerMod, cardMod, deckMod, miscMod ? function requestDeck(itemData = null){ return new Promise((resolve, reject) => { (async () => { @@ -300,53 +271,73 @@ function requestDeck(itemData = null){ // Get the deck(s) requested. // Not 100% on how to do two differening atm // Besides all of playerId, and all of deckId. But 1,2/2,3 for instance + + // Build array of decks to get + let deckIds = []; + for(let i = 0; i < itemData.players.length; i++){ + let deckStuff = itemData.players[i][1].deck; + deckIds.push([deckStuff.playerId, deckStuff.deckId]); + // So should be array of [playerId, deckId] which is primary key in DB + } + + // Get said decks, and their deckLists const [decks, deckList] = await Promise.all([ - getDecks(), - getDeckList() + getDecks(deckIds), + getDeckList(deckIds) ]); - //console.log(decks); - //console.log(deckList); + /* + console.log('--- decks ---'); + console.log(decks); + console.log('= deckLists =') + console.log(deckList); + console.log('------'); + */ + // Now loop the deckList for the cardIds let deckCardIds = []; deckList.forEach((deckItem) => { deckCardIds.push(deckItem.cardId); - }); - //console.log(deckCardIds); // Next, get the cards in the deck by their ID // TODO: https://stackoverflow.com/a/65510676 - // Change SQL to accept for just the cards passed // Get each cards data, colourReqs, and classes const [cards, cardClasses, cardColourRequirements, cardManaColours, cardPassives] = await Promise.all([ - getCards(), - getCardClasses(), - getCardColourRequirement(), - getCardManaColour(), - getCardPassive(), + getCards(deckCardIds), + getCardClasses(deckCardIds), + getCardColourRequirement(deckCardIds), + getCardManaColour(deckCardIds), + getCardPassive(deckCardIds), ]); - // ^^^^ 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 + // Return all effect data from DB + const [effects] = + await Promise.all([ + database.dbGetEffect(deckCardIds), // Get effects + ]); - // TODO: Build the card so far async for it's done alongside getting effects? - // Then when both done add the effects to the existing cardObjects? Yep good idea me - //console.log(cards); - //console.info(cardClasses); - //console.log(cardColourRequirements); + // Loop the effects for their effectIds to then get the steps/triggers from DB + let effectIds = []; + await effects.forEach((effect) => { + effectIds.push(effect.effectId); + }); - // Return all effect data from DB - const [effects, effectSteps, effectTriggers] = + // Then pass the effectIds to get their steps/triggers + const [effectSteps, effectTriggers] = await Promise.all([ - database.dbGetEffect(), - database.dbGetEffectStep(), - database.dbGetEffectTrigger(), + database.dbGetEffectStep(effectIds), + database.dbGetEffectTrigger(effectIds), ]); + /* + console.log('--- Effects ---'); + console.log(effects); + */ + // Build Effects const [cardEffects] = await Promise.all([ @@ -460,6 +451,8 @@ function requestDeck(itemData = null){ // For each new card, loop to the cardCount (how many cards in deck) // and add to the deck + // TODO: Associate player to deck differently (both could be using same deck + // via some kind of 'try a deck' or 'use friends deck' option) for(let i = 0; i < deckListItem.cardCount; i++){ item.push(itemCount); // Add new item to add stuff for // ^ not using item.length incase anything in future gets deleted diff --git a/database.js b/database.js index 8f89c93..ece68f0 100644 --- a/database.js +++ b/database.js @@ -17,7 +17,7 @@ function disconnect(){ } // My DB stuffs -function dbGetDecks(){ +function dbGetDecks(deckIds = false){ const dPromise = new Promise((resolve, reject) => { let cards = []; let sql = `SELECT @@ -25,8 +25,16 @@ function dbGetDecks(){ ,playerId ,deckName FROM deck - LIMIT 10 - `; // TODO: Remove limit when happy/this accepts params + `; + + // TODO: Jank, need to unjank it + if(deckIds){ + for(let i = 0; i < deckIds.length; i++){ + if(i == 0){ sql += ' WHERE '; } + else{ sql += ' OR '; } + sql += '(deckId = '+deckIds[i][1]+' AND playerId = '+deckIds[i][0]+')'; + } + } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -35,7 +43,7 @@ function dbGetDecks(){ }); return dPromise; } -function dbGetDeckList(){ +function dbGetDeckList(deckIds = false){ const dPromise = new Promise((resolve, reject) => { let cards = []; let sql = `SELECT @@ -46,6 +54,15 @@ function dbGetDeckList(){ FROM deck_cards `; + // TODO: Jank, need to unjank it + if(deckIds){ + for(let i = 0; i < deckIds.length; i++){ + if(i == 0){ sql += ' WHERE '; } + else{ sql += ' OR '; } + sql += '(deckId = '+deckIds[i][1]+' AND playerId = '+deckIds[i][0]+')'; + } + } + con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } resolve(result); @@ -53,7 +70,7 @@ function dbGetDeckList(){ }); return dPromise; } -function dbGetCards(){ +function dbGetCards(cardIds = false){ // Start with basic stuff in card table const dPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -65,6 +82,7 @@ function dbGetCards(){ ,cardRarity FROM card `; + if(cardIds){ sql += 'WHERE card.id IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -73,7 +91,7 @@ function dbGetCards(){ }); return dPromise; } -function dbGetCardClasses(){ +function dbGetCardClasses(cardIds = false){ // Get the classes assoc. on each card const dPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -81,6 +99,7 @@ function dbGetCardClasses(){ ,classId FROM card_class `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -89,7 +108,7 @@ function dbGetCardClasses(){ }); return dPromise; } -function dbGetCardColourRequirement(){ +function dbGetCardColourRequirement(cardIds = false){ // Get the classes assoc. on each card const dPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -98,6 +117,7 @@ function dbGetCardColourRequirement(){ ,cost FROM card_colour_requirement `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -107,7 +127,7 @@ function dbGetCardColourRequirement(){ return dPromise; } -function dbGetCardManaColour(){ +function dbGetCardManaColour(cardIds = false){ // Get the classes assoc. on each card const cPromise = new Promise((resolve, reject) => { let sql = `SELECT @@ -115,6 +135,7 @@ function dbGetCardManaColour(){ ,colourId FROM card_mana_colour `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -125,7 +146,7 @@ function dbGetCardManaColour(){ } // Effect stuff -function dbGetEffect(){ +function dbGetEffect(cardIds = false){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT cardId @@ -136,6 +157,7 @@ function dbGetEffect(){ INNER JOIN effect ON effect.id = card_effect.effectId `; + if(cardIds){ sql += 'WHERE cardId IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -144,7 +166,7 @@ function dbGetEffect(){ }); return ePromise; } -function dbGetEffectStep(){ +function dbGetEffectStep(effectIds = false){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT effectId, @@ -163,6 +185,7 @@ function dbGetEffectStep(){ effect_step_target ON effect_step_target.effectStep = effect_step.id `; + if(effectIds){ sql += 'WHERE effectId IN ('+effectIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -172,7 +195,7 @@ function dbGetEffectStep(){ return ePromise; } // Effect Trigger stuff -function dbGetEffectTrigger(){ +function dbGetEffectTrigger(effectIds = false){ const ePromise = new Promise((resolve, reject) => { let sql = `SELECT effect_trigger.id AS triggerId, @@ -196,6 +219,7 @@ function dbGetEffectTrigger(){ effect_trigger_target ON effect_trigger_target.effectTriggerId = effect_trigger.triggerTypeId `; + if(effectIds){ sql += 'WHERE effectId IN ('+effectIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } @@ -206,13 +230,14 @@ function dbGetEffectTrigger(){ } // Passive stuff -function dbGetPassive(){ +function dbGetPassive(cardIds = false){ const pPromise = new Promise((resolve, reject) => { let sql = `SELECT cardId ,passiveId FROM card_passive `; + if(cardIds){ sql += 'WHERE card.id IN ('+cardIds+')'; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } diff --git a/roomMod.js b/roomMod.js index 2b26e17..a305e51 100644 --- a/roomMod.js +++ b/roomMod.js @@ -44,6 +44,7 @@ function roomGeneration(playerCount, teamCount = null, playerTeams = null){ // This will be passed by the joinRoom for each player, then built // on startGame) + // Need to make it work if they're the same deck too let playerData = [ {'playerId': 1, 'deck':{'playerId':1,'deckId':1}}, {'playerId': 2, 'deck':{'playerId':2,'deckId':1}},