Skip to content

Commit

Permalink
feat(src content-script): optional wagerable property on clue
Browse files Browse the repository at this point in the history
Add wagerable?: boolean to clues.
  • Loading branch information
cmnord committed Mar 18, 2023
1 parent 4bf5687 commit f686e0e
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions src/content-script.ts
Expand Up @@ -22,6 +22,7 @@ interface Clue {
clue: string;
answer: string;
value: number;
wagerable?: boolean;
}

class NotFoundError extends Error {
Expand Down Expand Up @@ -75,7 +76,7 @@ export class GameParser {
constructor(document: Document) {
const title = document.querySelector("#game_title")?.textContent;
if (!title) {
throw new Error("could not find id game_title on page");
throw new NotFoundError("could not find id game_title on page");
}
this.title = title;

Expand All @@ -84,19 +85,21 @@ export class GameParser {

const jDiv = document.getElementById("jeopardy_round");
if (!jDiv) {
throw new Error("could not find id jeopardy_round on page");
throw new NotFoundError("could not find id jeopardy_round on page");
}
this.j = new RoundParser(jDiv);

const djDiv = document.getElementById("double_jeopardy_round");
if (!djDiv) {
throw new Error("could not find id double_jeopardy_round on page");
throw new NotFoundError(
"could not find id double_jeopardy_round on page"
);
}
this.dj = new RoundParser(djDiv);

const fjDiv = document.getElementById("final_jeopardy_round");
if (!fjDiv) {
throw new Error("could not find id final_jeopardy_round on page");
throw new NotFoundError("could not find id final_jeopardy_round on page");
}
this.fj = new FinalRoundParser(fjDiv);
}
Expand Down Expand Up @@ -141,12 +144,12 @@ class FinalRoundParser {
constructor(roundDiv: HTMLElement) {
const categoryName = roundDiv.querySelector(".category_name")?.textContent;
if (!categoryName) {
throw new Error("could not find class category_name on page");
throw new NotFoundError("could not find class category_name on page");
}
this.category = categoryName;
const clueText = roundDiv.querySelector(".clue_text")?.textContent;
if (!clueText) {
throw new Error("could not find class clue_text on page");
throw new NotFoundError("could not find class clue_text on page");
}
this.clue = clueText;

Expand All @@ -168,6 +171,7 @@ class FinalRoundParser {
clue: this.clue,
value: 0,
answer: this.answer,
wagerable: true,
},
],
},
Expand Down Expand Up @@ -235,6 +239,7 @@ class RoundParser {
value: clue.value,
clue: clue.clue,
answer: clue.answer,
wagerable: clue.wagerable,
};
jsonData.categories[categoryIdx].clues.push(clueDict);
}
Expand All @@ -248,39 +253,42 @@ class ClueParser {
category: string;
value: number;
answer: string;
isDailyDouble: boolean;
wagerable?: boolean;
i: number;
j: number;

constructor(clueDiv: Element, category: string, i: number, j: number) {
this.i = i;
this.j = j;
// Identify Clue Text and Category
try {
const clue = clueDiv.querySelector(".clue_text")?.textContent;
if (!clue) {
throw new Error(
`could not find class clue_text on page for clue ${i}, ${j}`
);
}
this.clue = clue;
} catch (error: unknown) {
// AttributeError
this.clue = "Unrevealed";
}
const clue = clueDiv.querySelector(".clue_text")?.textContent;
this.clue = clue ?? "Unrevealed";
this.category = category;

// Find Clue Value
try {
const valueStr = clueDiv
.querySelector(".clue_value")
?.textContent?.slice(1);
this.value = parseInt(valueStr ?? "") || 0;
this.isDailyDouble = false;
} catch (error: unknown) {
// AttributeError
const clueValueText = clueDiv.querySelector(".clue_value")?.textContent;
const clueValueDDText = clueDiv.querySelector(
".clue_value_daily_double"
)?.textContent;

if (clueValueText) {
if (!clueValueText.startsWith("$")) {
throw new Error("clue value does not start with '$'");
}
const clueValue = parseInt(clueValueText.slice(1));
if (isNaN(clueValue)) {
throw new Error("could not parse clue value " + clueValueText);
}
this.value = clueValue;
} else if (clueValueDDText) {
if (!clueValueDDText.startsWith("DD: $")) {
throw new Error("DD clue value does not start with 'DD: $'");
}
this.value = 0;
this.wagerable = true;
} else {
// Unrevealed
this.value = 0;
this.isDailyDouble = true;
}

const mouseOverDiv =
Expand Down

0 comments on commit f686e0e

Please sign in to comment.