Raspsnir/public_html/browserquest/js/storage.js

1 line
2.3 KiB
JavaScript

define([],function(){var e=Class.extend({init:function(){this.hasLocalStorage()&&localStorage.data?this.data=JSON.parse(localStorage.data):this.resetData()},resetData:function(){this.data={hasAlreadyPlayed:!1,player:{name:"",weapon:"",armor:"",image:""},achievements:{unlocked:[],ratCount:0,skeletonCount:0,totalKills:0,totalDmg:0,totalRevives:0}}},hasLocalStorage:function(){return Modernizr.localstorage},save:function(){this.hasLocalStorage()&&(localStorage.data=JSON.stringify(this.data))},clear:function(){this.hasLocalStorage()&&(localStorage.data="",this.resetData())},hasAlreadyPlayed:function(){return this.data.hasAlreadyPlayed},initPlayer:function(e){this.data.hasAlreadyPlayed=!0,this.setPlayerName(e)},setPlayerName:function(e){this.data.player.name=e,this.save()},setPlayerImage:function(e){this.data.player.image=e,this.save()},setPlayerArmor:function(e){this.data.player.armor=e,this.save()},setPlayerWeapon:function(e){this.data.player.weapon=e,this.save()},savePlayer:function(e,t,n){this.setPlayerImage(e),this.setPlayerArmor(t),this.setPlayerWeapon(n)},hasUnlockedAchievement:function(e){return _.include(this.data.achievements.unlocked,e)},unlockAchievement:function(e){return this.hasUnlockedAchievement(e)?!1:(this.data.achievements.unlocked.push(e),this.save(),!0)},getAchievementCount:function(){return _.size(this.data.achievements.unlocked)},getRatCount:function(){return this.data.achievements.ratCount},incrementRatCount:function(){this.data.achievements.ratCount<10&&(this.data.achievements.ratCount++,this.save())},getSkeletonCount:function(){return this.data.achievements.skeletonCount},incrementSkeletonCount:function(){this.data.achievements.skeletonCount<10&&(this.data.achievements.skeletonCount++,this.save())},getTotalDamageTaken:function(){return this.data.achievements.totalDmg},addDamage:function(e){this.data.achievements.totalDmg<5e3&&(this.data.achievements.totalDmg+=e,this.save())},getTotalKills:function(){return this.data.achievements.totalKills},incrementTotalKills:function(){this.data.achievements.totalKills<50&&(this.data.achievements.totalKills++,this.save())},getTotalRevives:function(){return this.data.achievements.totalRevives},incrementRevives:function(){this.data.achievements.totalRevives<5&&(this.data.achievements.totalRevives++,this.save())}});return e});