CORPORATION: Print error message when player cannot create corporation (#1820)

This commit is contained in:
catloversg 2024-12-02 04:50:32 +07:00 committed by GitHub
parent 6e1848dd79
commit 933ec964cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 8 deletions

@ -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 {

@ -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();

@ -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