From 13c486d639e10e117fb2743fd2b7d9c46f02e6eb Mon Sep 17 00:00:00 2001 From: phyzical Date: Fri, 1 Apr 2022 22:28:48 +0800 Subject: [PATCH] added logic to support material production limit * added ns functions for limit production and limit material closes #3180 --- src/Corporation/Actions.ts | 9 ++++ src/Corporation/ui/MaterialElem.tsx | 15 ++++++ .../modals/LimitMaterialProductionModal.tsx | 46 +++++++++++++++++++ src/NetscriptFunctions/Corporation.ts | 28 +++++++++++ src/ScriptEditor/NetscriptDefinitions.d.ts | 16 +++++++ 5 files changed, 114 insertions(+) create mode 100644 src/Corporation/ui/modals/LimitMaterialProductionModal.tsx diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index 8855e0478..520d880cd 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -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; } diff --git a/src/Corporation/ui/MaterialElem.tsx b/src/Corporation/ui/MaterialElem.tsx index 3385fa110..fcc0ca6c0 100644 --- a/src/Corporation/ui/MaterialElem.tsx +++ b/src/Corporation/ui/MaterialElem.tsx @@ -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 ( @@ -196,6 +203,14 @@ export function MaterialElem(props: IMaterialProps): React.ReactElement { /> )} + + setLimitProductionOpen(false)} + /> diff --git a/src/Corporation/ui/modals/LimitMaterialProductionModal.tsx b/src/Corporation/ui/modals/LimitMaterialProductionModal.tsx new file mode 100644 index 000000000..e8c9f1ca4 --- /dev/null +++ b/src/Corporation/ui/modals/LimitMaterialProductionModal.tsx @@ -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(null); + + function limitMaterialProduction(): void { + let qty = limit; + if (qty === null) qty = -1; + LimitMaterialProduction(props.material, qty); + props.onClose(); + } + + function onKeyDown(event: React.KeyboardEvent): void { + if (event.key === KEY.ENTER) limitMaterialProduction(); + } + + function onChange(event: React.ChangeEvent): void { + if (event.target.value === "") setLimit(null); + else setLimit(parseFloat(event.target.value)); + } + + return ( + + + Enter a limit to the amount of this material you would like to produce per second. Leave the box empty to set no + limit. + + + + + ); +} diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index 85114bf8e..8fe9f552a 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -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, diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 5e102ef1b..4ff95de9b 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -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