diff --git a/markdown/bitburner.codingcontract.createdummycontract.md b/markdown/bitburner.codingcontract.createdummycontract.md index e24dcd497..9ae425d83 100644 --- a/markdown/bitburner.codingcontract.createdummycontract.md +++ b/markdown/bitburner.codingcontract.createdummycontract.md @@ -9,7 +9,7 @@ Generate a dummy contract. **Signature:** ```typescript -createDummyContract(type: CodingContractName): string | null; +createDummyContract(type: CodingContractName, host?: string): string | null; ``` ## Parameters @@ -45,6 +45,22 @@ type Type of contract to generate + + + +host + + + + +string + + + + +_(Optional)_ Hostname/IP of the server containing the contract. Optional. Defaults to the server the calling script is running on. + + @@ -58,7 +74,7 @@ Filename of the contract. RAM cost: 2 GB -Generate a dummy contract on the home computer with no reward. Used to test various algorithms. +Generate a dummy contract on the current server with no reward. Used to test various algorithms. This function will return null and not generate a contract if the randomized contract name is the same as another contract's name. diff --git a/markdown/bitburner.codingcontract.md b/markdown/bitburner.codingcontract.md index 356a4efa5..071192a6b 100644 --- a/markdown/bitburner.codingcontract.md +++ b/markdown/bitburner.codingcontract.md @@ -38,7 +38,7 @@ Attempts a coding contract, returning a reward string on success or empty string -[createDummyContract(type)](./bitburner.codingcontract.createdummycontract.md) +[createDummyContract(type, host)](./bitburner.codingcontract.createdummycontract.md) diff --git a/src/CodingContract/ContractGenerator.ts b/src/CodingContract/ContractGenerator.ts index be9fa3d9a..238d60115 100644 --- a/src/CodingContract/ContractGenerator.ts +++ b/src/CodingContract/ContractGenerator.ts @@ -113,16 +113,15 @@ export function generateRandomContractOnHome(): void { serv.addContract(contract); } -export const generateDummyContract = (problemType: CodingContractName): string | null => { +export const generateDummyContract = (problemType: CodingContractName, server: BaseServer): string | null => { if (!CodingContractTypes[problemType]) throw new Error(`Invalid problem type: '${problemType}'`); - const serv = Player.getHomeComputer(); - const contractFn = getRandomFilename(serv); + const contractFn = getRandomFilename(server); if (contractFn == null) { return null; } const contract = new CodingContract(contractFn, problemType, null); - serv.addContract(contract); + server.addContract(contract); return contractFn; }; diff --git a/src/NetscriptFunctions/CodingContract.ts b/src/NetscriptFunctions/CodingContract.ts index 5afc2dbbd..e7c9f9558 100644 --- a/src/NetscriptFunctions/CodingContract.ts +++ b/src/NetscriptFunctions/CodingContract.ts @@ -127,9 +127,11 @@ export function NetscriptCodingContract(): InternalAPI { const contract = getCodingContract(ctx, host, filename); return contract.getMaxNumTries() - contract.tries; }, - createDummyContract: (ctx) => (_type) => { + createDummyContract: (ctx) => (_type, _host?) => { const type = getEnumHelper("CodingContractName").nsGetMember(ctx, _type); - return generateDummyContract(type); + const host = _host ? helpers.string(ctx, "host", _host) : ctx.workerScript.hostname; + const server = helpers.getServer(ctx, host); + return generateDummyContract(type, server); }, getContractTypes: () => () => Object.values(CodingContractName), }; diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 32660f9a8..60510c9a3 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4057,14 +4057,15 @@ export interface CodingContract { * @remarks * RAM cost: 2 GB * - * Generate a dummy contract on the home computer with no reward. Used to test various algorithms. + * Generate a dummy contract on the current server with no reward. Used to test various algorithms. * * This function will return null and not generate a contract if the randomized contract name is the same as another contract's name. * * @param type - Type of contract to generate + * @param host - Hostname/IP of the server containing the contract. Optional. Defaults to the server the calling script is running on. * @returns Filename of the contract. */ - createDummyContract(type: CodingContractName): string | null; + createDummyContract(type: CodingContractName, host?: string): string | null; /** * List all contract types. diff --git a/src/utils/APIBreaks/3.0.0.ts b/src/utils/APIBreaks/3.0.0.ts index 4c0bc0357..d00369718 100644 --- a/src/utils/APIBreaks/3.0.0.ts +++ b/src/utils/APIBreaks/3.0.0.ts @@ -499,11 +499,26 @@ export const breakingChanges300: VersionBreakingChange = { showWarning: false, }, { - brokenAPIs: [{ name: "createDummyContract" }], + brokenAPIs: [ + { + name: "createDummyContract", + migration: { + searchValue: "createDummyContract", + migrator: (line: string) => { + for (const match of line.matchAll(/createDummyContract\([^,]*?\)/g)) { + line = line.replace(match[0], match[0].slice(0, match[0].length - 1) + `, "home")`); + } + return line; + }, + }, + }, + ], info: "ns.codingcontract.createDummyContract might generate a contract with the same name of another contract.\n" + "This bug was fixed. Now this function will return null and not generate a contract if the randomized contract " + - "name is the same as another contract's name.", + "name is the same as another contract's name.\n\n" + + "ns.codingcontract.createDummyContract generated a dummy contract on home. Now you can specify the host that \n" + + 'gets the generated contract with a new optional parameter. Your code was migrated to specify "home" as the host.', showWarning: false, }, ], diff --git a/test/jest/CodingContract/ContractGenerator.test.ts b/test/jest/CodingContract/ContractGenerator.test.ts index e4bc9c950..bdc979c42 100644 --- a/test/jest/CodingContract/ContractGenerator.test.ts +++ b/test/jest/CodingContract/ContractGenerator.test.ts @@ -35,7 +35,7 @@ describe("Generator", () => { assertNumberOfContracts(1, [Player.getHomeComputer()]); }); test("generateDummyContract", () => { - generateDummyContract(CodingContractName.FindLargestPrimeFactor); + generateDummyContract(CodingContractName.FindLargestPrimeFactor, Player.getHomeComputer()); assertNumberOfContracts(1, GetAllServers()); }); // generateContract is flexible. All properties in IGenerateContractParams are optional. This test checks the usage in