0

I'm currently building a tool for the card game Hearthstone. If you're familiar with the game it's basically a tool that allows you to add your in game deck to a list so that you can monitor what cards you have left at all times along with the chance of drawing X card etc. Nothing too fancy but since I am a huge novice to the world of web development I'm using it as an exercise to help me learn more.

Anyway on to my problem!

At the moment I have a JSON database that has every card in hearthstone along with all of the different parameters associated with each card such as name, cost, playerClass etc.

I have figured out how to retrieve objects from the database but only via the name, since that's what players will use to search for the card they want to add to their deck.The problem I have at the moment is that the name of the card is a child of the card object which is itself a child of the card set object (basic, classic, naxx, gvg etc)

I would like to get ALL of the card data back when I search for it by name but try as I might, I can't figure out how to talk to a parent object via it's child.

to start with here is the search function from the users input:

 $.getJSON("json/AllSets.json",function(hearthStoneData){
  $('.submit').click(function(){
   var searchValue = $('#name').val();
   var returnValue = getObjects(hearthStoneData, "name", searchValue);
   console.log(hearthStoneData);
   console.log(returnValue);
  });
 });

and here is the request from the database:

function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key].toLowerCase() == val.toLowerCase()) {
            objects.push(obj[i]);
        }
    }
    return objects;
}

And finally here is an example of one of the JSON cards I am trying to talk to.

{
   "id":"EX1_066","name":"Acidic Swamp Ooze",                  
   "type":"Minion",
   "faction":"Alliance",
   "rarity":"Common",
   "cost":2,
   "attack":3,
   "health":2,
   "text":"<b>Battlecry:</b> Destroy your opponent's weapon.",
   "flavor":"Oozes love Flamenco.  Don't ask.",
   "artist":"Chris Rahn",
   "collectible":true,
   "howToGetGold":"Unlocked at Rogue Level 57.",
   "mechanics":["Battlecry"]}

So the output im getting when I console log is something like this:

Object {Basic: Array[210], Classic: Array[387], Credits: Array[17], Curse of Naxxramas: Array[160], Debug: Array[58]…}
Basic: Array[210]
[0 … 99]
6: Object
artist: "Howard Lyon"
collectible: true
cost: 2
faction: "Neutral"
flavor: "This spell is much better than Arcane Implosion."
howToGet: "Unlocked at Level 1."
howToGetGold: "Unlocked at Level 28."
id: "CS2_025"
name: "Arcane Explosion"
playerClass: "Mage"
rarity: "Free"
text: "Deal $1 damage to all enemy minions."
type: "Spell"

As you can see, there are several nested arrays before you actually get to the card. I can sort of visualise in my head what I think needs to be done but I definitely dont feel certain. Also alot of the syntax has been copy/pasted and modified to suit my needs, I'm still a total beginner with this stuff and really have no idea how I would write a fix to this problem myself.

Any help is hugely appreciated.

Thanks

pragmatic84
  • 67
  • 10

1 Answers1

0

I think there's a problem with how the data is stored:

  1. Every object needs to have a unique id
  2. Every Child object needs to return a reference to the parentId. This needs to be stored on insert or creation of the child object.
  3. There needs to be a way to look up any object by id
Kevin Seifert
  • 3,494
  • 1
  • 18
  • 14
  • Thanks for the reply, I had a feeling that was the issue, would you have any suggestions as to how I would go about doing that? – pragmatic84 Feb 24 '15 at 19:05
  • If your database is just JSON and small, you can add these by hand. Otherwise, you'll need to programmatically iterate through every object and assign it a unique id. Then in a second pass, iterate through all the child objects, and assign the parent id to it. Otherwise, just selecting a single object, it won't have any knowlegde of the data around it, by itself. Here's an example of looping over a json object: http://stackoverflow.com/questions/684672/loop-through-javascript-object – Kevin Seifert Feb 24 '15 at 20:54
  • One other thought... the json doesn't really need to be nested either. You can have a single array of objects like var = [o1, o2, o3]. In each oi. you can store two variables: id, parentId. The id could simply be the ordinal position in the array. Then if needed, you can quickly traverse up the tree if needed from any object in the array. If you need to traverse down the tree, then you also need to store childIds = [] – Kevin Seifert Feb 24 '15 at 20:58