Skip to content

Commit

Permalink
CORPORATION: Print error message when player cannot create corporation (
Browse files Browse the repository at this point in the history
  • Loading branch information
catloversg authored Dec 1, 2024
1 parent 6e1848d commit 933ec96
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/Corporation/Actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion src/Corporation/ui/modals/CreateCorporationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion src/NetscriptFunctions/Corporation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,11 @@ export function NetscriptCorporation(): InternalAPI<NSCorporation> {
(_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
Expand Down

0 comments on commit 933ec96

Please sign in to comment.