mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-19 04:35:46 +01:00
functions to generate dummy contracts
This commit is contained in:
parent
5d4b72e1d1
commit
f4668b21ca
@ -45,6 +45,15 @@ export function generateRandomContractOnHome(): void {
|
||||
serv.addContract(contract);
|
||||
}
|
||||
|
||||
export const generateDummyContract = (problemType: string): void => {
|
||||
if (!CodingContractTypes[problemType]) throw new Error(`Invalid problem type: '${problemType}'`);
|
||||
const serv = Player.getHomeComputer();
|
||||
|
||||
const contractFn = getRandomFilename(serv);
|
||||
const contract = new CodingContract(contractFn, problemType, null);
|
||||
serv.addContract(contract);
|
||||
};
|
||||
|
||||
interface IGenerateContractParams {
|
||||
problemType?: string;
|
||||
server?: string;
|
||||
@ -176,7 +185,7 @@ function getRandomServer(): BaseServer {
|
||||
return randServer;
|
||||
}
|
||||
|
||||
function getRandomFilename(server: BaseServer, reward: ICodingContractReward): string {
|
||||
function getRandomFilename(server: BaseServer, reward: ICodingContractReward = { name: "", type: 0 }): string {
|
||||
let contractFn = `contract-${getRandomInt(0, 1e6)}`;
|
||||
|
||||
for (let i = 0; i < 1000; ++i) {
|
||||
|
@ -279,6 +279,8 @@ const codingcontract = {
|
||||
getData: RamCostConstants.ScriptCodingContractBaseRamCost / 2,
|
||||
getDescription: RamCostConstants.ScriptCodingContractBaseRamCost / 2,
|
||||
getNumTriesRemaining: RamCostConstants.ScriptCodingContractBaseRamCost / 5,
|
||||
createDummyContract: RamCostConstants.ScriptCodingContractBaseRamCost / 5,
|
||||
getContractTypes: RamCostConstants.ScriptCodingContractBaseRamCost / 5,
|
||||
} as const;
|
||||
|
||||
// Duplicate Sleeve API
|
||||
|
@ -3,6 +3,8 @@ import { CodingContract } from "../CodingContracts";
|
||||
import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions";
|
||||
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
||||
import { helpers } from "../Netscript/NetscriptHelpers";
|
||||
import { codingContractTypesMetadata } from "../data/codingcontracttypes";
|
||||
import { generateDummyContract } from "../CodingContractGenerator";
|
||||
|
||||
export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
||||
const getCodingContract = function (
|
||||
@ -39,7 +41,6 @@ export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
||||
// Convert answer to string.
|
||||
const answerStr = typeof answer === "string" ? answer : JSON.stringify(answer);
|
||||
const creward = contract.reward;
|
||||
if (creward === null) throw new Error("Somehow solved a contract that didn't have a reward");
|
||||
|
||||
const serv = helpers.getServer(ctx, hostname);
|
||||
if (contract.isSolution(answerStr)) {
|
||||
@ -112,5 +113,12 @@ export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
||||
const contract = getCodingContract(ctx, "getNumTriesRemaining", hostname, filename);
|
||||
return contract.getMaxNumTries() - contract.tries;
|
||||
},
|
||||
createDummyContract:
|
||||
(ctx: NetscriptContext) =>
|
||||
(_type: unknown): void => {
|
||||
const type = helpers.string(ctx, "type", _type);
|
||||
generateDummyContract(type);
|
||||
},
|
||||
getContractTypes: () => (): string[] => codingContractTypesMetadata.map((c) => c.name),
|
||||
};
|
||||
}
|
||||
|
@ -1109,7 +1109,11 @@ export function queueAugmentation(this: PlayerObject, name: string): void {
|
||||
}
|
||||
|
||||
/************* Coding Contracts **************/
|
||||
export function gainCodingContractReward(this: PlayerObject, reward: ICodingContractReward, difficulty = 1): string {
|
||||
export function gainCodingContractReward(
|
||||
this: PlayerObject,
|
||||
reward: ICodingContractReward | null,
|
||||
difficulty = 1,
|
||||
): string {
|
||||
if (reward == null || reward.type == null) {
|
||||
return `No reward for this contract`;
|
||||
}
|
||||
|
18
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
18
src/ScriptEditor/NetscriptDefinitions.d.ts
vendored
@ -3251,6 +3251,24 @@ export interface CodingContract {
|
||||
* @returns How many attempts are remaining for the contract.
|
||||
*/
|
||||
getNumTriesRemaining(filename: string, host?: string): number;
|
||||
|
||||
/**
|
||||
* Generate a dummy contract.
|
||||
* @remarks
|
||||
* RAM cost: 2 GB
|
||||
*
|
||||
* Generate a dummy contract on the home computer with no reward. Used to test various algorithms.
|
||||
*
|
||||
* @param type - Type of contract to generate
|
||||
*/
|
||||
createDummyContract(type: string): void;
|
||||
|
||||
/**
|
||||
* List all contract types.
|
||||
* @remarks
|
||||
* RAM cost: 2 GB
|
||||
*/
|
||||
getContractTypes(): string[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user