prettify, sorry for the big ass commit

This commit is contained in:
Olivier Gagnon
2021-09-04 19:09:30 -04:00
parent 3d7cdb4ef9
commit a18bdd6afc
554 changed files with 91615 additions and 66138 deletions
+31 -32
View File
@@ -6,45 +6,44 @@ import { IMap } from "../types";
type cbFn = (...args: any[]) => any;
export interface ISubscriber {
/**
* Callback function that will be run when an event is emitted
*/
cb: cbFn;
/**
* Callback function that will be run when an event is emitted
*/
cb: cbFn;
/**
* Name/identifier for this subscriber
*/
id: string;
/**
* Name/identifier for this subscriber
*/
id: string;
}
export class EventEmitter {
/**
* Map of Subscriber name -> Callback function
*/
subscribers: IMap<cbFn> = {};
/**
* Map of Subscriber name -> Callback function
*/
subscribers: IMap<cbFn> = {};
constructor(subs?: ISubscriber[]) {
if (Array.isArray(subs)) {
for (const s of subs) {
this.addSubscriber(s);
}
}
constructor(subs?: ISubscriber[]) {
if (Array.isArray(subs)) {
for (const s of subs) {
this.addSubscriber(s);
}
}
}
addSubscriber(s: ISubscriber): void {
this.subscribers[s.id] = s.cb;
}
emitEvent(...args: any[]): void {
for (const s in this.subscribers) {
const sub = this.subscribers[s];
sub(args);
}
}
removeSubscriber(id: string): void {
delete this.subscribers[id];
addSubscriber(s: ISubscriber): void {
this.subscribers[s.id] = s.cb;
}
emitEvent(...args: any[]): void {
for (const s in this.subscribers) {
const sub = this.subscribers[s];
sub(args);
}
}
removeSubscriber(id: string): void {
delete this.subscribers[id];
}
}
+50 -44
View File
@@ -2,59 +2,65 @@
* This is an object that is used to keep track of where all of the player's
* money is coming from (or going to)
*/
import { Generic_fromJSON, Generic_toJSON, Reviver } from "../../utils/JSONReviver";
import {
Generic_fromJSON,
Generic_toJSON,
Reviver,
} from "../../utils/JSONReviver";
export class MoneySourceTracker {
// eslint-disable-next-line @typescript-eslint/ban-types
[key: string]: number | Function;
bladeburner = 0;
casino = 0;
class = 0;
codingcontract = 0;
corporation = 0;
crime = 0;
gang = 0;
hacking = 0;
hacknetnode = 0;
hospitalization = 0;
infiltration = 0;
sleeves = 0;
stock = 0;
total = 0;
work = 0;
// eslint-disable-next-line @typescript-eslint/ban-types
[key: string]: number | Function;
// Record money earned
record(amt: number, source: string): void {
const sanitizedSource = source.toLowerCase();
if (typeof this[sanitizedSource] !== "number") {
console.warn(`MoneySourceTracker.record() called with invalid source: ${source}`);
return;
}
bladeburner = 0;
casino = 0;
class = 0;
codingcontract = 0;
corporation = 0;
crime = 0;
gang = 0;
hacking = 0;
hacknetnode = 0;
hospitalization = 0;
infiltration = 0;
sleeves = 0;
stock = 0;
total = 0;
work = 0;
(<number> this[sanitizedSource]) += amt;
this.total += amt;
// Record money earned
record(amt: number, source: string): void {
const sanitizedSource = source.toLowerCase();
if (typeof this[sanitizedSource] !== "number") {
console.warn(
`MoneySourceTracker.record() called with invalid source: ${source}`,
);
return;
}
// Reset the money tracker by setting all stats to 0
reset(): void {
for (const prop in this) {
if (typeof this[prop] === "number") {
(this[prop] as number) = 0;
}
}
}
(<number>this[sanitizedSource]) += amt;
this.total += amt;
}
// Serialize the current object to a JSON save state.
toJSON(): any {
return Generic_toJSON("MoneySourceTracker", this);
// Reset the money tracker by setting all stats to 0
reset(): void {
for (const prop in this) {
if (typeof this[prop] === "number") {
(this[prop] as number) = 0;
}
}
}
// Initiatizes a MoneySourceTracker object from a JSON save state.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): MoneySourceTracker {
return Generic_fromJSON(MoneySourceTracker, value.data);
}
// Serialize the current object to a JSON save state.
toJSON(): any {
return Generic_toJSON("MoneySourceTracker", this);
}
// Initiatizes a MoneySourceTracker object from a JSON save state.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
static fromJSON(value: any): MoneySourceTracker {
return Generic_fromJSON(MoneySourceTracker, value.data);
}
}
Reviver.constructors.MoneySourceTracker = MoneySourceTracker;
+27 -19
View File
@@ -16,30 +16,38 @@
* can be called with the stat and the exponential/linear factors. The other is a
* class where the exponential and linear factors are defined upon construction.
*/
export function calculateEffectWithFactors(n: number, expFac: number, linearFac: number): number {
if (expFac <= 0 || expFac >= 1) {
console.warn(`Exponential factor is ${expFac}. This is not an intended value for it`);
}
if (linearFac < 1) {
console.warn(`Linear factor is ${linearFac}. This is not an intended value for it`);
}
export function calculateEffectWithFactors(
n: number,
expFac: number,
linearFac: number,
): number {
if (expFac <= 0 || expFac >= 1) {
console.warn(
`Exponential factor is ${expFac}. This is not an intended value for it`,
);
}
if (linearFac < 1) {
console.warn(
`Linear factor is ${linearFac}. This is not an intended value for it`,
);
}
return (Math.pow(n, expFac)) + (n / linearFac);
return Math.pow(n, expFac) + n / linearFac;
}
export class EffectWithFactors {
// Exponential factor
private expFac: number;
// Exponential factor
private expFac: number;
// Linear Factor
private linearFac: number;
// Linear Factor
private linearFac: number;
constructor(expFac: number, linearFac: number) {
this.expFac = expFac;
this.linearFac = linearFac;
}
constructor(expFac: number, linearFac: number) {
this.expFac = expFac;
this.linearFac = linearFac;
}
calculate(n: number): number {
return calculateEffectWithFactors(n, this.expFac, this.linearFac);
}
calculate(n: number): number {
return calculateEffectWithFactors(n, this.expFac, this.linearFac);
}
}
+5 -5
View File
@@ -2,11 +2,11 @@
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
export function createRandomString(n: number): string {
let str = "";
let str = "";
for (let i = 0; i < n; ++i) {
str += chars.charAt(Math.floor(Math.random() * chars.length));
}
for (let i = 0; i < n; ++i) {
str += chars.charAt(Math.floor(Math.random() * chars.length));
}
return str;
return str;
}
+6 -2
View File
@@ -2,7 +2,11 @@
// For this, a 2D array is an array which contains only other arrays.
// If one element in the array is a number or string, it is NOT a 2D array
export function is2DArray(arr: any[]): boolean {
if (arr.constructor !== Array) { return false; }
if (arr.constructor !== Array) {
return false;
}
return arr.every((e) => { return e.constructor === Array; });
return arr.every((e) => {
return e.constructor === Array;
});
}
+1 -1
View File
@@ -3,5 +3,5 @@
* must be a "number" type and cannot be NaN
*/
export function isValidNumber(n: number): boolean {
return (typeof n === "number") && !isNaN(n);
return typeof n === "number" && !isNaN(n);
}