Merge pull request #352 from kopelli/helper-functions

Helper functions
This commit is contained in:
danielyxie 2018-07-09 15:37:23 -05:00 committed by GitHub
commit 8080e28b64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
55 changed files with 798 additions and 476 deletions

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

@ -12,11 +12,13 @@ import {Script, RunningScript} from "./Script";
import {Server} from "./Server";
import {SourceFiles} from "./SourceFile";
import {dialogBoxCreate} from "../utils/DialogBox";
import {createElement, createAccordionElement,
removeChildrenFromElement, clearObject} from "../utils/HelperFunctions";
import {createAccordionElement} from "../utils/uiHelpers/createAccordionElement";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import {isString} from "../utils/StringHelperFunctions";
import {clearObject} from "../utils/helpers/clearObject";
import {createElement} from "../utils/uiHelpers/createElement";
import {isString} from "../utils/helpers/isString";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
//Augmentations
function Augmentation(params) {

@ -9,16 +9,22 @@ import {Player} from "./Player";
import {hackWorldDaemon, redPillFlag} from "./RedPill";
import {KEY} from "./Terminal";
import {createProgressBarText} from "../utils/helpers/createProgressBarText";
import {dialogBoxCreate} from "../utils/DialogBox";
import {getRandomInt, addOffset, clearObject,
createElement, removeChildrenFromElement,
exceptionAlert, createPopup, appendLineBreaks,
removeElementById, removeElement,
createProgressBarText} from "../utils/HelperFunctions";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import numeral from "numeral/min/numeral.min";
import {addOffset} from "../utils/helpers/addOffset";
import {appendLineBreaks} from "../utils/uiHelpers/appendLineBreaks";
import {clearObject} from "../utils/helpers/clearObject";
import {createElement} from "../utils/uiHelpers/createElement";
import {createPopup} from "../utils/uiHelpers/createPopup";
import {exceptionAlert} from "../utils/helpers/exceptionAlert";
import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {removeElement} from "../utils/uiHelpers/removeElement";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"];

@ -1,8 +1,9 @@
import {Engine} from "./engine";
import {removeChildrenFromElement,
createElement, exceptionAlert} from "../utils/HelperFunctions";
import {isString} from "../utils/StringHelperFunctions";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
import {createElement} from "../utils/uiHelpers/createElement";
import {exceptionAlert} from "../utils/helpers/exceptionAlert";
import {isString} from "../utils/helpers/isString";
var cinematicTextFlag = false;

@ -7,14 +7,17 @@ import {Player} from "./Player";
import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox";
import {getRandomInt, removeElementById,
createElement, createAccordionElement,
removeChildrenFromElement, createPopup,
clearSelector} from "../utils/HelperFunctions";
import {clearSelector} from "../utils/uiHelpers/clearSelector";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import numeral from "numeral/min/numeral.min";
import {formatNumber, isString, generateRandomString} from "../utils/StringHelperFunctions";
import {createElement} from "../utils/uiHelpers/createElement";
import {createPopup} from "../utils/uiHelpers/createPopup";
import {formatNumber, generateRandomString} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {isString} from "../utils/helpers/isString";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
@ -4921,3 +4924,4 @@ Corporation.fromJSON = function(value) {
Reviver.constructors.Corporation = Corporation;
export {Corporation};

@ -1,6 +1,6 @@
import {CONSTANTS} from "./Constants";
import {Player} from "./Player";
import {createElement} from "../utils/HelperFunctions";
import {createElement} from "../utils/uiHelpers/createElement";
// a function that returns a requirement for a program that requires only that
// the player has at least the given skill level.

@ -3,7 +3,7 @@ import {Player} from "./Player";
import {SpecialServerIps} from "./SpecialServerIps";
import {post} from "./Terminal";
import {isValidIPAddress} from "../utils/IPAddress";
import {isValidIPAddress} from "../utils/helpers/isValidIPAddress";
import {formatNumber} from "../utils/StringHelperFunctions";

@ -11,8 +11,8 @@ import {Settings} from "./Settings";
import {dialogBoxCreate} from "../utils/DialogBox";
import {factionInvitationBoxCreate} from "../utils/FactionInvitationBox";
import {clearEventListeners, createElement,
removeChildrenFromElement} from "../utils/HelperFunctions";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
import {createElement} from "../utils/uiHelpers/createElement";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import numeral from "numeral/min/numeral.min";

@ -6,12 +6,15 @@ import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import {getRandomInt, createElement,
removeChildrenFromElement,
createAccordionElement, createPopup,
removeElementById, removeElement} from "../utils/HelperFunctions";
import {createAccordionElement} from "../utils/uiHelpers/createAccordionElement";
import {createElement} from "../utils/uiHelpers/createElement";
import {createPopup} from "../utils/uiHelpers/createPopup";
import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
import {removeElement} from "../utils/uiHelpers/removeElement";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -5,11 +5,12 @@ import {iTutorialSteps, iTutorialNextStep,
iTutorialIsRunning, currITutorialStep} from "./InteractiveTutorial";
import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners, createElement,
getElementById} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import {createElement} from "../utils/uiHelpers/createElement";
import {formatNumber} from "../utils/StringHelperFunctions";
import {getElementById} from "../utils/uiHelpers/getElementById";
/**
* Overwrites the inner text of the specified HTML element if it is different from what currently exists.

@ -3,7 +3,8 @@ import {CONSTANTS} from "./Constants";
import {Engine} from "./engine";
import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners, getRandomInt} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {infiltrationBoxCreate} from "../utils/InfiltrationBox";
import {formatNumber} from "../utils/StringHelperFunctions";

@ -1,7 +1,7 @@
import {Engine} from "./engine";
import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
/* InteractiveTutorial.js */
let iTutorialSteps = {

@ -15,7 +15,7 @@ import {Settings} from "./Settings";
import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps";
import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners, createElement} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {createRandomIp} from "../utils/IPAddress";
import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions";

@ -3,10 +3,11 @@ import {Engine} from "./engine";
import {displayFactionContent} from "./Faction";
import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox";
import {addOffset, getRandomInt,
clearEventListenersEl,
clearEventListeners} from "../utils/HelperFunctions";
import {formatNumber, isString} from "../utils/StringHelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {addOffset} from "../utils/helpers/addOffset";
import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {isString} from "../utils/helpers/isString";
import jsplumb from 'jsplumb'
let inMission = false; //Flag to denote whether a mission is running

@ -9,9 +9,9 @@ import {Script, findRunningScript,
RunningScript} from "./Script";
import {parse, Node} from "../utils/acorn";
import {printArray} from "../utils/HelperFunctions";
import {isValidIPAddress} from "../utils/IPAddress";
import {isString} from "../utils/StringHelperFunctions";
import {arrayToString} from "../utils/helpers/arrayToString";
import {isValidIPAddress} from "../utils/helpers/isValidIPAddress";
import {isString} from "../utils/helpers/isString";
var Promise = require("bluebird");
@ -872,7 +872,7 @@ function runScriptFromScript(server, scriptname, args, workerScript, threads=1)
} else {
//Able to run script
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);
runningScriptObj.threads = threads;

