From d948a7bc54ade980d610e4000ec293695e839c85 Mon Sep 17 00:00:00 2001 From: CatLover <152669316+catloversg@users.noreply.github.com> Date: Sat, 17 Aug 2024 23:26:36 +0700 Subject: [PATCH 1/3] CORPORATION: Add new API to check if player can create corporation --- ...burner.corporation.cancreatecorporation.md | 30 +++++++++++++++++++ ...bitburner.corporation.createcorporation.md | 10 ++----- .../bitburner.corporation.hascorporation.md | 2 +- markdown/bitburner.corporation.md | 5 ++-- src/Corporation/Actions.ts | 30 +++++++++++-------- src/Corporation/helpers.ts | 25 ++++++++++++++++ src/Netscript/RamCostGenerator.ts | 1 + src/NetscriptFunctions/Corporation.ts | 30 ++++++++++++++----- src/ScriptEditor/NetscriptDefinitions.d.ts | 23 ++++++++------ 9 files changed, 116 insertions(+), 40 deletions(-) create mode 100644 markdown/bitburner.corporation.cancreatecorporation.md diff --git a/markdown/bitburner.corporation.cancreatecorporation.md b/markdown/bitburner.corporation.cancreatecorporation.md new file mode 100644 index 0000000000..6035103737 --- /dev/null +++ b/markdown/bitburner.corporation.cancreatecorporation.md @@ -0,0 +1,30 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [Corporation](./bitburner.corporation.md) > [canCreateCorporation](./bitburner.corporation.cancreatecorporation.md) + +## Corporation.canCreateCorporation() method + +Return whether the player can create a corporation. Does not require API access. + +**Signature:** + +```typescript +canCreateCorporation(selfFund: boolean): boolean; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| selfFund | boolean | true if you want to self-fund, false otherwise | + +**Returns:** + +boolean + +Whether the player can create a corporation + +## Remarks + +RAM cost: 0 GB + diff --git a/markdown/bitburner.corporation.createcorporation.md b/markdown/bitburner.corporation.createcorporation.md index 22f3710a5b..953e44f8c2 100644 --- a/markdown/bitburner.corporation.createcorporation.md +++ b/markdown/bitburner.corporation.createcorporation.md @@ -4,7 +4,7 @@ ## Corporation.createCorporation() method -Create a Corporation. +Create a Corporation. You should use [canCreateCorporation](./bitburner.corporation.cancreatecorporation.md) to check if you can do it before using this function, because it throws an error in some cases. **Signature:** @@ -16,7 +16,7 @@ createCorporation(corporationName: string, selfFund: boolean): boolean; | Parameter | Type | Description | | --- | --- | --- | -| corporationName | string | Name of the corporation | +| corporationName | string | Name of the corporation. It must be a non-empty string. | | selfFund | boolean | If you want to self-fund. Defaults to true, false will only work in BitNode 3. | **Returns:** @@ -29,9 +29,3 @@ true if created and false if not RAM cost: 20 GB -This function throws an error if: - -- Try to self-fund outside BitNode 3. - -- Be in a BitNode that has CorporationSoftcap (a BN modifier) less than 0.15. Use [getBitNodeMultipliers](./bitburner.ns.getbitnodemultipliers.md) to get the value of this modifier. - diff --git a/markdown/bitburner.corporation.hascorporation.md b/markdown/bitburner.corporation.hascorporation.md index a7e4a1be31..2db2464ed8 100644 --- a/markdown/bitburner.corporation.hascorporation.md +++ b/markdown/bitburner.corporation.hascorporation.md @@ -4,7 +4,7 @@ ## Corporation.hasCorporation() method -Returns whether the player has a corporation. Does not require API access. +Return whether the player has a corporation. Does not require API access. **Signature:** diff --git a/markdown/bitburner.corporation.md b/markdown/bitburner.corporation.md index 49cb2eb6de..c93695f545 100644 --- a/markdown/bitburner.corporation.md +++ b/markdown/bitburner.corporation.md @@ -20,7 +20,8 @@ export interface Corporation extends WarehouseAPI, OfficeAPI | [acceptInvestmentOffer()](./bitburner.corporation.acceptinvestmentoffer.md) | Accept the investment offer. The value of offer is based on current corporation valuation. | | [bribe(factionName, amountCash)](./bitburner.corporation.bribe.md) | Bribe a faction. | | [buyBackShares(amount)](./bitburner.corporation.buybackshares.md) | Buyback shares. Spend money from the player's wallet to transfer shares from public traders to the CEO. | -| [createCorporation(corporationName, selfFund)](./bitburner.corporation.createcorporation.md) | Create a Corporation. | +| [canCreateCorporation(selfFund)](./bitburner.corporation.cancreatecorporation.md) | Return whether the player can create a corporation. Does not require API access. | +| [createCorporation(corporationName, selfFund)](./bitburner.corporation.createcorporation.md) | Create a Corporation. You should use [canCreateCorporation](./bitburner.corporation.cancreatecorporation.md) to check if you can do it before using this function, because it throws an error in some cases. | | [expandCity(divisionName, city)](./bitburner.corporation.expandcity.md) | Expand to a new city. | | [expandIndustry(industryType, divisionName)](./bitburner.corporation.expandindustry.md) | Expand to a new industry. | | [getBonusTime()](./bitburner.corporation.getbonustime.md) | Get bonus time. Bonus time is accumulated when the game is offline or if the game is inactive in the browser. Bonus time makes the corporation progress faster. | @@ -34,7 +35,7 @@ export interface Corporation extends WarehouseAPI, OfficeAPI | [getUpgradeLevel(upgradeName)](./bitburner.corporation.getupgradelevel.md) | Get the level of a levelable upgrade. | | [getUpgradeLevelCost(upgradeName)](./bitburner.corporation.getupgradelevelcost.md) | Get the cost to unlock the next level of a levelable upgrade. | | [goPublic(numShares)](./bitburner.corporation.gopublic.md) | Go public. | -| [hasCorporation()](./bitburner.corporation.hascorporation.md) | Returns whether the player has a corporation. Does not require API access. | +| [hasCorporation()](./bitburner.corporation.hascorporation.md) | Return whether the player has a corporation. Does not require API access. | | [hasUnlock(upgradeName)](./bitburner.corporation.hasunlock.md) | Check if you have a one-time unlockable upgrade. | | [issueDividends(rate)](./bitburner.corporation.issuedividends.md) | Issue dividends. | | [issueNewShares(amount)](./bitburner.corporation.issuenewshares.md) | Issue new shares. | diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index 8bc79b9469..da6262e077 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -22,27 +22,33 @@ import { buybackSharesFailureReason, issueNewSharesFailureReason, costOfCreatingCorporation, + canCreateCorporation, + CreatingCorporationCheckResult, } from "./helpers"; import { PositiveInteger } from "../types"; -import { currentNodeMults } from "../BitNode/BitNodeMultipliers"; import { Factions } from "../Faction/Factions"; export function createCorporation(corporationName: string, selfFund: boolean, restart: boolean): boolean { - if (!Player.canAccessCorporation()) { - return false; - } - if (Player.corporation && !restart) { - return false; + const checkResult = canCreateCorporation(selfFund, restart); + switch (checkResult) { + case CreatingCorporationCheckResult.Success: + break; + case CreatingCorporationCheckResult.NoSf3OrDisabled: + case CreatingCorporationCheckResult.CorporationExists: + return false; + case CreatingCorporationCheckResult.UseSeedMoneyOutsideBN3: + case CreatingCorporationCheckResult.DisabledBySoftCap: + // In order to maintaining backward compatibility, we have to throw an error in these cases. + throw new Error(checkResult); + default: { + // Verify if switch statement is exhaustive + checkResult satisfies never; + } } + if (!corporationName) { return false; } - if (Player.bitNodeN !== 3 && !selfFund) { - throw new Error("Cannot use seed funds outside of BitNode 3"); - } - if (currentNodeMults.CorporationSoftcap < 0.15) { - throw new Error(`You cannot create a corporation in BitNode ${Player.bitNodeN}`); - } if (selfFund) { const cost = costOfCreatingCorporation(restart); diff --git a/src/Corporation/helpers.ts b/src/Corporation/helpers.ts index c9a431e113..55d95700cc 100644 --- a/src/Corporation/helpers.ts +++ b/src/Corporation/helpers.ts @@ -4,6 +4,31 @@ import { formatShares } from "../ui/formatNumber"; import { Corporation } from "./Corporation"; import { CorpUpgrade } from "./data/CorporationUpgrades"; import * as corpConstants from "./data/Constants"; +import { currentNodeMults } from "../BitNode/BitNodeMultipliers"; + +export enum CreatingCorporationCheckResult { + Success = "Success", + NoSf3OrDisabled = "You don't have SF3 or Corporation is disabled by an advanced option", + CorporationExists = "Corporation exists", + UseSeedMoneyOutsideBN3 = "You cannot use seed money outside BitNode 3", + DisabledBySoftCap = "You cannot create a corporation in this BitNode", +} + +export function canCreateCorporation(selfFund: boolean, restart: boolean): CreatingCorporationCheckResult { + if (!Player.canAccessCorporation()) { + return CreatingCorporationCheckResult.NoSf3OrDisabled; + } + if (Player.corporation && !restart) { + return CreatingCorporationCheckResult.CorporationExists; + } + if (Player.bitNodeN !== 3 && !selfFund) { + return CreatingCorporationCheckResult.UseSeedMoneyOutsideBN3; + } + if (currentNodeMults.CorporationSoftcap < 0.15) { + return CreatingCorporationCheckResult.DisabledBySoftCap; + } + return CreatingCorporationCheckResult.Success; +} export function costOfCreatingCorporation(restart: boolean): number { if (restart && !Player.corporation?.seedFunded) { diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index 71cdf2449a..4f5eadd737 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -399,6 +399,7 @@ const grafting = { const corporation = { hasCorporation: 0, + canCreateCorporation: 0, createCorporation: RamCostConstants.CorporationAction, hasUnlock: RamCostConstants.CorporationInfo, getUnlockCost: RamCostConstants.CorporationInfo, diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index 2cd5b24265..c9ae22b107 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -64,7 +64,12 @@ import { InternalAPI, NetscriptContext, setRemovedFunctions } from "../Netscript import { helpers } from "../Netscript/NetscriptHelpers"; import { getEnumHelper } from "../utils/EnumHelper"; import { MaterialInfo } from "../Corporation/MaterialInfo"; -import { calculateOfficeSizeUpgradeCost, calculateUpgradeCost } from "../Corporation/helpers"; +import { + CreatingCorporationCheckResult, + calculateOfficeSizeUpgradeCost, + calculateUpgradeCost, + canCreateCorporation, +} from "../Corporation/helpers"; import { PositiveInteger } from "../types"; import { getRecordKeys } from "../Types/Record"; @@ -588,6 +593,22 @@ export function NetscriptCorporation(): InternalAPI { ...warehouseAPI, ...officeAPI, hasCorporation: () => () => !!Player.corporation, + canCreateCorporation: (ctx) => (_selfFund) => { + const selfFund = !!_selfFund; + const checkResult = canCreateCorporation(selfFund, false); + if (checkResult !== CreatingCorporationCheckResult.Success) { + helpers.log(ctx, () => checkResult); + return false; + } + return true; + }, + createCorporation: + (ctx) => + (_corporationName, _selfFund = true): boolean => { + const corporationName = helpers.string(ctx, "corporationName", _corporationName); + const selfFund = !!_selfFund; + return createCorporation(corporationName, selfFund, false); + }, getConstants: () => () => { /* TODO 2.2: possibly just rework the whole corp constants structure to be more readable, and just use * structuredClone to provide it directly to player. @@ -691,13 +712,6 @@ export function NetscriptCorporation(): InternalAPI { }); return data; }, - createCorporation: - (ctx) => - (_corporationName, _selfFund = true): boolean => { - const corporationName = helpers.string(ctx, "corporationName", _corporationName); - const selfFund = !!_selfFund; - return createCorporation(corporationName, selfFund, false); - }, hasUnlock: (ctx) => (_unlockName) => { checkAccess(ctx); const unlockName = getEnumHelper("CorpUnlockName").nsGetMember(ctx, _unlockName, "unlockName"); diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 810048c415..c9dc158366 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -8468,7 +8468,7 @@ export interface WarehouseAPI { */ export interface Corporation extends WarehouseAPI, OfficeAPI { /** - * Returns whether the player has a corporation. Does not require API access. + * Return whether the player has a corporation. Does not require API access. * * @remarks * RAM cost: 0 GB @@ -8478,19 +8478,24 @@ export interface Corporation extends WarehouseAPI, OfficeAPI { hasCorporation(): boolean; /** - * Create a Corporation. + * Return whether the player can create a corporation. Does not require API access. * * @remarks - * RAM cost: 20 GB - * - * This function throws an error if: + * RAM cost: 0 GB * - * - Try to self-fund outside BitNode 3. + * @param selfFund - true if you want to self-fund, false otherwise + * @returns Whether the player can create a corporation + */ + canCreateCorporation(selfFund: boolean): boolean; + + /** + * Create a Corporation. You should use {@link Corporation.canCreateCorporation | canCreateCorporation} to check if + * you can do it before using this function, because it throws an error in some cases. * - * - Be in a BitNode that has CorporationSoftcap (a BN modifier) less than 0.15. Use - * {@link NS.getBitNodeMultipliers | getBitNodeMultipliers} to get the value of this modifier. + * @remarks + * RAM cost: 20 GB * - * @param corporationName - Name of the corporation + * @param corporationName - Name of the corporation. It must be a non-empty string. * @param selfFund - If you want to self-fund. Defaults to true, false will only work in BitNode 3. * @returns true if created and false if not */ From 45fa7a3b3b4a9e464e572c11e659778e262da0ed Mon Sep 17 00:00:00 2001 From: CatLover <152669316+catloversg@users.noreply.github.com> Date: Sun, 18 Aug 2024 21:59:35 +0700 Subject: [PATCH 2/3] Update description of createCorporation API --- markdown/bitburner.corporation.createcorporation.md | 6 +++++- markdown/bitburner.corporation.md | 2 +- src/ScriptEditor/NetscriptDefinitions.d.ts | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/markdown/bitburner.corporation.createcorporation.md b/markdown/bitburner.corporation.createcorporation.md index 953e44f8c2..c1ce111f28 100644 --- a/markdown/bitburner.corporation.createcorporation.md +++ b/markdown/bitburner.corporation.createcorporation.md @@ -4,7 +4,11 @@ ## Corporation.createCorporation() method -Create a Corporation. You should use [canCreateCorporation](./bitburner.corporation.cancreatecorporation.md) to check if you can do it before using this function, because it throws an error in some cases. +Create a Corporation. You should use [canCreateCorporation](./bitburner.corporation.cancreatecorporation.md) to check if you are unsure you can do it, because it throws an error in these cases: + +- Use seed money outside BitNode 3. + +- Be in a BitNode that has CorporationSoftcap (a BitNode modifier) less than 0.15. **Signature:** diff --git a/markdown/bitburner.corporation.md b/markdown/bitburner.corporation.md index c93695f545..c6c615868e 100644 --- a/markdown/bitburner.corporation.md +++ b/markdown/bitburner.corporation.md @@ -21,7 +21,7 @@ export interface Corporation extends WarehouseAPI, OfficeAPI | [bribe(factionName, amountCash)](./bitburner.corporation.bribe.md) | Bribe a faction. | | [buyBackShares(amount)](./bitburner.corporation.buybackshares.md) | Buyback shares. Spend money from the player's wallet to transfer shares from public traders to the CEO. | | [canCreateCorporation(selfFund)](./bitburner.corporation.cancreatecorporation.md) | Return whether the player can create a corporation. Does not require API access. | -| [createCorporation(corporationName, selfFund)](./bitburner.corporation.createcorporation.md) | Create a Corporation. You should use [canCreateCorporation](./bitburner.corporation.cancreatecorporation.md) to check if you can do it before using this function, because it throws an error in some cases. | +| [createCorporation(corporationName, selfFund)](./bitburner.corporation.createcorporation.md) |

