From 84de184f186b37b647c6240bd0bb309187af6122 Mon Sep 17 00:00:00 2001 From: Cody Seibert Date: Thu, 17 Oct 2024 23:30:06 -0400 Subject: [PATCH] better error handling --- models/index.ts | 59 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/models/index.ts b/models/index.ts index bfa64d3..50853fa 100644 --- a/models/index.ts +++ b/models/index.ts @@ -109,6 +109,35 @@ export async function runModel( throw new Error(`Tried running unknown model '${modelId}'`); } } + const originalMap = JSON.parse(JSON.stringify(map)); + const [playerRow, playerCol] = result.playerCoordinates; + + if (originalMap[playerRow][playerCol] !== " ") { + return { + reasoning: result.reasoning, + error: "Tried to place player in a non-empty space", + }; + } + + originalMap[playerRow][playerCol] = "P"; + + for (const block of result.boxCoordinates) { + const [blockRow, blockCol] = block; + + if (originalMap[blockRow][blockCol] !== " ") { + return { + reasoning: result.reasoning, + error: "Tried to place block in a non-empty space", + }; + } + + originalMap[blockRow][blockCol] = "B"; + } + + return { + solution: originalMap, + reasoning: result.reasoning, + }; } catch (error) { return { reasoning: "Internal error", @@ -120,34 +149,4 @@ export async function runModel( : "Unknown error", }; } - - const originalMap = JSON.parse(JSON.stringify(map)); - const [playerRow, playerCol] = result.playerCoordinates; - - if (originalMap[playerRow][playerCol] !== " ") { - return { - reasoning: result.reasoning, - error: "Tried to place player in a non-empty space", - }; - } - - originalMap[playerRow][playerCol] = "P"; - - for (const block of result.boxCoordinates) { - const [blockRow, blockCol] = block; - - if (originalMap[blockRow][blockCol] !== " ") { - return { - reasoning: result.reasoning, - error: "Tried to place block in a non-empty space", - }; - } - - originalMap[blockRow][blockCol] = "B"; - } - - return { - solution: originalMap, - reasoning: result.reasoning, - }; }