unknown-ify coding contract API.

This commit is contained in:
Olivier Gagnon 2022-03-29 20:29:05 -04:00
parent 6c9a3a336e
commit 4d9a8e7bba
2 changed files with 26 additions and 12 deletions

@ -4,14 +4,14 @@ import { IPlayer } from "../PersonObjects/IPlayer";
import { getRamCost } from "../Netscript/RamCostGenerator"; import { getRamCost } from "../Netscript/RamCostGenerator";
import { is2DArray } from "../utils/helpers/is2DArray"; import { is2DArray } from "../utils/helpers/is2DArray";
import { CodingContract } from "../CodingContracts"; import { CodingContract } from "../CodingContracts";
import { CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions"; import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions";
export function NetscriptCodingContract( export function NetscriptCodingContract(
player: IPlayer, player: IPlayer,
workerScript: WorkerScript, workerScript: WorkerScript,
helper: INetscriptHelper, helper: INetscriptHelper,
): ICodingContract { ): 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 server = helper.getServer(hostname, func);
const contract = server.getContract(filename); const contract = server.getContract(filename);
if (contract == null) { if (contract == null) {
@ -27,10 +27,12 @@ export function NetscriptCodingContract(
return { return {
attempt: function ( attempt: function (
answer: any, answer: any,
filename: any, _filename: unknown,
hostname: any = workerScript.hostname, _hostname: unknown = workerScript.hostname,
{ returnReward }: any = {}, { returnReward }: CodingAttemptOptions = { returnReward: false },
): boolean | string { ): boolean | string {
const filename = helper.string("attempt", "filename", _filename);
const hostname = helper.string("attempt", "hostname", _hostname);
helper.updateDynamicRam("attempt", getRamCost(player, "codingcontract", "attempt")); helper.updateDynamicRam("attempt", getRamCost(player, "codingcontract", "attempt"));
const contract = getCodingContract("attempt", hostname, filename); const contract = getCodingContract("attempt", hostname, filename);
@ -53,7 +55,10 @@ export function NetscriptCodingContract(
const serv = helper.getServer(hostname, "codingcontract.attempt"); const serv = helper.getServer(hostname, "codingcontract.attempt");
if (contract.isSolution(answer)) { if (contract.isSolution(answer)) {
const reward = player.gainCodingContractReward(creward, contract.getDifficulty()); 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); serv.removeContract(filename);
return returnReward ? reward : true; return returnReward ? reward : true;
} else { } else {
@ -68,7 +73,8 @@ export function NetscriptCodingContract(
workerScript.log( workerScript.log(
"codingcontract.attempt", "codingcontract.attempt",
() => () =>
`Coding Contract attempt '${filename}' failed. ${contract.getMaxNumTries() - contract.tries `Coding Contract attempt '${filename}' failed. ${
contract.getMaxNumTries() - contract.tries
} attempts remaining.`, } attempts remaining.`,
); );
} }
@ -76,12 +82,16 @@ export function NetscriptCodingContract(
return returnReward ? "" : false; 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")); helper.updateDynamicRam("getContractType", getRamCost(player, "codingcontract", "getContractType"));
const contract = getCodingContract("getContractType", hostname, filename); const contract = getCodingContract("getContractType", hostname, filename);
return contract.getType(); 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")); helper.updateDynamicRam("getData", getRamCost(player, "codingcontract", "getData"));
const contract = getCodingContract("getData", hostname, filename); const contract = getCodingContract("getData", hostname, filename);
const data = contract.getData(); const data = contract.getData();
@ -101,12 +111,16 @@ export function NetscriptCodingContract(
return data; 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")); helper.updateDynamicRam("getDescription", getRamCost(player, "codingcontract", "getDescription"));
const contract = getCodingContract("getDescription", hostname, filename); const contract = getCodingContract("getDescription", hostname, filename);
return contract.getDescription(); 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")); helper.updateDynamicRam("getNumTriesRemaining", getRamCost(player, "codingcontract", "getNumTriesRemaining"));
const contract = getCodingContract("getNumTriesRemaining", hostname, filename); const contract = getCodingContract("getNumTriesRemaining", hostname, filename);
return contract.getMaxNumTries() - contract.tries; return contract.getMaxNumTries() - contract.tries;

@ -163,7 +163,7 @@ export class BaseServer {
return false; return false;
} }
removeContract(contract: CodingContract): void { removeContract(contract: CodingContract | string): void {
if (contract instanceof CodingContract) { if (contract instanceof CodingContract) {
this.contracts = this.contracts.filter((c) => { this.contracts = this.contracts.filter((c) => {
return c.fn !== contract.fn; return c.fn !== contract.fn;