Storage - lzbk/BrowserQuest GitHub Wiki
Source file: client/js/storage.js
Class to handle local storage on the client side.
Properties
data--[object]An object containing all locally stored data. Keys ondatainclude:hasAlreadyPlayed--[boolean]A boolean set to true or false depending on if this is the user's first time playingplayer--[object]An object representing the player with the following keys:name--[string]The username given by the userweapon--[string]Name of the weapon the user is currently usingarmor--[string]Name of the armor the user is currently usingimage--[string]Name of the image to use as the character's sprite on screen
achievements--[object]An object containing information about the player's achievementsunlocked--[array]An array of unlocked achievement IDsratCount--[number]An accumulator of the number of rats the player has killedratCount--[number]An accumulator of the number of skeletons the player has killedtotalKills--[number]An accumulator of the total kills of the playertotalDmg--[number]An accumulator of the total damage the player has takentotalRevives--[number]An accumulator of the number of times the player has revived
Methods
init()resetData()hasLocalStorage()save()clear()hasAlreadyPlayed()initPlayer(name)setPlayerName(name)setPlayerImage(img)setPlayerArmor(armor)setPlayerWeapon(weapon)savePlayer(img, armor, weapon)hasUnlockedAchievement(id)unlockAchievement(id)getAchievementCount()getRatCount()incrementRatCount()getSkeletonCount()incrementSkeletonCount()getTotalDamageTaken()addDamage(damage)getTotalKills()incrementTotalKills()getTotalRevives()incrementRevives()
init()
If the user's browser supports HTML5 localStorage, it attempts to load any saved data into data. Otherwise, data gets set by a call to resetData().
resetData()
Sets hasPlayedAlready to false, sets all fields in data.player to empty strings, sets all accumulators in data.achievements to 0 and an empty array for data.achievements.unlocked.
hasLocalStorage()
Returns true if user's browser supports HTML5 local storage. Inquires through Modernizr.localstorage.
save()
If this.hasLocalStorage(), the current value of data will be stored in local storage.
clear()
If this.hasLocalStorage(), it will be set to "" and resetData() is called.
hasAlreadyPlayed()
Returns hasAlreadyPlayed data property.
hasUnlockedAchievement(id)
Returns true if id is in data.achievements.unlocked array.
unlockAchievement(id)
If this.hasUnlockedAchievement(id) returns false, push id to this.data.achievements.unlocked.