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