Best way to store Data

Discussion in 'Server and Client Scripting' started by Blastcore, Jul 26, 2011.

  1. Super SkarmPiss

    Super SkarmPiss Member

    Joined:
    Jul 27, 2011
    Messages:
    36
    Likes Received:
    0
    He means to split the string up into an array. Also known as a multidimensional array. You would access it like this:

    Code (text):
    1.  
    2. // genesect -- call second field (evolevel/evopoke)
    3. pokemonInfo[649][1]
    4.  
    Something like this (can be done better; it's mostly just an example):

    Code (text):
    1.  
    2. serverStartUp : function()
    3. {
    4.      var pokeStats = []; // multidimensional array to hold stats
    5.      
    6.      var pokeStatsTempFile = getFileContent("/db/pokes/poke_stats.txt");
    7.      var pokeStatsTempArr = pokeStatsFile.split("\n");
    8.      
    9.      var len = pokeStatsTempArr.length; // not required, but storing the length as an variable will make it faster
    10.      
    11.      for(var i = 0; i < len; i++)
    12.      {
    13.           pokeStats.push(pokeStatsTempArr.split(" "));
    14.      }
    15.      
    16.      pokeStatsTempFile = null;
    17.      pokeStatsTempArr = null;
    18. }
    19.  
    To access array element:

    Code (text):
    1.  
    2. function findStat(pokeNum, formId, statId)
    3. {
    4.      // returns stat of a pokemon
    5.      // for statId, 1 = atk, 2 = def, 3 = splatk, 4 = spldef, 5 = spd
    6.      var len = pokeStats.length;
    7.      
    8.      for(var i = 0; i < len; i++)
    9.      {
    10.           if(pokeStats[i][0] == pokeNum + ":" + formId)
    11.           {
    12.                return pokeStats[i][statId];
    13.           }
    14.      }
    15.      
    16.      return 0;
    17. }
    18.  
    19. // gets atk stat of arceus
    20. var atk = findStat(493, 0, 1);
    21.  
    22. // gets splatk stat of sky shaymin
    23. atk = findStat(492, 1, 3);
    24.  
     
  2. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    Oh gawd! Not again! I've had problems with multidimensional arrays! :suicides: lol
     
  3. Super SkarmPiss

    Super SkarmPiss Member

    Joined:
    Jul 27, 2011
    Messages:
    36
    Likes Received:
    0
    They're not hard at all. All you need to know is that a multidimensional array in an element within an array. Kinda gets complicated with 3+ elements though.

    You can make some global constants to make it easier, though... (well, other than the fact that js doesn't have constants, but that's trivial and won't matter)

    Code (text):
    1.  
    2. var STAT_ID_ATK = 1;
    3. var STAT_ID_DEF = 2;
    4. var STAT_ID_SPLATK = 3;
    5. var STAT_ID_SPLDEF = 4;
    6. var STAT_ID_SPD = 5;
    7.  
    8. // arceus - atk stat
    9. sys.sendAll(findStat(493, 0, STAT_ID_ATK));
    10.  
     
  4. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    In my case, would be hard since i do it in this way. Lol:

    PokeNum|EvoNum/EvoLevel|Ability1/2/3|DWAbility1/2?|Other usefull data here...

    So that code what exactly does is split all the file (newline) and sets everything in a multidimensional array. Then It splits everything that's spaced and put them somewhere that i'm confused and if i talk more i suicide of confusion, right?
     
  5. Super SkarmPiss

    Super SkarmPiss Member

    Joined:
    Jul 27, 2011
    Messages:
    36
    Likes Received:
    0
    Just split them with "|".
     
  6. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    Okay, got stuck. Or that seems.


    PHP:
    1.  
    2. loadPokemons : function() {
    3.     var temporaryFile = sys.getFileContent("RPGData/Pokemons.txt");
    4.     var temporaryArray = temporaryFile.split("\n");
    5.     var length = temporaryArray.length;
    6.     for(var A = 0; A < length; A++) {
    7.         PokemonData.push(
    8.     }
    9.     temporaryFile = null;
    10.     temporaryArray = null;
    11. }
    12.  
    I've always had problem with multidimensional arrays, haha.

    Whatever, i got stuck on the push part. Okay in tmporaryFile we LIKE "copy" until a \n (Newline) then we're supposed to push everything thats splitted with "|" but wouldn't that make this:

    1st element: 001
    2nd element: 002/16
    3rd element: blablabla

    Element of array. o.O

    This is what i exactly have:

    Code (text):
    1.  
    2. 494|---/--|lot of stuff with | and / missing here!
    3. 495|496/17|lot of stuff with | and / missing here!
    4. 496|497/36|lot of stuff with | and / missing here!
    5. 497|---/--|lot of stuff with | and / missing here!
    6. 498|499/17|lot of stuff with | and / missing here!
    7. 499|500/36|lot of stuff with | and / missing here!
    8. 500|---/--|lot of stuff with | and / missing here!
    9. 501|502/17|lot of stuff with | and / missing here!
    10. 502|503/36|lot of stuff with | and / missing here!
    11. 503|---/--|lot of stuff with | and / missing here!
    12.  

    Told you, multidimensional arrays are my enemies ;_;

    PS: new avatar <3
     
    Last edited: Jul 28, 2011
  7. Super SkarmPiss

    Super SkarmPiss Member

    Joined:
    Jul 27, 2011
    Messages:
    36
    Likes Received:
    0
    Code (text):
    1.  
    2. loadPokemons : function() {
    3.     var temporaryFile = sys.getFileContent("RPGData/Pokemons.txt");
    4.     var temporaryArray = temporaryFile.split("\n");
    5.     var length = temporaryArray.length;
    6.     for(var A = 0; A < length; A++) {
    7.         PokemonData.push("|");
    8.     }
    9.     temporaryFile = null;
    10.     temporaryArray = null;
    11. }
    12.  
    Splitting it with "|" will make a new element. This new element will hold another array, which will look like this:

    PokemonData[][]
     
  8. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    Then i'll do:

    PokemonData[][][][][][][][][] or what? I meant, for every element.

    PokemonData[495][496/17][blablabla][blablabla]?


    EDIT: Gah, i got confused again. :confused:

    Well, how i said i have this:

    Code (text):
    1.  
    2. 494|---/--|
    3. 495|496/17|
    4. 496|497/36|
    5. 497|---/--|
    6. 498|499/17|
    7. 499|500/36|
    8. 500|---/--|
    9. 501|502/17|
    10. 502|503/36|
    11. 503|---/--|
    12.  
    Well, then the following code:

    PHP:
    1.  
    2. loadPokemons : function() {
    3.     var temporaryFile = sys.getFileContent("RPGData/Pokemons.txt");
    4.     var temporaryArray = temporaryFile.split("\n");
    5.     var length = temporaryArray.length;
    6.     for(var A = 0; A < length; A++) {
    7.         PokemonData.push(
    8.     }
    9.     temporaryFile = null;
    10.     temporaryArray = null;
    11. }  
    12.  
    Yes, stuck on the push due to splitting issues.

    Okay, we know that temporaryArray splits all the data. I meant, when it finds a new line, it splits. Leaving it like this:

    Code (text):
    1.  
    2. 494|---/--| > Split 1
    3. 495|496/17| > Split 2
    4. 496|497/36| > Split 3
    5. 497|---/--| > Split 4
    6. 498|499/17| > Split 5
    7. 499|500/36| > Split 6
    8. 500|---/--| > Split 7
    9. 501|502/17| > Split 8
    10. 502|503/36| > Split 9
    11. 503|---/--| > Split 10
    12.  
    There all goes fine. But then, we have to split every | to leave it like this.

    Code (text):
    1.  
    2. 494 < Array Element 1 ---/-- < 494 Element 1 --/--/-- < Element 2
    3.  
    Something like that should be done like:

    PokemonData[494][---/--][--/--/--][--/--][][][] (For other data...)

    So basically setting data for every dimension. Like this:

    PokemonData[PokeNum][EvoPoke/EvoLevel][Ability1/2/3][DWAbility1/DWAbility2][][][]

    Isn't like that?

    Then to call it, how? How do i get the Pokemon Ability/1/2/3 or DWAbility?
     
    Last edited: Jul 28, 2011
  9. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    Huh, i've been playing a little with multidimensional arrays. They're basically array of arrays.

    In my case, they will be array of array of array of array... blabla.

    I've figured out that first i need an array, i've done this:

    PHP:
    1.  
    2. ({
    3. afterNewMessage : function(msg) {
    4.     if(msg == "Script Check: OK") {
    5.         PokemonData = new Array(1); // Creating an Array with 649 elements. Each element, is one Pokemon.
    6.         this.loadPokemons();
    7.     }
    8. }
    9. ,
    10. loadPokemons : function() {
    11.     var temporaryFile = sys.getFileContent("RPGData/Pokemons.txt");
    12.     var temporaryArray = temporaryFile.split("\n");
    13.     var temporarySplit = temporaryArray[0].split("|");
    14.     for(var i = 0; i < PokemonData.length; ++i) {
    15.         PokemonData[i] = new Array(3);
    16.  
    17.         /*
    18.         sys.sendAll(temporarySplit[0]); // Debuging purposes, if fine should return the pokenum data.
    19.         sys.sendAll(temporarySplit[1]); // Debuging purposes, if fine should return the evolution data.
    20.         */
    21.  
    22.         PokemonData[i][0] = temporarySplit[0]; // Pokenum
    23.         PokemonData[i][1] = temporarySplit[1]; // PokeEvo/EvoLevel
    24.  
    25.         sys.sendAll(PokemonData[i][0]);
    26.         sys.sendAll(PokemonData[i][1]);
    27.     }
    28.     temporaryFile = null;
    29.     temporaryArray = null;
    30. }
    31.  
    That code works, PERFECTLY. Basically, i first need to split every | that is find in the first line. Then create another dimension, which is done by doing PokemonData = new Array(1) (In this case 1 just i'm storing PokeNum and PokeEvo/EvoLevel), then just setting the data for each dimension.

    With that i'm done, it sets all the data for every Pokemon in the Array.

    (Will it load fast... I meant, 649 elements :/)

    If there's any way to optimize the code, would be cool if someone helps me with that. Hehe.
     
  10. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    You don't really care about that, once it's loaded in memory.
     
  11. Blastcore

    Blastcore Developer

    Joined:
    Jul 19, 2010
    Messages:
    763
    Likes Received:
    1
    Heh, also what about RPG Locations (I meant, cities, routes, caves, etc) Which are the double, triple or even cuadruple than Pokemon Data.

    And Walks? Maybe x5 bigger than Pokemon Data.

    :/
     
  12. TheUnknownOne

    TheUnknownOne Member

    Joined:
    Mar 28, 2011
    Messages:
    988
    Likes Received:
    3
    Fast processors work.

    Try removing some of the tiers(I hope your not making a battle+rpg server), it makes loading faster.
     
    Last edited: Aug 1, 2011