From 2e05f14c0df37e6b9a2738ac31e936c4f8006067 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Fri, 24 Sep 2021 16:34:21 -0400 Subject: [PATCH] convert to ts --- src/NetscriptFunctions.js | 3 +- src/Script/ScriptHelpers.d.ts | 6 --- .../{ScriptHelpers.js => ScriptHelpers.ts} | 46 +++++++++++-------- ...ScriptHelpersTS.ts => isScriptFilename.ts} | 1 - src/ScriptEditor/ui/Root.tsx | 2 +- src/Server/BaseServer.ts | 2 +- src/Terminal/Terminal.ts | 2 +- src/Terminal/commands/check.ts | 2 +- src/Terminal/commands/download.ts | 2 +- src/Terminal/commands/mv.ts | 2 +- src/Terminal/commands/nano.ts | 2 +- src/Terminal/commands/run.ts | 2 +- src/Terminal/commands/scp.ts | 2 +- src/Terminal/commands/tail.ts | 2 +- src/Terminal/commands/wget.ts | 2 +- 15 files changed, 39 insertions(+), 39 deletions(-) delete mode 100644 src/Script/ScriptHelpers.d.ts rename src/Script/{ScriptHelpers.js => ScriptHelpers.ts} (58%) rename src/Script/{ScriptHelpersTS.ts => isScriptFilename.ts} (82%) diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 86627e160..e1746e55b 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -103,7 +103,8 @@ import { Player } from "./Player"; import { Programs } from "./Programs/Programs"; import { Script } from "./Script/Script"; import { findRunningScript, findRunningScriptByPid } from "./Script/ScriptHelpers"; -import { isScriptFilename } from "./Script/ScriptHelpersTS"; +import { isScriptFilename } from "./Script/isScriptFilename"; + import { AllServers, AddToAllServers, createUniqueRandomIp } from "./Server/AllServers"; import { RunningScript } from "./Script/RunningScript"; import { diff --git a/src/Script/ScriptHelpers.d.ts b/src/Script/ScriptHelpers.d.ts deleted file mode 100644 index 6027bef75..000000000 --- a/src/Script/ScriptHelpers.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -export declare function findRunningScript( - filename: string, - args: (string | number)[], - server: BaseServer, -): RunningScript | null; -export declare function findRunningScriptByPid(pid: number, server: BaseServer): RunningScript | null; diff --git a/src/Script/ScriptHelpers.js b/src/Script/ScriptHelpers.ts similarity index 58% rename from src/Script/ScriptHelpers.js rename to src/Script/ScriptHelpers.ts index 8defa638c..23de0c792 100644 --- a/src/Script/ScriptHelpers.js +++ b/src/Script/ScriptHelpers.ts @@ -1,13 +1,16 @@ import { CONSTANTS } from "../Constants"; import { Player } from "../Player"; import { AllServers } from "../Server/AllServers"; +import { BaseServer } from "../Server/BaseServer"; +import { Server } from "../Server/Server"; +import { RunningScript } from "../Script/RunningScript"; import { processSingleServerGrowth } from "../Server/ServerHelpers"; import { numeralWrapper } from "../ui/numeralFormat"; import { compareArrays } from "../../utils/helpers/compareArrays"; -export function scriptCalculateOfflineProduction(runningScriptObj) { +export function scriptCalculateOfflineProduction(runningScript: RunningScript) { //The Player object stores the last update time from when we were online const thisUpdate = new Date().getTime(); const lastUpdate = Player.lastUpdate; @@ -16,7 +19,7 @@ export function scriptCalculateOfflineProduction(runningScriptObj) { //Calculate the "confidence" rating of the script's true production. This is based //entirely off of time. We will arbitrarily say that if a script has been running for //4 hours (14400 sec) then we are completely confident in its ability - let confidence = runningScriptObj.onlineRunningTime / 14400; + let confidence = runningScript.onlineRunningTime / 14400; if (confidence >= 1) { confidence = 1; } @@ -24,9 +27,9 @@ export function scriptCalculateOfflineProduction(runningScriptObj) { //Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken] // Grow - for (const ip in runningScriptObj.dataMap) { - if (runningScriptObj.dataMap.hasOwnProperty(ip)) { - if (runningScriptObj.dataMap[ip][2] == 0 || runningScriptObj.dataMap[ip][2] == null) { + for (const ip in runningScript.dataMap) { + if (runningScript.dataMap.hasOwnProperty(ip)) { + if (runningScript.dataMap[ip][2] == 0 || runningScript.dataMap[ip][2] == null) { continue; } const serv = AllServers[ip]; @@ -34,12 +37,13 @@ export function scriptCalculateOfflineProduction(runningScriptObj) { continue; } const timesGrown = Math.round( - ((0.5 * runningScriptObj.dataMap[ip][2]) / runningScriptObj.onlineRunningTime) * timePassed, + ((0.5 * runningScript.dataMap[ip][2]) / runningScript.onlineRunningTime) * timePassed, ); - runningScriptObj.log(`Called on ${serv.hostname} ${timesGrown} times while offline`); - const host = AllServers[runningScriptObj.server]; + runningScript.log(`Called on ${serv.hostname} ${timesGrown} times while offline`); + const host = AllServers[runningScript.server]; + if (!(serv instanceof Server)) throw new Error("trying to grow a non-normal server"); const growth = processSingleServerGrowth(serv, timesGrown, Player, host.cpuCores); - runningScriptObj.log( + runningScript.log( `'${serv.hostname}' grown by ${numeralWrapper.format(growth * 100 - 100, "0.000000%")} while offline`, ); } @@ -47,28 +51,30 @@ export function scriptCalculateOfflineProduction(runningScriptObj) { // Offline EXP gain // A script's offline production will always be at most half of its online production. - const expGain = confidence * (runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime) * timePassed; + const expGain = confidence * (runningScript.onlineExpGained / runningScript.onlineRunningTime) * timePassed; Player.gainHackingExp(expGain); // Update script stats - runningScriptObj.offlineRunningTime += timePassed; - runningScriptObj.offlineExpGained += expGain; + runningScript.offlineRunningTime += timePassed; + runningScript.offlineExpGained += expGain; // Weaken - for (const ip in runningScriptObj.dataMap) { - if (runningScriptObj.dataMap.hasOwnProperty(ip)) { - if (runningScriptObj.dataMap[ip][3] == 0 || runningScriptObj.dataMap[ip][3] == null) { + for (const ip in runningScript.dataMap) { + if (runningScript.dataMap.hasOwnProperty(ip)) { + if (runningScript.dataMap[ip][3] == 0 || runningScript.dataMap[ip][3] == null) { continue; } const serv = AllServers[ip]; if (serv == null) { continue; } - const host = AllServers[runningScriptObj.server]; + + if (!(serv instanceof Server)) throw new Error("trying to weaken a non-normal server"); + const host = AllServers[runningScript.server]; const timesWeakened = Math.round( - ((0.5 * runningScriptObj.dataMap[ip][3]) / runningScriptObj.onlineRunningTime) * timePassed, + ((0.5 * runningScript.dataMap[ip][3]) / runningScript.onlineRunningTime) * timePassed, ); - runningScriptObj.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`); + runningScript.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`); const coreBonus = 1 + (host.cpuCores - 1) / 16; serv.weaken(CONSTANTS.ServerWeakenAmount * timesWeakened * coreBonus); } @@ -77,7 +83,7 @@ export function scriptCalculateOfflineProduction(runningScriptObj) { //Returns a RunningScript object matching the filename and arguments on the //designated server, and false otherwise -export function findRunningScript(filename, args, server) { +export function findRunningScript(filename: string, args: (string | number)[], server: BaseServer) { for (var i = 0; i < server.runningScripts.length; ++i) { if (server.runningScripts[i].filename === filename && compareArrays(server.runningScripts[i].args, args)) { return server.runningScripts[i]; @@ -88,7 +94,7 @@ export function findRunningScript(filename, args, server) { //Returns a RunningScript object matching the pid on the //designated server, and false otherwise -export function findRunningScriptByPid(pid, server) { +export function findRunningScriptByPid(pid: number, server: BaseServer) { for (var i = 0; i < server.runningScripts.length; ++i) { if (server.runningScripts[i].pid === pid) { return server.runningScripts[i]; diff --git a/src/Script/ScriptHelpersTS.ts b/src/Script/isScriptFilename.ts similarity index 82% rename from src/Script/ScriptHelpersTS.ts rename to src/Script/isScriptFilename.ts index 0b2214425..3506828e6 100644 --- a/src/Script/ScriptHelpersTS.ts +++ b/src/Script/isScriptFilename.ts @@ -1,4 +1,3 @@ -// Script helper functions export function isScriptFilename(f: string): boolean { return f.endsWith(".js") || f.endsWith(".script") || f.endsWith(".ns"); } diff --git a/src/ScriptEditor/ui/Root.tsx b/src/ScriptEditor/ui/Root.tsx index ec3fe83e9..25450dd60 100644 --- a/src/ScriptEditor/ui/Root.tsx +++ b/src/ScriptEditor/ui/Root.tsx @@ -11,7 +11,7 @@ import { isValidFilePath } from "../../Terminal/DirectoryHelpers"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { IRouter } from "../../ui/Router"; import { dialogBoxCreate } from "../../../utils/DialogBox"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; import { Script } from "../../Script/Script"; import { TextFile } from "../../TextFile"; import { calculateRamUsage } from "../../Script/RamCalculations"; diff --git a/src/Server/BaseServer.ts b/src/Server/BaseServer.ts index c5c33e9eb..42ed00386 100644 --- a/src/Server/BaseServer.ts +++ b/src/Server/BaseServer.ts @@ -9,7 +9,7 @@ import { isValidFilePath } from "../Terminal/DirectoryHelpers"; import { TextFile } from "../TextFile"; import { IReturnStatus } from "../types"; -import { isScriptFilename } from "../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../Script/isScriptFilename"; import { createRandomIp } from "../../utils/IPAddress"; import { compareArrays } from "../../utils/helpers/compareArrays"; diff --git a/src/Terminal/Terminal.ts b/src/Terminal/Terminal.ts index 45fa46b75..967fe72ce 100644 --- a/src/Terminal/Terminal.ts +++ b/src/Terminal/Terminal.ts @@ -10,7 +10,7 @@ import { TerminalEvents, TerminalClearEvents } from "./TerminalEvents"; import { TextFile } from "../TextFile"; import { Script } from "../Script/Script"; -import { isScriptFilename } from "../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../Script/isScriptFilename"; import { CONSTANTS } from "../Constants"; import { AllServers } from "../Server/AllServers"; diff --git a/src/Terminal/commands/check.ts b/src/Terminal/commands/check.ts index ad4d155a9..c2b98c644 100644 --- a/src/Terminal/commands/check.ts +++ b/src/Terminal/commands/check.ts @@ -3,7 +3,7 @@ import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; import { findRunningScript } from "../../Script/ScriptHelpers"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; export function check( terminal: ITerminal, diff --git a/src/Terminal/commands/download.ts b/src/Terminal/commands/download.ts index cad18d762..c01e41b22 100644 --- a/src/Terminal/commands/download.ts +++ b/src/Terminal/commands/download.ts @@ -2,7 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; import FileSaver from "file-saver"; import JSZip from "jszip"; diff --git a/src/Terminal/commands/mv.ts b/src/Terminal/commands/mv.ts index f62f5579f..98d07f754 100644 --- a/src/Terminal/commands/mv.ts +++ b/src/Terminal/commands/mv.ts @@ -2,7 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; import { TextFile } from "../../TextFile"; import { Script } from "../../Script/Script"; diff --git a/src/Terminal/commands/nano.ts b/src/Terminal/commands/nano.ts index b028cf9ac..242c83963 100644 --- a/src/Terminal/commands/nano.ts +++ b/src/Terminal/commands/nano.ts @@ -2,7 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; export function nano( terminal: ITerminal, diff --git a/src/Terminal/commands/run.ts b/src/Terminal/commands/run.ts index cacc9b864..70652ad86 100644 --- a/src/Terminal/commands/run.ts +++ b/src/Terminal/commands/run.ts @@ -2,7 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; import { runScript } from "./runScript"; import { runProgram } from "./runProgram"; diff --git a/src/Terminal/commands/scp.ts b/src/Terminal/commands/scp.ts index 78d5a559b..0c01f517b 100644 --- a/src/Terminal/commands/scp.ts +++ b/src/Terminal/commands/scp.ts @@ -4,7 +4,7 @@ import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; import { Message } from "../../Message/Message"; import { getServer } from "../../Server/ServerHelpers"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; export function scp( terminal: ITerminal, diff --git a/src/Terminal/commands/tail.ts b/src/Terminal/commands/tail.ts index ef17463cf..0506ca58b 100644 --- a/src/Terminal/commands/tail.ts +++ b/src/Terminal/commands/tail.ts @@ -4,8 +4,8 @@ import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; import { logBoxCreate } from "../../../utils/LogBox"; import { findRunningScriptByPid } from "../../Script/ScriptHelpers"; +import { isScriptFilename } from "../../Script/isScriptFilename"; import { compareArrays } from "../../../utils/helpers/compareArrays"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; export function tail( terminal: ITerminal, diff --git a/src/Terminal/commands/wget.ts b/src/Terminal/commands/wget.ts index 066416bee..9d951770a 100644 --- a/src/Terminal/commands/wget.ts +++ b/src/Terminal/commands/wget.ts @@ -2,7 +2,7 @@ import { ITerminal } from "../ITerminal"; import { IRouter } from "../../ui/Router"; import { IPlayer } from "../../PersonObjects/IPlayer"; import { BaseServer } from "../../Server/BaseServer"; -import { isScriptFilename } from "../../Script/ScriptHelpersTS"; +import { isScriptFilename } from "../../Script/isScriptFilename"; export function wget( terminal: ITerminal,