moved updateRam to first call to avoid lack of mocks for tests

* added wrapper funcs to simplify updateRam
* added static parser tests and skiped what isnt worker still for next person to try and fix
This commit is contained in:
phyzical
2022-03-31 21:41:19 +08:00
parent d814a81b79
commit 18af3f8413
13 changed files with 846 additions and 487 deletions

View File

@@ -2,7 +2,12 @@ import { INetscriptHelper } from "./INetscriptHelper";
import { WorkerScript } from "../Netscript/WorkerScript";
import { IPlayer } from "../PersonObjects/IPlayer";
import { getRamCost } from "../Netscript/RamCostGenerator";
import { GameInfo, IStyleSettings, UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions";
import {
GameInfo,
IStyleSettings,
UserInterface as IUserInterface,
UserInterfaceTheme,
} from "../ScriptEditor/NetscriptDefinitions";
import { Settings } from "../Settings/Settings";
import { ThemeEvents } from "../Themes/ui/Theme";
import { defaultTheme } from "../Themes/Themes";
@@ -15,27 +20,28 @@ export function NetscriptUserInterface(
workerScript: WorkerScript,
helper: INetscriptHelper,
): IUserInterface {
const updateRam = (funcName: string): void => helper.updateDynamicRam(funcName, getRamCost(player, "ui", funcName));
return {
getTheme: function (): UserInterfaceTheme {
helper.updateDynamicRam("getTheme", getRamCost(player, "ui", "getTheme"));
updateRam("getTheme");
return { ...Settings.theme };
},
getStyles: function (): IStyleSettings {
helper.updateDynamicRam("getStyles", getRamCost(player, "ui", "getStyles"));
updateRam("getStyles");
return { ...Settings.styles };
},
setTheme: function (newTheme: UserInterfaceTheme): void {
helper.updateDynamicRam("setTheme", getRamCost(player, "ui", "setTheme"));
updateRam("setTheme");
const hex = /^(#)((?:[A-Fa-f0-9]{2}){3,4}|(?:[A-Fa-f0-9]{3}))$/;
const currentTheme = {...Settings.theme}
const currentTheme = { ...Settings.theme };
const errors: string[] = [];
for (const key of Object.keys(newTheme)) {
if (!currentTheme[key]) {
// Invalid key
errors.push(`Invalid key "${key}"`);
} else if (!hex.test(newTheme[key] ?? '')) {
} else if (!hex.test(newTheme[key] ?? "")) {
errors.push(`Invalid color "${key}": ${newTheme[key]}`);
} else {
currentTheme[key] = newTheme[key];
@@ -47,17 +53,17 @@ export function NetscriptUserInterface(
ThemeEvents.emit();
workerScript.log("ui.setTheme", () => `Successfully set theme`);
} else {
workerScript.log("ui.setTheme", () => `Failed to set theme. Errors: ${errors.join(', ')}`);
workerScript.log("ui.setTheme", () => `Failed to set theme. Errors: ${errors.join(", ")}`);
}
},
setStyles: function (newStyles: IStyleSettings): void {
helper.updateDynamicRam("setStyles", getRamCost(player, "ui", "setStyles"));
updateRam("setStyles");
const currentStyles = {...Settings.styles}
const currentStyles = { ...Settings.styles };
const errors: string[] = [];
for (const key of Object.keys(newStyles)) {
if (!((currentStyles as any)[key])) {
if (!(currentStyles as any)[key]) {
// Invalid key
errors.push(`Invalid key "${key}"`);
} else {
@@ -70,35 +76,37 @@ export function NetscriptUserInterface(
ThemeEvents.emit();
workerScript.log("ui.setStyles", () => `Successfully set styles`);
} else {
workerScript.log("ui.setStyles", () => `Failed to set styles. Errors: ${errors.join(', ')}`);
workerScript.log("ui.setStyles", () => `Failed to set styles. Errors: ${errors.join(", ")}`);
}
},
resetTheme: function (): void {
helper.updateDynamicRam("resetTheme", getRamCost(player, "ui", "resetTheme"));
updateRam("resetTheme");
Settings.theme = { ...defaultTheme };
ThemeEvents.emit();
workerScript.log("ui.resetTheme", () => `Reinitialized theme to default`);
},
resetStyles: function (): void {
helper.updateDynamicRam("resetStyles", getRamCost(player, "ui", "resetStyles"));
updateRam("resetStyles");
Settings.styles = { ...defaultStyles };
ThemeEvents.emit();
workerScript.log("ui.resetStyles", () => `Reinitialized styles to default`);
},
getGameInfo: function (): GameInfo {
helper.updateDynamicRam("getGameInfo", getRamCost(player, "ui", "getGameInfo"));
updateRam("getGameInfo");
const version = CONSTANTS.VersionString;
const commit = hash();
const platform = (navigator.userAgent.toLowerCase().indexOf(" electron/") > -1) ? 'Steam' : 'Browser';
const platform = navigator.userAgent.toLowerCase().indexOf(" electron/") > -1 ? "Steam" : "Browser";
const gameInfo = {
version, commit, platform,
}
version,
commit,
platform,
};
return gameInfo;
}
}
},
};
}