CONTRACT: codingcontract.attempt always returns a string (#231)

* ns.codingcontract always returns a string (reward on success, empty on fail), simplifying usage and documentation of function.
* Because of the above, return value still works when used as a boolean, as long as no direct equality comparison to true/false.
* Documentation expanded and examples added.
Co-authored by @quacksouls
This commit is contained in:
Snarling
2022-11-28 09:15:09 -05:00
committed by GitHub
parent 6af36e3b29
commit 837c6bd1c2
8 changed files with 80 additions and 94 deletions

43
dist/bitburner.d.ts vendored
View File

@@ -651,39 +651,42 @@ export declare interface BladeburnerCurAction {
name: string;
}
/**
* Options to affect the behavior of {@link CodingContract} attempt.
* @public
*/
export declare interface CodingAttemptOptions {
/** If truthy, then the function will return a string that states the contracts reward when it is successfully solved. */
returnReward: boolean;
}
/**
* Coding Contract API
* @public
*/
export declare interface CodingContract {
/**
* Attempts a coding contract.
* Attempts a coding contract, returning a reward string on success or empty string on failure.
* @remarks
* RAM cost: 10 GB
*
* Attempts to solve the Coding Contract with the provided solution.
*
* @param answer - Solution for the contract.
* @example
* ```js
* // NS1
* var reward = codingcontract.attempt(yourSolution, filename, hostname);
* if (reward) {
* tprint("Contract solved successfully! Reward: " + reward)
* } else tprint("Failed to solve contract.")
* ```
* @example
* ```js
* // NS2
* const reward = codingcontract.attempt(yourSolution, filename, hostname);
* if (reward) {
* ns.tprint(`Contract solved successfully! Reward: ${reward}`)
* } else ns.tprint("Failed to solve contract.")
* ```
*
* @param answer - Attempted solution for the contract.
* @param filename - Filename of the contract.
* @param host - Hostname of the server containing the contract. Optional. Defaults to current server if not provided.
* @param opts - Optional parameters for configuring function behavior.
* @returns True if the solution was correct, false otherwise. If the returnReward option is configured, then the function will instead return a string. If the contract is successfully solved, the string will contain a description of the contracts reward. Otherwise, it will be an empty string.
* @param host - Hostname of the server containing the contract. Optional. Defaults to current server if not
* provided.
* @returns A reward description string on success, or an empty string on failure.
*/
attempt(
answer: string | number | any[],
filename: string,
host?: string,
opts?: CodingAttemptOptions,
): boolean | string;
attempt(answer: string | number | any[], filename: string, host?: string): string;
/**
* Get the type of a coding contract.