Create a Corporation. You should use [canCreateCorporation](./bitburner.corporation.cancreatecorporation.md) to check if you are unsure you can do it, because it throws an error in these cases:

- Use seed money outside BitNode 3.

- Be in a BitNode that has CorporationSoftcap (a BitNode modifier) less than 0.15.

| | [expandCity(divisionName, city)](./bitburner.corporation.expandcity.md) | Expand to a new city. | | [expandIndustry(industryType, divisionName)](./bitburner.corporation.expandindustry.md) | Expand to a new industry. | | [getBonusTime()](./bitburner.corporation.getbonustime.md) | Get bonus time. Bonus time is accumulated when the game is offline or if the game is inactive in the browser. Bonus time makes the corporation progress faster. | diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index c9dc158366..53603922d2 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -8490,7 +8490,11 @@ export interface Corporation extends WarehouseAPI, OfficeAPI { /** * Create a Corporation. You should use {@link Corporation.canCreateCorporation | canCreateCorporation} to check if - * you can do it before using this function, because it throws an error in some cases. + * you are unsure you can do it, because it throws an error in these cases: + * + * - Use seed money outside BitNode 3. + * + * - Be in a BitNode that has CorporationSoftcap (a BitNode modifier) less than 0.15. * * @remarks * RAM cost: 20 GB From 86aa108ba75d4cc975c1bd07b6993a1b3c8dcec5 Mon Sep 17 00:00:00 2001 From: CatLover <152669316+catloversg@users.noreply.github.com> Date: Tue, 20 Aug 2024 00:20:58 +0700 Subject: [PATCH 3/3] Return enum instead of true/false --- ...burner.corporation.cancreatecorporation.md | 6 ++--- ...itburner.creatingcorporationcheckresult.md | 23 ++++++++++++++++ markdown/bitburner.md | 1 + src/Corporation/Actions.ts | 3 +-- src/Corporation/Enums.ts | 8 ++++++ src/Corporation/helpers.ts | 27 ++++++++++++++----- src/NetscriptFunctions/Corporation.ts | 9 +++---- src/ScriptEditor/NetscriptDefinitions.d.ts | 15 +++++++++-- 8 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 markdown/bitburner.creatingcorporationcheckresult.md diff --git a/markdown/bitburner.corporation.cancreatecorporation.md b/markdown/bitburner.corporation.cancreatecorporation.md index 6035103737..0b769e4c19 100644 --- a/markdown/bitburner.corporation.cancreatecorporation.md +++ b/markdown/bitburner.corporation.cancreatecorporation.md @@ -9,7 +9,7 @@ Return whether the player can create a corporation. Does not require API access. **Signature:** ```typescript -canCreateCorporation(selfFund: boolean): boolean; +canCreateCorporation(selfFund: boolean): CreatingCorporationCheckResult; ``` ## Parameters @@ -20,9 +20,9 @@ canCreateCorporation(selfFund: boolean): boolean; **Returns:** -boolean +[CreatingCorporationCheckResult](./bitburner.creatingcorporationcheckresult.md) -Whether the player can create a corporation +Result of the check ## Remarks diff --git a/markdown/bitburner.creatingcorporationcheckresult.md b/markdown/bitburner.creatingcorporationcheckresult.md new file mode 100644 index 0000000000..16bb82a318 --- /dev/null +++ b/markdown/bitburner.creatingcorporationcheckresult.md @@ -0,0 +1,23 @@ + + +[Home](./index.md) > [bitburner](./bitburner.md) > [CreatingCorporationCheckResult](./bitburner.creatingcorporationcheckresult.md) + +## CreatingCorporationCheckResult enum + + +**Signature:** + +```typescript +declare enum CreatingCorporationCheckResult +``` + +## Enumeration Members + +| Member | Value | Description | +| --- | --- | --- | +| CorporationExists | "CorporationExists" | | +| DisabledBySoftCap | "DisabledBySoftCap" | | +| NoSf3OrDisabled | "NoSf3OrDisabled" | | +| Success | "Success" | | +| UseSeedMoneyOutsideBN3 | "UseSeedMoneyOutsideBN3" | | + diff --git a/markdown/bitburner.md b/markdown/bitburner.md index 41de47a505..3821d40a31 100644 --- a/markdown/bitburner.md +++ b/markdown/bitburner.md @@ -10,6 +10,7 @@ | --- | --- | | [CityName](./bitburner.cityname.md) | Names of all cities | | [CompanyName](./bitburner.companyname.md) | Names of all companies | +| [CreatingCorporationCheckResult](./bitburner.creatingcorporationcheckresult.md) | | | [CrimeType](./bitburner.crimetype.md) | | | [FactionWorkType](./bitburner.factionworktype.md) | | | [GymType](./bitburner.gymtype.md) | | diff --git a/src/Corporation/Actions.ts b/src/Corporation/Actions.ts index da6262e077..d42e0f85fe 100644 --- a/src/Corporation/Actions.ts +++ b/src/Corporation/Actions.ts @@ -10,7 +10,7 @@ import { OfficeSpace } from "./OfficeSpace"; import { Material } from "./Material"; import { Product } from "./Product"; import { Warehouse } from "./Warehouse"; -import { FactionName, IndustryType } from "@enums"; +import { CreatingCorporationCheckResult, FactionName, IndustryType } from "@enums"; import { ResearchMap } from "./ResearchMap"; import { isRelevantMaterial } from "./ui/Helpers"; import { CityName } from "@enums"; @@ -23,7 +23,6 @@ import { issueNewSharesFailureReason, costOfCreatingCorporation, canCreateCorporation, - CreatingCorporationCheckResult, } from "./helpers"; import { PositiveInteger } from "../types"; import { Factions } from "../Faction/Factions"; diff --git a/src/Corporation/Enums.ts b/src/Corporation/Enums.ts index fa852a6d59..7aaa576c3a 100644 --- a/src/Corporation/Enums.ts +++ b/src/Corporation/Enums.ts @@ -109,3 +109,11 @@ export type CorpProductResearchName = Member; export const CorpResearchName = { ...CorpProductResearchName, ...CorpBaseResearchName }; export type CorpResearchName = Member; + +export enum CreatingCorporationCheckResult { + Success = "Success", + NoSf3OrDisabled = "NoSf3OrDisabled", + CorporationExists = "CorporationExists", + UseSeedMoneyOutsideBN3 = "UseSeedMoneyOutsideBN3", + DisabledBySoftCap = "DisabledBySoftCap", +} diff --git a/src/Corporation/helpers.ts b/src/Corporation/helpers.ts index 55d95700cc..f6ada84d06 100644 --- a/src/Corporation/helpers.ts +++ b/src/Corporation/helpers.ts @@ -5,13 +5,26 @@ import { Corporation } from "./Corporation"; import { CorpUpgrade } from "./data/CorporationUpgrades"; import * as corpConstants from "./data/Constants"; import { currentNodeMults } from "../BitNode/BitNodeMultipliers"; - -export enum CreatingCorporationCheckResult { - Success = "Success", - NoSf3OrDisabled = "You don't have SF3 or Corporation is disabled by an advanced option", - CorporationExists = "Corporation exists", - UseSeedMoneyOutsideBN3 = "You cannot use seed money outside BitNode 3", - DisabledBySoftCap = "You cannot create a corporation in this BitNode", +import { CreatingCorporationCheckResult } from "@enums"; + +export function convertCreatingCorporationCheckResultToMessage(checkResult: CreatingCorporationCheckResult): string { + switch (checkResult) { + case CreatingCorporationCheckResult.Success: + return "Success"; + case CreatingCorporationCheckResult.NoSf3OrDisabled: + return "You don't have SF3 or Corporation is disabled by an advanced option"; + case CreatingCorporationCheckResult.CorporationExists: + return "Corporation exists"; + case CreatingCorporationCheckResult.UseSeedMoneyOutsideBN3: + return "You cannot use seed money outside BitNode 3"; + case CreatingCorporationCheckResult.DisabledBySoftCap: + return "You cannot create a corporation in this BitNode"; + default: { + // Verify if switch statement is exhaustive + checkResult satisfies never; + } + } + return String(checkResult); } export function canCreateCorporation(selfFund: boolean, restart: boolean): CreatingCorporationCheckResult { diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index c9ae22b107..ffb46317b3 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -56,7 +56,7 @@ import { } from "../Corporation/Actions"; import { CorpUnlocks } from "../Corporation/data/CorporationUnlocks"; import { CorpUpgrades } from "../Corporation/data/CorporationUpgrades"; -import { CorpUnlockName, CorpUpgradeName, CorpEmployeeJob, CityName } from "@enums"; +import { CorpUnlockName, CorpUpgradeName, CorpEmployeeJob, CityName, CreatingCorporationCheckResult } from "@enums"; import { IndustriesData, IndustryResearchTrees } from "../Corporation/data/IndustryData"; import * as corpConstants from "../Corporation/data/Constants"; import { ResearchMap } from "../Corporation/ResearchMap"; @@ -65,10 +65,10 @@ import { helpers } from "../Netscript/NetscriptHelpers"; import { getEnumHelper } from "../utils/EnumHelper"; import { MaterialInfo } from "../Corporation/MaterialInfo"; import { - CreatingCorporationCheckResult, calculateOfficeSizeUpgradeCost, calculateUpgradeCost, canCreateCorporation, + convertCreatingCorporationCheckResultToMessage, } from "../Corporation/helpers"; import { PositiveInteger } from "../types"; import { getRecordKeys } from "../Types/Record"; @@ -597,10 +597,9 @@ export function NetscriptCorporation(): InternalAPI { const selfFund = !!_selfFund; const checkResult = canCreateCorporation(selfFund, false); if (checkResult !== CreatingCorporationCheckResult.Success) { - helpers.log(ctx, () => checkResult); - return false; + helpers.log(ctx, () => convertCreatingCorporationCheckResultToMessage(checkResult)); } - return true; + return checkResult; }, createCorporation: (ctx) => diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 53603922d2..52a2ed8f81 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -8462,6 +8462,17 @@ export interface WarehouseAPI { hasWarehouse(divisionName: string, city: CityName | `${CityName}`): boolean; } +/** + * @public + */ +declare enum CreatingCorporationCheckResult { + Success = "Success", + NoSf3OrDisabled = "NoSf3OrDisabled", + CorporationExists = "CorporationExists", + UseSeedMoneyOutsideBN3 = "UseSeedMoneyOutsideBN3", + DisabledBySoftCap = "DisabledBySoftCap", +} + /** * Corporation API * @public @@ -8484,9 +8495,9 @@ export interface Corporation extends WarehouseAPI, OfficeAPI { * RAM cost: 0 GB * * @param selfFund - true if you want to self-fund, false otherwise - * @returns Whether the player can create a corporation + * @returns Result of the check */ - canCreateCorporation(selfFund: boolean): boolean; + canCreateCorporation(selfFund: boolean): CreatingCorporationCheckResult; /** * Create a Corporation. You should use {@link Corporation.canCreateCorporation | canCreateCorporation} to check if