From 959307aa13ace29a82eb8ddc21ee26c6840cc1ee Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Tue, 25 Oct 2022 12:51:03 -0400 Subject: [PATCH] CORP: Better fix for MP validation (#158) * Prevent negative profit selling * Update validation strings for MP * Allow negative MP formula price --- src/Corporation/Actions.ts | 8 ++++---- src/Corporation/Industry.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index 7358e25a8..0b3857fc1 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -95,7 +95,7 @@ export function SellMaterial(mat: Material, amt: string, price: string): void { if (amt === "") amt = "0"; let cost = price.replace(/\s+/g, ""); cost = cost.replace(/[^-()\d/*+.MPe]/g, ""); //Sanitize cost - let temp = cost.replace(/MP/, mat.bCost + ""); + let temp = cost.replace(/MP/, "1.234e5"); try { if (temp.includes("MP")) throw "Only one reference to MP is allowed in sell price."; temp = eval(temp); @@ -103,7 +103,7 @@ export function SellMaterial(mat: Material, amt: string, price: string): void { throw new Error("Invalid value or expression for sell price field: " + e); } - if (temp == null || isNaN(parseFloat(temp)) || parseFloat(temp) < 0) { + if (temp == null || isNaN(parseFloat(temp))) { throw new Error("Invalid value or expression for sell price field"); } @@ -156,14 +156,14 @@ export function SellProduct(product: Product, city: string, amt: string, price: //Sanitize input, then replace dynamic variables with arbitrary numbers price = price.replace(/\s+/g, ""); price = price.replace(/[^-()\d/*+.MPe]/g, ""); - let temp = price.replace(/MP/, product.pCost + ""); + let temp = price.replace(/MP/, "1.234e5"); try { if (temp.includes("MP")) throw "Only one reference to MP is allowed in sell price."; temp = eval(temp); } catch (e) { throw new Error("Invalid value or expression for sell price field: " + e); } - if (temp == null || isNaN(parseFloat(temp)) || parseFloat(temp) < 0) { + if (temp == null || isNaN(parseFloat(temp))) { throw new Error("Invalid value or expression for sell price field."); } product.sCost = price; //Use sanitized price diff --git a/src/Corporation/Industry.ts b/src/Corporation/Industry.ts index 4f0008dff..d42130add 100644 --- a/src/Corporation/Industry.ts +++ b/src/Corporation/Industry.ts @@ -900,7 +900,7 @@ export class Industry { product.mku = 1; } sCost = sCostString.replace(/MP/g, product.pCost + product.rat / product.mku + ""); - sCost = eval(sCost); + sCost = Math.max(product.pCost, eval(sCost)); } else { sCost = product.sCost; }