mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-17 23:08:36 +02:00
functions to generate dummy contracts
This commit is contained in:
@@ -45,6 +45,15 @@ export function generateRandomContractOnHome(): void {
|
|||||||
serv.addContract(contract);
|
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 {
|
interface IGenerateContractParams {
|
||||||
problemType?: string;
|
problemType?: string;
|
||||||
server?: string;
|
server?: string;
|
||||||
@@ -176,7 +185,7 @@ function getRandomServer(): BaseServer {
|
|||||||
return randServer;
|
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)}`;
|
let contractFn = `contract-${getRandomInt(0, 1e6)}`;
|
||||||
|
|
||||||
for (let i = 0; i < 1000; ++i) {
|
for (let i = 0; i < 1000; ++i) {
|
||||||
|
|||||||
@@ -279,6 +279,8 @@ const codingcontract = {
|
|||||||
getData: RamCostConstants.ScriptCodingContractBaseRamCost / 2,
|
getData: RamCostConstants.ScriptCodingContractBaseRamCost / 2,
|
||||||
getDescription: RamCostConstants.ScriptCodingContractBaseRamCost / 2,
|
getDescription: RamCostConstants.ScriptCodingContractBaseRamCost / 2,
|
||||||
getNumTriesRemaining: RamCostConstants.ScriptCodingContractBaseRamCost / 5,
|
getNumTriesRemaining: RamCostConstants.ScriptCodingContractBaseRamCost / 5,
|
||||||
|
createDummyContract: RamCostConstants.ScriptCodingContractBaseRamCost / 5,
|
||||||
|
getContractTypes: RamCostConstants.ScriptCodingContractBaseRamCost / 5,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
// Duplicate Sleeve API
|
// Duplicate Sleeve API
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { CodingContract } from "../CodingContracts";
|
|||||||
import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions";
|
import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions";
|
||||||
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper";
|
||||||
import { helpers } from "../Netscript/NetscriptHelpers";
|
import { helpers } from "../Netscript/NetscriptHelpers";
|
||||||
|
import { codingContractTypesMetadata } from "../data/codingcontracttypes";
|
||||||
|
import { generateDummyContract } from "../CodingContractGenerator";
|
||||||
|
|
||||||
export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
||||||
const getCodingContract = function (
|
const getCodingContract = function (
|
||||||
@@ -39,7 +41,6 @@ export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
|||||||
// Convert answer to string.
|
// Convert answer to string.
|
||||||
const answerStr = typeof answer === "string" ? answer : JSON.stringify(answer);
|
const answerStr = typeof answer === "string" ? answer : JSON.stringify(answer);
|
||||||
const creward = contract.reward;
|
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);
|
const serv = helpers.getServer(ctx, hostname);
|
||||||
if (contract.isSolution(answerStr)) {
|
if (contract.isSolution(answerStr)) {
|
||||||
@@ -112,5 +113,12 @@ export function NetscriptCodingContract(): InternalAPI<ICodingContract> {
|
|||||||
const contract = getCodingContract(ctx, "getNumTriesRemaining", hostname, filename);
|
const contract = getCodingContract(ctx, "getNumTriesRemaining", hostname, filename);
|
||||||
return contract.getMaxNumTries() - contract.tries;
|
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 **************/
|
/************* 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) {
|
if (reward == null || reward.type == null) {
|
||||||
return `No reward for this contract`;
|
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.
|
* @returns How many attempts are remaining for the contract.
|
||||||
*/
|
*/
|
||||||
getNumTriesRemaining(filename: string, host?: string): number;
|
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[];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user