Fix contract input handling

This commit is contained in:
Snarling 2022-08-02 11:11:49 -04:00
parent 39cf0cb57f
commit b700b0246b
3 changed files with 9 additions and 29 deletions

@ -1,6 +1,5 @@
import { WorkerScript } from "../Netscript/WorkerScript"; import { WorkerScript } from "../Netscript/WorkerScript";
import { IPlayer } from "../PersonObjects/IPlayer"; import { IPlayer } from "../PersonObjects/IPlayer";
import { is2DArray } from "../utils/helpers/is2DArray";
import { CodingContract } from "../CodingContracts"; import { CodingContract } from "../CodingContracts";
import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions"; import { CodingAttemptOptions, CodingContract as ICodingContract } from "../ScriptEditor/NetscriptDefinitions";
import { InternalAPI, NetscriptContext } from "src/Netscript/APIWrapper"; 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 hostname = ctx.helper.string("hostname", _hostname);
const contract = getCodingContract(ctx, "attempt", hostname, filename); const contract = getCodingContract(ctx, "attempt", hostname, filename);
// Convert answer to string. If the answer is a 2D array, then we have to // Convert answer to string.
// manually add brackets for the inner arrays const answerStr = JSON.stringify(answer);
let answerStr = ""; if (answerStr === undefined) throw new Error("The provided answer could not be stringified");
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);
}
const creward = contract.reward; const creward = contract.reward;
if (creward === null) throw new Error("Somehow solved a contract that didn't have a reward"); if (creward === null) throw new Error("Somehow solved a contract that didn't have a reward");

@ -1422,9 +1422,13 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
solver: (_data: unknown, ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as [number, [number, number][]]; const data = _data as [number, [number, number][]];
//Sanitize player input
const sanitizedPlayerAns: string = removeBracketsFromArrayString(ans);
//Case where the player believes there is no solution. //Case where the player believes there is no solution.
//Attempt to construct one to check if this is correct. //Attempt to construct one to check if this is correct.
if (ans == "[]") { if (sanitizedPlayerAns === "") {
//Helper function to get neighbourhood of a vertex //Helper function to get neighbourhood of a vertex
function neighbourhood(vertex: number): number[] { function neighbourhood(vertex: number): number[] {
const adjLeft = data[1].filter(([a]) => a == vertex).map(([, b]) => b); const adjLeft = data[1].filter(([a]) => a == vertex).map(([, b]) => b);
@ -1473,12 +1477,9 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return false; return false;
} }
//Sanitize player input //Solution provided case
const sanitizedPlayerAns: string = removeBracketsFromArrayString(ans);
const sanitizedPlayerAnsArr: string[] = sanitizedPlayerAns.split(","); const sanitizedPlayerAnsArr: string[] = sanitizedPlayerAns.split(",");
const coloring: number[] = sanitizedPlayerAnsArr.map((val) => parseInt(val)); const coloring: number[] = sanitizedPlayerAnsArr.map((val) => parseInt(val));
//Solution provided case
if (coloring.length == data[0]) { if (coloring.length == data[0]) {
const edges = data[1]; const edges = data[1];
const validColors = [0, 1]; const validColors = [0, 1];

@ -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));
}