mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-10 01:33:54 +01:00
fix RAM calculation on a different server (#612)
This commit is contained in:
parent
192c1947a6
commit
12b5c00d14
@ -1,19 +1,18 @@
|
|||||||
import React, { useContext, useState } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
|
|
||||||
import { Player } from "@player";
|
|
||||||
|
|
||||||
import { Settings } from "../../Settings/Settings";
|
import { Settings } from "../../Settings/Settings";
|
||||||
import { calculateRamUsage } from "../../Script/RamCalculations";
|
import { calculateRamUsage } from "../../Script/RamCalculations";
|
||||||
import { RamCalculationErrorCode } from "../../Script/RamCalculationErrorCodes";
|
import { RamCalculationErrorCode } from "../../Script/RamCalculationErrorCodes";
|
||||||
import { formatRam } from "../../ui/formatNumber";
|
import { formatRam } from "../../ui/formatNumber";
|
||||||
import { useBoolean } from "../../ui/React/hooks";
|
import { useBoolean } from "../../ui/React/hooks";
|
||||||
|
import { BaseServer } from "../../Server/BaseServer";
|
||||||
|
|
||||||
import { Options } from "./Options";
|
import { Options } from "./Options";
|
||||||
|
|
||||||
export interface ScriptEditorContextShape {
|
export interface ScriptEditorContextShape {
|
||||||
ram: string;
|
ram: string;
|
||||||
ramEntries: string[][];
|
ramEntries: string[][];
|
||||||
updateRAM: (newCode: string | null) => void;
|
updateRAM: (newCode: string | null, server: BaseServer | null) => void;
|
||||||
|
|
||||||
isUpdatingRAM: boolean;
|
isUpdatingRAM: boolean;
|
||||||
startUpdatingRAM: () => void;
|
startUpdatingRAM: () => void;
|
||||||
@ -25,23 +24,18 @@ export interface ScriptEditorContextShape {
|
|||||||
|
|
||||||
const ScriptEditorContext = React.createContext({} as ScriptEditorContextShape);
|
const ScriptEditorContext = React.createContext({} as ScriptEditorContextShape);
|
||||||
|
|
||||||
interface IProps {
|
export function ScriptEditorContextProvider({ children, vim }: { children: React.ReactNode; vim: boolean }) {
|
||||||
children: React.ReactNode;
|
|
||||||
vim: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function ScriptEditorContextProvider({ children, vim }: IProps) {
|
|
||||||
const [ram, setRAM] = useState("RAM: ???");
|
const [ram, setRAM] = useState("RAM: ???");
|
||||||
const [ramEntries, setRamEntries] = useState<string[][]>([["???", ""]]);
|
const [ramEntries, setRamEntries] = useState<string[][]>([["???", ""]]);
|
||||||
|
|
||||||
function updateRAM(newCode: string | null): void {
|
const updateRAM: ScriptEditorContextShape["updateRAM"] = (newCode, server) => {
|
||||||
if (newCode === null) {
|
if (newCode === null || server === null) {
|
||||||
setRAM("N/A");
|
setRAM("N/A");
|
||||||
setRamEntries([["N/A", ""]]);
|
setRamEntries([["N/A", ""]]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const codeCopy = newCode + "";
|
const codeCopy = newCode + "";
|
||||||
const ramUsage = calculateRamUsage(codeCopy, Player.getCurrentServer().scripts);
|
const ramUsage = calculateRamUsage(codeCopy, server.scripts);
|
||||||
if (ramUsage.cost > 0) {
|
if (ramUsage.cost > 0) {
|
||||||
const entries = ramUsage.entries?.sort((a, b) => b.cost - a.cost) ?? [];
|
const entries = ramUsage.entries?.sort((a, b) => b.cost - a.cost) ?? [];
|
||||||
const entriesDisp = [];
|
const entriesDisp = [];
|
||||||
@ -71,7 +65,7 @@ export function ScriptEditorContextProvider({ children, vim }: IProps) {
|
|||||||
}
|
}
|
||||||
setRAM(RAM);
|
setRAM(RAM);
|
||||||
setRamEntries(entriesDisp);
|
setRamEntries(entriesDisp);
|
||||||
}
|
};
|
||||||
|
|
||||||
const [isUpdatingRAM, { on: startUpdatingRAM, off: finishUpdatingRAM }] = useBoolean(false);
|
const [isUpdatingRAM, { on: startUpdatingRAM, off: finishUpdatingRAM }] = useBoolean(false);
|
||||||
|
|
||||||
|
@ -137,7 +137,10 @@ function Root(props: IProps): React.ReactElement {
|
|||||||
|
|
||||||
const debouncedCodeParsing = debounce((newCode: string) => {
|
const debouncedCodeParsing = debounce((newCode: string) => {
|
||||||
infLoop(newCode);
|
infLoop(newCode);
|
||||||
updateRAM(!currentScript || currentScript.isTxt ? null : newCode);
|
updateRAM(
|
||||||
|
!currentScript || currentScript.isTxt ? null : newCode,
|
||||||
|
currentScript && GetServer(currentScript.hostname),
|
||||||
|
);
|
||||||
finishUpdatingRAM();
|
finishUpdatingRAM();
|
||||||
}, 300);
|
}, 300);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user