From 16e27ec04c2576a734d8b92135b897ee1b14829b Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sun, 17 Apr 2016 13:45:43 -0600 Subject: [PATCH] Added untested Save, Load, Erase functions for handling game data. --- Oversimplified.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Oversimplified.js b/Oversimplified.js index 54fcbe3..83c1466 100644 --- a/Oversimplified.js +++ b/Oversimplified.js @@ -1361,6 +1361,57 @@ Oversimplified.Copy = function (object, newID, newName) { return resultingCopy; } +Oversimplified.Save = function (location, data) { + // Set and overwrite data at specified location in browser's Local Storage + if(typeof(Storage) !== "undefined") { + localStorage.setItem(location, data); + if (localStorage.getItem(location) == data) { + if (Oversimplified.DEBUG.showMessages) console.log("Successfully saved " + data + " to localStorage[\"" + location + "\"]."); + return true; + } else { + if (Oversimplified.DEBUG.showMessages) console.log("Could not save " + data + " to localStorage[\"" + location + "\"]."); + } + } else { + if (Oversimplified.DEBUG.showMessages) console.log("This browser does not support saving to localStorage."); + } + return false; +} + +Oversimplified.Load = function (location) { + // Load data from specified location in browser's Local Storage + if(typeof(Storage) !== "undefined") { + if (localStorage.getItem(location)) { + if (Oversimplified.DEBUG.showMessages) console.log("Successfully saved " + data + " to localStorage[\"" + location + "\"]."); + return localStorage.getItem(location); + } else { + if (Oversimplified.DEBUG.showMessages) console.log("No data saved in localStorage[\"" + location + "\"]."); + } + } else { + if (Oversimplified.DEBUG.showMessages) console.log("This browser does not support loading from localStorage."); + } + return false; +} + +Oversimplified.Erase = function (location) { + // Remove data at specified location in browser's Local Storage + if(typeof(Storage) !== "undefined") { + if (localStorage.getItem(location)) { + localStorage.removeItem(location); + if (!localStorage.getItem(location)) { + if (Oversimplified.DEBUG.showMessages) console.log("Successfully erased localStorage[\"" + location + "\"]."); + return true; + } else { + if (Oversimplified.DEBUG.showMessages) console.log("Could not erase localStorage[\"" + location + "\"]."); + } + } else { + if (Oversimplified.DEBUG.showMessages) console.log("There is no data to remove from localStorage[\"" + location + "\"]."); + } + } else { + if (Oversimplified.DEBUG.showMessages) console.log("This browser does not support manipulating localStorage."); + } + return false; +} + // DEBUG object Oversimplified.DEBUG = { // Show console.log messages.