first conversion

This commit is contained in:
Olivier Gagnon 2018-06-22 17:30:24 -04:00
parent 343d474702
commit 53ddfbc9ec
23 changed files with 74 additions and 67 deletions

@ -10,7 +10,7 @@ import {printArray, createElement,
removeChildrenFromElement, exceptionAlert} from "../utils/HelperFunctions.js"; removeChildrenFromElement, exceptionAlert} from "../utils/HelperFunctions.js";
import {logBoxCreate} from "../utils/LogBox.js"; import {logBoxCreate} from "../utils/LogBox.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
/* { /* {
* serverName: { * serverName: {

@ -16,7 +16,7 @@ import {createElement, createAccordionElement,
removeChildrenFromElement, clearObject} from "../utils/HelperFunctions.js"; removeChildrenFromElement, clearObject} from "../utils/HelperFunctions.js";
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import {isString} from "../utils/StringHelperFunctions.js"; import {isString} from "../utils/StringHelperFunctions";
//Augmentations //Augmentations
function Augmentation(params) { function Augmentation(params) {

@ -17,7 +17,7 @@ import {getRandomInt, addOffset, clearObject,
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"]; var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"];

@ -2,7 +2,7 @@ import {Engine} from "./engine.js";
import {removeChildrenFromElement, import {removeChildrenFromElement,
createElement, exceptionAlert} from "../utils/HelperFunctions.js"; createElement, exceptionAlert} from "../utils/HelperFunctions.js";
import {isString} from "../utils/StringHelperFunctions.js"; import {isString} from "../utils/StringHelperFunctions";
var cinematicTextFlag = false; var cinematicTextFlag = false;

@ -14,7 +14,7 @@ import {getRandomInt, removeElementById,
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber, isString, generateRandomString} from "../utils/StringHelperFunctions.js"; import {formatNumber, isString, generateRandomString} from "../utils/StringHelperFunctions";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -4,7 +4,7 @@ import {SpecialServerIps} from "./SpecialServerIps.js";
import {post} from "./Terminal.js"; import {post} from "./Terminal.js";
import {isValidIPAddress} from "../utils/IPAddress.js"; import {isValidIPAddress} from "../utils/IPAddress.js";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
/* DarkWeb.js */ /* DarkWeb.js */

