bitburner-src/src/NetscriptFunctions/CodingContract.ts

113 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-10-14 09:22:02 +02:00
import { INetscriptHelper } from "./INetscriptHelper";
import { WorkerScript } from "../Netscript/WorkerScript";
import { IPlayer } from "../PersonObjects/IPlayer";
import { getRamCost } from "../Netscript/RamCostGenerator";
import { is2DArray } from "../utils/helpers/is2DArray";
import { CodingContract } from "../CodingContracts";
import { CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions";
2021-10-14 09:22:02 +02:00
export function NetscriptCodingContract(
player: IPlayer,
workerScript: WorkerScript,
helper: INetscriptHelper,
): ICodingContract {
2021-11-20 21:01:04 +01:00
const getCodingContract = function (func: any, hostname: any, filename: any): CodingContract {
const server = helper.getServer(hostname, func);
const contract = server.getContract(filename);
2021-10-14 09:22:02 +02:00
if (contract == null) {
2021-11-20 21:01:04 +01:00
throw helper.makeRuntimeErrorMsg(
`codingcontract.${func}`,
`Cannot find contract '${filename}' on server '${hostname}'`,
);
2021-10-14 09:22:02 +02:00
}
return contract;
};
return {
attempt: function (
answer: any,
2021-11-20 21:01:04 +01:00
filename: any,
hostname: any = workerScript.hostname,
{ returnReward }: any = {},
): boolean | string {
2021-10-14 09:22:02 +02:00
helper.updateDynamicRam("attempt", getRamCost("codingcontract", "attempt"));
2021-11-20 21:01:04 +01:00
const contract = getCodingContract("attempt", hostname, filename);
2021-10-14 09:22:02 +02:00
// Convert answer to string. If the answer is a 2D array, then we have to
// manually add brackets for the inner arrays
if (is2DArray(answer)) {
const answerComponents = [];
for (let i = 0; i < answer.length; ++i) {
answerComponents.push(["[", answer[i].toString(), "]"].join(""));
}
answer = answerComponents.join(",");
} else {
answer = String(answer);
}
const creward = contract.reward;
if (creward === null) throw new Error("Somehow solved a contract that didn't have a reward");
2021-11-20 21:01:04 +01:00
const serv = helper.getServer(hostname, "codingcontract.attempt");
2021-10-14 09:22:02 +02:00
if (contract.isSolution(answer)) {
const reward = player.gainCodingContractReward(creward, contract.getDifficulty());
2021-11-20 21:01:04 +01:00
workerScript.log("attempt", `Successfully completed Coding Contract '${filename}'. Reward: ${reward}`);
serv.removeContract(filename);
2021-10-14 09:22:02 +02:00
return returnReward ? reward : true;
} else {
++contract.tries;
if (contract.tries >= contract.getMaxNumTries()) {
2021-11-20 21:01:04 +01:00
workerScript.log("attempt", `Coding Contract attempt '${filename}' failed. Contract is now self-destructing`);
serv.removeContract(filename);
2021-10-14 09:22:02 +02:00
} else {
workerScript.log(
"attempt",
2021-11-20 21:01:04 +01:00
`Coding Contract attempt '${filename}' failed. ${
contract.getMaxNumTries() - contract.tries
} attempts remaining.`,
2021-10-14 09:22:02 +02:00
);
}
return returnReward ? "" : false;
}
},
2021-11-20 21:01:04 +01:00
getContractType: function (filename: any, hostname: any = workerScript.hostname): string {
2021-10-14 09:22:02 +02:00
helper.updateDynamicRam("getContractType", getRamCost("codingcontract", "getContractType"));
2021-11-20 21:01:04 +01:00
const contract = getCodingContract("getContractType", hostname, filename);
2021-10-14 09:22:02 +02:00
return contract.getType();
},
2021-11-20 21:01:04 +01:00
getData: function (filename: any, hostname: any = workerScript.hostname): any {
2021-10-14 09:22:02 +02:00
helper.updateDynamicRam("getData", getRamCost("codingcontract", "getData"));
2021-11-20 21:01:04 +01:00
const contract = getCodingContract("getData", hostname, filename);
2021-10-14 09:22:02 +02:00
const data = contract.getData();
if (data.constructor === Array) {
// For two dimensional arrays, we have to copy the internal arrays using
// slice() as well. As of right now, no contract has arrays that have
// more than two dimensions
const copy = data.slice();
for (let i = 0; i < copy.length; ++i) {
if (data[i].constructor === Array) {
copy[i] = data[i].slice();
}
}
return copy;
} else {
return data;
}
},
2021-11-20 21:01:04 +01:00
getDescription: function (filename: any, hostname: any = workerScript.hostname): string {
2021-10-14 09:22:02 +02:00
helper.updateDynamicRam("getDescription", getRamCost("codingcontract", "getDescription"));
2021-11-20 21:01:04 +01:00
const contract = getCodingContract("getDescription", hostname, filename);
2021-10-14 09:22:02 +02:00
return contract.getDescription();
},
2021-11-20 21:01:04 +01:00
getNumTriesRemaining: function (filename: any, hostname: any = workerScript.hostname): number {
2021-10-14 09:22:02 +02:00
helper.updateDynamicRam("getNumTriesRemaining", getRamCost("codingcontract", "getNumTriesRemaining"));
2021-11-20 21:01:04 +01:00
const contract = getCodingContract("getNumTriesRemaining", hostname, filename);
2021-10-14 09:22:02 +02:00
return contract.getMaxNumTries() - contract.tries;
},
};
}