From 3a0dfa82e7b9d6370fe4aed9c376793ca39c5b90 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 18 Oct 2024 09:52:09 +0100 Subject: [PATCH] Trialing card build from DB --- database.js | 98 +++++++++++++++++++++++++++++++++ server.js | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 249 insertions(+), 4 deletions(-) diff --git a/database.js b/database.js index fd02343..f7a2c88 100644 --- a/database.js +++ b/database.js @@ -170,9 +170,107 @@ function dbResultToCards(result){ return cards; } + +// Testing, and trialing rewrites +function dbGetDecks(){ + const dPromise = new Promise((resolve, reject) => { + let cards = []; + let sql = `SELECT + deckId + ,playerId + ,deckName + FROM deck + LIMIT 10 + `; // TODO: Remove limit when happy/this accepts params + + con.query(sql, function (err, result, fields) { + if (err) { throw err; reject(new Error(err)); } + resolve(result); + }); + }); + return dPromise; +} +function dbGetDeckList(){ + const dPromise = new Promise((resolve, reject) => { + let cards = []; + let sql = `SELECT + cardId + ,cardCount + FROM deck_cards + `; + + con.query(sql, function (err, result, fields) { + if (err) { throw err; reject(new Error(err)); } + resolve(result); + }); + }); + return dPromise; +} +function dbGetCards(){ + // Start with basic stuff in card table + const dPromise = new Promise((resolve, reject) => { + let sql = `SELECT + id + ,cardName + ,cardCost + ,cardType + ,cardAttack + ,cardRarity + FROM card + LIMIT 2 + `; + + con.query(sql, function (err, result, fields) { + if (err) { throw err; reject(new Error(err)); } + resolve(result); + }); + }); + return dPromise; +} +function dbGetCardClasses(){ + // Get the classes assoc. on each card + const dPromise = new Promise((resolve, reject) => { + let sql = `SELECT + cardId + ,classId + FROM card_class + `; + + con.query(sql, function (err, result, fields) { + if (err) { throw err; reject(new Error(err)); } + resolve(result); + }); + }); + return dPromise; +} +function dbGetCardColourRequirement(){ + // Get the classes assoc. on each card + const dPromise = new Promise((resolve, reject) => { + let sql = `SELECT + cardId + ,colourId + ,cost + FROM card_colour_requirement + `; + + con.query(sql, function (err, result, fields) { + if (err) { throw err; reject(new Error(err)); } + resolve(result); + }); + }); + return dPromise; +} + + module.exports = { connect, disconnect , getCards , getCardById , getCardsFromDeck + // Testing, and trailing + , dbGetDecks + , dbGetDeckList + , dbGetCards + , dbGetCardClasses + , dbGetCardColourRequirement }; diff --git a/server.js b/server.js index a1d79b4..8ce6104 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,4 @@ + const express = require('express'); const database = require('./database'); const app = express(); @@ -5,6 +6,11 @@ const http = require('http').Server(app); const port = process.env.PORT || 3000; const io = require('socket.io')(http); +// util is what nodejs uses for console.log, but has a depth of 2 set +// so console.logs show [Array]/[Object] instead of useful info. +// This can be overridden console.log(util.inspect(LOGDATA, true, 4, true)) +// 4 being new depth, true (last one) is to show colours +const util = require('util') app.use(express.static(__dirname + '/public')); @@ -33,13 +39,151 @@ for (let roomId = 1; roomId <= numRoomsToPreGen; roomId++) { createRoom(roomId); } + +// For testing DB/makeshift ORM/building my objects +// Maybe if this works, put it into a deck and card module +// Doing similar to I have to other stuff, that will need to be +// migrated serverside anyways. +let item = []; +let deckItem = {}; +let deckList = {}; +let cardItem = {}; +// 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? +function getDecks(deckId, playerId){ + // Await promise, once it's done get the data, if errors send err + database.dbGetDecks().then(data => { + data.forEach((deck) => { + let itemId = item.length; + item.push(itemId); // Add the next available item + // Add the deck info to deckItem + deckItem[itemId] = { + 'deckId': deck.deckId, + 'playerId': deck.playerId, + 'deckName': deck.deckName, + }; + }); + //console.log(item); + //console.log(deckItem); + }) + .catch(err => { throw err; }) +} +getDecks(); +function getDeckList(){ + database.dbGetDeckList().then(data => { + data.forEach((listItem) => { + let itemId = item.length; + item.push(itemId); // Add the next available item + // Add the deck info to deckItem + deckList[itemId] = { + 'cardId': listItem.cardId, + 'cardCount': listItem.cardCount, + }; + }); + //console.log(item); + //console.log(deckList); + }) + .catch(err => { throw err; }) +} +getDeckList(); +function getCards(){ + console.log + database.dbGetCards().then(data => { + data.forEach((card) => { + let itemId = item.length; + item.push(itemId); // Add the next available item + // Add the deck info to deckItem + cardItem[itemId] = { + 'cardId': card.id, + 'cardName': card.cardName, + 'cardCost': card.cardCost, + 'cardType': card.cardType, + 'cardAttack': card.cardAttack, + 'cardRarity': card.cardRarity, + 'cardClass': [], + 'cardColourRequirement': [], + }; + }); + //console.log(item); + //console.log(cardItem); + }) + .catch(err => { throw err; }) +} +getCards(); +function getCardClasses(){ + database.dbGetCardClasses().then(data => { + data.forEach((cardClass) => { + + // Loop existing cardItems, compare cardId + // If there's a match add cardClass to the card? + for(let i = 0; i < item.length; i++){ + // If the item has a cardItem + if(i in cardItem){ + // And the cardId matches the linked table + if(cardItem[i].cardId == cardClass.cardId){ + cardItem[i].cardClass.push(cardClass.classId); + } + } + } + }); + //console.log(item); + //console.log(cardItem); + }) + .catch(err => { throw err; }) +} +getCardClasses(); +function getCardColourRequirement(){ + database.dbGetCardColourRequirement().then(data => { + data.forEach((cardColourReq) => { + + // Loop existing cardItems, compare cardId + // If there's a match add cardClass to the card? + for(let i = 0; i < item.length; i++){ + // If the item has a cardItem (needed to add reqs.) + if(i in cardItem){ + // And the cardId matches the linked table + if(cardItem[i].cardId == cardColourReq.cardId){ + cardItem[i].cardColourRequirement.push([cardColourReq.colourId, cardColourReq.cost]); + } + } + } + }); + //console.log(item); + console.log(util.inspect(cardItem, true, 4, true)); + //console.log(cardItem); + }) + .catch(err => { throw err; }) +} +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) + + +// End For testing + function onConnection(socket){ console.log('+ User connected'); console.log(''); - socket.on('requestGetCards', function() { - requestGetCards(socket); + socket.on('requestGetCards', function(deckId, playerId) { + requestGetCards(socket, deckId, playerId); }); socket.on('requestRooms', function(filter) { @@ -204,12 +348,15 @@ function requestJoinRoom(socket, playerName, roomId){ // Change to Decks, or 'getPlayerDecks' for the game // Could also be a specific deck if player clicks into one? Although maybe when entering // decks they get all their decks (maybe just id actually) then load from there? -function requestGetCards(socket){ +function requestGetCards(socket, deckId, playerId){ + console.log(deckId); + console.log(playerId); let response = {'success':false, 'message':'Nothing happened'}; response.success = true; // Await promise, once it's done get the data, if errors send err - database.getCardsFromDeck(1,1).then(data => { + database.getCardsFromDeck(deckId, playerId).then(data => { + console.log(data); response.message = data; io.to(socket.id).emit('responseGetCards', response); })