From f24412ceda8ebcf9f6de3c1874b752a713f28053 Mon Sep 17 00:00:00 2001 From: Claire Nord Date: Thu, 4 May 2023 14:13:43 -0700 Subject: [PATCH] fix(src parser): handle unrevealed clues, categories, answers "=" Clues, categories, and answers are marked with "=" when they weren't recorded. Handle these the same as "unrevealed". --- src/parser.ts | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index bc3b891..ec0f5db 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -33,6 +33,10 @@ interface Clue { /** ERROR_PLACEHOLDER is used when a field has an error. */ const ERROR_PLACEHOLDER = "***ERROR***"; +const UNREVEALED_PLACEHOLDER = "Unrevealed"; + +/** MISSING_CLUE_FLAG is used by j-archive when a clue was not recorded. */ +const MISSING_CLUE_FLAG = "="; /** parseGame parses the j-archive website and returns a representation of the * game in JSON. @@ -128,7 +132,7 @@ class FinalBoardParser { const categoryName = roundDiv?.querySelector(".category_name")?.textContent; if (!categoryName) { - this.errors.push("could not find class category_name on page"); + this.errors.push("could not find class category_name in final round"); this.category = ERROR_PLACEHOLDER; } else { this.category = categoryName; @@ -136,7 +140,7 @@ class FinalBoardParser { const clueText = roundDiv?.querySelector(".clue_text")?.textContent; if (!clueText) { - this.errors.push("could not find class clue_text on page"); + this.errors.push("could not find class clue_text in final round"); this.clue = ERROR_PLACEHOLDER; } else { this.clue = clueText; @@ -206,6 +210,8 @@ class BoardParser { `could not find class category_name in category ${i} round ${round}` ); name = ERROR_PLACEHOLDER; + } else if (categoryName === MISSING_CLUE_FLAG) { + name = UNREVEALED_PLACEHOLDER; } else { name = categoryName; } @@ -275,9 +281,9 @@ class ClueParser { // Identify Clue Text const clue = clueDiv.querySelector(".clue_text")?.textContent; - if (!clue) { + if (!clue || clue === MISSING_CLUE_FLAG) { unrevealed = true; - this.clue = "Unrevealed"; + this.clue = UNREVEALED_PLACEHOLDER; } else { this.clue = clue; } @@ -289,10 +295,8 @@ class ClueParser { )?.textContent; if (clueValueText) { - if (!clueValueText.startsWith("$")) { - this.errors.push(`clue value (${i}, ${j}) does not start with '$'`); - } - const clueValue = parseInt(clueValueText.slice(1)); + const startIdx = clueValueText.startsWith("$") ? 1 : 0; + const clueValue = parseInt(clueValueText.slice(startIdx)); if (isNaN(clueValue)) { this.errors.push( `could not parse clue value (${i}, ${j}) text ${clueValueText}` @@ -300,9 +304,9 @@ class ClueParser { } this.value = clueValue; } else if (clueValueDDText) { - if (!clueValueDDText.startsWith("DD: $")) { + if (!clueValueDDText.startsWith("DD: ")) { this.errors.push( - `DD clue value (${i}, ${j}) does not start with 'DD: $'` + `DD clue value (${i}, ${j}) does not start with 'DD: '` ); } this.value = getExpectedClueValue(i, round); @@ -313,15 +317,13 @@ class ClueParser { } const answerText = clueDiv.querySelector(".correct_response")?.textContent; - if (!answerText) { - if (unrevealed) { - this.answer = "Unrevealed"; - } else { - this.errors.push( - `could not find class correct_response in round ${round}, clue (${i}, ${j})` - ); - this.answer = ERROR_PLACEHOLDER; - } + if (unrevealed) { + this.answer = UNREVEALED_PLACEHOLDER; + } else if (!answerText) { + this.errors.push( + `could not find class correct_response in round ${round}, clue (${i}, ${j})` + ); + this.answer = ERROR_PLACEHOLDER; } else { this.answer = parseCorrectResponse(answerText); }