mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-20 21:25:47 +01:00
unknown-ify coding contract API.
This commit is contained in:
parent
6c9a3a336e
commit
4d9a8e7bba
@ -4,14 +4,14 @@ 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";
|
||||
import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions";
|
||||
|
||||
export function NetscriptCodingContract(
|
||||
player: IPlayer,
|
||||
workerScript: WorkerScript,
|
||||
helper: INetscriptHelper,
|
||||
): ICodingContract {
|
||||
const getCodingContract = function (func: any, hostname: any, filename: any): CodingContract {
|
||||
const getCodingContract = function (func: string, hostname: string, filename: string): CodingContract {
|
||||
const server = helper.getServer(hostname, func);
|
||||
const contract = server.getContract(filename);
|
||||
if (contract == null) {
|
||||
@ -27,10 +27,12 @@ export function NetscriptCodingContract(
|
||||
return {
|
||||
attempt: function (
|
||||
answer: any,
|
||||
filename: any,
|
||||
hostname: any = workerScript.hostname,
|
||||
{ returnReward }: any = {},
|
||||
_filename: unknown,
|
||||
_hostname: unknown = workerScript.hostname,
|
||||
{ returnReward }: CodingAttemptOptions = { returnReward: false },
|
||||
): boolean | string {
|
||||
const filename = helper.string("attempt", "filename", _filename);
|
||||
const hostname = helper.string("attempt", "hostname", _hostname);
|
||||
helper.updateDynamicRam("attempt", getRamCost(player, "codingcontract", "attempt"));
|
||||
const contract = getCodingContract("attempt", hostname, filename);
|
||||
|
||||
@ -53,7 +55,10 @@ export function NetscriptCodingContract(
|
||||
const serv = helper.getServer(hostname, "codingcontract.attempt");
|
||||
if (contract.isSolution(answer)) {
|
||||
const reward = player.gainCodingContractReward(creward, contract.getDifficulty());
|
||||
workerScript.log("codingcontract.attempt", () => `Successfully completed Coding Contract '${filename}'. Reward: ${reward}`);
|
||||
workerScript.log(
|
||||
"codingcontract.attempt",
|
||||
() => `Successfully completed Coding Contract '${filename}'. Reward: ${reward}`,
|
||||
);
|
||||
serv.removeContract(filename);
|
||||
return returnReward ? reward : true;
|
||||
} else {
|
||||
@ -68,7 +73,8 @@ export function NetscriptCodingContract(
|
||||
workerScript.log(
|
||||
"codingcontract.attempt",
|
||||
() =>
|
||||
`Coding Contract attempt '${filename}' failed. ${contract.getMaxNumTries() - contract.tries
|
||||
`Coding Contract attempt '${filename}' failed. ${
|
||||
contract.getMaxNumTries() - contract.tries
|
||||
} attempts remaining.`,
|
||||
);
|
||||
}
|
||||
@ -76,12 +82,16 @@ export function NetscriptCodingContract(
|
||||
return returnReward ? "" : false;
|
||||
}
|
||||
},
|
||||
getContractType: function (filename: any, hostname: any = workerScript.hostname): string {
|
||||
getContractType: function (_filename: unknown, _hostname: unknown = workerScript.hostname): string {
|
||||
const filename = helper.string("getContractType", "filename", _filename);
|
||||
const hostname = helper.string("getContractType", "hostname", _hostname);
|
||||
helper.updateDynamicRam("getContractType", getRamCost(player, "codingcontract", "getContractType"));
|
||||
const contract = getCodingContract("getContractType", hostname, filename);
|
||||
return contract.getType();
|
||||
},
|
||||
getData: function (filename: any, hostname: any = workerScript.hostname): any {
|
||||
getData: function (_filename: unknown, _hostname: unknown = workerScript.hostname): any {
|
||||
const filename = helper.string("getContractType", "filename", _filename);
|
||||
const hostname = helper.string("getContractType", "hostname", _hostname);
|
||||
helper.updateDynamicRam("getData", getRamCost(player, "codingcontract", "getData"));
|
||||
const contract = getCodingContract("getData", hostname, filename);
|
||||
const data = contract.getData();
|
||||
@ -101,12 +111,16 @@ export function NetscriptCodingContract(
|
||||
return data;
|
||||
}
|
||||
},
|
||||
getDescription: function (filename: any, hostname: any = workerScript.hostname): string {
|
||||
getDescription: function (_filename: unknown, _hostname: unknown = workerScript.hostname): string {
|
||||
const filename = helper.string("getDescription", "filename", _filename);
|
||||
const hostname = helper.string("getDescription", "hostname", _hostname);
|
||||
helper.updateDynamicRam("getDescription", getRamCost(player, "codingcontract", "getDescription"));
|
||||
const contract = getCodingContract("getDescription", hostname, filename);
|
||||
return contract.getDescription();
|
||||
},
|
||||
getNumTriesRemaining: function (filename: any, hostname: any = workerScript.hostname): number {
|
||||
getNumTriesRemaining: function (_filename: unknown, _hostname: unknown = workerScript.hostname): number {
|
||||
const filename = helper.string("getNumTriesRemaining", "filename", _filename);
|
||||
const hostname = helper.string("getNumTriesRemaining", "hostname", _hostname);
|
||||
helper.updateDynamicRam("getNumTriesRemaining", getRamCost(player, "codingcontract", "getNumTriesRemaining"));
|
||||
const contract = getCodingContract("getNumTriesRemaining", hostname, filename);
|
||||
return contract.getMaxNumTries() - contract.tries;
|
||||
|
@ -163,7 +163,7 @@ export class BaseServer {
|
||||
return false;
|
||||
}
|
||||
|
||||
removeContract(contract: CodingContract): void {
|
||||
removeContract(contract: CodingContract | string): void {
|
||||
if (contract instanceof CodingContract) {
|
||||
this.contracts = this.contracts.filter((c) => {
|
||||
return c.fn !== contract.fn;
|
||||
|
Loading…
Reference in New Issue
Block a user