Gankro/Jterniabound — pull request #18 by LtSquigs (2012-09-03 03:44:43+00:00)
Title: Adding support to save state to local or session storage

This pull request adds the ability to save the state to the local or session storage of the browser, if supported, using the HTML5 API.

It adds the following functions to the Sburb object

1. saveStateToStorage(local), public function when called saves state to either session or local storage (the local parameter switches between the two)
2. loadStateFromStorage(local), public function when called loads state from either session or local storage
3. deleteStateFromStorage(local), public deletes state from session or local storage
4. isStateInStorage(local), public checks to see if there is a saved state in storage
5. lzw_encode(s), private compresses a string using the lzw algorithm
6. lzw_decode(s), private decodes a string using the lzw algorithm

For those unfamiliar with the HTML5 storage APIs, the difference between local and session storage is that session storage lasts until the user closes the browser whereas local storage never expires. All the functions default to session storage, but they can use local storage if passed the local parameter as true (if no parameter is passed, it is assumed false).

The lzw_encode and lzw_decode methods are used to compress the saved state XML. Most browser have a limit to how much you can throw into local/session storage that seems to hover around 5-10MB (I don't believe its defined in the specs), so in order to conserve as much space as possible I think compressing the string before hand is a good idea. Using the lzw_encode I noticed that the encoded string was as much as 1/6 the length of the unencoded string.

Finally, in order to avoid collisions with other games that might have saved states in storage (like for example if someone played 3 in a row when they all get put out there), I added a paramater defined in Jterniabound.js on the Sburb object that gives the game a name, used to prefix the saved state key.