@ -16,7 +16,7 @@ import {clearEventListeners, createElement,
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
import {yesNoBoxCreate, yesNoBoxGetYesButton, import {yesNoBoxCreate, yesNoBoxGetYesButton,
yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox.js"; yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox.js";

@ -11,7 +11,7 @@ import {getRandomInt, createElement,
createAccordionElement, createPopup, createAccordionElement, createPopup,
removeElementById, removeElement} from "../utils/HelperFunctions.js"; removeElementById, removeElement} from "../utils/HelperFunctions.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -9,7 +9,7 @@ import {clearEventListeners, createElement,
getElementById} from "../utils/HelperFunctions.js"; getElementById} from "../utils/HelperFunctions.js";
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
/** /**
* Overwrites the inner text of the specified HTML element if it is different from what currently exists. * Overwrites the inner text of the specified HTML element if it is different from what currently exists.

@ -5,7 +5,7 @@ import {Player} from "./Player.js";
import {dialogBoxCreate} from "../utils/DialogBox.js"; import {dialogBoxCreate} from "../utils/DialogBox.js";
import {clearEventListeners, getRandomInt} from "../utils/HelperFunctions.js"; import {clearEventListeners, getRandomInt} from "../utils/HelperFunctions.js";
import {infiltrationBoxCreate} from "../utils/InfiltrationBox.js"; import {infiltrationBoxCreate} from "../utils/InfiltrationBox.js";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
/* Infiltration.js /* Infiltration.js
* *

@ -18,7 +18,7 @@ import {dialogBoxCreate} from "../utils/DialogBox.js";
import {clearEventListeners, createElement} from "../utils/HelperFunctions.js"; import {clearEventListeners, createElement} from "../utils/HelperFunctions.js";
import {createRandomIp} from "../utils/IPAddress.js"; import {createRandomIp} from "../utils/IPAddress.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -6,7 +6,7 @@ import {dialogBoxCreate} from "../utils/DialogBox.js"
import {addOffset, getRandomInt, import {addOffset, getRandomInt,
clearEventListenersEl, clearEventListenersEl,
clearEventListeners} from "../utils/HelperFunctions.js"; clearEventListeners} from "../utils/HelperFunctions.js";
import {formatNumber, isString} from "../utils/StringHelperFunctions.js"; import {formatNumber, isString} from "../utils/StringHelperFunctions";
import jsplumb from 'jsplumb' import jsplumb from 'jsplumb'
let inMission = false; //Flag to denote whether a mission is running let inMission = false; //Flag to denote whether a mission is running

@ -11,7 +11,7 @@ import {Script, findRunningScript,
import {parse, Node} from "../utils/acorn.js"; import {parse, Node} from "../utils/acorn.js";
import {printArray} from "../utils/HelperFunctions.js"; import {printArray} from "../utils/HelperFunctions.js";
import {isValidIPAddress} from "../utils/IPAddress.js"; import {isValidIPAddress} from "../utils/IPAddress.js";
import {isString} from "../utils/StringHelperFunctions.js"; import {isString} from "../utils/StringHelperFunctions";
var Promise = require("bluebird"); var Promise = require("bluebird");

@ -51,7 +51,7 @@ import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox.js"; import {dialogBoxCreate} from "../utils/DialogBox.js";
import {printArray, powerOfTwo} from "../utils/HelperFunctions.js"; import {printArray, powerOfTwo} from "../utils/HelperFunctions.js";
import {createRandomIp} from "../utils/IPAddress.js"; import {createRandomIp} from "../utils/IPAddress.js";
import {formatNumber, isString, isHTML} from "../utils/StringHelperFunctions.js"; import {formatNumber, isString, isHTML} from "../utils/StringHelperFunctions";
import {yesNoBoxClose, yesNoBoxGetYesButton, import {yesNoBoxClose, yesNoBoxGetYesButton,
yesNoBoxGetNoButton, yesNoBoxCreate, yesNoBoxGetNoButton, yesNoBoxCreate,
yesNoBoxOpen} from "../utils/YesNoBox.js"; yesNoBoxOpen} from "../utils/YesNoBox.js";

@ -27,7 +27,7 @@ import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber, import {formatNumber,
convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions.js"; convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions";
function PlayerObject() { function PlayerObject() {
//Skills and stats //Skills and stats

@ -23,7 +23,7 @@ import {clearEventListeners, createElement,
createPopup, removeElementById} from "../utils/HelperFunctions.js"; createPopup, removeElementById} from "../utils/HelperFunctions.js";
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
import Decimal from "decimal.js"; import Decimal from "decimal.js";

@ -38,7 +38,7 @@ import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import {compareArrays, createElement} from "../utils/HelperFunctions.js"; import {compareArrays, createElement} from "../utils/HelperFunctions.js";
import {formatNumber, numOccurrences, import {formatNumber, numOccurrences,
numNetscriptOperators} from "../utils/StringHelperFunctions.js"; numNetscriptOperators} from "../utils/StringHelperFunctions";
var keybindings = { var keybindings = {
ace: null, ace: null,

@ -12,7 +12,7 @@ import {clearEventListeners, getRandomInt,
import {Reviver, Generic_toJSON, import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js"; Generic_fromJSON} from "../utils/JSONReviver.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions.js"; import {formatNumber} from "../utils/StringHelperFunctions";
import {yesNoBoxCreate, yesNoTxtInpBoxCreate, import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

@ -33,7 +33,7 @@ import {SpecialServerIps,
import {TextFile, getTextFile} from "./TextFile"; import {TextFile, getTextFile} from "./TextFile";
import {containsAllStrings, longestCommonStart, import {containsAllStrings, longestCommonStart,
formatNumber, isString} from "../utils/StringHelperFunctions.js"; formatNumber, isString} from "../utils/StringHelperFunctions";
import {addOffset, printArray} from "../utils/HelperFunctions.js"; import {addOffset, printArray} from "../utils/HelperFunctions.js";
import {logBoxCreate} from "../utils/LogBox.js"; import {logBoxCreate} from "../utils/LogBox.js";
import {yesNoBoxCreate, import {yesNoBoxCreate,

@ -5,7 +5,8 @@ import {clearEventListeners, createElement,
exceptionAlert} from "../utils/HelperFunctions.js"; exceptionAlert} from "../utils/HelperFunctions.js";
import numeral from "numeral/min/numeral.min"; import numeral from "numeral/min/numeral.min";
import {formatNumber, import {formatNumber,
convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions.js"; convertTimeMsToTimeElapsedString,
replaceAt} from "../utils/StringHelperFunctions";
import {loxBoxCreate, logBoxUpdateText, import {loxBoxCreate, logBoxUpdateText,
logBoxOpened} from "../utils/LogBox.js"; logBoxOpened} from "../utils/LogBox.js";
@ -1170,7 +1171,7 @@ let Engine = {
//Update progress bar //Update progress bar
while (Engine._actionProgressBarCount * 2 <= percent) { while (Engine._actionProgressBarCount * 2 <= percent) {
Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|"); Engine._actionProgressStr = replaceAt(Engine._actionProgressStr, Engine._actionProgressBarCount, "|");
Engine._actionProgressBarCount += 1; Engine._actionProgressBarCount += 1;
} }

@ -1,5 +1,5 @@
//General helper functions //General helper functions
import {isString} from "./StringHelperFunctions.js"; import {isString} from "./StringHelperFunctions";
import {dialogBoxCreate} from "./DialogBox.js"; import {dialogBoxCreate} from "./DialogBox.js";
//Returns the size (number of keys) of an object //Returns the size (number of keys) of an object

@ -4,7 +4,7 @@ import {Factions, Faction} from "../src/Faction.js";
import {Player} from "../src/Player.js"; import {Player} from "../src/Player.js";
import {dialogBoxCreate} from "./DialogBox.js"; import {dialogBoxCreate} from "./DialogBox.js";
import {clearEventListeners} from "./HelperFunctions.js"; import {clearEventListeners} from "./HelperFunctions.js";
import {formatNumber} from "./StringHelperFunctions.js"; import {formatNumber} from "./StringHelperFunctions";
/* InfiltrationBox.js */ /* InfiltrationBox.js */
function infiltrationBoxClose() { function infiltrationBoxClose() {

@ -4,12 +4,14 @@ import {dialogBoxCreate} from "./DialogBox.js";
//Searches for every occurence of searchStr within str and returns an array of the indices of //Searches for every occurence of searchStr within str and returns an array of the indices of
//all these occurences //all these occurences
function getIndicesOf(searchStr, str, caseSensitive) { function getIndicesOf(searchStr: string, str: string, caseSensitive: boolean): number[] {
var searchStrLen = searchStr.length; let searchStrLen: number = searchStr.length;
if (searchStrLen == 0) { if (searchStrLen == 0) {
return []; return [];
} }
var startIndex = 0, index, indices = []; let startIndex: number = 0;
let index: number = 0;
let indices: number[] = [];
if (!caseSensitive) { if (!caseSensitive) {
str = str.toLowerCase(); str = str.toLowerCase();
searchStr = searchStr.toLowerCase(); searchStr = searchStr.toLowerCase();
@ -22,30 +24,30 @@ function getIndicesOf(searchStr, str, caseSensitive) {
} }
//Replaces the character at an index with a new character //Replaces the character at an index with a new character
String.prototype.replaceAt=function(index, character) { function replaceAt(base: string, index: number, character: string): string {
return this.substr(0, index) + character + this.substr(index+character.length); return base.substr(0, index) + character + base.substr(index+character.length);
} }
//Converts a date representing time in milliseconds to a string with the format //Converts a date representing time in milliseconds to a string with the format
// H hours M minutes and S seconds // H hours M minutes and S seconds
// e.g. 10000 -> "0 hours 0 minutes and 10 seconds" // e.g. 10000 -> "0 hours 0 minutes and 10 seconds"
// 120000 -> "0 0 hours 2 minutes and 0 seconds" // 120000 -> "0 0 hours 2 minutes and 0 seconds"
function convertTimeMsToTimeElapsedString(time) { function convertTimeMsToTimeElapsedString(time: number): string {
//Convert ms to seconds, since we only have second-level precision //Convert ms to seconds, since we only have second-level precision
time = Math.floor(time / 1000); time = Math.floor(time / 1000);
var days = Math.floor(time / 86400); let days = Math.floor(time / 86400);
time %= 86400; time %= 86400;
var hours = Math.floor(time / 3600); let hours = Math.floor(time / 3600);
time %= 3600; time %= 3600;
var minutes = Math.floor(time / 60); let minutes = Math.floor(time / 60);
time %= 60; time %= 60;
var seconds = time; let seconds: number = time;
var res = ""; let res = "";
if (days) {res += days + " days ";} if (days) {res += days + " days ";}
if (hours) {res += hours + " hours ";} if (hours) {res += hours + " hours ";}
if (minutes) {res += minutes + " minutes ";} if (minutes) {res += minutes + " minutes ";}
@ -54,29 +56,32 @@ function convertTimeMsToTimeElapsedString(time) {
} }
//Finds the longest common starting substring in a set of strings //Finds the longest common starting substring in a set of strings
function longestCommonStart(strings) { function longestCommonStart(strings: string[]): string {
if (!containsAllStrings(strings)) {return;} if (!containsAllStrings(strings)) {return "";}
if (strings.length == 0) {return;} if (strings.length == 0) {return "";}
var A = strings.concat().sort(), let A: string[] = strings.concat().sort();
a1= A[0], a2= A[A.length-1], L= a1.length, i= 0; let a1: string = A[0];
let a2: string = A[A.length-1];
let L: number = a1.length;
let i: number = 0;
while(i<L && a1.charAt(i).toLowerCase() === a2.charAt(i).toLowerCase()) i++; while(i<L && a1.charAt(i).toLowerCase() === a2.charAt(i).toLowerCase()) i++;
return a1.substring(0, i); return a1.substring(0, i);
} }
//Returns whether a variable is a string //Returns whether a variable is a string
function isString(str) { function isString(str: any): boolean {
return (typeof str === 'string' || str instanceof String); return (typeof str === 'string' || str instanceof String);
} }
//Returns whether an array contains entirely of string objects //Returns whether an array contains entirely of string objects
function containsAllStrings(arr) { function containsAllStrings(arr: string[]): boolean {
return arr.every(isString); return arr.every(isString);
} }
//Formats a number with commas and a specific number of decimal digits //Formats a number with commas and a specific number of decimal digits
function formatNumber(num, numFractionDigits) { function formatNumber(num: number, numFractionDigits: number): string {
return num.toLocaleString(undefined, { return num.toLocaleString(undefined, {
minimumFractionDigits: numFractionDigits, minimumFractionDigits: numFractionDigits,
maximumFractionDigits: numFractionDigits maximumFractionDigits: numFractionDigits
@ -84,15 +89,17 @@ function formatNumber(num, numFractionDigits) {
} }
//Count the number of times a substring occurs in a string //Count the number of times a substring occurs in a string
function numOccurrences(string, subString) { function numOccurrences(text: string, subString: string): number {
string += ""; text += "";
subString += ""; subString += "";
if (subString.length <= 0) return (string.length + 1); if (subString.length <= 0) return (text.length + 1);
var n = 0, pos = 0, step = subString.length; let n: number = 0;
let pos: number = 0;
let step: number = subString.length;
while (true) { while (true) {
pos = string.indexOf(subString, pos); pos = text.indexOf(subString, pos);
if (pos >= 0) { if (pos >= 0) {
++n; ++n;
pos += step; pos += step;
@ -102,30 +109,29 @@ function numOccurrences(string, subString) {
} }
//Counters the number of Netscript operators in a string //Counters the number of Netscript operators in a string
function numNetscriptOperators(string) { function numNetscriptOperators(text: string): number {
var total = 0; const total: number = numOccurrences(text, "+") +
total += numOccurrences(string, "+"); numOccurrences(text, "-") +
total += numOccurrences(string, "-"); numOccurrences(text, "*") +
total += numOccurrences(string, "*"); numOccurrences(text, "/") +
total += numOccurrences(string, "/"); numOccurrences(text, "%") +
total += numOccurrences(string, "%"); numOccurrences(text, "&&") +
total += numOccurrences(string, "&&"); numOccurrences(text, "||") +
total += numOccurrences(string, "||"); numOccurrences(text, "<") +
total += numOccurrences(string, "<"); numOccurrences(text, ">") +
total += numOccurrences(string, ">"); numOccurrences(text, "<=") +
total += numOccurrences(string, "<="); numOccurrences(text, ">=") +
total += numOccurrences(string, ">="); numOccurrences(text, "==") +
total += numOccurrences(string, "=="); numOccurrences(text, "!=");
total += numOccurrences(string, "!=");
if (isNaN(total)) { if (isNaN(total)) {
dialogBoxCreate("ERROR in counting number of operators in script. This is a bug, please report to game developer"); dialogBoxCreate("ERROR in counting number of operators in script. This is a bug, please report to game developer", false);
total = 0; return 0;
} }
return total; return total;
} }
//Checks if a string contains HTML elements //Checks if a string contains HTML elements
function isHTML(str) { function isHTML(str: string): boolean {
var a = document.createElement('div'); var a = document.createElement('div');
a.innerHTML = str; a.innerHTML = str;
for (var c = a.childNodes, i = c.length; i--; ) { for (var c = a.childNodes, i = c.length; i--; ) {
@ -135,7 +141,7 @@ function isHTML(str) {
} }
//Generates a random alphanumeric string with N characters //Generates a random alphanumeric string with N characters
function generateRandomString(n) { function generateRandomString(n: number): string {
var str = "", var str = "",
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@ -147,4 +153,4 @@ function generateRandomString(n) {
export {getIndicesOf, convertTimeMsToTimeElapsedString, longestCommonStart, export {getIndicesOf, convertTimeMsToTimeElapsedString, longestCommonStart,
isString, containsAllStrings, formatNumber, isString, containsAllStrings, formatNumber,
numOccurrences, numNetscriptOperators, isHTML, generateRandomString}; numOccurrences, numNetscriptOperators, isHTML, generateRandomString, replaceAt};