From 933ec964cf7e092b442374b7d815e18648d82ff4 Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Mon, 2 Dec 2024 04:50:32 +0700 Subject: [PATCH] CORPORATION: Print error message when player cannot create corporation (#1820) --- src/Corporation/Actions.ts | 17 +++++++++++------ .../ui/modals/CreateCorporationModal.tsx | 9 ++++++++- src/NetscriptFunctions/Corporation.ts | 6 +++++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index 29068d8f06..89a08ca233 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -23,42 +23,47 @@ import { issueNewSharesFailureReason, costOfCreatingCorporation, canCreateCorporation, + convertCreatingCorporationCheckResultToMessage, } from "./helpers"; import { PositiveInteger, Result } from "../types"; import { Factions } from "../Faction/Factions"; import { throwIfReachable } from "../utils/helpers/throwIfReachable"; +import { formatMoney } from "../ui/formatNumber"; -export function createCorporation(corporationName: string, selfFund: boolean, restart: boolean): boolean { +export function createCorporation(corporationName: string, selfFund: boolean, restart: boolean): Result { const checkResult = canCreateCorporation(selfFund, restart); switch (checkResult) { case CreatingCorporationCheckResult.Success: break; case CreatingCorporationCheckResult.NoSf3OrDisabled: case CreatingCorporationCheckResult.CorporationExists: - return false; + return { success: false, message: convertCreatingCorporationCheckResultToMessage(checkResult) }; case CreatingCorporationCheckResult.UseSeedMoneyOutsideBN3: case CreatingCorporationCheckResult.DisabledBySoftCap: // In order to maintain backward compatibility, we have to throw an error in these cases. - throw new Error(checkResult); + throw new Error(convertCreatingCorporationCheckResultToMessage(checkResult)); default: throwIfReachable(checkResult); } if (!corporationName) { - return false; + return { success: false, message: "Corporation name cannot be an empty string." }; } if (selfFund) { const cost = costOfCreatingCorporation(restart); if (!Player.canAfford(cost)) { - return false; + return { + success: false, + message: `You don't have enough money to create a corporation. It costs ${formatMoney(cost)}.`, + }; } Player.startCorporation(corporationName, false); Player.loseMoney(cost, "corporation"); } else { Player.startCorporation(corporationName, true); } - return true; + return { success: true }; } export function createDivision(corporation: Corporation, industry: IndustryType, name: string): void { diff --git a/src/Corporation/ui/modals/CreateCorporationModal.tsx b/src/Corporation/ui/modals/CreateCorporationModal.tsx index d2aa9500a7..a8fa001a55 100644 --- a/src/Corporation/ui/modals/CreateCorporationModal.tsx +++ b/src/Corporation/ui/modals/CreateCorporationModal.tsx @@ -11,6 +11,7 @@ import { ButtonWithTooltip } from "../../../ui/Components/ButtonWithTooltip"; import TextField from "@mui/material/TextField"; import { createCorporation } from "../../Actions"; import { costOfCreatingCorporation } from "../../helpers"; +import { exceptionAlert } from "../../../utils/helpers/exceptionAlert"; interface IProps { open: boolean; @@ -34,7 +35,13 @@ export function CreateCorporationModal(props: IProps): React.ReactElement { } function createCorporationWithUI(corporationName: string, selfFund: boolean): void { - if (!createCorporation(corporationName, selfFund, props.restart)) { + const result = createCorporation(corporationName, selfFund, props.restart); + if (!result.success) { + /** + * This should not happen. We always check if the player can create a corporation before enabling UI elements + * needed to do that. + */ + exceptionAlert(new Error(result.message)); return; } props.onClose(); diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index 8b7dcf9437..952d23684d 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -606,7 +606,11 @@ export function NetscriptCorporation(): InternalAPI { (_corporationName, _selfFund = true): boolean => { const corporationName = helpers.string(ctx, "corporationName", _corporationName); const selfFund = !!_selfFund; - return createCorporation(corporationName, selfFund, false); + const result = createCorporation(corporationName, selfFund, false); + if (!result.success) { + helpers.log(ctx, () => result.message); + } + return result.success; }, getConstants: () => () => { /* TODO 2.2: possibly just rework the whole corp constants structure to be more readable, and just use