[HTML5] Is there any database facility?

edited September 2012 in Client Help
Currently I'm building an array of locations I've visited.  This isn't for mapping or autowalking, just a record of where I've been.  This would be so much easier with access to a database of some sort.

I understand that linking the game to anything outside makes everyone nervous, and to a point quite rightly so.  However, the session log is stored somewhere and that's an example of an off-line storage so I wondered if there's also the facility to create and access a database.

Best Answer

  • Accepted Answer
    Somewhat along the lines of your other question, you should be able to accomplish this using variables and some clever-ish scripting to end up with a "database" that persists so long as you save your system on the server.

    The clever-ish bit is that client variables (the ones that will persist and show up on the variables tab) are technically all strings, which isn't what you're after.  Javascript has some really nifty built-in functions, however, that might work.

    If, for instance, you have a javascript array that looks like this:

    var locations = ["Shallam", "Hashan", "Eleusis"];

    You can use the function "JSON.stringify()" to turn that into a standard javascript string that can be stored in a client variable, like this:

    set_variable("visited_locations", JSON.stringify(locations));

    Then, if you take a look at the variables tab in the client Settings, you should see something very much like what you started with:

    ["Shallam", "Hashan", "Eleusis"]

    To actually use that as a real Javascript array again, you'll want to use the JSON.parse() function:

    var locations_array = JSON.parse(get_variable("visited_locations"));

    Which will let you do stuff like this:

    print(locations_array[1], "green"); // Will print "Hashan" to the output window //

    Since this method will result in client variables (again: those stored as part of your reflexes), when you save your system via the Settings window, it will permanently save the current state of all the variables you've built in this way.

    Another thing to keep in mind for more complex applications is that JSON.stringify/parse will support any valid Javascript entity.  So you could have, for example, an array of Javascript Objects that represents an affliction table:

    var afflictions = [ 
         {name: "paralysis", cure_method: "eat", cure: "bloodroot", priority: 1000},
         {name: "anorexia", cure_method: "apply", cure: "epidermal", priority: 950} // etc.
    ];

    set_variable ("affliction_table", JSON.stringify(afflictions));

    Now, in this particular instance, you're not talking about a table that's going to change very often, so you can probably just code that directly into one of your scripts -- I just meant it as an example of what you CAN do with JSON.stringify/parse.

    Let me know if you have any questions!

    - Cromm

Answers

  • edited September 2012
    Thank you Cromm.

    With the answers to the two questions I think I can at least now code some persistance to my arrray/database.

    One nagging doubt I have is size limits.  I haven't even covered all of western Sapience yet (as defined by HELP GEOGRAPHY) and already my array is 1038 elements in size.

    I'm currently using two arrays, an array of ids of locations visited and an array of names of locations visited.  I doubt the numeric array will ever brake any limits, but I'm beginning to wonder when my location name array is going to break the line length limit of my editor.  I use ActiveState's Komodo.
  • edited September 2012
    I hit the line length limit in the HTML5 settings editor today.

    Line length is approximately 70,250 characters long so maybe 64k is the limit?

    My editor Komodo was unphased (no clue what it's line length limit is but I'm sure it has one) and the import was successful (which should have failed if the line in question was split in two since both half-lines would be malformed) but when the function was run it failed with a syntax error.

    I'll probably be fine with storing simply the room ids, but not the room names.  Currently I use the GMCP Room.Info message to list the name of the known location names next to their respective exits.

    Back to the drawing board.
  • Hmm... while it's certainly possible that you've run into some kind of limit, I've never heard of one, and I've also built some fairly long strings in my scripts....

    One problem with editing your system as you are -- changing the raw export -- is that you have have to be EXTREMELY careful about escaping every single string exactly right.  A stray single quote in the middle of a 75,000 byte string can completely destroy it.

    I would recommend, if there's any way, making absolutely sure your data is good and is being escaped properly.  If possible, I highly, HIGHLY suggest doing all or at least the vast majority of your editing within the Settings window, since the client does a very good job of manipulating imports and exports as expected.

    I'll keep poking around otherwise to see if I can identify any other potential hangups.  Sorry for the hassle!

    - Cromm
  • You wouldn't believe how careful I have to be since I break the code sections of triggers and function into new lines to make them more readable, requiring judicious quoting and quotation marks within quotation marks all need escaping etc etc.  Sometimes I change just one line and then import it to make sure there's no mistake before importing more.  The single biggest reason I use an external editor is for copying and pasting.  When you create a new trigger, for example, you have fill out all the fields no matter how similar they are to the last one you created.  With an external editor I can copy and paste and edit the copy to my liking.  All the triggers I've created for afflictions would have been murder to create with the built-in settings editor.

    That said, I'm fairly certain it's a line length limit since I've not had bother before when it was smaller and after importing the 70k line and checking it in the built-in settings editor it now shows on two lines (they're numbered so it's easy to spot).

    I can provide an export of my settings if that will help and/or my external text file I use for import.

    I've thought about breaking some of my settings up into packages.  Can custom packages be referenced if I post them to a web server someplace?  The three included packages all reside on Iron Realms servers.
  • You are correct -- packages would be a great way of hosting more readable content, but keep in mind that you'll still have to store any persistent variables in your local system (within client settings) so that they can be exported or saved internally to the game server.

    I tested line lengths out to 256,000 characters, so I'm still thinking there might be something else at play here.  The export would definitely help narrow it down -- please shoot it to cromm@ironrealms.com and I'll let you know what I find. 

    Thanks!

    - Cromm
Sign In or Register to comment.