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";
import {logBoxCreate} from "../utils/LogBox.js";
import numeral from "numeral/min/numeral.min";
import {formatNumber} from "../utils/StringHelperFunctions.js";
import {formatNumber} from "../utils/StringHelperFunctions";
/* {
* serverName: {

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

@ -17,7 +17,7 @@ import {getRandomInt, addOffset, clearObject,
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js";
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"];

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

@ -14,7 +14,7 @@ import {getRandomInt, removeElementById,
import {Reviver, Generic_toJSON,
Generic_fromJSON} from "../utils/JSONReviver.js";
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,
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,

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

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

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

@ -9,7 +9,7 @@ import {clearEventListeners, createElement,
getElementById} from "../utils/HelperFunctions.js";
import {Reviver, Generic_toJSON,
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.

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

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

@ -6,7 +6,7 @@ import {dialogBoxCreate} from "../utils/DialogBox.js"
import {addOffset, getRandomInt,
clearEventListenersEl,
clearEventListeners} from "../utils/HelperFunctions.js";
import {formatNumber, isString} from "../utils/StringHelperFunctions.js";
import {formatNumber, isString} from "../utils/StringHelperFunctions";
import jsplumb from 'jsplumb'
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 {printArray} from "../utils/HelperFunctions.js";
import {isValidIPAddress} from "../utils/IPAddress.js";
import {isString} from "../utils/StringHelperFunctions.js";
import {isString} from "../utils/StringHelperFunctions";
var Promise = require("bluebird");

@ -51,7 +51,7 @@ import Decimal from "decimal.js";
import {dialogBoxCreate} from "../utils/DialogBox.js";
import {printArray, powerOfTwo} from "../utils/HelperFunctions.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,
yesNoBoxGetNoButton, yesNoBoxCreate,
yesNoBoxOpen} from "../utils/YesNoBox.js";

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

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

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

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

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

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

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