CORP: Better fix for MP validation (#158)

* Prevent negative profit selling
* Update validation strings for MP
* Allow negative MP formula price
This commit is contained in:
Snarling 2022-10-25 12:51:03 -04:00 committed by GitHub
parent 5b2a96fa0c
commit 959307aa13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 5 deletions

@ -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

@ -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;
}