@ -48,9 +48,11 @@ import {NetscriptPort} from "./NetscriptPort";
import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox";
import {printArray, powerOfTwo} from "../utils/HelperFunctions";
import {isPowerOfTwo} from "../utils/helpers/isPowerOfTwo";
import {arrayToString} from "../utils/helpers/arrayToString";
import {createRandomIp} from "../utils/IPAddress";
import {formatNumber, isString, isHTML} from "../utils/StringHelperFunctions";
import {formatNumber, isHTML} from "../utils/StringHelperFunctions";
import {isString} from "../utils/helpers/isString";
import {yesNoBoxClose, yesNoBoxGetYesButton,
yesNoBoxGetNoButton, yesNoBoxCreate,
yesNoBoxOpen} from "../utils/YesNoBox";
@ -685,18 +687,18 @@ function NetscriptFunctions(workerScript) {
}
var runningScriptObj = findRunningScript(filename, argsForKillTarget, server);
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;
}
var res = killWorkerScript(runningScriptObj, server.ip);
if (res) {
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;
} else {
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;
}
@ -1546,7 +1548,7 @@ function NetscriptFunctions(workerScript) {
}
ram = Math.round(ram);
if (isNaN(ram) || !powerOfTwo(ram)) {
if (isNaN(ram) || !isPowerOfTwo(ram)) {
workerScript.scriptRef.log("ERROR: purchaseServer() failed due to invalid ram argument. Must be numeric and a power of 2");
return "";
}
@ -1977,7 +1979,7 @@ function NetscriptFunctions(workerScript) {
}
var runningScriptObj = findRunningScript(scriptname, argsForScript, server);
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 runningScriptObj.onlineMoneyMade / runningScriptObj.onlineRunningTime;
@ -2007,7 +2009,7 @@ function NetscriptFunctions(workerScript) {
}
var runningScriptObj = findRunningScript(scriptname, argsForScript, server);
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 runningScriptObj.onlineExpGained / runningScriptObj.onlineRunningTime;

@ -16,8 +16,9 @@ import {Settings} from "./Settings";
import {parse} from "../utils/acorn";
import {dialogBoxCreate} from "../utils/DialogBox";
import {compareArrays, printArray,
roundToTwo} from "../utils/HelperFunctions";
import {compareArrays} from "../utils/helpers/compareArrays";
import {arrayToString} from "../utils/helpers/arrayToString";
import {roundToTwo} from "../utils/helpers/roundToTwo";
import {isString} from "../utils/StringHelperFunctions";
function WorkerScript(runningScriptObj) {
@ -319,7 +320,7 @@ function runScriptsLoop() {
dialogBoxCreate("Script runtime error: <br>Server Ip: " + serverIp +
"<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");
} else {
w.scriptRef.log("Script killed");
@ -379,7 +380,7 @@ function addWorkerScript(runningScriptObj, server) {
var ramAvailable = server.maxRam - server.ramUsed;
if (ramUsage > ramAvailable) {
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 " +
"your changes to the script.)");
return;

@ -21,7 +21,7 @@ import {SourceFiles, applySourceFile} from "./SourceFile";
import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {createRandomIp} from "../utils/IPAddress";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";

@ -30,8 +30,10 @@ import {initStockMarket, initSymbolToStockMap,
import {Terminal, postNetburnerText} from "./Terminal";
import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox";
import {createPopup, createElement,
removeElementById, exceptionAlert} from "../utils/HelperFunctions";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import {createElement} from "../utils/uiHelpers/createElement";
import {createPopup} from "../utils/uiHelpers/createPopup";
import {exceptionAlert} from "../utils/helpers/exceptionAlert";
import {yesNoBoxCreate, yesNoBoxGetYesButton,
yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox";

@ -6,9 +6,9 @@ import {SourceFiles, SourceFile,
PlayerOwnedSourceFile} from "./SourceFile";
import {Terminal} from "./Terminal";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners,
removeChildrenFromElement} from "../utils/HelperFunctions";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
import {yesNoBoxCreate, yesNoBoxGetYesButton,
yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox";

@ -19,11 +19,13 @@ import {loadSpecialServerIps, SpecialServerIps} from "./SpecialServerIps";
import {loadStockMarket, StockMarket} from "./StockMarket";
import {dialogBoxCreate} from "../utils/DialogBox";
import {gameOptionsBoxClose} from "../utils/GameOptions";
import {clearEventListeners, createElement,
createPopup, removeElementById} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import {createElement} from "../utils/uiHelpers/createElement";
import {createPopup} from "../utils/uiHelpers/createPopup";
import {formatNumber} from "../utils/StringHelperFunctions";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import Decimal from "decimal.js";

@ -36,10 +36,10 @@ import {parse, Node} from "../utils/acorn";
import {dialogBoxCreate} from "../utils/DialogBox";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import {compareArrays, createElement,
roundToTwo} from "../utils/HelperFunctions";
import {formatNumber, numOccurrences,
numNetscriptOperators} from "../utils/StringHelperFunctions";
import {compareArrays} from "../utils/helpers/compareArrays";
import {createElement} from "../utils/uiHelpers/createElement";
import {formatNumber} from "../utils/StringHelperFunctions";
import {roundToTwo} from "../utils/helpers/roundToTwo";
var keybindings = {
ace: null,

@ -4,10 +4,11 @@ import {Programs} from "./CreateProgram";
import {Player} from "./Player";
import {RunningScript, Script} from "./Script";
import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps";
import {getRandomInt} from "../utils/HelperFunctions";
import {createRandomIp, isValidIPAddress, ipExists} from "../utils/IPAddress";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {createRandomIp, ipExists} from "../utils/IPAddress";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import {isValidIPAddress} from "../utils/helpers/isValidIPAddress";
function Server(params={ip:createRandomIp(), hostname:""}) {
/* Properties */

@ -6,13 +6,13 @@ import {WorkerScript} from "./NetscriptWorker";
import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox";
import {clearEventListeners, getRandomInt,
removeElementById,
clearEventListenersEl} from "../utils/HelperFunctions";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver";
import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {removeElementById} from "../utils/uiHelpers/removeElementById";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -34,8 +34,10 @@ import {SpecialServerIps,
import {TextFile, getTextFile} from "./TextFile";
import {containsAllStrings, longestCommonStart,
formatNumber, isString} from "../utils/StringHelperFunctions";
import {addOffset, printArray} from "../utils/HelperFunctions";
formatNumber} from "../utils/StringHelperFunctions";
import {addOffset} from "../utils/helpers/addOffset";
import {isString} from "../utils/helpers/isString";
import {arrayToString} from "../utils/helpers/arrayToString";
import {logBoxCreate} from "../utils/LogBox";
import {yesNoBoxCreate,
yesNoBoxGetYesButton,
@ -2033,7 +2035,7 @@ let Terminal = {
return;
} else {
//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...");
var runningScriptObj = new RunningScript(script, args);
runningScriptObj.threads = numThreads;

@ -1,8 +1,9 @@
import {dialogBoxCreate} from "../utils/DialogBox";
import {gameOptionsBoxOpen, gameOptionsBoxClose}from "../utils/GameOptions";
import {clearEventListeners, createElement,
removeChildrenFromElement,
exceptionAlert} from "../utils/HelperFunctions";
import {removeChildrenFromElement} from "../utils/uiHelpers/removeChildrenFromElement";
import {clearEventListeners} from "../utils/uiHelpers/clearEventListeners";
import {createElement} from "../utils/uiHelpers/createElement";
import {exceptionAlert} from "../utils/helpers/exceptionAlert";
import numeral from "numeral/min/numeral.min";
import {formatNumber,
convertTimeMsToTimeElapsedString,

@ -1,7 +1,7 @@
import {Faction, joinFaction} from "../src/Faction";
import {Engine} from "../src/engine";
import {Player} from "../src/Player";
import {clearEventListeners} from "./HelperFunctions";
import {clearEventListeners} from "./uiHelpers/clearEventListeners";
/* Faction Invitation Pop-up box */
function factionInvitationBoxClose() {

@ -1,298 +0,0 @@
//General helper functions
import {isString} from "./StringHelperFunctions";
import {dialogBoxCreate} from "./DialogBox";
//Returns the size (number of keys) of an object
function sizeOfObject(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
}
function clearObject(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
delete obj[key];
}
}
}
//Adds a random offset to a number within a certain percentage
//e.g. addOffset(100, 5) will return anything from 95 to 105.
//The percentage argument must be between 0 and 100;
function addOffset(n, percentage) {
if (percentage < 0 || percentage > 100) {return n;}
var offset = n * (percentage / 100);
return n + ((Math.random() * (2 * offset)) - offset);
}
//Given an element by its Id(usually an 'a' element), removes all event listeners
//from that element by cloning and replacing. Then returns the new cloned element
function clearEventListeners(elemId) {
var elem = document.getElementById(elemId);
if (elem == null) {console.log("ERR: Could not find element for: " + elemId); return null;}
var newElem = elem.cloneNode(true);
elem.parentNode.replaceChild(newElem, elem);
return newElem;
}
//Same as clearEventListeners except it takes a DOM element object rather than an ID
function clearEventListenersEl(el) {
if (el == null) {console.log("ERR: element passed into clearEventListenersEl is null"); return null;}
var newElem = el.cloneNode(true);
el.parentNode.replaceChild(newElem, el);
return newElem;
}
//Given its id, this function removes an element AND its children
function removeElementById(id) {
var elem = document.getElementById(id);
if (elem == null) {return;}
while(elem.firstChild) {elem.removeChild(elem.firstChild);}
elem.parentNode.removeChild(elem);
}
function removeElement(elem) {
if (elem == null || !(elem instanceof Element)) {return;}
while(elem.firstChild) {elem.removeChild(elem.firstChild);}
elem.parentNode.removeChild(elem);
}
function removeChildrenFromElement(el) {
if (isString(el)) {
el = document.getElementById(el);
}
if (el == null) {return;}
if (el instanceof Element) {
while(el.firstChild) {
el.removeChild(el.firstChild);
}
}
}
/**
* Returns a reference to the first object with the specified value of the ID or NAME attribute, throwing an error if it is unable to find it.
* @param {string} elementId The HTML ID to retrieve the element by.
* @returns {HTMLElement} The single element.
* @throws {Error} When the 'idString' cannot be found.
*/
function getElementById(elementId) {
var el = document.getElementById(elementId);
if (el == null) {
throw new Error("Unable to find element with id '" + elementId + "'");
}
return el;
}
function createElement(type, params={}) {
var el = document.createElement(type);
if (params.id) {el.id = params.id;}
if (params.class) {el.className = params.class;}
if (params.name) {el.name = params.name;}
if (params.innerHTML) {el.innerHTML = params.innerHTML;}
if (params.innerText) {el.innerText = params.innerText;}
if (params.value) {el.value = params.value;}
if (params.text) {el.text = params.text;}
if (params.display) {el.style.display = params.display;}
if (params.visibility) {el.style.visibility = params.visibility;}
if (params.margin) {el.style.margin = params.margin;}
if (params.marginLeft) {el.style.marginLeft = params.marginLeft;}
if (params.marginTop) {el.style.marginTop = params.marginTop;}
if (params.padding) {el.style.padding = params.padding;}
if (params.color) {el.style.color = params.color;}
if (params.border) {el.style.border = params.border;}
if (params.float) {el.style.cssFloat = params.float;}
if (params.fontSize) {el.style.fontSize = params.fontSize;}
if (params.whiteSpace) {el.style.whiteSpace = params.whiteSpace;}
if (params.width) {el.style.width = params.width;}
if (params.backgroundColor) {
el.style.backgroundColor = params.backgroundColor
}
if (params.position) {el.style.position = params.position;}
if (params.type) {el.type = params.type;}
if (params.checked) {el.checked = params.checked;}
if (params.for) {el.htmlFor = params.for;}
if (params.pattern) {el.pattern = params.pattern;}
if (params.maxLength) {el.maxLength = params.maxLength;}
if (params.placeholder) {el.placeholder = params.placeholder;}
if (params.tooltip && params.tooltip !== "") {
el.className += " tooltip";
el.appendChild(createElement("span", {
class:"tooltiptext",
innerHTML:params.tooltip
}));
} else if (params.tooltipleft) {
el.className += " tooltip";
el.appendChild(createElement("span", {
class:"tooltiptextleft",
innerHTML:params.tooltipleft
}));
}
if (params.href) {el.href = params.href;}
if (params.target) {el.target = params.target;}
if (params.tabIndex) {el.tabIndex = params.tabIndex;}
if (params.clickListener) {
el.addEventListener("click", params.clickListener);
}
if (params.inputListener) {
el.addEventListener("input", params.inputListener);
}
if (params.changeListener) {
el.addEventListener("change", params.changeListener);
}
if (params.onkeyup) {
el.addEventListener("keyup", params.onkeyup);
}
if (params.onfocus) {
el.addEventListener("focus", params.onfocus);
}
return el;
}
function createPopup(id, elems) {
var container = createElement("div", {
class:"popup-box-container",
id:id,
display:"block"
}),
content = createElement("div", {
class:"popup-box-content",
id:id + "-content",
});
for (var i = 0; i < elems.length; ++i) {
content.appendChild(elems[i]);
}
container.appendChild(content);
document.getElementById("entire-game-container").appendChild(container);
return container;
}
//Creates both the header and panel element of an accordion and sets the click handler
function createAccordionElement(params) {
var li = document.createElement("li"),
hdr = document.createElement("button"),
panel = document.createElement("div");
hdr.classList.add("accordion-header");
panel.classList.add("accordion-panel");
if (params.id) {
hdr.id = params.id + "-hdr";
panel.id = params.id + "-panel";
}
if (params.hdrText) {hdr.innerHTML = params.hdrText;}
if (params.panelText) {panel.innerHTML = params.panelText;}
li.appendChild(hdr);
li.appendChild(panel);
//Click handler
hdr.onclick = function() {
this.classList.toggle("active");
var tmpPanel = this.nextElementSibling;
if (tmpPanel.style.display === "block") {
tmpPanel.style.display = "none";
} else {
tmpPanel.style.display = "block";
}
}
return [li, hdr, panel];
}
//Appends n line breaks (as children) to the Element el
function appendLineBreaks(el, n) {
for (var i = 0; i < n; ++i) {
el.appendChild(createElement("br"));
}
}
function clearSelector(selector) {
for (var i = selector.options.length - 1; i >= 0; --i) {
selector.remove(i);
}
}
function getRandomInt(min, max) {
if (min > max) {return getRandomInt(max, min);}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//Returns true if all elements are equal, and false otherwise
//Assumes both arguments are arrays and that there are no nested arrays
function compareArrays(a1, a2) {
if (a1.length != a2.length) {
return false;
}
for (var i = 0; i < a1.length; ++i) {
if (a1[i] != a2[i]) {return false;}
}
return true;
}
function printArray(a) {
return "[" + a.join(", ") + "]";
}
//Returns bool indicating whether or not its a power of 2
function powerOfTwo(n) {
if (isNaN(n)) {return false;}
return n && (n & (n-1)) === 0;
}
//Rounds a number to two decimal places
function roundToTwo(n) {
return +(Math.round(n + "e+2") + "e-2");
}
function exceptionAlert(e) {
dialogBoxCreate("Caught an exception: " + e + "<br><br>" +
"Filename: " + e.fileName + "<br><br>" +
"Line Number: " + e.lineNumber + "<br><br>" +
"This is a bug, please report to game developer with this " +
"message as well as details about how to reproduce the bug.<br><br>" +
"If you want to be safe, I suggest refreshing the game WITHOUT saving so that your " +
"safe doesn't get corrupted");
}
/*Creates a graphical "progress bar"
* e.g.: [||||---------------]
* params:
* @totalTicks - Total number of ticks in progress bar. Preferably a factor of 100
* @progress - Current progress, taken as a decimal (i.e. 0.6 to represent 60%)
*/
function createProgressBarText(params={}) {
//Default values
var totalTicks = (params.totalTicks == null ? 20 : params.totalTicks);
var progress = (params.progress == null ? 0 : params.progress);
var percentPerTick = 1 / totalTicks;
var numTicks = Math.floor(progress / percentPerTick);
var numDashes = totalTicks - numTicks;
return "[" + Array(numTicks+1).join("|") + Array(numDashes+1).join("-") + "]";
}
export {sizeOfObject,
clearObject,
addOffset,
clearEventListeners,
getRandomInt,
compareArrays,
printArray,
powerOfTwo,
roundToTwo,
clearEventListenersEl,
removeElementById,
removeElement,
createElement,
createAccordionElement,
appendLineBreaks,
removeChildrenFromElement,
createPopup,
clearSelector,
exceptionAlert,
createProgressBarText,
getElementById};

@ -1,13 +1,15 @@
import {AllServers} from "../src/Server";
import {getRandomByte} from "./helpers/getRandomByte";
/* Functions to deal with manipulating IP addresses*/
//Generate a random IP address
//Will not return an IP address that already exists in the AllServers array
function createRandomIp() {
var ip = createRandomByte(99) +'.' +
createRandomByte(9) +'.' +
createRandomByte(9) +'.' +
createRandomByte(9);
var ip = getRandomByte(99) + '.' +
getRandomByte(9) + '.' +
getRandomByte(9) + '.' +
getRandomByte(9);
//If the Ip already exists, recurse to create a new one
if (ipExists(ip)) {
@ -28,16 +30,4 @@ function ipExists(ip) {
return false;
}
function createRandomByte(n=9) {
return Math.round(Math.random()*n);
}
function isValidIPAddress(ipaddress) {
if (/^(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress))
{
return true;
}
return false;
}
export {createRandomIp, ipExists, isValidIPAddress};
export {createRandomIp, ipExists};

@ -3,7 +3,7 @@ import {CONSTANTS} from "../src/Constants";
import {Factions, Faction} from "../src/Faction";
import {Player} from "../src/Player";
import {dialogBoxCreate} from "./DialogBox";
import {clearEventListeners} from "./HelperFunctions";
import {clearEventListeners} from "./uiHelpers/clearEventListeners";
import {formatNumber} from "./StringHelperFunctions";
//Keep track of last faction

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

@ -1,5 +1,5 @@
import { EqualityFunc } from "../src/types";
import { dialogBoxCreate } from "./DialogBox";
import { isString } from "./helpers/isString";
// Netburner String helper functions
@ -49,7 +49,8 @@ function longestCommonStart(strings: string[]): string {
if (!containsAllStrings(strings)) {return ""; }
if (strings.length === 0) {return ""; }
const A: string[] = strings.concat().sort();
const A: string[] = strings.concat()
.sort();
const a1: string = A[0];
const a2: string = A[A.length - 1];
const L: number = a1.length;
@ -62,11 +63,6 @@ function longestCommonStart(strings: string[]): string {
return a1.substring(0, i);
}
// Returns whether a variable is a string
function isString(str: any): boolean {
return (typeof str === "string" || str instanceof String);
}
// Returns whether an array contains entirely of string objects
function containsAllStrings(arr: string[]): boolean {
return arr.every(isString);
@ -80,59 +76,12 @@ function formatNumber(num: number, numFractionDigits: number): string {
});
}
// Count the number of times a substring occurs in a string
function numOccurrences(text: string, subString: string): number {
const input: string = `${text}`;
const search: string = `${subString}`;
if (search.length <= 0) { return (input.length + 1); }
let n: number = 0;
let pos: number = 0;
const step: number = search.length;
while (true) {
pos = input.indexOf(search, pos);
if (pos >= 0) {
++n;
pos += step;
} else { break; }
}
return n;
}
// Counters the number of Netscript operators in a string
function numNetscriptOperators(text: string): number {
const total: number = numOccurrences(text, "+") +
numOccurrences(text, "-") +
numOccurrences(text, "*") +
numOccurrences(text, "/") +
numOccurrences(text, "%") +
numOccurrences(text, "&&") +
numOccurrences(text, "||") +
numOccurrences(text, "<") +
numOccurrences(text, ">") +
numOccurrences(text, "<=") +
numOccurrences(text, ">=") +
numOccurrences(text, "==") +
numOccurrences(text, "!=");
if (isNaN(total)) {
// tslint:disable-next-line:max-line-length
const message: string = "ERROR in counting number of operators in script. This is a bug, please report to game developer";
dialogBoxCreate(message, false);
return 0;
}
return total;
}
// Checks if a string contains HTML elements
function isHTML(str: string): boolean {
const element: HTMLDivElement = document.createElement("div");
element.innerHTML = str;
const c: NodeListOf<Node & ChildNode> = element.childNodes;
for (let i: number = c.length-1; i >= 0; i--) {
for (let i: number = c.length - 1; i >= 0; i--) {
if (c[i].nodeType === 1) {
return true;
}
@ -154,5 +103,5 @@ function generateRandomString(n: number): string {
}
export {convertTimeMsToTimeElapsedString, longestCommonStart,
isString, containsAllStrings, formatNumber,
numOccurrences, numNetscriptOperators, isHTML, generateRandomString, replaceAt};
containsAllStrings, formatNumber,
isHTML, generateRandomString, replaceAt};

@ -1,4 +1,4 @@
import {clearEventListeners} from "./HelperFunctions";
import {clearEventListeners} from "./uiHelpers/clearEventListeners";
/* Generic Yes-No Pop-up box
* Can be used to create pop-up boxes that require a yes/no response from player
*/

@ -0,0 +1,23 @@
/**
* Adds a random offset to a number within a certain percentage
* @example
* // Returns between 95-105
* addOffset(100, 5);
* @example
* // Returns between 63-77
* addOffSet(70, 10);
* @param midpoint The number to be the midpoint of the offset range
* @param percentage The percentage (in a range of 0-100) to offset
*/
export function addOffset(midpoint: number, percentage: number) {
const maxPercent: number = 100;
if (percentage < 0 || percentage > maxPercent) {
return midpoint;
}
const offset: number = midpoint * (percentage / maxPercent);
// Double the range to account for both sides of the midpoint.
// tslint:disable-next-line:no-magic-numbers
return midpoint + ((Math.random() * (offset * 2)) - offset);
}

@ -0,0 +1,6 @@
/**
* Returns the input array as a comma separated string.
*/
export function arrayToString<T>(a: T[]) {
return `[${a.join(", ")}]`;
}

@ -0,0 +1,14 @@
/**
* Clears defined properties from an object.
* Does not delete up the prototype chain.
* @deprecated Look into using `Map` or `Set` rather than manipulating properties on an Object.
* @param obj the object to clear all properties
*/
export function clearObject(obj: any) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
// tslint:disable-next-line:no-dynamic-delete
delete obj[key];
}
}
}

@ -0,0 +1,19 @@
/**
* Does a shallow compare of two arrays to determine if they are equal.
* @param a1 The first array
* @param a2 The second array
*/
export function compareArrays<T>(a1: T[], a2: T[]) {
if (a1.length !== a2.length) {
return false;
}
for (let i: number = 0; i < a1.length; ++i) {
if (a1[i] !== a2[i]) {
return false;
}
}
return true;
}

@ -0,0 +1,44 @@
/**
* Represents the possible configuration values that can be provided when creating the progress bar text.
*/
interface IProgressBarConfiguration {
/**
* Current progress, taken as a decimal (i.e. '0.6' to represent '60%')
*/
progress?: number;
/**
* Total number of ticks in progress bar. Preferably a factor of 100.
*/
totalTicks?: number;
}
/**
* Represents concrete configuration values when creating the progress bar text.
*/
interface IProgressBarConfigurationMaterialized extends IProgressBarConfiguration {
progress: number;
totalTicks: number;
}
/**
* Creates a graphical "progress bar"
* e.g.: [||||---------------]
* @param params The configuration parameters for the progress bar
*/
export function createProgressBarText(params: IProgressBarConfiguration) {
// Default values
const defaultParams: IProgressBarConfigurationMaterialized = {
progress: 0,
totalTicks: 20,
};
// tslint:disable-next-line:prefer-object-spread
const derivedParams: IProgressBarConfigurationMaterialized = Object.assign({}, params, defaultParams);
const bars: number = Math.floor(derivedParams.progress / (1 / derivedParams.totalTicks));
const dashes: number = derivedParams.totalTicks - bars;
// String.prototype.repeat isn't completley supported, but good enough for our purposes
return `[${"|".repeat(bars + 1)}${"-".repeat(dashes + 1)}]`;
}

@ -0,0 +1,16 @@
import { dialogBoxCreate } from "../DialogBox";
function exceptionAlert(e) {
dialogBoxCreate("Caught an exception: " + e + "<br><br>" +
"Filename: " + (e.fileName || "UNKNOWN FILE NAME") + "<br><br>" +
"Line Number: " + (e.lineNumber || "UNKNOWN LINE NUMBER") + "<br><br>" +
"This is a bug, please report to game developer with this " +
"message as well as details about how to reproduce the bug.<br><br>" +
"If you want to be safe, I suggest refreshing the game WITHOUT saving so that your " +
"safe doesn't get corrupted");
}
export {
exceptionAlert
}

@ -0,0 +1,13 @@
import { getRandomInt } from "./getRandomInt";
/**
* Gets a random value in the range of a byte (0 - 255), or up to the maximum.
* @param max The maximum value (up to 255).
*/
export function getRandomByte(max: number) {
// Technically 2^8 is 256, but the values are 0-255, not 1-256.
const byteMaximum: number = 255;
const upper: number = Math.max(Math.min(max, byteMaximum), 0);
return getRandomInt(0, upper);
}

@ -0,0 +1,11 @@
/**
* Gets a random integer bounded by the values passed in.
* @param min The minimum value in the range.
* @param max The maximum value in the range.
*/
export function getRandomInt(min: number, max: number) {
const lower: number = Math.min(min, max);
const upper: number = Math.max(min, max);
return Math.floor(Math.random() * (upper - lower + 1)) + lower;
}

@ -0,0 +1,17 @@
/**
* Determines if the number is a power of 2
* @param n The number to check.
*/
export function isPowerOfTwo(n: number) {
if (isNaN(n)) {
return false;
}
if (n === 0) {
return false;
}
// Disabiling the bitwise rule because it's honestly the most effecient way to check for this.
// tslint:disable-next-line:no-bitwise
return (n & (n - 1)) === 0;
}

@ -0,0 +1,7 @@
/**
* Checks whether the value passed in can be considered a string.
* @param value The value to check if it is a string.
*/
export function isString(value: any): boolean {
return (typeof value === "string" || value instanceof String);
}

@ -0,0 +1,12 @@
/**
* Checks whether a IP Address string is valid.
* @param ipaddress A string representing a potential IP Address
*/
export function isValidIPAddress(ipaddress: string) {
const byteRange: string = "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
const regexStr: string = `^${byteRange}\.${byteRange}\.${byteRange}\.${byteRange}$`;
const ipAddressRegex: RegExp = new RegExp(regexStr);
return ipAddressRegex.test(ipaddress);
}

@ -0,0 +1,9 @@
/**
* Rounds a number to two decimal places.
* @param decimal A decimal value to trim to two places.
*/
export function roundToTwo(decimal: number) {
const leftShift: number = Math.round(parseInt(`${decimal}e+2`, 10));
return +(`${leftShift}e-2`);
}

@ -0,0 +1,12 @@
import { createElement } from "./createElement";
/**
* Appends the specified number of breaks (as children) to the specified element
* @param el The element to add child break elements to.
* @param n The number of breaks to add.
*/
export function appendLineBreaks(el: HTMLElement, n: number) {
for (let i: number = 0; i < n; ++i) {
el.appendChild(createElement("br"));
}
}

@ -0,0 +1,23 @@
import { getElementById } from "./getElementById";
/**
* Given an element by its ID, removes all event listeners from that element by cloning and
* replacing. Then returns the new cloned element.
* @param elemId The HTML ID to retrieve the element by.
*/
export function clearEventListeners(elemId: string) {
try {
const elem: HTMLElement = getElementById(elemId);
const newElem: HTMLElement = elem.cloneNode(true) as HTMLElement;
if (elem.parentNode !== null) {
elem.parentNode.replaceChild(newElem, elem);
}
return newElem;
} catch (e) {
// tslint:disable-next-line:no-console
console.error(e);
return null;
}
}

@ -0,0 +1,9 @@
/**
* Clears all <option> elements from a <select>.
* @param selector The <select> element
*/
export function clearSelector(selector: HTMLSelectElement) {
for (let i: number = selector.options.length - 1; i >= 0; i--) {
selector.remove(i);
}
}

@ -0,0 +1,51 @@
import { createElement } from "./createElement";
/**
* Possible configuration parameters when creating the accordion element.
*/
interface IAccordionConfigurationParameters {
/**
* The HTML to appear in the accordion header.
*/
hdrText?: string;
/**
* A (hopefully) unique identifier for the accordion.
*/
id?: string;
/**
* The HTML to appear in the expanded accordion.
*/
panelText?: string;
}
/**
* Creates both the header and panel element of an accordion and sets the click handler
* @param params The creation parameters.
*/
export function createAccordionElement(params: IAccordionConfigurationParameters) {
const liElem: HTMLLIElement = createElement("li") as HTMLLIElement;
const header: HTMLButtonElement = createElement("button", {
clickListener() {
this.classList.toggle("active");
const pnl: CSSStyleDeclaration = (this.nextElementSibling as HTMLDivElement).style;
pnl.display = pnl.display === "block" ? "none" : "block";
},
id: params.id !== undefined ? `${params.id}-hdr` : undefined,
innerHTML: params.hdrText,
}) as HTMLButtonElement;
const panel: HTMLDivElement = createElement("div", {
id: params.id !== undefined ? `${params.id}-panel` : undefined,
innerHTML: params.panelText,
}) as HTMLDivElement;
liElem.appendChild(header);
liElem.appendChild(panel);
return [
liElem,
header,
panel,
];
}

@ -0,0 +1,250 @@
/**
* Options specific to creating an anchor ("<a>") element.
*/
interface ICreateElementAnchorOptions {
href?: string;
target?: string;
text?: string;
}
/**
* Options specific to creating an input ("<input>") element.
*/
interface ICreateElementInputOptions {
checked?: boolean;
maxLength?: number;
name?: string;
pattern?: string;
placeholder?: string;
type?: string;
value?: string;
}
/**
* Options specific to creating a label ("<label>") element.
*/
interface ICreateElementLabelOptions {
for?: string;
}
/**
* Options for setting up event listeners on the element.
*/
interface ICreateElementListenerOptions {
changeListener?(this: HTMLElement, ev: Event): any;
clickListener?(this: HTMLElement, ev: MouseEvent): any;
inputListener?(this: HTMLElement, ev: Event): any;
onfocus?(this: HTMLElement, ev: FocusEvent): any;
onkeyup?(this: HTMLElement, ev: KeyboardEvent): any;
}
/**
* Options for setting up the inline-styling of element.
* NOTE: Relying on CSS styling should be preferred over forcing the higher specificity via inline styles.
*/
interface ICreateElementStyleOptions {
backgroundColor?: string;
border?: string;
color?: string;
display?: string;
float?: string;
fontSize?: string;
margin?: string;
marginLeft?: string;
marginTop?: string;
padding?: string;
position?: string;
visibility?: string;
whiteSpace?: string;
width?: string;
}
/**
* Options for adding an in-game tooltip to the element.
*/
interface ICreateElementTooltipOptions {
tooltip?: string;
tooltipleft?: string;
}
/**
* All possible configuration options when creating an element.
*/
interface ICreateElementOptions extends
ICreateElementStyleOptions,
ICreateElementListenerOptions,
ICreateElementInputOptions,
ICreateElementAnchorOptions,
ICreateElementLabelOptions,
ICreateElementTooltipOptions {
/**
* CSS Class(es) to initially set.
*/
class?: string;
/**
* A (hopefully) unique identifier for the element.
*/
id?: string;
innerHTML?: string;
innerText?: string;
tabIndex?: number;
}
function setElementAnchor(el: HTMLAnchorElement, params: ICreateElementAnchorOptions) {
if (params.text !== undefined) {
el.text = params.text;
}
if (params.href !== undefined) {
el.href = params.href;
}
if (params.target !== undefined) {
el.target = params.target;
}
}
function setElementInput(el: HTMLInputElement, params: ICreateElementInputOptions) {
if (params.name !== undefined) {
el.name = params.name;
}
if (params.value !== undefined) {
el.value = params.value;
}
if (params.type !== undefined) {
el.type = params.type;
}
if (params.checked !== undefined) {
el.checked = params.checked;
}
if (params.pattern !== undefined) {
el.pattern = params.pattern;
}
if (params.maxLength !== undefined) {
el.maxLength = params.maxLength;
}
if (params.placeholder !== undefined) {
el.placeholder = params.placeholder;
}
}
function setElementLabel(el: HTMLLabelElement, params: ICreateElementLabelOptions) {
if (params.for !== undefined) {
el.htmlFor = params.for;
}
}
function setElementListeners(el: HTMLElement, params: ICreateElementListenerOptions) {
// tslint:disable:no-unbound-method
if (params.clickListener !== undefined) {
el.addEventListener("click", params.clickListener);
}
if (params.inputListener !== undefined) {
el.addEventListener("input", params.inputListener);
}
if (params.changeListener !== undefined) {
el.addEventListener("change", params.changeListener);
}
if (params.onkeyup !== undefined) {
el.addEventListener("keyup", params.onkeyup);
}
if (params.onfocus !== undefined) {
el.addEventListener("focus", params.onfocus);
}
// tslint:enable:no-unbound-method
}
function setElementStyle(el: HTMLElement, params: ICreateElementStyleOptions) {
if (params.display !== undefined) {
el.style.display = params.display;
}
if (params.visibility !== undefined) {
el.style.visibility = params.visibility;
}
if (params.margin !== undefined) {
el.style.margin = params.margin;
}
if (params.marginLeft !== undefined) {
el.style.marginLeft = params.marginLeft;
}
if (params.marginTop !== undefined) {
el.style.marginTop = params.marginTop;
}
if (params.padding !== undefined) {
el.style.padding = params.padding;
}
if (params.color !== undefined) {
el.style.color = params.color;
}
if (params.border !== undefined) {
el.style.border = params.border;
}
if (params.float !== undefined) {
el.style.cssFloat = params.float;
}
if (params.fontSize !== undefined) {
el.style.fontSize = params.fontSize;
}
if (params.whiteSpace !== undefined) {
el.style.whiteSpace = params.whiteSpace;
}
if (params.width !== undefined) {
el.style.width = params.width;
}
if (params.backgroundColor !== undefined) {
el.style.backgroundColor = params.backgroundColor;
}
if (params.position !== undefined) {
el.style.position = params.position;
}
}
function setElementTooltip(el: HTMLElement, params: ICreateElementTooltipOptions) {
if (params.tooltip !== undefined && params.tooltip !== "") {
el.className += " tooltip";
el.appendChild(createElement("span", {
class: "tooltiptext",
innerHTML: params.tooltip,
}));
} else if (params.tooltipleft !== undefined) {
el.className += " tooltip";
el.appendChild(createElement("span", {
class: "tooltiptextleft",
innerHTML: params.tooltipleft,
}));
}
}
/**
* An all-in-one-call way of creating an element to be added to the DOM at some point.
* @param tagName The HTML tag/element name
* @param params Additional parameters to set on the element
*/
export function createElement(tagName: string, params: ICreateElementOptions = {}) {
const el: HTMLElement = document.createElement(tagName);
if (params.id !== undefined) {
el.id = params.id;
}
if (params.class !== undefined) {
el.className = params.class;
}
if (params.innerHTML !== undefined) {
el.innerHTML = params.innerHTML;
}
if (params.innerText !== undefined) {
el.innerText = params.innerText;
}
if (params.tabIndex !== undefined) {
el.tabIndex = params.tabIndex;
}
setElementAnchor(el as HTMLAnchorElement, params);
setElementInput(el as HTMLInputElement, params);
setElementLabel(el as HTMLLabelElement, params);
setElementListeners(el, params);
setElementStyle(el, params);
setElementTooltip(el, params);
return el;
}

@ -0,0 +1,28 @@
import { createElement } from "./createElement";
import { getElementById } from "./getElementById";
/**
* Creates the necessary DOM elements to present an in-game popup to the player.
* @param id The (hopefully) unique identifier for the popup container.
* @param elems The collection of HTML Elements to show within the popup.
*/
export function createPopup(id: string, elems: HTMLElement[]) {
const container: HTMLDivElement = createElement("div", {
class: "popup-box-container",
display: "block",
id,
}) as HTMLDivElement;
const content: HTMLElement = createElement("div", {
class: "popup-box-content",
id: `${id}-content`,
});
for (const elem of elems) {
content.appendChild(elem);
}
container.appendChild(content);
getElementById("entire-game-container")
.appendChild(container);
return container;
}

@ -0,0 +1,14 @@
/**
* Returns a reference to the first object with the specified value of the ID or NAME attribute,
* throwing an error if it is unable to find it.
* @param elementId The HTML ID to retrieve the element by.
* @throws {Error} When the 'elementId' cannot be found.
*/
export function getElementById(elementId: string) {
const el: HTMLElement | null = document.getElementById(elementId);
if (el === null) {
throw new Error(`Unable to find element with id '${elementId}'`);
}
return el;
}

@ -0,0 +1,28 @@
import { isString } from "../helpers/isString";
import { getElementById } from "./getElementById";
/**
* Clears out all children from the provided element.
* If a string is passed in, it will treat it as an ID and search for the element to delete all children from.
* @param el The element or ID of an element to remove all children from.
*/
export function removeChildrenFromElement(el: string | null | Element) {
if (el === null) {
return;
}
try {
const elem: HTMLElement | Element = (isString(el) ? getElementById(el as string) : el as Element);
if (elem instanceof Element) {
while (elem.firstChild !== null) {
elem.removeChild(elem.firstChild);
}
}
} catch (e) {
// tslint:disable-next-line:no-console
console.debug(e);
return;
}
}

@ -0,0 +1,26 @@
/**
* For a given element, this function removes it AND its children
* @param elem The element to remove.
*/
export function removeElement(elem: Element | null) {
if (elem === null) {
// tslint:disable-next-line:no-console
console.debug("The element passed into 'removeElement' was null.");
return;
}
if (!(elem instanceof Element)) {
// tslint:disable-next-line:no-console
console.debug("The element passed into 'removeElement' was not an instance of an Element.");
return;
}
while (elem.firstChild !== null) {
elem.removeChild(elem.firstChild);
}
if (elem.parentNode !== null) {
elem.parentNode.removeChild(elem);
}
}

@ -0,0 +1,15 @@
import { getElementById } from "./getElementById";
import { removeElement } from "./removeElement";
/**
* Given its id, this function removes an element AND its children
* @param id The HTML identifier to search for and remove.
*/
export function removeElementById(id: string) {
try {
const elem: HTMLElement = getElementById(id);
removeElement(elem);
} catch (e) {
// Probably should log this as we're trying to remove elements that don't exist.
}
}