2021-09-05 01:09:30 +02:00
|
|
|
import { Material } from "./Material";
|
2022-09-20 12:47:54 +02:00
|
|
|
import { Corporation } from "./Corporation";
|
|
|
|
import { Industry } from "./Industry";
|
2022-12-25 09:33:13 +01:00
|
|
|
import { MaterialInfo } from "./MaterialInfo";
|
2022-07-15 01:00:10 +02:00
|
|
|
import { Generic_fromJSON, Generic_toJSON, IReviverValue, Reviver } from "../utils/JSONReviver";
|
2021-09-25 20:42:57 +02:00
|
|
|
import { exceptionAlert } from "../utils/helpers/exceptionAlert";
|
2022-12-30 02:28:53 +01:00
|
|
|
import { CityName } from "../Enums";
|
|
|
|
import { CorpMaterialName } from "@nsdefs";
|
|
|
|
import { materialNames } from "./data/Constants";
|
2019-03-15 10:37:06 +01:00
|
|
|
|
2019-03-13 23:17:30 +01:00
|
|
|
interface IConstructorParams {
|
2022-09-20 12:47:54 +02:00
|
|
|
corp?: Corporation;
|
|
|
|
industry?: Industry;
|
2022-12-30 02:28:53 +01:00
|
|
|
loc?: CityName;
|
2021-09-05 01:09:30 +02:00
|
|
|
size?: number;
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Warehouse {
|
2021-09-05 01:09:30 +02:00
|
|
|
// Warehouse's level, which affects its maximum size
|
|
|
|
level = 1;
|
|
|
|
|
|
|
|
// City that this Warehouse is in
|
2022-12-30 02:28:53 +01:00
|
|
|
loc: CityName;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Map of Materials held by this Warehouse
|
2022-12-30 02:28:53 +01:00
|
|
|
materials: Record<CorpMaterialName, Material>;
|
2021-09-05 01:09:30 +02:00
|
|
|
|
|
|
|
// Maximum amount warehouse can hold
|
|
|
|
size: number;
|
|
|
|
|
|
|
|
// Amount of space currently used by warehouse
|
|
|
|
sizeUsed = 0;
|
|
|
|
|
|
|
|
// Whether Smart Supply is enabled for this Industry (the Industry that this Warehouse is for)
|
|
|
|
smartSupplyEnabled = false;
|
|
|
|
|
2021-09-07 22:46:36 +02:00
|
|
|
// Decide if smart supply should use the materials already in the warehouse when deciding on the amount to buy.
|
2022-12-30 02:28:53 +01:00
|
|
|
smartSupplyUseLeftovers: Record<CorpMaterialName, boolean>;
|
2021-09-07 22:46:36 +02:00
|
|
|
|
2021-09-05 01:09:30 +02: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
|
|
|
|
smartSupplyStore = 0;
|
|
|
|
|
|
|
|
constructor(params: IConstructorParams = {}) {
|
2022-12-30 02:28:53 +01:00
|
|
|
this.loc = params.loc ? params.loc : CityName.Sector12;
|
2021-09-05 01:09:30 +02:00
|
|
|
this.size = params.size ? params.size : 0;
|
|
|
|
|
2022-12-30 02:28:53 +01:00
|
|
|
this.materials = {} as Record<CorpMaterialName, Material>;
|
|
|
|
this.smartSupplyUseLeftovers = {} as Record<CorpMaterialName, boolean>;
|
|
|
|
for (const matName of materialNames) {
|
|
|
|
this.materials[matName] = new Material({ name: matName });
|
|
|
|
this.smartSupplyUseLeftovers[matName] = true;
|
|
|
|
}
|
2021-09-07 22:46:36 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (params.corp && params.industry) {
|
|
|
|
this.updateSize(params.corp, params.industry);
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
2021-09-15 04:01:23 +02:00
|
|
|
|
|
|
|
// Default smart supply to being enabled if the upgrade is unlocked
|
|
|
|
if (params.corp?.unlockUpgrades[1]) {
|
|
|
|
this.smartSupplyEnabled = true;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-calculate how much space is being used by this Warehouse
|
|
|
|
updateMaterialSizeUsed(): void {
|
|
|
|
this.sizeUsed = 0;
|
2022-12-30 02:28:53 +01:00
|
|
|
for (const matName of Object.values(materialNames)) {
|
2021-09-05 01:09:30 +02:00
|
|
|
const mat = this.materials[matName];
|
2022-12-30 02:28:53 +01:00
|
|
|
this.sizeUsed += mat.qty * MaterialInfo[matName].size;
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
if (this.sizeUsed > this.size) {
|
2021-09-09 05:47:34 +02:00
|
|
|
console.warn("Warehouse size used greater than capacity, something went wrong");
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 12:47:54 +02:00
|
|
|
updateSize(corporation: Corporation, industry: Industry): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-09 05:47:34 +02:00
|
|
|
this.size = this.level * 100 * corporation.getStorageMultiplier() * industry.getStorageMultiplier();
|
2022-07-15 07:51:30 +02:00
|
|
|
} catch (e: unknown) {
|
2021-09-05 01:09:30 +02:00
|
|
|
exceptionAlert(e);
|
2021-05-01 09:17:31 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the current object to a JSON save state.
|
2022-07-15 01:00:10 +02:00
|
|
|
toJSON(): IReviverValue {
|
2021-09-05 01:09:30 +02:00
|
|
|
return Generic_toJSON("Warehouse", this);
|
|
|
|
}
|
|
|
|
|
2022-10-09 07:25:31 +02:00
|
|
|
// Initializes a Warehouse object from a JSON save state.
|
2022-07-15 01:00:10 +02:00
|
|
|
static fromJSON(value: IReviverValue): Warehouse {
|
2022-12-30 02:28:53 +01:00
|
|
|
//Gracefully load saves where AICores and RealEstate material names sometimes did not use spaces
|
|
|
|
if (value.data?.materials?.AICores) {
|
|
|
|
value.data.materials["AI Cores"] = value.data.materials.AICores;
|
|
|
|
value.data.smartSupplyUseLeftovers["AI Cores"] = value.data.smartSupplyUseLeftovers.AICores;
|
|
|
|
delete value.data.materials.AICores;
|
|
|
|
delete value.data.smartSupplyUseLeftovers.AICores;
|
|
|
|
}
|
|
|
|
if (value.data?.materials?.RealEstate) {
|
|
|
|
value.data.materials["Real Estate"] = value.data.materials.RealEstate;
|
|
|
|
value.data.smartSupplyUseLeftovers["Real Estate"] = value.data.smartSupplyUseLeftovers.RealEstate;
|
|
|
|
delete value.data.materials.RealEstate;
|
|
|
|
delete value.data.smartSupplyUseLeftovers.RealEstate;
|
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
return Generic_fromJSON(Warehouse, value.data);
|
|
|
|
}
|
2019-03-13 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Reviver.constructors.Warehouse = Warehouse;
|