From b700b0246b0d5549f3b6c85bf2c3e4e22f250cbe Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Tue, 2 Aug 2022 11:11:49 -0400 Subject: [PATCH] Fix contract input handling --- src/NetscriptFunctions/CodingContract.ts | 17 +++-------------- src/data/codingcontracttypes.ts | 11 ++++++----- src/utils/helpers/is2DArray.ts | 10 ---------- 3 files changed, 9 insertions(+), 29 deletions(-) delete mode 100644 src/utils/helpers/is2DArray.ts diff --git a/src/NetscriptFunctions/CodingContract.ts b/src/NetscriptFunctions/CodingContract.ts index 77afb7f22..388ce1202 100644 --- a/src/NetscriptFunctions/CodingContract.ts +++ b/src/NetscriptFunctions/CodingContract.ts @@ -1,6 +1,5 @@ import { WorkerScript } from "../Netscript/WorkerScript"; import { IPlayer } from "../PersonObjects/IPlayer"; -import { is2DArray } from "../utils/helpers/is2DArray"; import { CodingContract } from "../CodingContracts"; import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions"; import { InternalAPI, NetscriptContext } from "src/Netscript/APIWrapper"; @@ -34,19 +33,9 @@ export function NetscriptCodingContract(player: IPlayer, workerScript: WorkerScr const hostname = ctx.helper.string("hostname", _hostname); const contract = getCodingContract(ctx, "attempt", hostname, filename); - // Convert answer to string. If the answer is a 2D array, then we have to - // manually add brackets for the inner arrays - let answerStr = ""; - if (is2DArray(answer)) { - const answerComponents = []; - for (let i = 0; i < answer.length; ++i) { - answerComponents.push(["[", String(answer[i]), "]"].join("")); - } - - answerStr = answerComponents.join(","); - } else { - answerStr = String(answer); - } + // Convert answer to string. + const answerStr = JSON.stringify(answer); + if (answerStr === undefined) throw new Error("The provided answer could not be stringified"); const creward = contract.reward; if (creward === null) throw new Error("Somehow solved a contract that didn't have a reward"); diff --git a/src/data/codingcontracttypes.ts b/src/data/codingcontracttypes.ts index d18cb12f6..e8dfbea88 100644 --- a/src/data/codingcontracttypes.ts +++ b/src/data/codingcontracttypes.ts @@ -1422,9 +1422,13 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ }, solver: (_data: unknown, ans: string): boolean => { const data = _data as [number, [number, number][]]; + + //Sanitize player input + const sanitizedPlayerAns: string = removeBracketsFromArrayString(ans); + //Case where the player believes there is no solution. //Attempt to construct one to check if this is correct. - if (ans == "[]") { + if (sanitizedPlayerAns === "") { //Helper function to get neighbourhood of a vertex function neighbourhood(vertex: number): number[] { const adjLeft = data[1].filter(([a]) => a == vertex).map(([, b]) => b); @@ -1473,12 +1477,9 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ return false; } - //Sanitize player input - const sanitizedPlayerAns: string = removeBracketsFromArrayString(ans); + //Solution provided case const sanitizedPlayerAnsArr: string[] = sanitizedPlayerAns.split(","); const coloring: number[] = sanitizedPlayerAnsArr.map((val) => parseInt(val)); - - //Solution provided case if (coloring.length == data[0]) { const edges = data[1]; const validColors = [0, 1]; diff --git a/src/utils/helpers/is2DArray.ts b/src/utils/helpers/is2DArray.ts deleted file mode 100644 index 868785d04..000000000 --- a/src/utils/helpers/is2DArray.ts +++ /dev/null @@ -1,10 +0,0 @@ -export function isArray(arr: unknown): arr is unknown[] { - return Array.isArray(arr); -} - -// Checks whether an array is a 2D array. -// For this, a 2D array is an array which contains only other arrays. -// If one element in the array is a number or string, it is NOT a 2D array -export function is2DArray(arr: unknown): arr is unknown[][] { - return isArray(arr) && arr.every((u) => isArray(u)); -}