[refactor] Moved 'printArray' to 'arrayToString' TS file

This commit is contained in:
Steven Evans 2018-07-05 14:12:20 -04:00
parent d20516b03c
commit 1a5208f78f
8 changed files with 25 additions and 24 deletions

@ -5,9 +5,9 @@ import {workerScripts,
import {Player} from "./Player"; import {Player} from "./Player";
import {getServer} from "./Server"; import {getServer} from "./Server";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {printArray, import {createAccordionElement, removeElement,
createAccordionElement, removeElement,
removeChildrenFromElement} from "../utils/HelperFunctions"; removeChildrenFromElement} from "../utils/HelperFunctions";
import {arrayToString} from "../utils/helpers/arrayToString";
import {createElement} from "../utils/uiHelpers/createElement"; import {createElement} from "../utils/uiHelpers/createElement";
import {exceptionAlert} from "../utils/helpers/exceptionAlert"; import {exceptionAlert} from "../utils/helpers/exceptionAlert";
import {logBoxCreate} from "../utils/LogBox"; import {logBoxCreate} from "../utils/LogBox";
@ -112,7 +112,7 @@ function addActiveScriptsItem(workerscript) {
//Threads, args, kill/log button //Threads, args, kill/log button
panel.appendChild(createElement("p", { panel.appendChild(createElement("p", {
innerHTML: "Threads: " + workerscript.scriptRef.threads + "<br>" + innerHTML: "Threads: " + workerscript.scriptRef.threads + "<br>" +
"Args: " + printArray(workerscript.args) "Args: " + arrayToString(workerscript.args)
})); }));
var panelText = createElement("p", { var panelText = createElement("p", {
innerText:"Loading...", fontSize:"14px", innerText:"Loading...", fontSize:"14px",

@ -9,7 +9,7 @@ import {Script, findRunningScript,
RunningScript} from "./Script"; RunningScript} from "./Script";
import {parse, Node} from "../utils/acorn"; import {parse, Node} from "../utils/acorn";
import {printArray} from "../utils/HelperFunctions"; import {arrayToString} from "../utils/helpers/arrayToString";
import {isValidIPAddress} from "../utils/helpers/isValidIPAddress"; import {isValidIPAddress} from "../utils/helpers/isValidIPAddress";
import {isString} from "../utils/helpers/isString"; import {isString} from "../utils/helpers/isString";
@ -872,7 +872,7 @@ function runScriptFromScript(server, scriptname, args, workerScript, threads=1)
} else { } else {
//Able to run script //Able to run script
if(workerScript.disableLogs.ALL == null && workerScript.disableLogs.exec == null && workerScript.disableLogs.run == null && workerScript.disableLogs.spawn == null) { if(workerScript.disableLogs.ALL == null && workerScript.disableLogs.exec == null && workerScript.disableLogs.run == null && workerScript.disableLogs.spawn == null) {
workerScript.scriptRef.log("Running script: " + scriptname + " on " + server.hostname + " with " + threads + " threads and args: " + printArray(args) + ". May take a few seconds to start up..."); workerScript.scriptRef.log("Running script: " + scriptname + " on " + server.hostname + " with " + threads + " threads and args: " + arrayToString(args) + ". May take a few seconds to start up...");
} }
var runningScriptObj = new RunningScript(script, args); var runningScriptObj = new RunningScript(script, args);
runningScriptObj.threads = threads; runningScriptObj.threads = threads;

@ -48,7 +48,8 @@ import {NetscriptPort} from "./NetscriptPort";
import Decimal from "decimal.js"; import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {printArray, powerOfTwo} from "../utils/HelperFunctions"; import {powerOfTwo} from "../utils/HelperFunctions";
import {arrayToString} from "../utils/helpers/arrayToString";
import {createRandomIp} from "../utils/IPAddress"; import {createRandomIp} from "../utils/IPAddress";
import {formatNumber, isHTML} from "../utils/StringHelperFunctions"; import {formatNumber, isHTML} from "../utils/StringHelperFunctions";
import {isString} from "../utils/helpers/isString"; import {isString} from "../utils/helpers/isString";
@ -686,18 +687,18 @@ function NetscriptFunctions(workerScript) {
} }
var runningScriptObj = findRunningScript(filename, argsForKillTarget, server); var runningScriptObj = findRunningScript(filename, argsForKillTarget, server);
if (runningScriptObj == null) { if (runningScriptObj == null) {
workerScript.scriptRef.log("kill() failed. No such script "+ filename + " on " + server.hostname + " with args: " + printArray(argsForKillTarget)); workerScript.scriptRef.log("kill() failed. No such script "+ filename + " on " + server.hostname + " with args: " + arrayToString(argsForKillTarget));
return false; return false;
} }
var res = killWorkerScript(runningScriptObj, server.ip); var res = killWorkerScript(runningScriptObj, server.ip);
if (res) { if (res) {
if (workerScript.disableLogs.ALL == null && workerScript.disableLogs.kill == null) { if (workerScript.disableLogs.ALL == null && workerScript.disableLogs.kill == null) {
workerScript.scriptRef.log("Killing " + filename + " on " + server.hostname + " with args: " + printArray(argsForKillTarget) + ". May take up to a few minutes for the scripts to die..."); workerScript.scriptRef.log("Killing " + filename + " on " + server.hostname + " with args: " + arrayToString(argsForKillTarget) + ". May take up to a few minutes for the scripts to die...");
} }
return true; return true;
} else { } else {
if (workerScript.disableLogs.ALL == null && workerScript.disableLogs.kill == null) { if (workerScript.disableLogs.ALL == null && workerScript.disableLogs.kill == null) {
workerScript.scriptRef.log("kill() failed. No such script "+ filename + " on " + server.hostname + " with args: " + printArray(argsForKillTarget)); workerScript.scriptRef.log("kill() failed. No such script "+ filename + " on " + server.hostname + " with args: " + arrayToString(argsForKillTarget));
} }
return false; return false;
} }
@ -1978,7 +1979,7 @@ function NetscriptFunctions(workerScript) {
} }
var runningScriptObj = findRunningScript(scriptname, argsForScript, server); var runningScriptObj = findRunningScript(scriptname, argsForScript, server);
if (runningScriptObj == null) { if (runningScriptObj == null) {
workerScript.scriptRef.log("getScriptIncome() failed. No such script "+ scriptname + " on " + server.hostname + " with args: " + printArray(argsForScript)); workerScript.scriptRef.log("getScriptIncome() failed. No such script "+ scriptname + " on " + server.hostname + " with args: " + arrayToString(argsForScript));
return -1; return -1;
} }
return runningScriptObj.onlineMoneyMade / runningScriptObj.onlineRunningTime; return runningScriptObj.onlineMoneyMade / runningScriptObj.onlineRunningTime;
@ -2008,7 +2009,7 @@ function NetscriptFunctions(workerScript) {
} }
var runningScriptObj = findRunningScript(scriptname, argsForScript, server); var runningScriptObj = findRunningScript(scriptname, argsForScript, server);
if (runningScriptObj == null) { if (runningScriptObj == null) {
workerScript.scriptRef.log("getScriptExpGain() failed. No such script "+ scriptname + " on " + server.hostname + " with args: " + printArray(argsForScript)); workerScript.scriptRef.log("getScriptExpGain() failed. No such script "+ scriptname + " on " + server.hostname + " with args: " + arrayToString(argsForScript));
return -1; return -1;
} }
return runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime; return runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime;

@ -16,7 +16,8 @@ import {Settings} from "./Settings";
import {parse} from "../utils/acorn"; import {parse} from "../utils/acorn";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {compareArrays, printArray} from "../utils/HelperFunctions"; import {compareArrays} from "../utils/HelperFunctions";
import {arrayToString} from "../utils/helpers/arrayToString";
import {roundToTwo} from "../utils/helpers/roundToTwo"; import {roundToTwo} from "../utils/helpers/roundToTwo";
import {isString} from "../utils/StringHelperFunctions"; import {isString} from "../utils/StringHelperFunctions";
@ -319,7 +320,7 @@ function runScriptsLoop() {
dialogBoxCreate("Script runtime error: <br>Server Ip: " + serverIp + dialogBoxCreate("Script runtime error: <br>Server Ip: " + serverIp +
"<br>Script name: " + scriptName + "<br>Script name: " + scriptName +
"<br>Args:" + printArray(w.args) + "<br>" + errorMsg); "<br>Args:" + arrayToString(w.args) + "<br>" + errorMsg);
w.scriptRef.log("Script crashed with runtime error"); w.scriptRef.log("Script crashed with runtime error");
} else { } else {
w.scriptRef.log("Script killed"); w.scriptRef.log("Script killed");
@ -379,7 +380,7 @@ function addWorkerScript(runningScriptObj, server) {
var ramAvailable = server.maxRam - server.ramUsed; var ramAvailable = server.maxRam - server.ramUsed;
if (ramUsage > ramAvailable) { if (ramUsage > ramAvailable) {
dialogBoxCreate("Not enough RAM to run script " + runningScriptObj.filename + " with args " + dialogBoxCreate("Not enough RAM to run script " + runningScriptObj.filename + " with args " +
printArray(runningScriptObj.args) + ". This likely occurred because you re-loaded " + arrayToString(runningScriptObj.args) + ". This likely occurred because you re-loaded " +
"the game and the script's RAM usage increased (either because of an update to the game or " + "the game and the script's RAM usage increased (either because of an update to the game or " +
"your changes to the script.)"); "your changes to the script.)");
return; return;

@ -37,7 +37,7 @@ import {containsAllStrings, longestCommonStart,
formatNumber} from "../utils/StringHelperFunctions"; formatNumber} from "../utils/StringHelperFunctions";
import {addOffset} from "../utils/helpers/addOffset"; import {addOffset} from "../utils/helpers/addOffset";
import {isString} from "../utils/helpers/isString"; import {isString} from "../utils/helpers/isString";
import {printArray} from "../utils/HelperFunctions"; import {arrayToString} from "../utils/helpers/arrayToString";
import {logBoxCreate} from "../utils/LogBox"; import {logBoxCreate} from "../utils/LogBox";
import {yesNoBoxCreate, import {yesNoBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetYesButton,
@ -2035,7 +2035,7 @@ let Terminal = {
return; return;
} else { } else {
//Able to run script //Able to run script
post("Running script with " + numThreads + " thread(s) and args: " + printArray(args) + "."); post("Running script with " + numThreads + " thread(s) and args: " + arrayToString(args) + ".");
post("May take a few seconds to start up the process..."); post("May take a few seconds to start up the process...");
var runningScriptObj = new RunningScript(script, args); var runningScriptObj = new RunningScript(script, args);
runningScriptObj.threads = numThreads; runningScriptObj.threads = numThreads;

@ -110,10 +110,6 @@ function compareArrays(a1, a2) {
return true; return true;
} }
function printArray(a) {
return "[" + a.join(", ") + "]";
}
//Returns bool indicating whether or not its a power of 2 //Returns bool indicating whether or not its a power of 2
function powerOfTwo(n) { function powerOfTwo(n) {
if (isNaN(n)) {return false;} if (isNaN(n)) {return false;}
@ -124,7 +120,6 @@ export {sizeOfObject,
clearObject, clearObject,
clearEventListeners, clearEventListeners,
compareArrays, compareArrays,
printArray,
powerOfTwo, powerOfTwo,
clearEventListenersEl, clearEventListenersEl,
removeElement, removeElement,

@ -1,5 +1,6 @@
import {killWorkerScript} from "../src/NetscriptWorker"; import {killWorkerScript} from "../src/NetscriptWorker";
import {printArray, clearEventListeners} from "./HelperFunctions"; import {clearEventListeners} from "./HelperFunctions";
import {arrayToString} from "./helpers/arrayToString";
$(document).keydown(function(event) { $(document).keydown(function(event) {
if (logBoxOpened && event.keyCode == 27) { if (logBoxOpened && event.keyCode == 27) {
@ -47,7 +48,7 @@ function logBoxCreate(script) {
document.getElementById('log-box-kill-script').style.display = "inline-block"; document.getElementById('log-box-kill-script').style.display = "inline-block";
logBoxOpen(); logBoxOpen();
document.getElementById("log-box-text-header").innerHTML = document.getElementById("log-box-text-header").innerHTML =
logBoxCurrentScript.filename + " " + printArray(logBoxCurrentScript.args) + ":<br><br>"; logBoxCurrentScript.filename + " " + arrayToString(logBoxCurrentScript.args) + ":<br><br>";
logBoxCurrentScript.logUpd = true; logBoxCurrentScript.logUpd = true;
logBoxUpdateText(); logBoxUpdateText();
} }

@ -0,0 +1,3 @@
export function arrayToString<T>(a: T[]) {
return `[${a.join(", ")}]`;
}