added logic to support material production limit

* added ns functions for limit production and limit material
closes #3180
This commit is contained in:
phyzical 2022-04-01 22:28:48 +08:00
parent 60f56a55ab
commit 13c486d639
5 changed files with 114 additions and 0 deletions

@ -497,6 +497,15 @@ export function LimitProductProduction(product: Product, cityName: string, qty:
}
}
export function LimitMaterialProduction(material: Material, qty: number): void {
if (qty < 0 || isNaN(qty)) {
material.prdman[0] = false;
} else {
material.prdman[0] = true;
material.prdman[1] = qty;
}
}
export function SetMaterialMarketTA1(material: Material, on: boolean): void {
material.marketTa1 = on;
}

@ -21,6 +21,7 @@ import Tooltip from "@mui/material/Tooltip";
import Paper from "@mui/material/Paper";
import Button from "@mui/material/Button";
import Box from "@mui/material/Box";
import { LimitMaterialProductionModal } from "./modals/LimitMaterialProductionModal";
interface IMaterialProps {
warehouse: Warehouse;
@ -112,6 +113,12 @@ export function MaterialElem(props: IMaterialProps): React.ReactElement {
sellButtonText = <>Sell (0.000/0.000)</>;
}
// Limit Production button
let limitMaterialButtonText = "Limit Material";
if (mat.prdman[0]) {
limitMaterialButtonText += " (" + numeralWrapper.format(mat.prdman[1], nf) + ")";
}
return (
<Paper>
<Box sx={{ display: "grid", gridTemplateColumns: "2fr 1fr", m: "5px" }}>
@ -196,6 +203,14 @@ export function MaterialElem(props: IMaterialProps): React.ReactElement {
/>
</>
)}
<Button color={tutorial ? "error" : "primary"} onClick={() => setLimitProductionOpen(true)}>
{limitMaterialButtonText}
</Button>
<LimitMaterialProductionModal
material={mat}
open={limitProductionOpen}
onClose={() => setLimitProductionOpen(false)}
/>
</Box>
</Box>
</Paper>

@ -0,0 +1,46 @@
import React, { useState } from "react";
import { LimitMaterialProduction } from "../../Actions";
import { Modal } from "../../../ui/React/Modal";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
import { KEY } from "../../../utils/helpers/keyCodes";
import { Material } from "../../Material";
interface IProps {
open: boolean;
onClose: () => void;
material: Material;
}
// Create a popup that lets the player limit the production of a product
export function LimitMaterialProductionModal(props: IProps): React.ReactElement {
const [limit, setLimit] = useState<number | null>(null);
function limitMaterialProduction(): void {
let qty = limit;
if (qty === null) qty = -1;
LimitMaterialProduction(props.material, qty);
props.onClose();
}
function onKeyDown(event: React.KeyboardEvent<HTMLInputElement>): void {
if (event.key === KEY.ENTER) limitMaterialProduction();
}
function onChange(event: React.ChangeEvent<HTMLInputElement>): void {
if (event.target.value === "") setLimit(null);
else setLimit(parseFloat(event.target.value));
}
return (
<Modal open={props.open} onClose={props.onClose}>
<Typography>
Enter a limit to the amount of this material you would like to produce per second. Leave the box empty to set no
limit.
</Typography>
<TextField autoFocus={true} placeholder="Limit" type="number" onChange={onChange} onKeyDown={onKeyDown} />
<Button onClick={limitMaterialProduction}>Limit production</Button>
</Modal>
);
}

@ -53,6 +53,8 @@ import {
SellShares,
BuyBackShares,
SetSmartSupplyUseLeftovers,
LimitMaterialProduction,
LimitProductProduction,
} from "../Corporation/Actions";
import { CorporationUnlockUpgrades } from "../Corporation/data/CorporationUnlockUpgrades";
import { CorporationUpgrades } from "../Corporation/data/CorporationUpgrades";
@ -498,6 +500,19 @@ export function NetscriptCorporation(
const corporation = getCorporation();
MakeProduct(corporation, getDivision(divisionName), cityName, productName, designInvest, marketingInvest);
},
limitProductProduction: function (
_divisionName: unknown,
_productName: unknown,
_cityName: unknown,
_qty: unknown,
) {
checkAccess("limitProductProduction", 7);
const divisionName = helper.string("limitProductProduction", "divisionName", _divisionName);
const cityName = helper.city("limitMaterialProduction", "cityName", _cityName);
const productName = helper.string("limitProductProduction", "productName", _productName);
const qty = helper.number("limitMaterialProduction", "qty", _qty);
LimitProductProduction(getProduct(divisionName, productName), cityName, qty);
},
exportMaterial: function (
_sourceDivision: unknown,
_sourceCity: unknown,
@ -538,6 +553,19 @@ export function NetscriptCorporation(
const amt = helper.string("cancelExportMaterial", "amt", _amt);
CancelExportMaterial(targetDivision, targetCity, getMaterial(sourceDivision, sourceCity, materialName), amt + "");
},
limitMaterialProduction: function (
_divisionName: unknown,
_cityName: unknown,
_materialName: unknown,
_qty: unknown,
) {
checkAccess("limitMaterialProduction", 7);
const divisionName = helper.string("limitMaterialProduction", "divisionName", _divisionName);
const cityName = helper.city("limitMaterialProduction", "cityName", _cityName);
const materialName = helper.string("limitMaterialProduction", "materialName", _materialName);
const qty = helper.number("limitMaterialProduction", "qty", _qty);
LimitMaterialProduction(getMaterial(divisionName, cityName, materialName), qty);
},
setMaterialMarketTA1: function (
_divisionName: unknown,
_cityName: unknown,

@ -6656,6 +6656,22 @@ export interface WarehouseAPI {
designInvest: number,
marketingInvest: number,
): void;
/**
* Limit Material Production.
* @param divisionName - Name of the division
* @param cityName - Name of the city
* @param materialName - Name of the material
* @param qty - Amount to limit to
*/
limitMaterialProduction(divisionName: string, cityName: string, materialName: string, qty: number): void;
/**
* Limit Product Production.
* @param divisionName - Name of the division
* @param cityName - Name of the city
* @param productName - Name of the product
* @param qty - Amount to limit to
*/
limitProductProduction(divisionName: string, cityName: string, productName: string, qty: number): void;
/**
* Gets the cost to purchase a warehouse
* @returns cost