mirror of
https://bitbucket.org/grumdrig/pq-web.git
synced 2026-09-29 08:44:00 -06:00
Way too much effort improving no localStorage version
This commit is contained in:
@@ -146,48 +146,50 @@ function GenerateName() {
|
|||||||
return result.charAt(0).toUpperCase() + result.slice(1);
|
return result.charAt(0).toUpperCase() + result.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
var cookieStorage = {
|
function CookieStorage() {
|
||||||
getItem: function(key) {
|
this.getItem = function(key) {
|
||||||
try {
|
try {
|
||||||
var sheet = JSON.parse(unescape(document.cookie));
|
var result;
|
||||||
var result = {};
|
$.each(document.cookie.split(";"), function (i,cook) {
|
||||||
result[sheet.name] = sheet;
|
if (cook.split("=")[0] === key)
|
||||||
|
result = unescape(cook.split("=")[1]);
|
||||||
|
});
|
||||||
return result;
|
return result;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Probably a JSON parse error
|
// Probably a JSON parse error
|
||||||
return {}
|
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
setItem: function (key, value) {
|
this.setItem = function (key, value) {
|
||||||
document.cookie = escape(JSON.stringify(value[something]));
|
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;
|
var storage = window.localStorage;
|
||||||
|
if (!storage)
|
||||||
|
storage = new CookieStorage();
|
||||||
|
|
||||||
function loadRoster() {
|
function loadRoster() {
|
||||||
if (storage) {
|
if (storage) {
|
||||||
var r = storage.getItem("roster");
|
var r = storage.getItem("roster");
|
||||||
if (!r) return {};
|
if (r) {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(r);
|
return JSON.parse(r);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {};
|
// aight
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadSheet(name) {
|
function loadSheet(name) {
|
||||||
if (storage) {
|
return loadRoster()[name];
|
||||||
return loadRoster()[name];
|
|
||||||
} else try {
|
|
||||||
return JSON.parse(unescape(document.cookie));
|
|
||||||
} catch (err) {
|
|
||||||
alert("Cookies must be enabled");
|
|
||||||
window.location = "roster.html";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeRoster(roster) {
|
function storeRoster(roster) {
|
||||||
@@ -200,8 +202,6 @@ function addToRoster(newguy) {
|
|||||||
var r = loadRoster();
|
var r = loadRoster();
|
||||||
r[newguy.name] = newguy;
|
r[newguy.name] = newguy;
|
||||||
storeRoster(r);
|
storeRoster(r);
|
||||||
} else {
|
|
||||||
document.cookie = escape(JSON.stringify(newguy));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -978,6 +978,18 @@ function LoadCharacter() {
|
|||||||
var name = window.location.href.split('#')[1];
|
var name = window.location.href.split('#')[1];
|
||||||
var sheet = loadSheet(name);
|
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) {
|
if (!sheet.Traits) {
|
||||||
// New game
|
// New game
|
||||||
InitializeCharacter(sheet);
|
InitializeCharacter(sheet);
|
||||||
@@ -1101,7 +1113,12 @@ function LoadGame(sheet) {
|
|||||||
StartTimer();
|
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();
|
StopTimer();
|
||||||
SaveGame();
|
SaveGame();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,12 +6,18 @@ $(document).ready(function () {
|
|||||||
var roster = $("#roster");
|
var roster = $("#roster");
|
||||||
roster.empty();
|
roster.empty();
|
||||||
|
|
||||||
if (!storage) {
|
if (!window.localStorage) {
|
||||||
roster.html("<b>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.");
|
roster.html("<b>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;
|
return;
|
||||||
}
|
}
|
||||||
games = loadRoster() || {};
|
|
||||||
|
games = loadRoster();
|
||||||
$.each(games, function (key, c) {
|
$.each(games, function (key, c) {
|
||||||
|
if (!c.Traits) {
|
||||||
|
delete games[key];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var name = c.Traits.Name;
|
var name = c.Traits.Name;
|
||||||
|
|
||||||
var br = brag(c);
|
var br = brag(c);
|
||||||
|
|||||||
Reference in New Issue
Block a user