bitburner-src/src/Company/Companies.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

// Constructs all CompanyPosition objects using the metadata in data/companypositions.ts
2021-09-05 01:09:30 +02:00
import { companiesMetadata } from "./data/CompaniesMetadata";
import { Company, IConstructorParams } from "./Company";
import { IMap } from "../types";
2021-09-25 20:42:57 +02:00
import { Reviver } from "../utils/JSONReviver";
export let Companies: IMap<Company> = {};
2021-05-01 09:17:31 +02:00
function addCompany(params: IConstructorParams): void {
2021-09-05 01:09:30 +02:00
if (Companies[params.name] != null) {
console.warn(`Duplicate Company Position being defined: ${params.name}`);
}
Companies[params.name] = new Company(params);
}
// Used to initialize new Company objects for the Companies map
// Called when creating new game or after a prestige/reset
2021-05-01 09:17:31 +02:00
export function initCompanies(): void {
2021-09-05 01:09:30 +02:00
// Save Old Company data for 'favor'
const oldCompanies = Companies;
2021-09-05 01:09:30 +02:00
// Re-construct all Companies
Companies = {};
companiesMetadata.forEach((e) => {
addCompany(e);
});
2021-09-05 01:09:30 +02:00
// Reset data
2022-01-16 01:45:03 +01:00
for (const companyName of Object.keys(Companies)) {
2021-09-05 01:09:30 +02:00
const company = Companies[companyName];
const oldCompany = oldCompanies[companyName];
if (!(oldCompany instanceof Company)) {
// New game, so no OldCompanies data
company.favor = 0;
} else {
company.favor = oldCompanies[companyName].favor;
if (isNaN(company.favor)) {
company.favor = 0;
}
}
2021-09-05 01:09:30 +02:00
}
}
// Used to load Companies map from a save
2021-05-01 09:17:31 +02:00
export function loadCompanies(saveString: string): void {
2021-09-05 01:09:30 +02:00
Companies = JSON.parse(saveString, Reviver);
}