[refactor] Moved 'getRandomInt' to its own TS file

This commit is contained in:
Steven Evans 2018-07-04 20:35:33 -04:00
parent 39c9488768
commit be9891d93b
9 changed files with 24 additions and 13 deletions

@ -11,7 +11,7 @@ import {KEY} from "./Terminal";
import {createProgressBarText} from "../utils/helpers/createProgressBarText"; import {createProgressBarText} from "../utils/helpers/createProgressBarText";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {getRandomInt, addOffset, clearObject, import {addOffset, clearObject,
createElement, removeChildrenFromElement, createElement, removeChildrenFromElement,
createPopup, appendLineBreaks, createPopup, appendLineBreaks,
removeElementById, removeElement} from "../utils/HelperFunctions"; removeElementById, removeElement} from "../utils/HelperFunctions";
@ -20,6 +20,7 @@ import {Reviver, Generic_toJSON,
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {exceptionAlert} from "../utils/helpers/exceptionAlert"; import {exceptionAlert} from "../utils/helpers/exceptionAlert";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"]; var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"];

@ -7,7 +7,7 @@ import {Player} from "./Player";
import Decimal from "decimal.js"; import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {getRandomInt, removeElementById, import {removeElementById,
createElement, createAccordionElement, createElement, createAccordionElement,
removeChildrenFromElement, createPopup, removeChildrenFromElement, createPopup,
clearSelector} from "../utils/HelperFunctions"; clearSelector} from "../utils/HelperFunctions";
@ -15,6 +15,7 @@ import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver"; Generic_fromJSON} from "../utils/JSONReviver";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber, generateRandomString} from "../utils/StringHelperFunctions"; import {formatNumber, generateRandomString} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {isString} from "../utils/helpers/isString"; import {isString} from "../utils/helpers/isString";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,

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

@ -3,7 +3,8 @@ import {CONSTANTS} from "./Constants";
import {Engine} from "./engine"; import {Engine} from "./engine";
import {Player} from "./Player"; import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; 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 {infiltrationBoxCreate} from "../utils/InfiltrationBox";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";

@ -3,10 +3,11 @@ import {Engine} from "./engine";
import {displayFactionContent} from "./Faction"; import {displayFactionContent} from "./Faction";
import {Player} from "./Player"; import {Player} from "./Player";
import {dialogBoxCreate} from "../utils/DialogBox"; import {dialogBoxCreate} from "../utils/DialogBox";
import {addOffset, getRandomInt, import {addOffset,
clearEventListenersEl, clearEventListenersEl,
clearEventListeners} from "../utils/HelperFunctions"; clearEventListeners} from "../utils/HelperFunctions";
import {formatNumber} from "../utils/StringHelperFunctions"; import {formatNumber} from "../utils/StringHelperFunctions";
import {getRandomInt} from "../utils/helpers/getRandomInt";
import {isString} from "../utils/helpers/isString"; import {isString} from "../utils/helpers/isString";
import jsplumb from 'jsplumb' import jsplumb from 'jsplumb'

@ -4,7 +4,7 @@ import {Programs} from "./CreateProgram";
import {Player} from "./Player"; import {Player} from "./Player";
import {RunningScript, Script} from "./Script"; import {RunningScript, Script} from "./Script";
import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps"; import {SpecialServerNames, SpecialServerIps} from "./SpecialServerIps";
import {getRandomInt} from "../utils/HelperFunctions"; import {getRandomInt} from "../utils/helpers/getRandomInt";
import {createRandomIp, isValidIPAddress, ipExists} from "../utils/IPAddress"; import {createRandomIp, isValidIPAddress, ipExists} from "../utils/IPAddress";
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver"; Generic_fromJSON} from "../utils/JSONReviver";

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

@ -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 //Returns true if all elements are equal, and false otherwise
//Assumes both arguments are arrays and that there are no nested arrays //Assumes both arguments are arrays and that there are no nested arrays
function compareArrays(a1, a2) { function compareArrays(a1, a2) {
@ -246,7 +241,6 @@ export {sizeOfObject,
clearObject, clearObject,
addOffset, addOffset,
clearEventListeners, clearEventListeners,
getRandomInt,
compareArrays, compareArrays,
printArray, printArray,
powerOfTwo, powerOfTwo,

@ -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;
}