Skip to content

Commit

Permalink
CORPORATION: Fix "Use same 'Sell Amount' for all cities" toggle for p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Caldwell-74 authored Sep 12, 2023
1 parent d2b3659 commit bf5e638
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
41 changes: 17 additions & 24 deletions src/Corporation/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export function SellMaterial(material: Material, amount: string, price: string):

export function SellProduct(product: Product, city: CityName, amt: string, price: string, all: boolean): void {
//Parse price
// initliaze newPrice with oldPrice as default
let newPrice = product.cityData[city].desiredSellPrice;
if (price.includes("MP")) {
//Dynamically evaluated quantity. First test to make sure its valid
//Sanitize input, then replace dynamic variables with arbitrary numbers
Expand All @@ -181,17 +183,19 @@ export function SellProduct(product: Product, city: CityName, amt: string, price
if (temp == null || isNaN(parseFloat(temp))) {
throw new Error("Invalid value or expression for sell price field.");
}
product.cityData[city].desiredSellPrice = price; //Use sanitized price
newPrice = price; //Use sanitized price
} else {
const cost = parseFloat(price);
if (isNaN(cost)) {
throw new Error("Invalid value for sell price field");
}
product.cityData[city].desiredSellPrice = cost;
newPrice = cost;
}

// Parse quantity
amt = amt.toUpperCase();
//initialize newAmount with old as default
let newAmount = product.cityData[city].desiredSellAmount;
if (amt.includes("MAX") || amt.includes("PROD") || amt.includes("INV")) {
//Dynamically evaluated quantity. First test to make sure its valid
let qty = amt.replace(/\s+/g, "");
Expand All @@ -208,36 +212,25 @@ export function SellProduct(product: Product, city: CityName, amt: string, price
if (temp == null || isNaN(parseFloat(temp))) {
throw new Error("Invalid value or expression for sell quantity field");
}

if (all) {
for (const cityName of Object.values(CityName)) {
product.cityData[cityName].desiredSellAmount = qty; //Use sanitized input
}
} else {
product.cityData[city].desiredSellAmount = qty; //Use sanitized input
}
newAmount = qty; //Use sanitized input
} else if (isNaN(parseFloat(amt)) || parseFloat(amt) < 0) {
throw new Error("Invalid value for sell quantity field! Must be numeric or 'PROD' or 'MAX'");
} else {
let qty = parseFloat(amt);
if (isNaN(qty)) {
qty = 0;
}
if (qty === 0) {
if (all) {
for (const cityName of Object.values(CityName)) {
product.cityData[cityName].desiredSellAmount = 0;
}
} else {
product.cityData[city].desiredSellAmount = 0;
}
} else if (all) {
for (const cityName of Object.values(CityName)) {
product.cityData[cityName].desiredSellAmount = qty;
}
} else {
product.cityData[city].desiredSellAmount = qty;
newAmount = qty;
}
//apply new price and amount to all or just current
if (all) {
for (const cityName of Object.values(CityName)) {
product.cityData[cityName].desiredSellAmount = newAmount;
product.cityData[cityName].desiredSellPrice = newPrice;
}
} else {
product.cityData[city].desiredSellAmount = newAmount;
product.cityData[city].desiredSellPrice = newPrice;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Corporation/ui/modals/SellProductModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function SellProductModal(props: IProps): React.ReactElement {
<Button onClick={sellProduct}>Confirm</Button>
<FormControlLabel
control={<Switch checked={checked} onChange={onCheckedChange} />}
label={<Typography>Use same 'Sell Amount' for all cities</Typography>}
label={<Typography>Set for all cities</Typography>}
/>
</Modal>
);
Expand Down
2 changes: 1 addition & 1 deletion src/ScriptEditor/NetscriptDefinitions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7138,7 +7138,7 @@ export interface WarehouseAPI {
* @param productName - Name of the product
* @param amt - Amount to sell, can be "MAX"
* @param price - Price to sell, can be "MP"
* @param all - Sell in all city
* @param all - Set sell amount and price in all cities
*/
sellProduct(
divisionName: string,
Expand Down

0 comments on commit bf5e638

Please sign in to comment.