From be9891d93bbaa6b320bd6071102f86d1cfa6bbfe Mon Sep 17 00:00:00 2001 From: Steven Evans Date: Wed, 4 Jul 2018 20:35:33 -0400 Subject: [PATCH] [refactor] Moved 'getRandomInt' to its own TS file --- src/Bladeburner.js | 3 ++- src/CompanyManagement.js | 3 ++- src/Gang.js | 3 ++- src/Infiltration.js | 3 ++- src/Missions.js | 3 ++- src/Server.js | 2 +- src/StockMarket.js | 3 ++- utils/HelperFunctions.js | 6 ------ utils/helpers/getRandomInt.ts | 11 +++++++++++ 9 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 utils/helpers/getRandomInt.ts diff --git a/src/Bladeburner.js b/src/Bladeburner.js index 8d8d5c03f..39ee2228b 100644 --- a/src/Bladeburner.js +++ b/src/Bladeburner.js @@ -11,7 +11,7 @@ import {KEY} from "./Terminal"; import {createProgressBarText} from "../utils/helpers/createProgressBarText"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {getRandomInt, addOffset, clearObject, +import {addOffset, clearObject, createElement, removeChildrenFromElement, createPopup, appendLineBreaks, removeElementById, removeElement} from "../utils/HelperFunctions"; @@ -20,6 +20,7 @@ import {Reviver, Generic_toJSON, import numeral from "numeral/min/numeral.min"; import {exceptionAlert} from "../utils/helpers/exceptionAlert"; import {formatNumber} from "../utils/StringHelperFunctions"; +import {getRandomInt} from "../utils/helpers/getRandomInt"; var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"]; diff --git a/src/CompanyManagement.js b/src/CompanyManagement.js index de82616da..6e326d5ad 100644 --- a/src/CompanyManagement.js +++ b/src/CompanyManagement.js @@ -7,7 +7,7 @@ import {Player} from "./Player"; import Decimal from "decimal.js"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {getRandomInt, removeElementById, +import {removeElementById, createElement, createAccordionElement, removeChildrenFromElement, createPopup, clearSelector} from "../utils/HelperFunctions"; @@ -15,6 +15,7 @@ import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import numeral from "numeral/min/numeral.min"; import {formatNumber, generateRandomString} from "../utils/StringHelperFunctions"; +import {getRandomInt} from "../utils/helpers/getRandomInt"; import {isString} from "../utils/helpers/isString"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, diff --git a/src/Gang.js b/src/Gang.js index 99c07afac..42d514263 100644 --- a/src/Gang.js +++ b/src/Gang.js @@ -6,12 +6,13 @@ import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; -import {getRandomInt, createElement, +import {createElement, removeChildrenFromElement, createAccordionElement, createPopup, removeElementById, removeElement} from "../utils/HelperFunctions"; import numeral from "numeral/min/numeral.min"; import {formatNumber} from "../utils/StringHelperFunctions"; +import {getRandomInt} from "../utils/helpers/getRandomInt"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/src/Infiltration.js b/src/Infiltration.js index 1c236e7be..0f3c392cf 100644 --- a/src/Infiltration.js +++ b/src/Infiltration.js @@ -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/HelperFunctions"; +import {getRandomInt} from "../utils/helpers/getRandomInt"; import {infiltrationBoxCreate} from "../utils/InfiltrationBox"; import {formatNumber} from "../utils/StringHelperFunctions"; diff --git a/src/Missions.js b/src/Missions.js index 8a6c98f5d..f0217f645 100644 --- a/src/Missions.js +++ b/src/Missions.js @@ -3,10 +3,11 @@ import {Engine} from "./engine"; import {displayFactionContent} from "./Faction"; import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {addOffset, getRandomInt, +import {addOffset, clearEventListenersEl, clearEventListeners} from "../utils/HelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions"; +import {getRandomInt} from "../utils/helpers/getRandomInt"; import {isString} from "../utils/helpers/isString"; import jsplumb from 'jsplumb' diff --git a/src/Server.js b/src/Server.js index 59bfcb913..a03e6c229 100644 --- a/src/Server.js +++ b/src/Server.js @@ -4,7 +4,7 @@ import {Programs} from "./CreateProgram"; import {Player} from "./Player"; import {RunningScript, Script} from "./Script"; import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps"; -import {getRandomInt} from "../utils/HelperFunctions"; +import {getRandomInt} from "../utils/helpers/getRandomInt"; import {createRandomIp, isValidIPAddress, ipExists} from "../utils/IPAddress"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; diff --git a/src/StockMarket.js b/src/StockMarket.js index 7468eac8c..affc5e294 100644 --- a/src/StockMarket.js +++ b/src/StockMarket.js @@ -6,13 +6,14 @@ import {WorkerScript} from "./NetscriptWorker"; import {Player} from "./Player"; import {dialogBoxCreate} from "../utils/DialogBox"; -import {clearEventListeners, getRandomInt, +import {clearEventListeners, removeElementById, clearEventListenersEl} from "../utils/HelperFunctions"; 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 {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/utils/HelperFunctions.js b/utils/HelperFunctions.js index 3c83f5dee..191092130 100644 --- a/utils/HelperFunctions.js +++ b/utils/HelperFunctions.js @@ -214,11 +214,6 @@ function clearSelector(selector) { } } -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) { @@ -246,7 +241,6 @@ export {sizeOfObject, clearObject, addOffset, clearEventListeners, - getRandomInt, compareArrays, printArray, powerOfTwo, diff --git a/utils/helpers/getRandomInt.ts b/utils/helpers/getRandomInt.ts new file mode 100644 index 000000000..4f5dc15b5 --- /dev/null +++ b/utils/helpers/getRandomInt.ts @@ -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; +}