From 174f10d1f5e9155a0b8e3e63659aad191773e3f6 Mon Sep 17 00:00:00 2001 From: Aleksei Bezrodnov Date: Sat, 10 Jun 2023 01:34:35 +0200 Subject: [PATCH] MISC: tiny corp code style improvement (#586) --- src/Corporation/Actions.ts | 14 ++++---- .../{Corporation.tsx => Corporation.ts} | 12 ++++--- src/Corporation/Division.ts | 2 +- src/Corporation/Product.ts | 2 +- src/Corporation/Warehouse.ts | 8 +++-- src/Corporation/data/Constants.ts | 5 +-- .../IndustryData.ts} | 32 +++-------------- src/Corporation/ui/IndustryDescription.tsx | 34 +++++++++++++++++++ src/Corporation/ui/NewDivisionTab.tsx | 20 +++++------ .../ui/modals/MakeProductModal.tsx | 2 +- src/Corporation/ui/modals/ResearchModal.tsx | 2 +- src/NetscriptFunctions/Corporation.ts | 5 +-- .../Player/PlayerObjectCorporationMethods.ts | 2 +- src/Prestige.ts | 2 +- src/ScriptEditor/NetscriptDefinitions.d.ts | 8 ++++- 15 files changed, 87 insertions(+), 63 deletions(-) rename src/Corporation/{Corporation.tsx => Corporation.ts} (98%) rename src/Corporation/{IndustryData.tsx => data/IndustryData.ts} (89%) create mode 100644 src/Corporation/ui/IndustryDescription.tsx diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index 105568905..9c4d50789 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -1,7 +1,11 @@ +import { isInteger } from "lodash"; + import { Player } from "@player"; +import { CorpResearchName, CorpSmartSupplyOption } from "@nsdefs"; + import { MaterialInfo } from "./MaterialInfo"; import { Corporation } from "./Corporation"; -import { IndustryResearchTrees, IndustriesData } from "./IndustryData"; +import { IndustryResearchTrees, IndustriesData } from "./data/IndustryData"; import { Division } from "./Division"; import * as corpConstants from "./data/Constants"; import { OfficeSpace } from "./OfficeSpace"; @@ -13,8 +17,6 @@ import { ResearchMap } from "./ResearchMap"; import { isRelevantMaterial } from "./ui/Helpers"; import { CityName } from "../Enums"; import { getRandomInt } from "../utils/helpers/getRandomInt"; -import { CorpResearchName } from "@nsdefs"; -import { isInteger } from "lodash"; import { getRecordValues } from "../Types/Record"; export function NewDivision(corporation: Corporation, industry: IndustryType, name: string): void { @@ -206,6 +208,7 @@ 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 @@ -242,10 +245,7 @@ export function SetSmartSupply(warehouse: Warehouse, smartSupply: boolean): void warehouse.smartSupplyEnabled = smartSupply; } -export function SetSmartSupplyOption(warehouse: Warehouse, material: Material, useOption: string): void { - if (!corpConstants.smartSupplyUseOptions.includes(useOption)) { - throw new Error(`Invalid Smart Supply option '${useOption}'`); - } +export function SetSmartSupplyOption(warehouse: Warehouse, material: Material, useOption: CorpSmartSupplyOption): void { warehouse.smartSupplyOptions[material.name] = useOption; } diff --git a/src/Corporation/Corporation.tsx b/src/Corporation/Corporation.ts similarity index 98% rename from src/Corporation/Corporation.tsx rename to src/Corporation/Corporation.ts index 68a7680fd..3f5da798f 100644 --- a/src/Corporation/Corporation.tsx +++ b/src/Corporation/Corporation.ts @@ -36,8 +36,10 @@ export class Corporation { revenue = 0; expenses = 0; fundingRound = 0; - public = false; //Publicly traded - totalShares = corpConstants.initialShares; // Total existing shares + /** Publicly traded */ + public = false; + /** Total existing shares */ + totalShares = corpConstants.initialShares; numShares = corpConstants.initialShares; // Total shares owned by player shareSalesUntilPriceUpdate = corpConstants.sharesPerPriceUpdate; shareSaleCooldown = 0; // Game cycles until player can sell shares again @@ -64,7 +66,7 @@ export class Corporation { state = new CorporationState(); constructor(params: IParams = {}) { - this.name = params.name ? params.name : "The Corporation"; + this.name = params.name || "The Corporation"; this.seedFunded = params.seedFunded ?? false; } @@ -73,14 +75,14 @@ export class Corporation { console.error("Trying to add invalid amount of funds. Report to a developer."); return; } - this.funds = this.funds + amt; + this.funds += amt; } getState(): CorpStateName { return this.state.getState(); } - storeCycles(numCycles = 1): void { + storeCycles(numCycles: number): void { this.storedCycles += numCycles; } diff --git a/src/Corporation/Division.ts b/src/Corporation/Division.ts index a3df66b4d..481383438 100644 --- a/src/Corporation/Division.ts +++ b/src/Corporation/Division.ts @@ -1,6 +1,6 @@ import { constructorsForReviver, Generic_toJSON, Generic_fromJSON, IReviverValue } from "../utils/JSONReviver"; import { CityName } from "../Enums"; -import { IndustryResearchTrees, IndustriesData } from "./IndustryData"; +import { IndustryResearchTrees, IndustriesData } from "./data/IndustryData"; import * as corpConstants from "./data/Constants"; import { CorpEmployeeJob, IndustryType } from "./data/Enums"; import { getRandomInt } from "../utils/helpers/getRandomInt"; diff --git a/src/Corporation/Product.ts b/src/Corporation/Product.ts index 21379e5d9..3963cb091 100644 --- a/src/Corporation/Product.ts +++ b/src/Corporation/Product.ts @@ -1,7 +1,7 @@ import { CorpEmployeeJob } from "./data/Enums"; +import { IndustriesData } from "./data/IndustryData"; import { MaterialInfo } from "./MaterialInfo"; import { Division } from "./Division"; -import { IndustriesData } from "./IndustryData"; import { Generic_fromJSON, Generic_toJSON, IReviverValue, constructorsForReviver } from "../utils/JSONReviver"; import { getRandomInt } from "../utils/helpers/getRandomInt"; diff --git a/src/Corporation/Warehouse.ts b/src/Corporation/Warehouse.ts index f34bbe489..c20d5bda6 100644 --- a/src/Corporation/Warehouse.ts +++ b/src/Corporation/Warehouse.ts @@ -1,3 +1,6 @@ +import { Player } from "@player"; +import { CorpMaterialName, CorpSmartSupplyOption } from "@nsdefs"; + import { Material } from "./Material"; import { Corporation } from "./Corporation"; import { Division } from "./Division"; @@ -7,7 +10,6 @@ import { CityName } from "../Enums"; import { materialNames } from "./data/Constants"; import { createFullRecordFromEntries, getRecordEntries } from "../Types/Record"; import { CorpUnlockName } from "./data/Enums"; -import { Player } from "@player"; interface IConstructorParams { division: Division; @@ -35,7 +37,9 @@ export class Warehouse { smartSupplyEnabled = false; // Decide if smart supply should use the amount of materials imported into account when deciding on the amount to buy. - smartSupplyOptions = createFullRecordFromEntries(materialNames.map((matName) => [matName, "leftovers"])); + smartSupplyOptions = createFullRecordFromEntries( + materialNames.map((matName) => [matName, "leftovers"]), + ); // Stores the amount of product to be produced. Used for Smart Supply unlock. // The production tracked by smart supply is always based on the previous cycle, diff --git a/src/Corporation/data/Constants.ts b/src/Corporation/data/Constants.ts index ccc1f9864..dacf362e9 100644 --- a/src/Corporation/data/Constants.ts +++ b/src/Corporation/data/Constants.ts @@ -3,6 +3,7 @@ import { CorpIndustryName, CorpMaterialName, CorpResearchName, + CorpSmartSupplyOption, CorpStateName, CorpUnlockName, CorpUpgradeName, @@ -121,8 +122,8 @@ export const stateNames: CorpStateName[] = ["START", "PURCHASE", "PRODUCTION", " valuationLength = 10, /** Minimum decay value for employee morale/energy */ minEmployeeDecay = 10, - /**smart supply ot */ - smartSupplyUseOptions = ["leftovers", "imports", "none"], + /** smart supply options */ + smartSupplyOptions: CorpSmartSupplyOption[] = ["leftovers", "imports", "none"], PurchaseMultipliers = { x1: 1 as PositiveInteger, x5: 5 as PositiveInteger, diff --git a/src/Corporation/IndustryData.tsx b/src/Corporation/data/IndustryData.ts similarity index 89% rename from src/Corporation/IndustryData.tsx rename to src/Corporation/data/IndustryData.ts index d44408741..1c2b9ddb4 100644 --- a/src/Corporation/IndustryData.tsx +++ b/src/Corporation/data/IndustryData.ts @@ -1,10 +1,8 @@ -import React from "react"; -import { Corporation } from "./Corporation"; -import { getBaseResearchTreeCopy, getProductIndustryResearchTreeCopy } from "./data/BaseResearchTree"; -import { MoneyCost } from "./ui/MoneyCost"; import { CorpIndustryData } from "@nsdefs"; -import { IndustryType } from "./data/Enums"; -import { createFullRecordFromEntries } from "../Types/Record"; + +import { createFullRecordFromEntries } from "../../Types/Record"; +import { IndustryType } from "./Enums"; +import { getBaseResearchTreeCopy, getProductIndustryResearchTreeCopy } from "./BaseResearchTree"; export const IndustriesData: Record = { [IndustryType.Agriculture]: { @@ -293,28 +291,6 @@ export const IndustriesData: Record = { export const IndustryStartingCosts = {}; -// Map of description for each industry -export const IndustryDescriptions = (industry: IndustryType, corp: Corporation) => { - const data = IndustriesData[industry]; - return ( - <> - {data.description} -
-
- Required Materials: {Object.keys(data.requiredMaterials).toString().replace(/,/gi, ", ")} -
- Produces Materials: {data.producedMaterials ? data.producedMaterials.toString().replace(/,/gi, ", ") : "NONE"} -
- Produces products: {data.product ? "YES" : "NO"} -
-
- Starting cost: -
- Recommended starting Industry: {data.recommendStarting ? "YES" : "NO"} - - ); -}; - export const IndustryResearchTrees = createFullRecordFromEntries( Object.values(IndustryType).map((industryType) => { return [ diff --git a/src/Corporation/ui/IndustryDescription.tsx b/src/Corporation/ui/IndustryDescription.tsx new file mode 100644 index 000000000..19140281c --- /dev/null +++ b/src/Corporation/ui/IndustryDescription.tsx @@ -0,0 +1,34 @@ +import React from "react"; + +import Typography from "@mui/material/Typography"; + +import { MoneyCost } from "./MoneyCost"; +import { Corporation } from "../Corporation"; +import { IndustryType } from "../data/Enums"; +import { IndustriesData } from "../data/IndustryData"; + +interface IProps { + industry: IndustryType; + corp: Corporation; +} + +export const IndustryDescription = ({ industry, corp }: IProps) => { + const data = IndustriesData[industry]; + return ( + + {data.description} +
+
+ Required Materials: {Object.keys(data.requiredMaterials).toString().replace(/,/gi, ", ")} +
+ Produces Materials: {data.producedMaterials ? data.producedMaterials.toString().replace(/,/gi, ", ") : "NONE"} +
+ Produces products: {data.product ? "YES" : "NO"} +
+
+ Starting cost: +
+ Recommended starting Industry: {data.recommendStarting ? "YES" : "NO"} +
+ ); +}; diff --git a/src/Corporation/ui/NewDivisionTab.tsx b/src/Corporation/ui/NewDivisionTab.tsx index 3b0e2200e..9110fc6b1 100644 --- a/src/Corporation/ui/NewDivisionTab.tsx +++ b/src/Corporation/ui/NewDivisionTab.tsx @@ -1,17 +1,20 @@ import React, { useState } from "react"; + +import Box from "@mui/material/Box"; +import MenuItem from "@mui/material/MenuItem"; +import Select, { SelectChangeEvent } from "@mui/material/Select"; +import TextField from "@mui/material/TextField"; +import Typography from "@mui/material/Typography"; + import { dialogBoxCreate } from "../../ui/React/DialogBox"; -import { IndustryDescriptions, IndustriesData } from "../IndustryData"; +import { IndustriesData } from "../data/IndustryData"; import { IndustryType } from "../data/Enums"; import { useCorporation } from "./Context"; import { NewDivision } from "../Actions"; -import Typography from "@mui/material/Typography"; import { ButtonWithTooltip } from "../../ui/Components/ButtonWithTooltip"; -import TextField from "@mui/material/TextField"; -import MenuItem from "@mui/material/MenuItem"; -import Box from "@mui/material/Box"; -import Select, { SelectChangeEvent } from "@mui/material/Select"; import { KEY } from "../../utils/helpers/keyCodes"; +import { IndustryDescription } from "./IndustryDescription"; interface IProps { setDivisionName: (name: string) => void; } @@ -58,9 +61,6 @@ export function NewDivisionTab(props: IProps): React.ReactElement { setIndustry(event.target.value as IndustryType); } - const desc = IndustryDescriptions(industry, corp); - if (desc === undefined) throw new Error(`Desired industry for new division doesn't exists: '${industry}'`); - return ( <> @@ -74,7 +74,7 @@ export function NewDivisionTab(props: IProps): React.ReactElement { ))} - {desc} +

diff --git a/src/Corporation/ui/modals/MakeProductModal.tsx b/src/Corporation/ui/modals/MakeProductModal.tsx index 410d88ba3..6d8275c56 100644 --- a/src/Corporation/ui/modals/MakeProductModal.tsx +++ b/src/Corporation/ui/modals/MakeProductModal.tsx @@ -1,7 +1,7 @@ import React, { useState } from "react"; import { dialogBoxCreate } from "../../../ui/React/DialogBox"; import { Modal } from "../../../ui/React/Modal"; -import { IndustriesData } from "../../IndustryData"; +import { IndustriesData } from "../../data/IndustryData"; import { IndustryType } from "../../data/Enums"; import { MakeProduct } from "../../Actions"; import { useCorporation, useDivision } from "../Context"; diff --git a/src/Corporation/ui/modals/ResearchModal.tsx b/src/Corporation/ui/modals/ResearchModal.tsx index b405b0d05..9852b34f3 100644 --- a/src/Corporation/ui/modals/ResearchModal.tsx +++ b/src/Corporation/ui/modals/ResearchModal.tsx @@ -1,6 +1,6 @@ import React, { useState } from "react"; import { Modal } from "../../../ui/React/Modal"; -import { IndustryResearchTrees } from "../../IndustryData"; +import { IndustryResearchTrees } from "../../data/IndustryData"; import * as corpConstants from "../../data/Constants"; import { Division } from "../../Division"; import { Research } from "../../Actions"; diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index c82a2b68d..f38a950b2 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -52,7 +52,7 @@ import { import { CorpUnlocks } from "../Corporation/data/CorporationUnlocks"; import { CorpUpgrades } from "../Corporation/data/CorporationUpgrades"; import { CorpUnlockName, CorpUpgradeName, CorpEmployeeJob, IndustryType } from "../Corporation/data/Enums"; -import { IndustriesData, IndustryResearchTrees } from "../Corporation/IndustryData"; +import { IndustriesData, IndustryResearchTrees } from "../Corporation/data/IndustryData"; import * as corpConstants from "../Corporation/data/Constants"; import { ResearchMap } from "../Corporation/ResearchMap"; import { Factions } from "../Faction/Factions"; @@ -413,6 +413,7 @@ export function NetscriptCorporation(): InternalAPI { const warehouse = getWarehouse(divisionName, cityName); const material = getMaterial(divisionName, cityName, materialName); const option = helpers.string(ctx, "option", _option); + assertMember(ctx, corpConstants.smartSupplyOptions, "Smart Supply Option", "option", option); if (!hasUnlock(CorpUnlockName.SmartSupply)) throw helpers.makeRuntimeErrorMsg(ctx, `You have not purchased the Smart Supply upgrade!`); SetSmartSupplyOption(warehouse, material, option); @@ -682,8 +683,8 @@ export function NetscriptCorporation(): InternalAPI { /* TODO 2.2: possibly just rework the whole corp constants structure to be more readable, and just use cloneDeep * to provide it directly to player. * TODO 2.2: Roll product information into industriesData, there's no reason to look up a product separately */ - return cloneDeep(omit(corpConstants, "fundingRoundShares", "fundingRoundMultiplier", "valuationLength")); // TODO: add functions for getting materialInfo and research info + return cloneDeep(omit(corpConstants, "fundingRoundShares", "fundingRoundMultiplier", "valuationLength")); }, getIndustryData: (ctx) => (_industryName) => { checkAccess(ctx); diff --git a/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts b/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts index 3f245c8ba..cd8eabda4 100644 --- a/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts +++ b/src/PersonObjects/Player/PlayerObjectCorporationMethods.ts @@ -1,6 +1,6 @@ import { CorpUnlockName } from "../../Corporation/data/Enums"; +import { resetIndustryResearchTrees } from "../../Corporation/data/IndustryData"; import { Corporation } from "../../Corporation/Corporation"; -import { resetIndustryResearchTrees } from "../../Corporation/IndustryData"; import type { PlayerObject } from "./PlayerObject"; diff --git a/src/Prestige.ts b/src/Prestige.ts index ce35407cf..a6e445acd 100755 --- a/src/Prestige.ts +++ b/src/Prestige.ts @@ -5,7 +5,7 @@ import { augmentationExists, initAugmentations } from "./Augmentation/Augmentati import { AugmentationNames } from "./Augmentation/data/AugmentationNames"; import { initBitNodeMultipliers } from "./BitNode/BitNode"; import { Companies, initCompanies } from "./Company/Companies"; -import { resetIndustryResearchTrees } from "./Corporation/IndustryData"; +import { resetIndustryResearchTrees } from "./Corporation/data/IndustryData"; import { CompletedProgramName } from "./Programs/Programs"; import { Factions, initFactions } from "./Faction/Factions"; import { joinFaction } from "./Faction/FactionHelpers"; diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index be01a6511..8451ab3c3 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -6747,6 +6747,9 @@ type CorpIndustryName = | "Healthcare" | "Real Estate"; +/** @public */ +type CorpSmartSupplyOption = "leftovers" | "imports" | "none"; + /** Names of all cities * @public */ declare enum CityName { @@ -7002,7 +7005,7 @@ export interface WarehouseAPI { divisionName: string, city: CityName | `${CityName}`, materialName: string, - option: string, + option: CorpSmartSupplyOption, ): void; /** * Set material buy data @@ -7369,6 +7372,8 @@ interface CorporationInfo { interface CorpConstants { /** Names of all corporation game states */ stateNames: CorpStateName[]; + /** Names of all employee positions */ + employeePositions: CorpEmployeePosition[]; /** Names of all industries */ industryNames: CorpIndustryName[]; /** Names of all materials */ @@ -7412,6 +7417,7 @@ interface CorpConstants { maxProductsBase: number; /** The minimum decay value for morale/energy */ minEmployeeDecay: number; + smartSupplyOptions: CorpSmartSupplyOption[]; } /** @public */ type CorpStateName = "START" | "PURCHASE" | "PRODUCTION" | "EXPORT" | "SALE";