for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
/* globals versionCompare, atob, btoa */
'use strict';
angular
.module('game')
.service('savegame', ['$state',
'state',
'data',
function ($state, state, data) {
this.initSave = function () {
state.player = {};
this.versionControl();
state.init();
$state.go('matter');
};
this.save = function () {
localStorage.setItem('player', JSON.stringify(state.player));
}
this.load = function () {
try {
let storedPlayer = localStorage.getItem('player');
if (!storedPlayer) {
this.initSave();
} else {
state.player = JSON.parse(storedPlayer);
} catch (err) {
alert('Error loading savegame, reset forced.');
this.versionControl = function () {
// delete saves older than this version
if (state.player.version && versionCompare(state.player.version, '2.1.0') < 0) {
// we merge the properties of the player with the start player to
// avoid undefined errors with new properties
state.player = angular.merge({}, data.start_player, state.player);
// append an id if it doesn't exist
if (!state.player.id) {
state.player.id = Math.random().toString().substring(3);
]);