Skip to content

Commit

Permalink
fix(src parser): handle unrevealed clues, categories, answers "="
Browse files Browse the repository at this point in the history
Clues, categories, and answers are marked with "=" when they weren't
recorded. Handle these the same as "unrevealed".
  • Loading branch information
cmnord committed May 4, 2023
1 parent 49159f6 commit 50215d5
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/parser.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -128,15 +132,15 @@ 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;
}

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;
Expand Down Expand Up @@ -206,6 +210,9 @@ class BoardParser {
`could not find class category_name in category ${i} round ${round}`
);
name = ERROR_PLACEHOLDER;
} else if (categoryName === MISSING_CLUE_FLAG) {
this.errors.push(`category ${i} round ${round} is missing`);
name = UNREVEALED_PLACEHOLDER;
} else {
name = categoryName;
}
Expand Down Expand Up @@ -275,9 +282,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;
}
Expand All @@ -289,20 +296,18 @@ 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}`
);
}
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);
Expand All @@ -313,15 +318,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);
}
Expand Down

0 comments on commit 50215d5

Please sign in to comment.