Skip to content

Commit

Permalink
Save implemented
Browse files Browse the repository at this point in the history
Implements the functions Save, Reset save, Export and Import
  • Loading branch information
angarg12 committed Dec 26, 2014
1 parent 64b57cf commit 10741da
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ <h2>Cash multiplier: {{(player.multiplier).toFixed(4)}}</h2>
<h2>Options</h2>
<table id="optionTable">
<tr><td>
<button id="reset" onclick="reset()">Reset Save</button>
<button id="reset" ng-click="reset()">Reset Save</button>
</td></tr>
<tr><td>
<button id="save" onclick="save()">Save</button>
<td>Last save time: <span id="lastSave">None</span></td>
<button id="save" ng-click="save()">Save</button>
<td>Last save time: {{lastSave}}</td>
</td></tr>
<tr><td>
<button id="exportSave" onclick="exportSave()">Export Save</button>
<button id="exportSave" ng-click="exportSave()">Export Save</button>
<td id="exportSaveContents" style="display: none">
Copy this text:
<input id="exportSaveText" type="text" readonly>
</td>
</td></tr>
<tr><td>
<button id="importSave" onclick="importSave()">Import Save</button>
<button id="importSave" ng-click="importSave()">Import Save</button>
</td></tr>
</table>
</div>
Expand Down
56 changes: 55 additions & 1 deletion js/exponential.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('incremental',[])
.controller('IncCtrl',['$scope','$document','$interval', '$sce',function($scope,$document,$interval,$sce) {
$scope.version = 0.3;
$scope.version = 0.4;

var startPlayer = {
cashPerClick:1,
Expand Down Expand Up @@ -52,6 +52,44 @@ angular.module('incremental',[])
}
};

$scope.save = function save() {
localStorage.setItem("playerStored", JSON.stringify($scope.player));
var d = new Date();
$scope.lastSave = d.toLocaleTimeString();
}

$scope.load = function load() {
$scope.player = JSON.parse(localStorage.getItem("playerStored"));
$scope.player.currency = new Decimal($scope.player.currency);
}

$scope.reset = function reset() {
var confirmation = confirm("Are you sure you want to permanently erase your savefile?");
if(confirmation === true){
$scope.player = angular.copy(startPlayer);
localStorage.removeItem("playerStored");
}
}

$scope.exportSave = function exportSave() {
var exportText = btoa(JSON.stringify($scope.player));

document.getElementById("exportSaveContents").style = "display: initial";
document.getElementById("exportSaveText").value = exportText;
document.getElementById("exportSaveText").select();
}

$scope.importSave = function importSave(){
var importText = prompt("Paste the text you were given by the export save dialog here.\n" +
"Warning: this will erase your current save!");
if(importText){
$scope.player = JSON.parse(atob(importText));
$scope.player.currency = new Decimal($scope.player.currency);
versionControl(true);
save();
}
}

function update() {
var updateTime = new Date().getTime();
var timeDiff = (Math.min(1000, Math.max(updateTime - lastUpdate,0))) / 1000;
Expand All @@ -74,10 +112,26 @@ angular.module('incremental',[])
return number.toString();
};

function versionControl(ifImport){
if($scope.player.version < $scope.version ||
typeof $scope.player.version == 'undefined'){
$scope.player.version = $scope.version;
}
};


$document.ready(function(){
if(localStorage.getItem("playerStored") != null){
$scope.load();
}
if(typeof $scope.player === 'undefined'){
$scope.player = angular.copy(startPlayer);
}
if(typeof $scope.lastSave === 'undefined'){
$scope.lastSave = "None";
}
versionControl(false);
$interval(update,80);
$interval($scope.save,60000);
});
}]);

0 comments on commit 10741da

Please sign in to comment.