From 8dbca029de179ad5f07666dbd685ad1642ef16b1 Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Wed, 21 Dec 2016 12:36:42 -0600 Subject: [PATCH] Factions should save and load properly (not fully tested). Terminal now scrolls to the bottom when something is posted --- README.md | 5 ++++- src/Faction.js | 15 ++++++++++----- src/Terminal.js | 13 ++++++++++--- src/engine.js | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 06e4c0ec1..0afd380d0 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,12 @@ TESTING TODO: ctrl+C functionality for all running command like hack(), analyze(), and tail Implemented for hack() and analyze(). Seems to work + Saving/Loading factions + No errors thrown when saving/loading game at the start -Tasks TODO: Scroll all the way down when something is post()ed + +Tasks TODO: Script logging functionality? Logs to internal "log file" (property of script itself) Tutorial and help Secret Servers diff --git a/src/Faction.js b/src/Faction.js index 99a77d9e6..4c26755a0 100644 --- a/src/Faction.js +++ b/src/Faction.js @@ -8,17 +8,22 @@ function Faction(name) { this.playerReputation = 0; //"Reputation" within faction }; -//TODO -Faction.prototype.init = function() { - -} - Faction.prototype.setAugmentations = function(augs) { for (var i = 0; i < augs.length; i++) { this.augmentations.push(augs[i]); } } +Faction.prototype.toJSON = function() { + return Generic_toJSON("Faction", this); +} + +Faction.fromJSON = function(value) { + return Generic_fromJSON(Faction, value.data); +} + +Reviver.constructors.Faction = Faction; + //Map of factions indexed by faction name Factions = {} diff --git a/src/Terminal.js b/src/Terminal.js index 236e8c489..49c1ddd13 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -1,18 +1,25 @@ //Terminal + +/* Write text to terminal */ var post = function(input) { $("#terminal-input").before('' + input.replace( / /g, " " ) + ''); - window.scrollTo(0, document.body.scrollHeight); + updateTerminalScroll(); } //Same thing as post but the td cells have ids so they can be animated for the hack progress bar var hackProgressBarPost = function(input) { $("#terminal-input").before('' + input + ''); - window.scrollTo(0, document.body.scrollHeight); + updateTerminalScroll(); } var hackProgressPost = function(input) { $("#terminal-input").before('' + input + ''); - window.scrollTo(0, document.body.scrollHeight); + updateTerminalScroll(); +} + +function updateTerminalScroll() { + var element = document.getElementById("terminal-container"); + element.scrollTop = element.scrollHeight; } var postNetburnerText = function() { diff --git a/src/engine.js b/src/engine.js index bfe4f8909..e27c9984c 100644 --- a/src/engine.js +++ b/src/engine.js @@ -55,11 +55,13 @@ var Engine = { var PlayerSave = JSON.stringify(Player); var AllServersSave = JSON.stringify(AllServers); var CompaniesSave = JSON.stringify(Companies); + var FactionsSave = JSON.stringify(Factions); //TODO Add factions + companies here when they're done window.localStorage.setItem("netburnerPlayerSave", PlayerSave); window.localStorage.setItem("netburnerAllServersSave", AllServersSave); window.localStorage.setItem("netburnerCompaniesSave", CompaniesSave); + window.localStorage.setItem("netburnerFactionsSave", FactionsSave); console.log("Game saved to local storage"); }, @@ -75,19 +77,26 @@ var Engine = { } else if (!window.localStorage.getItem("netburnerCompaniesSave")) { console.log("No Companies save to load"); return false; + } else if (!window.localStorage.getItem("netburnerFactionsSave")) { + console.log("No Factions save to load"); + return false; } else { var PlayerSave = window.localStorage.getItem("netburnerPlayerSave"); var AllServersSave = window.localStorage.getItem("netburnerAllServersSave"); var CompaniesSave = window.localStorage.getItem("netburnerCompaniesSave"); + var FactionsSave = window.localStorage.getItem("netburnerFactionsSave"); Player = JSON.parse(PlayerSave, Reviver); AllServers = JSON.parse(AllServersSave, Reviver); Companies = JSON.parse(CompaniesSave, Reviver); + Factions = JSON.parse(FactionsSave, Reviver); return true; } }, //Delete saved game function deleteSave: function() { + //TODO if a save doesn't exist..maybe I shouldn't return? I just keep going + //or else nothing gets deleted. TODO Fix this if (!window.localStorage.getItem("netburnerPlayerSave")) { console.log("No Player Save to delete"); return false; @@ -97,10 +106,14 @@ var Engine = { } else if (!window.localStorage.getItem("netburnerCompaniesSave")) { console.log("No Companies Save to delete"); return false; + } else if (!window.localStorage.getItem("netburnerFactionsSave")) { + console.log("No Factions Save to delete"); + return false; } else { window.localStorage.removeItem("netburnerPlayerSave"); window.localStorage.removeItem("netburnerAllServersSave"); window.localStorage.removeItem("netburnerCompaniesSave"); + window.localStorage.removeItem("netburnerFactionsSave"); console.log("Deleted saves") return true; } @@ -388,6 +401,7 @@ var Engine = { initForeignServers(); CompanyPositions.init(); initCompanies(); + initFactions(); } //Main menu buttons and content