Now that I have created most of the levels for the game, I needed to put together a level select screen. I needed it to also show information on whether or not you've previously completed the level along with any relevant statistics. But, I also don't want to have a 50 column spreadsheet, can't have too many statistics!
I actually had a little fun working on building the file that populates this dialog screen, in so much as working with json files and for loops is fun.
Individual levels in the game are saved as plain text JSON files. The format is terrifyingly straightforward: title, intro message, snakes[], gems[], you get the idea. When time came to put together a master record of levels, I created a small function buildLevelIndex() that loops through all possible levels. (This was easy enough, as the level names are 0.1.json, 0.2.json, ...) After retrieving each file via an AJAX call, I parsed out the title, difficulty, etc. and saved it into an overall list, that tracks all of the game's levels. levels.json, or something like that.
When the user requests the level select screen, a single AJAX call retrieves that master list, and then uses it to populate the dialog itself. Each level has a "record key," which is used by the population routine to query localStorage for previous completion data on that level. If the record data exists, then those columns are successfully datafied.
Each table column across each row also contains a simple rel attribute for sorting purposes. Consider the difficulty column; the text string is converted to a simple integer by a reverse lookup. The user chooses to sort via the dropdown, the dialog removes all table rows, sorts them using a custom .sort() function that compares the appropriate column's rel attribute, and then the dialog re-parents the rows in the properly sorted order.
With that job out of the way, I'm moving on to less complicated dialogs like "how to play," also known as the dialogs that no one other than the search engine spiders will ever read!