mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
API: Make ns.codingcontract.createDummyContract throw error if type is invalid (#2188)
This commit is contained in:
@@ -9,14 +9,14 @@ Generate a dummy contract.
|
||||
**Signature:**
|
||||
|
||||
```typescript
|
||||
createDummyContract(type: string): string;
|
||||
createDummyContract(type: CodingContractName): string;
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
| Parameter | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| type | string | Type of contract to generate |
|
||||
| type | [CodingContractName](./bitburner.codingcontractname.md) | Type of contract to generate |
|
||||
|
||||
**Returns:**
|
||||
|
||||
|
||||
@@ -103,9 +103,6 @@ export function convert2DArrayToString(arr: number[][]): string {
|
||||
return components.join(",").replace(/\s/g, "");
|
||||
}
|
||||
|
||||
export const isCodingContractName = (v: unknown): v is CodingContractName =>
|
||||
Object.values(CodingContractName).some((a) => a === v);
|
||||
|
||||
export const CodingContractDefinitions: CodingContractTypes = {
|
||||
...algorithmicStockTrader,
|
||||
...arrayJumpingGame,
|
||||
|
||||
@@ -14,14 +14,16 @@ import {
|
||||
generateRandomContract,
|
||||
generateRandomContractOnHome,
|
||||
} from "../../CodingContract/ContractGenerator";
|
||||
import { isCodingContractName } from "../../CodingContract/ContractTypes";
|
||||
import { CodingContractName } from "@enums";
|
||||
import { getEnumHelper } from "../../utils/EnumHelper";
|
||||
|
||||
export function CodingContractsDev(): React.ReactElement {
|
||||
const [codingcontract, setCodingcontract] = useState(CodingContractName.FindLargestPrimeFactor);
|
||||
function setCodingcontractDropdown(event: SelectChangeEvent): void {
|
||||
const value = event.target.value;
|
||||
if (!isCodingContractName(value)) return;
|
||||
if (!getEnumHelper("CodingContractName").isMember(value)) {
|
||||
return;
|
||||
}
|
||||
setCodingcontract(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
||||
import { helpers } from "../Netscript/NetscriptHelpers";
|
||||
import { CodingContractName } from "@enums";
|
||||
import { generateDummyContract } from "../CodingContract/ContractGenerator";
|
||||
import { isCodingContractName } from "../CodingContract/ContractTypes";
|
||||
import { type BaseServer } from "../Server/BaseServer";
|
||||
import { exceptionAlert } from "../utils/helpers/exceptionAlert";
|
||||
import { getEnumHelper } from "../utils/EnumHelper";
|
||||
|
||||
export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
||||
const getCodingContract = function (ctx: NetscriptContext, hostname: string, filename: string): CodingContract {
|
||||
@@ -128,9 +128,7 @@ export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
||||
return contract.getMaxNumTries() - contract.tries;
|
||||
},
|
||||
createDummyContract: (ctx) => (_type) => {
|
||||
const type = helpers.string(ctx, "type", _type);
|
||||
if (!isCodingContractName(type))
|
||||
return helpers.errorMessage(ctx, `The given type is not a valid contract type. Got '${type}'`);
|
||||
const type = getEnumHelper("CodingContractName").nsGetMember(ctx, _type);
|
||||
return generateDummyContract(type);
|
||||
},
|
||||
getContractTypes: () => () => Object.values(CodingContractName),
|
||||
|
||||
2
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
2
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@@ -4037,7 +4037,7 @@ export interface CodingContract {
|
||||
* @param type - Type of contract to generate
|
||||
* @returns Filename of the contract.
|
||||
*/
|
||||
createDummyContract(type: string): string;
|
||||
createDummyContract(type: CodingContractName): string;
|
||||
|
||||
/**
|
||||
* List all contract types.
|
||||
|
||||
Reference in New Issue
Block a user