2017-05-05 03:08:44 +02:00
|
|
|
/* SaveObject.js
|
|
|
|
* Defines the object used to save/load games
|
|
|
|
*/
|
|
|
|
var saveObject = new BitburnerSaveObject();
|
|
|
|
|
|
|
|
function BitburnerSaveObject() {
|
|
|
|
this.PlayerSave = "";
|
|
|
|
this.AllServersSave = "";
|
|
|
|
this.CompaniesSave = "";
|
|
|
|
this.FactionsSave = "";
|
|
|
|
this.SpecialServerIpsSave = "";
|
|
|
|
this.AugmentationsSave = "";
|
2017-05-23 17:12:09 +02:00
|
|
|
this.AliasesSave = "";
|
2017-06-02 06:15:45 +02:00
|
|
|
this.MessagesSave = "";
|
2017-06-07 02:04:18 +02:00
|
|
|
this.VersionSave = "";
|
2017-05-05 03:08:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BitburnerSaveObject.prototype.saveGame = function() {
|
|
|
|
this.PlayerSave = JSON.stringify(Player);
|
|
|
|
this.AllServersSave = JSON.stringify(AllServers);
|
|
|
|
this.CompaniesSave = JSON.stringify(Companies);
|
|
|
|
this.FactionsSave = JSON.stringify(Factions);
|
|
|
|
this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps);
|
|
|
|
this.AugmentationsSave = JSON.stringify(Augmentations);
|
2017-05-23 17:12:09 +02:00
|
|
|
this.AliasesSave = JSON.stringify(Aliases);
|
2017-06-02 06:15:45 +02:00
|
|
|
this.MessagesSave = JSON.stringify(Messages);
|
2017-06-07 02:04:18 +02:00
|
|
|
this.VersionSave = JSON.stringify(CONSTANTS.Version);
|
2017-05-05 03:08:44 +02:00
|
|
|
|
2017-05-05 06:54:40 +02:00
|
|
|
var saveString = btoa(unescape(encodeURIComponent(JSON.stringify(this))));
|
2017-05-05 03:08:44 +02:00
|
|
|
window.localStorage.setItem("bitburnerSave", saveString);
|
2017-05-15 18:54:23 +02:00
|
|
|
|
2017-05-15 19:23:20 +02:00
|
|
|
console.log("Game saved!");
|
2017-05-15 18:54:23 +02:00
|
|
|
Engine.createStatusText("Game saved!");
|
2017-05-05 03:08:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 06:57:40 +02:00
|
|
|
loadGame = function(saveObj) {
|
|
|
|
if (!window.localStorage.getItem("bitburnerSave")) {
|
|
|
|
console.log("No save file to load");
|
|
|
|
return false;
|
2017-05-05 03:08:44 +02:00
|
|
|
}
|
2017-06-08 06:57:40 +02:00
|
|
|
var saveString = decodeURIComponent(escape(atob(window.localStorage.getItem("bitburnerSave"))));
|
|
|
|
saveObj = JSON.parse(saveString, Reviver);
|
2017-05-05 03:08:44 +02:00
|
|
|
|
|
|
|
Player = JSON.parse(saveObj.PlayerSave, Reviver);
|
|
|
|
AllServers = JSON.parse(saveObj.AllServersSave, Reviver);
|
|
|
|
Companies = JSON.parse(saveObj.CompaniesSave, Reviver);
|
|
|
|
Factions = JSON.parse(saveObj.FactionsSave, Reviver);
|
|
|
|
SpecialServerIps = JSON.parse(saveObj.SpecialServerIpsSave, Reviver);
|
|
|
|
Augmentations = JSON.parse(saveObj.AugmentationsSave, Reviver);
|
2017-05-23 19:36:35 +02:00
|
|
|
if (saveObj.hasOwnProperty("AliasesSave")) {
|
2017-05-23 18:15:17 +02:00
|
|
|
try {
|
|
|
|
Aliases = JSON.parse(saveObj.AliasesSave, Reviver);
|
|
|
|
} catch(e) {
|
|
|
|
Aliases = {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Aliases = {};
|
|
|
|
}
|
2017-06-02 06:15:45 +02:00
|
|
|
if (saveObj.hasOwnProperty("MessagesSave")) {
|
|
|
|
try {
|
|
|
|
Messages = JSON.parse(saveObj.MessagesSave, Reviver);
|
|
|
|
} catch(e) {
|
|
|
|
initMessages();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
initMessages();
|
|
|
|
}
|
2017-06-07 02:04:18 +02:00
|
|
|
if (saveObj.hasOwnProperty("VersionSave")) {
|
|
|
|
try {
|
|
|
|
var ver = JSON.parse(saveObj.VersionSave, Reviver);
|
|
|
|
if (ver != CONSTANTS.Version) {
|
2017-06-07 02:28:20 +02:00
|
|
|
createNewUpdateText();
|
2017-06-07 02:04:18 +02:00
|
|
|
}
|
|
|
|
} catch(e) {
|
2017-06-07 02:28:20 +02:00
|
|
|
createNewUpdateText();
|
2017-06-07 02:04:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-07 02:28:20 +02:00
|
|
|
createNewUpdateText();
|
2017-06-07 02:04:18 +02:00
|
|
|
}
|
2017-05-23 18:15:17 +02:00
|
|
|
|
2017-05-05 03:08:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-08 06:57:40 +02:00
|
|
|
loadImportedGame = function(saveObj, saveString) {
|
|
|
|
var tempSaveObj = null;
|
|
|
|
var tempPlayer = null;
|
|
|
|
var tempAllServers = null;
|
|
|
|
var tempCompanies = null;
|
|
|
|
var tempFactions = null;
|
|
|
|
var tempSpecialServerIps = null;
|
|
|
|
var tempAugmentations = null;
|
|
|
|
var tempAliases = null;
|
|
|
|
var tempMessages = null;
|
|
|
|
try {
|
|
|
|
saveString = decodeURIComponent(escape(atob(saveString)));
|
|
|
|
tempSaveObj = new BitburnerSaveObject();
|
|
|
|
tempSaveObj = JSON.parse(saveString, Reviver);
|
|
|
|
|
|
|
|
tempPlayer = JSON.parse(tempSaveObj.PlayerSave, Reviver);
|
|
|
|
tempAllServers = JSON.parse(tempSaveObj.AllServersSave, Reviver);
|
|
|
|
tempCompanies = JSON.parse(tempSaveObj.CompaniesSave, Reviver);
|
|
|
|
tempFactions = JSON.parse(tempSaveObj.FactionsSave, Reviver);
|
|
|
|
tempSpecialServerIps = JSON.parse(tempSaveObj.SpecialServerIpsSave, Reviver);
|
|
|
|
tempAugmentations = JSON.parse(tempSaveObj.AugmentationsSave, Reviver);
|
|
|
|
if (tempSaveObj.hasOwnProperty("AliasesSave")) {
|
|
|
|
try {
|
|
|
|
tempAliases = JSON.parse(tempSaveObj.AliasesSave, Reviver);
|
|
|
|
} catch(e) {
|
|
|
|
tempAliases = {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tempAliases = {};
|
|
|
|
}
|
|
|
|
if (tempSaveObj.hasOwnProperty("MessagesSave")) {
|
|
|
|
try {
|
|
|
|
tempMessages = JSON.parse(tempSaveObj.MessagesSave, Reviver);
|
|
|
|
} catch(e) {
|
|
|
|
initMessages();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
initMessages();
|
|
|
|
}
|
|
|
|
if (tempSaveObj.hasOwnProperty("VersionSave")) {
|
|
|
|
try {
|
|
|
|
var ver = JSON.parse(tempSaveObj.VersionSave, Reviver);
|
|
|
|
if (ver != CONSTANTS.Version) {
|
|
|
|
createNewUpdateText();
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
createNewUpdateText();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
createNewUpdateText();
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
dialogBoxCreate("Error importing game");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveObj = tempSaveObj;
|
|
|
|
Player = tempPlayer;
|
|
|
|
AllServers = tempAllServers;
|
|
|
|
Companies = tempCompanies;
|
|
|
|
Factions = tempFactions;
|
|
|
|
SpecialServerIps = tempSpecialServerIps;
|
|
|
|
Augmentations = tempAugmentations;
|
|
|
|
if (tempAliases) {
|
|
|
|
Aliases = tempAliases;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tempMessages) {
|
|
|
|
Messages = tempMessages;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogBoxCreate("Imported game");
|
2017-06-08 07:08:53 +02:00
|
|
|
gameOptionsBoxClose();
|
2017-06-08 17:59:22 +02:00
|
|
|
|
|
|
|
//Re-start game
|
|
|
|
console.log("Importing game");
|
|
|
|
Engine.setDisplayElements(); //Sets variables for important DOM elements
|
|
|
|
Engine.init(); //Initialize buttons, work, etc.
|
|
|
|
CompanyPositions.init();
|
|
|
|
|
|
|
|
//Calculate the number of cycles have elapsed while offline
|
|
|
|
Engine._lastUpdate = new Date().getTime();
|
|
|
|
var lastUpdate = Player.lastUpdate;
|
|
|
|
var numCyclesOffline = Math.floor((Engine._lastUpdate - lastUpdate) / Engine._idleSpeed);
|
|
|
|
|
|
|
|
/* Process offline progress */
|
|
|
|
var offlineProductionFromScripts = loadAllRunningScripts(); //This also takes care of offline production for those scripts
|
|
|
|
if (Player.isWorking) {
|
|
|
|
console.log("work() called in load() for " + numCyclesOffline * Engine._idleSpeed + " milliseconds");
|
|
|
|
if (Player.workType == CONSTANTS.WorkTypeFaction) {
|
|
|
|
Player.workForFaction(numCyclesOffline);
|
|
|
|
} else if (Player.workType == CONSTANTS.WorkTypeCreateProgram) {
|
|
|
|
Player.createProgramWork(numCyclesOffline);
|
|
|
|
} else if (Player.workType == CONSTANTS.WorkTypeStudyClass) {
|
|
|
|
Player.takeClass(numCyclesOffline);
|
|
|
|
} else if (Player.workType == CONSTANTS.WorkTypeCrime) {
|
|
|
|
Player.commitCrime(numCyclesOffline);
|
|
|
|
} else if (Player.workType == CONSTANTS.WorkTypeCompanyPartTime) {
|
|
|
|
Player.workPartTime(numCyclesOffline);
|
|
|
|
} else {
|
|
|
|
Player.work(numCyclesOffline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Hacknet Nodes offline progress
|
|
|
|
var offlineProductionFromHacknetNodes = processAllHacknetNodeEarnings(numCyclesOffline);
|
|
|
|
|
|
|
|
//Passive faction rep gain offline
|
|
|
|
processPassiveFactionRepGain(numCyclesOffline);
|
|
|
|
|
|
|
|
//Update total playtime
|
|
|
|
var time = numCyclesOffline * Engine._idleSpeed;
|
|
|
|
if (Player.totalPlaytime == null) {Player.totalPlaytime = 0;}
|
|
|
|
Player.totalPlaytime += time;
|
|
|
|
|
|
|
|
//Re-apply augmentations
|
|
|
|
Player.reapplyAllAugmentations();
|
|
|
|
|
2017-06-08 20:32:38 +02:00
|
|
|
//Clear terminal
|
|
|
|
$("#terminal tr:not(:last)").remove();
|
|
|
|
|
2017-06-08 17:59:22 +02:00
|
|
|
Player.lastUpdate = Engine._lastUpdate;
|
|
|
|
Engine.start(); //Run main game loop and Scripts loop
|
|
|
|
dialogBoxCreate("While you were offline, your scripts generated $" +
|
|
|
|
formatNumber(offlineProductionFromScripts, 2) + " and your Hacknet Nodes generated $" +
|
|
|
|
formatNumber(offlineProductionFromHacknetNodes, 2));
|
2017-06-08 06:57:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-08 01:35:56 +02:00
|
|
|
BitburnerSaveObject.prototype.exportGame = function() {
|
2017-06-08 06:57:40 +02:00
|
|
|
this.PlayerSave = JSON.stringify(Player);
|
|
|
|
this.AllServersSave = JSON.stringify(AllServers);
|
|
|
|
this.CompaniesSave = JSON.stringify(Companies);
|
|
|
|
this.FactionsSave = JSON.stringify(Factions);
|
|
|
|
this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps);
|
|
|
|
this.AugmentationsSave = JSON.stringify(Augmentations);
|
|
|
|
this.AliasesSave = JSON.stringify(Aliases);
|
|
|
|
this.MessagesSave = JSON.stringify(Messages);
|
|
|
|
this.VersionSave = JSON.stringify(CONSTANTS.Version);
|
|
|
|
|
|
|
|
var saveString = btoa(unescape(encodeURIComponent(JSON.stringify(this))));
|
|
|
|
|
|
|
|
var file = new Blob([saveString], {type: 'text/plain'});
|
|
|
|
if (window.navigator.msSaveOrOpenBlob) {// IE10+
|
|
|
|
window.navigator.msSaveOrOpenBlob(file, filename);
|
|
|
|
} else { // Others
|
|
|
|
var a = document.createElement("a"),
|
|
|
|
url = URL.createObjectURL(file);
|
|
|
|
a.href = url;
|
|
|
|
a.download = "bitburnerSave.json";
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
setTimeout(function() {
|
|
|
|
document.body.removeChild(a);
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BitburnerSaveObject.prototype.importGame = function() {
|
|
|
|
if (window.File && window.FileReader && window.FileList && window.Blob) {
|
|
|
|
var fileSelector = clearEventListeners("import-game-file-selector");
|
|
|
|
fileSelector.addEventListener("change", openImportFileHandler, false);
|
|
|
|
$("#import-game-file-selector").click();
|
|
|
|
} else {
|
|
|
|
dialogBoxCreate("ERR: Your browser does not support HTML5 File API. Cannot import.");
|
|
|
|
}
|
2017-06-08 01:35:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-05 03:08:44 +02:00
|
|
|
BitburnerSaveObject.prototype.deleteGame = function() {
|
|
|
|
if (window.localStorage.getItem("bitburnerSave")) {
|
|
|
|
window.localStorage.removeItem("bitburnerSave");
|
|
|
|
}
|
2017-05-15 18:54:23 +02:00
|
|
|
Engine.createStatusText("Game deleted!");
|
2017-05-05 03:08:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-07 02:28:20 +02:00
|
|
|
createNewUpdateText = function() {
|
|
|
|
dialogBoxCreate("New update!<br>" +
|
|
|
|
"Please report any bugs/issues through the github repository " +
|
|
|
|
"or the Bitburner subreddit (reddit.com/r/bitburner).<br><br>" +
|
|
|
|
CONSTANTS.LatestUpdate);
|
|
|
|
}
|
|
|
|
|
2017-05-05 03:08:44 +02:00
|
|
|
|
|
|
|
BitburnerSaveObject.prototype.toJSON = function() {
|
|
|
|
return Generic_toJSON("BitburnerSaveObject", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
BitburnerSaveObject.fromJSON = function(value) {
|
|
|
|
return Generic_fromJSON(BitburnerSaveObject, value.data);
|
|
|
|
}
|
|
|
|
|
2017-06-08 06:57:40 +02:00
|
|
|
Reviver.constructors.BitburnerSaveObject = BitburnerSaveObject;
|
|
|
|
|
|
|
|
//Import game
|
|
|
|
|
|
|
|
function openImportFileHandler(evt) {
|
|
|
|
var file = evt.target.files[0];
|
|
|
|
if (!file) {
|
|
|
|
dialogBoxCreate("Invalid file selected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = function(e) {
|
|
|
|
var contents = e.target.result;
|
|
|
|
loadImportedGame(saveObject, contents);
|
|
|
|
};
|
|
|
|
reader.readAsText(file);
|
|
|
|
}
|