2019-03-13 23:17:30 +01:00
|
|
|
import { Material } from "./Material";
|
2021-08-31 20:47:07 +02:00
|
|
|
import { ICorporation } from "./ICorporation";
|
|
|
|
import { IIndustry } from "./IIndustry";
|
2019-03-13 23:17:30 +01:00
|
|
|
import { MaterialSizes } from "./MaterialSizes";
|
|
|
|
import { IMap } from "../types";
|
|
|
|
import { numeralWrapper } from "../ui/numeralFormat";
|
|
|
|
import { Generic_fromJSON,
|
|
|
|
Generic_toJSON,
|
|
|
|
Reviver } from "../../utils/JSONReviver";
|
2019-03-15 10:37:06 +01:00
|
|
|
import { exceptionAlert } from "../../utils/helpers/exceptionAlert";
|
|
|
|
|
2019-03-13 23:17:30 +01:00
|
|
|
interface IConstructorParams {
|
2021-08-31 20:47:07 +02:00
|
|
|
corp?: ICorporation;
|
|
|
|
industry?: IIndustry;
|
2019-03-13 23:17:30 +01:00
|
|
|
loc?: string;
|
|
|
|
size?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Warehouse {
|
|
|
|
|
|
|
|
// Text that describes how the space in this Warehouse is being used
|
|
|
|
// Used to create a tooltip in the UI
|
2021-04-30 05:52:56 +02:00
|
|
|
breakdown = "";
|
2019-03-13 23:17:30 +01:00
|
|
|
|
|
|
|
// Warehouse's level, which affects its maximum size
|
2021-04-30 05:52:56 +02:00
|
|
|
level = 1;
|
2019-03-13 23:17:30 +01:00
|
|
|
|
|
|
|
// City that this Warehouse is in
|
|
|
|
loc: string;
|
|
|
|
|
|
|
|
// Map of Materials held by this Warehouse
|
|
|
|
materials: IMap<Material>;
|
|
|
|
|
|
|
|
// Maximum amount warehouse can hold
|
|
|
|
size: number;
|
|
|
|
|
|
|
|
// Amount of space currently used by warehouse
|
2021-04-30 05:52:56 +02:00
|
|
|
sizeUsed = 0;
|
2019-03-13 23:17:30 +01:00
|
|
|
|
|
|
|
// Whether Smart Supply is enabled for this Industry (the Industry that this Warehouse is for)
|
2021-04-30 05:52:56 +02:00
|
|
|
smartSupplyEnabled = false;
|
2019-03-13 23:17:30 +01:00
|
|
|
|
2019-03-18 01:58:06 +01:00
|
|
|
// Flag that indicates whether Smart Supply accounts for imports when calculating
|
|
|
|
// the amount fo purchase
|
2021-04-30 05:52:56 +02:00
|
|
|
smartSupplyConsiderExports = false;
|
2019-03-18 01:58:06 +01:00
|
|
|
|
2019-03-13 23:17:30 +01:00
|
|
|
// Stores the amount of product to be produced. Used for Smart Supply unlock.
|
|
|
|
// The production tracked by smart supply is always based on the previous cycle,
|
|
|
|
// so it will always trail the "true" production by 1 cycle
|
2021-04-30 05:52:56 +02:00
|
|
|
smartSupplyStore = 0;
|
2019-03-13 23:17:30 +01:00
|
|
|
|
|
|
|
constructor(params: IConstructorParams = {}) {
|
|
|
|
this.loc = params.loc ? params.loc : "";
|
|
|
|
this.size = params.size ? params.size : 0;
|
|
|
|
|
|
|
|
this.materials = {
|
|
|
|
Water: new Material({name: "Water"}),
|
|
|
|
Energy: new Material({name: "Energy"}),
|
|
|
|
Food: new Material({name: "Food"}),
|
|
|
|
Plants: new Material({name: "Plants"}),
|
|
|
|
Metal: new Material({name: "Metal"}),
|
|
|
|
Hardware: new Material({name: "Hardware"}),
|
|
|
|
Chemicals: new Material({name: "Chemicals"}),
|
|
|
|
Drugs: new Material({name: "Drugs"}),
|
|
|
|
Robots: new Material({name: "Robots"}),
|
|
|
|
AICores: new Material({name: "AI Cores"}),
|
2021-04-30 05:52:56 +02:00
|
|
|
RealEstate: new Material({name: "Real Estate"}),
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
2019-03-15 10:37:06 +01:00
|
|
|
|
|
|
|
if (params.corp && params.industry) {
|
|
|
|
this.updateSize(params.corp, params.industry);
|
|
|
|
}
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-calculate how much space is being used by this Warehouse
|
2021-05-01 09:17:31 +02:00
|
|
|
updateMaterialSizeUsed(): void {
|
2019-03-13 23:17:30 +01:00
|
|
|
this.sizeUsed = 0;
|
|
|
|
this.breakdown = "";
|
|
|
|
for (const matName in this.materials) {
|
2021-04-30 05:52:56 +02:00
|
|
|
const mat = this.materials[matName];
|
2019-03-13 23:17:30 +01:00
|
|
|
if (MaterialSizes.hasOwnProperty(matName)) {
|
|
|
|
this.sizeUsed += (mat.qty * MaterialSizes[matName]);
|
|
|
|
if (mat.qty > 0) {
|
2019-03-15 10:37:06 +01:00
|
|
|
this.breakdown += (matName + ": " + numeralWrapper.format(mat.qty * MaterialSizes[matName], "0,0.0") + "<br>");
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.sizeUsed > this.size) {
|
|
|
|
console.warn("Warehouse size used greater than capacity, something went wrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:47:07 +02:00
|
|
|
updateSize(corporation: ICorporation, industry: IIndustry): void {
|
2019-03-15 10:37:06 +01:00
|
|
|
try {
|
|
|
|
this.size = (this.level * 100)
|
|
|
|
* corporation.getStorageMultiplier()
|
|
|
|
* industry.getStorageMultiplier();
|
|
|
|
} catch(e) {
|
|
|
|
exceptionAlert(e);
|
|
|
|
}
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the current object to a JSON save state.
|
|
|
|
toJSON(): any {
|
|
|
|
return Generic_toJSON("Warehouse", this);
|
|
|
|
}
|
2021-05-01 09:17:31 +02:00
|
|
|
|
|
|
|
// Initiatizes a Warehouse object from a JSON save state.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
static fromJSON(value: any): Warehouse {
|
|
|
|
return Generic_fromJSON(Warehouse, value.data);
|
|
|
|
}
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.Warehouse = Warehouse;
|