From 445e3659596eff86d93a80e3949a53bf627bb0a4 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Tue, 23 Aug 2022 16:38:30 -0400 Subject: [PATCH] debounce updateRAM --- src/NetscriptFunctions/Singularity.ts | 3 +- .../Sleeve/Work/SleeveCrimeWork.ts | 16 +++---- src/SaveObject.tsx | 1 - src/Script/RamCalculationErrorCodes.ts | 1 - src/ScriptEditor/ui/ScriptEditorRoot.tsx | 43 +++++++++---------- src/Work/CrimeWork.ts | 3 +- 6 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/NetscriptFunctions/Singularity.ts b/src/NetscriptFunctions/Singularity.ts index 8f3afd4cf..7ce3893d0 100644 --- a/src/NetscriptFunctions/Singularity.ts +++ b/src/NetscriptFunctions/Singularity.ts @@ -1278,8 +1278,7 @@ export function NetscriptSingularity(): InternalAPI { const callbackScript = helpers.string(ctx, "callbackScript", _callbackScript); const wd = GetServer(SpecialServers.WorldDaemon); - if (!(wd instanceof Server)) - throw new Error("WorldDaemon was not a normal server. This is a bug contact dev."); + if (!(wd instanceof Server)) throw new Error("WorldDaemon was not a normal server. This is a bug contact dev."); const hackingRequirements = (): boolean => { if (player.skills.hacking < wd.requiredHackingSkill) return false; if (!wd.hasAdminRights) return false; diff --git a/src/PersonObjects/Sleeve/Work/SleeveCrimeWork.ts b/src/PersonObjects/Sleeve/Work/SleeveCrimeWork.ts index 0d72adde2..5610af6f9 100644 --- a/src/PersonObjects/Sleeve/Work/SleeveCrimeWork.ts +++ b/src/PersonObjects/Sleeve/Work/SleeveCrimeWork.ts @@ -28,14 +28,14 @@ export class SleeveCrimeWork extends Work { getExp(): WorkStats { const crime = this.getCrime(); return newWorkStats({ - money: crime.money*BitNodeMultipliers.CrimeMoney, - hackExp: crime.hacking_exp*BitNodeMultipliers.CrimeExpGain, - strExp: crime.strength_exp*BitNodeMultipliers.CrimeExpGain, - defExp: crime.defense_exp*BitNodeMultipliers.CrimeExpGain, - dexExp: crime.dexterity_exp*BitNodeMultipliers.CrimeExpGain, - agiExp: crime.agility_exp*BitNodeMultipliers.CrimeExpGain, - chaExp: crime.charisma_exp*BitNodeMultipliers.CrimeExpGain, - intExp: crime.intelligence_exp*BitNodeMultipliers.CrimeExpGain, + money: crime.money * BitNodeMultipliers.CrimeMoney, + hackExp: crime.hacking_exp * BitNodeMultipliers.CrimeExpGain, + strExp: crime.strength_exp * BitNodeMultipliers.CrimeExpGain, + defExp: crime.defense_exp * BitNodeMultipliers.CrimeExpGain, + dexExp: crime.dexterity_exp * BitNodeMultipliers.CrimeExpGain, + agiExp: crime.agility_exp * BitNodeMultipliers.CrimeExpGain, + chaExp: crime.charisma_exp * BitNodeMultipliers.CrimeExpGain, + intExp: crime.intelligence_exp * BitNodeMultipliers.CrimeExpGain, }); } diff --git a/src/SaveObject.tsx b/src/SaveObject.tsx index 5113d210c..b1cf7a872 100755 --- a/src/SaveObject.tsx +++ b/src/SaveObject.tsx @@ -36,7 +36,6 @@ import { Faction } from "./Faction/Faction"; import { safetlyCreateUniqueServer } from "./Server/ServerHelpers"; import { SpecialServers } from "./Server/data/SpecialServers"; import { v2APIBreak } from "./utils/v2APIBreak"; -import { Script } from "./Script/Script"; /* SaveObject.js * Defines the object used to save/load games diff --git a/src/Script/RamCalculationErrorCodes.ts b/src/Script/RamCalculationErrorCodes.ts index 330a692d5..b433bb863 100644 --- a/src/Script/RamCalculationErrorCodes.ts +++ b/src/Script/RamCalculationErrorCodes.ts @@ -1,5 +1,4 @@ export enum RamCalculationErrorCode { SyntaxError = -1, ImportError = -2, - URLImportError = -3, } diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 64e16c406..3cb10e3bd 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useRef, useMemo } from "react"; +import React, { useState, useEffect, useRef } from "react"; import Editor, { Monaco } from "@monaco-editor/react"; import * as monaco from "monaco-editor"; @@ -240,22 +240,17 @@ export function Root(props: IProps): React.ReactElement { } } - const debouncedSetRAM = useMemo( - () => - debounce((s, e) => { - setRAM(s); - setRamEntries(e); - setUpdatingRam(false); - }, 300), - [], - ); + const debouncedUpdateRAM = debounce((newCode: string) => { + updateRAM(newCode); + setUpdatingRam(false); + }, 300); - async function updateRAM(newCode: string): Promise { + function updateRAM(newCode: string): void { if (currentScript != null && currentScript.isTxt) { - debouncedSetRAM("N/A", [["N/A", ""]]); + setRAM("N/A"); + setRamEntries([["N/A", ""]]); return; } - setUpdatingRam(true); const codeCopy = newCode + ""; const ramUsage = calculateRamUsage(props.player, codeCopy, props.player.getCurrentServer().scripts); if (ramUsage.cost > 0) { @@ -265,25 +260,28 @@ export function Root(props: IProps): React.ReactElement { entriesDisp.push([`${entry.name} (${entry.type})`, numeralWrapper.formatRAM(entry.cost)]); } - debouncedSetRAM("RAM: " + numeralWrapper.formatRAM(ramUsage.cost), entriesDisp); + setRAM("RAM: " + numeralWrapper.formatRAM(ramUsage.cost)); + setRamEntries(entriesDisp); return; } + let RAM = ""; + const entriesDisp = []; switch (ramUsage.cost) { case RamCalculationErrorCode.ImportError: { - debouncedSetRAM("RAM: Import Error", [["Import Error", ""]]); - break; - } - case RamCalculationErrorCode.URLImportError: { - debouncedSetRAM("RAM: HTTP Import Error", [["HTTP Import Error", ""]]); + RAM = "RAM: Import Error"; + entriesDisp.push(["Import Error", ""]); break; } case RamCalculationErrorCode.SyntaxError: default: { - debouncedSetRAM("RAM: Syntax Error", [["Syntax Error", ""]]); + RAM = "RAM: Syntax Error"; + entriesDisp.push(["Syntax Error", ""]); break; } } - return new Promise(() => undefined); + setRAM(RAM); + setRamEntries(entriesDisp); + return; } // Formats the code @@ -446,7 +444,8 @@ export function Root(props: IProps): React.ReactElement { // When the code is updated within the editor function updateCode(newCode?: string): void { if (newCode === undefined) return; - updateRAM(newCode); + setUpdatingRam(true); + debouncedUpdateRAM(newCode); if (editorRef.current === null) return; const newPos = editorRef.current.getPosition(); if (newPos === null) return; diff --git a/src/Work/CrimeWork.ts b/src/Work/CrimeWork.ts index af4d9d711..28a813ea1 100644 --- a/src/Work/CrimeWork.ts +++ b/src/Work/CrimeWork.ts @@ -7,8 +7,7 @@ import { IPlayer } from "../PersonObjects/IPlayer"; import { dialogBoxCreate } from "../ui/React/DialogBox"; import { CrimeType } from "../utils/WorkType"; import { Work, WorkType } from "./Work"; -import { newWorkStats, scaleWorkStats, WorkStats } from "./WorkStats"; -import { BitNodeMultipliers } from "../BitNode/BitNodeMultipliers"; +import { scaleWorkStats, WorkStats } from "./WorkStats"; import { calculateCrimeWorkStats } from "./formulas/Crime"; enum newCrimeType {