diff --git a/config.js b/config.js index da70249..44ebba7 100644 --- a/config.js +++ b/config.js @@ -146,48 +146,50 @@ function GenerateName() { return result.charAt(0).toUpperCase() + result.slice(1); } -var cookieStorage = { - getItem: function(key) { +function CookieStorage() { + this.getItem = function(key) { try { - var sheet = JSON.parse(unescape(document.cookie)); - var result = {}; - result[sheet.name] = sheet; + var result; + $.each(document.cookie.split(";"), function (i,cook) { + if (cook.split("=")[0] === key) + result = unescape(cook.split("=")[1]); + }); return result; } catch (err) { // Probably a JSON parse error - return {} } - }, + }; - setItem: function (key, value) { - document.cookie = escape(JSON.stringify(value[something])); + this.setItem = function (key, value) { + document.cookie = key + "=" + escape(value); + }; + + this.removeItem = function (key) { + document.cookie = key + "=; expires=Thu, 01-Jan-70 00:00:01 GMT;"; } }; var storage = window.localStorage; +if (!storage) + storage = new CookieStorage(); function loadRoster() { if (storage) { var r = storage.getItem("roster"); - if (!r) return {}; - try { - return JSON.parse(r); - } catch (err) { - return {}; + if (r) { + try { + return JSON.parse(r); + } catch (err) { + // aight + } } } + return {}; } function loadSheet(name) { - if (storage) { - return loadRoster()[name]; - } else try { - return JSON.parse(unescape(document.cookie)); - } catch (err) { - alert("Cookies must be enabled"); - window.location = "roster.html"; - } + return loadRoster()[name]; } function storeRoster(roster) { @@ -200,8 +202,6 @@ function addToRoster(newguy) { var r = loadRoster(); r[newguy.name] = newguy; storeRoster(r); - } else { - document.cookie = escape(JSON.stringify(newguy)); } } diff --git a/main.js b/main.js index 61a4865..4448fdc 100644 --- a/main.js +++ b/main.js @@ -978,6 +978,18 @@ function LoadCharacter() { var name = window.location.href.split('#')[1]; var sheet = loadSheet(name); + if (!sheet) { + alert("Error loading " + name); + window.location = "roster.html"; + return; + } + + if (true || !window.localStorage) { + // Cookies can't hold a whole game save + storage.removeItem("roster"); + storage = null; + } + if (!sheet.Traits) { // New game InitializeCharacter(sheet); @@ -1101,7 +1113,12 @@ function LoadGame(sheet) { StartTimer(); } -$(window).unload(function () { +$(document).bind('beforeunload', function () { + if (!storage) + return "Are you sure you want to quit? All your progress will be lost!"; +}); + +$(window).unload(function (event) { StopTimer(); SaveGame(); }); diff --git a/roster.js b/roster.js index ac06a77..49d3dfb 100644 --- a/roster.js +++ b/roster.js @@ -6,12 +6,18 @@ $(document).ready(function () { var roster = $("#roster"); roster.empty(); - if (!storage) { + if (!window.localStorage) { roster.html("This browser does not support local storage. You may still play fast and loose: the game will last only as long as the page stays open in your browser."); return; } - games = loadRoster() || {}; + + games = loadRoster(); $.each(games, function (key, c) { + if (!c.Traits) { + delete games[key]; + return; + } + var name = c.Traits.Name; var br = brag(c);