corp api fixes

* added early out in UpgradeWarehouse when not enough cash
* added checks to enforce maxProducts
* added checks to enforce export material is valid for location
* added checks for market ti reasearches
This commit is contained in:
phyzical 2022-02-11 23:48:19 +08:00
parent 9294ff3e9e
commit 8b55b0293c
3 changed files with 36 additions and 10 deletions

@ -19,7 +19,6 @@ export function NewIndustry(corporation: ICorporation, industry: string, name: s
for (let i = 0; i < corporation.divisions.length; ++i) {
if (corporation.divisions[i].name === name) {
throw new Error("This division name is already in use!");
return;
}
}
@ -290,6 +289,7 @@ export function PurchaseWarehouse(corp: ICorporation, division: IIndustry, city:
export function UpgradeWarehouse(corp: ICorporation, division: IIndustry, warehouse: Warehouse): void {
const sizeUpgradeCost = CorporationConstants.WarehouseUpgradeBaseCost * Math.pow(1.07, warehouse.level + 1);
if (corp.funds < sizeUpgradeCost) return;
++warehouse.level;
warehouse.updateSize(corp, division);
corp.funds = corp.funds - sizeUpgradeCost;
@ -343,17 +343,29 @@ export function MakeProduct(
if (corp.funds < designInvest + marketingInvest) {
throw new Error("You don't have enough company funds to make this large of an investment");
}
let maxProducts = 3
if (division.hasResearch("uPgrade: Capacity.II")) {
maxProducts = 5
} else if (division.hasResearch("uPgrade: Capacity.I")) {
maxProducts = 4
}
const products = division.products
if (Object.keys(products).length >= maxProducts) {
throw new Error(`You are already at the max products (${maxProducts}) for division: ${division.name}!`);
}
const product = new Product({
name: productName.replace(/[<>]/g, ""), //Sanitize for HTMl elements
createCity: city,
designCost: designInvest,
advCost: marketingInvest,
});
if (division.products[product.name] instanceof Product) {
if (products[product.name] instanceof Product) {
throw new Error(`You already have a product with this name!`);
}
corp.funds = corp.funds - (designInvest + marketingInvest);
division.products[product.name] = product;
products[product.name] = product;
}
export function Research(division: IIndustry, researchName: string): void {
@ -372,7 +384,7 @@ export function Research(division: IIndustry, researchName: string): void {
division.researched[researchName] = true;
}
export function ExportMaterial(divisionName: string, cityName: string, material: Material, amt: string): void {
export function ExportMaterial(divisionName: string, cityName: string, material: Material, amt: string, warehouse?: Warehouse | 0): void {
// Sanitize amt
let sanitizedAmt = amt.replace(/\s+/g, "").toUpperCase();
sanitizedAmt = sanitizedAmt.replace(/[^-()\d/*+.MAX]/g, "");
@ -388,6 +400,11 @@ export function ExportMaterial(divisionName: string, cityName: string, material:
if (n == null || isNaN(n) || n < 0) {
throw new Error("Invalid amount entered for export");
}
if (!warehouse || !warehouse.materials.hasOwnProperty(material.name)) {
throw new Error(`You cannot export material: ${material.name} to division: ${divisionName}!`);
}
const exportObj = { ind: divisionName, city: cityName, amt: sanitizedAmt };
material.exp.push(exportObj);
}

@ -50,7 +50,8 @@ export function ExportModal(props: IProps): React.ReactElement {
function exportMaterial(): void {
try {
ExportMaterial(industry, city, props.mat, amt);
const warehouse = currentDivision && currentDivision.warehouses.first
ExportMaterial(industry, city, props.mat, amt, warehouse);
} catch (err) {
dialogBoxCreate(err + "");
}

@ -462,7 +462,7 @@ export function NetscriptCorporation(
const targetCity = helper.string("exportMaterial", "targetCity", atargetCity);
const materialName = helper.string("exportMaterial", "materialName", amaterialName);
const amt = helper.string("exportMaterial", "amt", aamt);
ExportMaterial(targetDivision, targetCity, getMaterial(sourceDivision, sourceCity, materialName), amt + "");
ExportMaterial(targetDivision, targetCity, getMaterial(sourceDivision, sourceCity, materialName), amt + "", getWarehouse(targetDivision,targetCity));
},
cancelExportMaterial: function (
asourceDivision: any,
@ -487,7 +487,9 @@ export function NetscriptCorporation(
const cityName = helper.string("setMaterialMarketTA1", "cityName", acityName);
const materialName = helper.string("setMaterialMarketTA1", "materialName", amaterialName);
const on = helper.boolean(aon);
SetMaterialMarketTA1(getMaterial(divisionName, cityName, materialName), on);
if (!getDivision(divisionName).hasResearch("Market-TA.I"))
throw helper.makeRuntimeErrorMsg(`corporation.setMaterialMarketTA1`, `You have not researched MarketTA.I for division: ${divisionName}`);
SetMaterialMarketTA1( getMaterial(divisionName, cityName, materialName), on);
},
setMaterialMarketTA2: function (adivisionName: any, acityName: any, amaterialName: any, aon: any): void {
checkAccess("setMaterialMarketTA2", 7);
@ -495,6 +497,8 @@ export function NetscriptCorporation(
const cityName = helper.string("setMaterialMarketTA2", "cityName", acityName);
const materialName = helper.string("setMaterialMarketTA2", "materialName", amaterialName);
const on = helper.boolean(aon);
if (!getDivision(divisionName).hasResearch("Market-TA.II"))
throw helper.makeRuntimeErrorMsg(`corporation.setMaterialMarketTA2`, `You have not researched MarketTA.II for division: ${divisionName}`);
SetMaterialMarketTA2(getMaterial(divisionName, cityName, materialName), on);
},
setProductMarketTA1: function (adivisionName: any, aproductName: any, aon: any): void {
@ -502,6 +506,8 @@ export function NetscriptCorporation(
const divisionName = helper.string("setProductMarketTA1", "divisionName", adivisionName);
const productName = helper.string("setProductMarketTA1", "productName", aproductName);
const on = helper.boolean(aon);
if (!getDivision(divisionName).hasResearch("Market-TA.I"))
throw helper.makeRuntimeErrorMsg(`corporation.setProductMarketTA1`, `You have not researched MarketTA.I for division: ${divisionName}`);
SetProductMarketTA1(getProduct(divisionName, productName), on);
},
setProductMarketTA2: function (adivisionName: any, aproductName: any, aon: any): void {
@ -509,6 +515,8 @@ export function NetscriptCorporation(
const divisionName = helper.string("setProductMarketTA2", "divisionName", adivisionName);
const productName = helper.string("setProductMarketTA2", "productName", aproductName);
const on = helper.boolean(aon);
if (!getDivision(divisionName).hasResearch("Market-TA.II"))
throw helper.makeRuntimeErrorMsg(`corporation.setMaterialMarketTA2`, `You have not researched MarketTA.II for division: ${divisionName}`);
SetProductMarketTA2(getProduct(divisionName, productName), on);
},
};