[refactor] Moved Settings to TypeScript

Moved the UI binding to a separate file as there is still a circular
dependency with 'engine'. But every other file that depends on Settings
is no longer part of a larger dependency cycle.
This commit is contained in:
Steven Evans 2018-07-30 23:02:12 -04:00
parent 826a8799fa
commit 974bc3c002
5 changed files with 155 additions and 44 deletions

@ -13,7 +13,7 @@ import {loadMessages, initMessages, Messages} from "./Message";
import {Player, loadPlayer} from "./Player";
import {loadAllRunningScripts} from "./Script";
import {AllServers, loadAllServers} from "./Server";
import {loadSettings, initSettings, Settings} from "./Settings";
import {Settings} from "./Settings";
import {loadSpecialServerIps, SpecialServerIps} from "./SpecialServerIps";
import {loadStockMarket, StockMarket} from "./StockMarket";
import {dialogBoxCreate} from "../utils/DialogBox";
@ -169,13 +169,13 @@ function loadGame(saveString) {
}
if (saveObj.hasOwnProperty("SettingsSave")) {
try {
loadSettings(saveObj.SettingsSave);
Settings.load(saveObj.SettingsSave);
} catch(e) {
console.log("ERROR: Failed to parse Settings. Re-initing default values");
initSettings();
Settings.init();
}
} else {
initSettings();
Settings.init();
}
if (saveObj.hasOwnProperty("FconfSettingsSave")) {
try {
@ -388,12 +388,12 @@ function loadImportedGame(saveObj, saveString) {
}
if (saveObj.hasOwnProperty("SettingsSave")) {
try {
loadSettings(saveObj.SettingsSave);
Settings.load(saveObj.SettingsSave);
} catch(e) {
initSettings();
Settings.init();
}
} else {
initSettings();
Settings.init();
}
if (saveObj.hasOwnProperty("FconfSettingsSave")) {
try {

122
src/Settings.ts Normal file

@ -0,0 +1,122 @@
import { ISelfInitializer, ISelfLoading } from "./types";
/**
* Represents the default settings the player could customize.
*/
interface IDefaultSettings {
/**
* How often the game should autosave the player's progress, in seconds.
*/
AutosaveInterval: number;
/**
* How many milliseconds between execution points for Netscript 1 statements.
*/
CodeInstructionRunTime: number;
/**
* Whether global keyboard shortcuts should be recognized throughout the game.
*/
DisableHotkeys: boolean;
/**
* Limit the number of log entries for each script being executed on each server.
*/
MaxLogCapacity: number;
/**
* Limit how many entries can be written to a Netscript Port before entries start to get pushed out.
*/
MaxPortCapacity: number;
/**
* Whether the player should be asked to confirm purchasing each and every augmentation.
*/
SuppressBuyAugmentationConfirmation: boolean;
/**
* Whether the user should be prompted to join each faction via a dialog box.
*/
SuppressFactionInvites: boolean;
/**
* Whether the user should be shown a dialog box whenever they receive a new message file.
*/
SuppressMessages: boolean;
/**
* Whether the user should be asked to confirm travelling between cities.
*/
SuppressTravelConfirmation: boolean;
}
/**
* Represents all possible settings the player wants to customize to their play style.
*/
interface ISettings extends IDefaultSettings {
/**
* The keybinding to use in the script editor.
* TODO: This should really be an enum of allowed values.
*/
EditorKeybinding: string;
/**
* The theme used in the script editor.
* TODO: This should really be an enum of allowed values.
*/
EditorTheme: string;
/**
* The CSS background theme color to apply across the game.
*/
ThemeBackgroundColor: string;
/**
* The CSS text theme color to apply across the game.
*/
ThemeFontColor: string;
/**
* The CSS foreground theme color to apply across the game.
*/
ThemeHighlightColor: string;
}
const defaultSettings: IDefaultSettings = {
AutosaveInterval: 60,
CodeInstructionRunTime: 50,
DisableHotkeys: false,
MaxLogCapacity: 50,
MaxPortCapacity: 50,
SuppressBuyAugmentationConfirmation: false,
SuppressFactionInvites: false,
SuppressMessages: false,
SuppressTravelConfirmation: false,
};
/**
* The current options the player has customized to their play style.
*/
// tslint:disable-next-line:variable-name
export const Settings: ISettings & ISelfInitializer & ISelfLoading = {
AutosaveInterval: defaultSettings.AutosaveInterval,
CodeInstructionRunTime: 25,
DisableHotkeys: defaultSettings.DisableHotkeys,
EditorKeybinding: "ace",
EditorTheme: "Monokai",
MaxLogCapacity: defaultSettings.MaxLogCapacity,
MaxPortCapacity: defaultSettings.MaxPortCapacity,
SuppressBuyAugmentationConfirmation: defaultSettings.SuppressBuyAugmentationConfirmation,
SuppressFactionInvites: defaultSettings.SuppressFactionInvites,
SuppressMessages: defaultSettings.SuppressMessages,
SuppressTravelConfirmation: defaultSettings.SuppressTravelConfirmation,
ThemeBackgroundColor: "#000000",
ThemeFontColor: "#66ff33",
ThemeHighlightColor: "#ffffff",
init() {
Object.assign(Settings, defaultSettings);
},
load(saveString: string) {
Object.assign(Settings, JSON.parse(saveString));
},
};

@ -55,7 +55,8 @@ import {saveObject, loadGame} from "./SaveObject";
import {loadAllRunningScripts, scriptEditorInit,
updateScriptEditorContent} from "./Script";
import {AllServers, Server, initForeignServers} from "./Server";
import {Settings, setSettingsLabels} from "./Settings";
import {Settings} from "./Settings";
import {setSettingsLabels} from "./ui/setSettingsLabels";
import {initSourceFiles, SourceFiles,
PlayerOwnedSourceFile} from "./SourceFile";
import {SpecialServerIps, initSpecialServerIps} from "./SpecialServerIps";

@ -14,3 +14,24 @@ export interface IMap<T> {
* Performs some action, with no returned value.
*/
export type Action = () => void;
/**
* Contains a method to initialize itself to a known state.
*/
export interface ISelfInitializer {
/**
* Initialize/reset the object to a known, default state.
*/
init(): void;
}
/**
* Contains a method to repopulate itself based on a JSON string.
*/
export interface ISelfLoading {
/**
* Loads the save state onto the current object.
* @param saveState JSON string representing the save state.
*/
load(saveState: string): void;
}

@ -1,38 +1,5 @@
import {Engine} from "./engine";
/* Settings.js */
let Settings = {
CodeInstructionRunTime: 25,
MaxLogCapacity: 50,
MaxPortCapacity: 50,
SuppressMessages: false,
SuppressFactionInvites: false,
SuppressTravelConfirmation: false,
SuppressBuyAugmentationConfirmation: false,
AutosaveInterval: 60,
DisableHotkeys: false,
ThemeHighlightColor: "#ffffff",
ThemeFontColor: "#66ff33",
ThemeBackgroundColor: "#000000",
EditorTheme: "Monokai",
EditorKeybinding: "ace",
}
function loadSettings(saveString) {
Settings = JSON.parse(saveString);
}
function initSettings() {
Settings.CodeInstructionRunTime = 50;
Settings.MaxLogCapacity = 50;
Settings.MaxPortCapacity = 50;
Settings.SuppressMessages = false;
Settings.SuppressFactionInvites = false;
Settings.SuppressTravelConfirmation = false;
Settings.SuppressBuyAugmentationConfirmation = false;
Settings.AutosaveInterval = 60;
Settings.DisableHotkeys = false;
}
import {Engine} from "../engine";
import {Settings} from "../Settings";
function setSettingsLabels() {
var nsExecTime = document.getElementById("settingsNSExecTimeRangeValLabel");
@ -126,4 +93,4 @@ function setSettingsLabels() {
}
}
export {Settings, initSettings, setSettingsLabels, loadSettings};
export { setSettingsLabels };