Skip to content

Commit

Permalink
Merge pull request #66 from bskinn/56-fix-debug-printing
Browse files Browse the repository at this point in the history
Add mechanism for debug printing from inside the active extension
  • Loading branch information
bskinn authored Mar 29, 2024
2 parents 511c309 + 2346b2e commit 2f3f6fc
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## CHANGELOG: bga-wingspan-scraper

### Firefox extension to scrape data from a Wingspan game replay on Board Game Arena

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

This project follows a `yyyy.mm.dd.n` flavor of
[Calendar Versioning](https://calver.org/). The `n` value is sequential and
serves to distinguish between multiple releases created on the same day.

### _Unreleased_

#### Features

- TypeScript codebase, webpacked, manifested, and zipped. Builds to a
single-file artifact ready to be loaded into Firefox's 'Load Temporary Add-on'
feature.

- Set up to scrape each player's score at the end of each 'turnset' (at the
point when the active turn returns to the player with the first-turn token
that round).

- Automatically advances the replay through the game history in order to put the
page into the state needed to accurately scrape the scores.

- Includes a UI for debugging during development, lightweight consistency
checking of key data for a given replay, and kickoff of the score-scraping
process.
61 changes: 58 additions & 3 deletions src/ui/ui.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { checkFullPlaySequence, checkMoveListLength } from '@/checks/moves'
import { getRawMoveInfo } from '@/data/moves'
import { sleepHelper } from '@/helpers/async'
import { scrapeAndSave } from '@/scrape/scores'

const addDebugControls = () => {
const GET_RAW_MOVE_INFO = 'getRawMoveInfo()'
const RAW_MOVE_ID_LIST = 'Raw Move ID List'

const OPTIONS = [GET_RAW_MOVE_INFO, RAW_MOVE_ID_LIST]

const addDebugControls = (dropdown: HTMLSelectElement) => {
// Button to trigger debug eval
// These controls may be obsolete, since it's hard to reach into the
// webpack this way
Expand All @@ -29,8 +35,33 @@ const addDebugControls = () => {

// Evalaute the expression in the input box when the button is clicked
buttonDebugPrint.addEventListener('click', () => {
var func: () => any
const commandArgStrs = inputDebugEval.value.split(',')

// @ts-ignore
const commandArgVals = commandArgStrs.map((arg) => eval(arg))

const evaluator = (func: () => any) => {
return func()
}

// TODO: Refactor this switch for func creation to a new file.
// No need to bloat this one.
switch (dropdown.value) {
case GET_RAW_MOVE_INFO:
func = () => getRawMoveInfo()
break

case RAW_MOVE_ID_LIST:
func = () => getRawMoveInfo().map((rm) => parseInt(rm.moveNum))
break

default:
func = () => 'Not Implemented'
}

try {
const result = eval(inputDebugEval.value)
const result = evaluator(func)
alert(JSON.stringify(result))
} catch (err) {
alert(`Error: ${(err as Error).message}`)
Expand Down Expand Up @@ -97,6 +128,29 @@ const addButtonCheckMoveList = () => {
document.body.appendChild(buttonCheckMoveList)
}

const addDropDownRunFunction = (): HTMLSelectElement => {
// Dropdown for selecting which function to run
// Pulls arguments from inputDebugEval
const dropdownRunFunction = document.createElement('select')
dropdownRunFunction.id = 'dropdownRunFunction'
dropdownRunFunction.style.position = 'fixed'
dropdownRunFunction.style.top = '90%'
dropdownRunFunction.style.left = '26em'
dropdownRunFunction.style.height = '2.25em'
dropdownRunFunction.style.width = '15em'
dropdownRunFunction.style.paddingLeft = '0.5em'

OPTIONS.forEach((opText) => {
var option = document.createElement('option')
option.textContent = opText
dropdownRunFunction.appendChild(option)
})

document.body.appendChild(dropdownRunFunction)

return dropdownRunFunction
}

const addButtonAwaitTest = () => {
// Button for testing await behavior
const buttonAwaitTest = document.createElement('button')
Expand All @@ -120,5 +174,6 @@ export const buildUI = () => {
addButtonCheckMoveList()
addButtonCheckPlaySeq()
addButtonScrapeScores()
addDebugControls()
const dropdown = addDropDownRunFunction()
addDebugControls(dropdown)
}

0 comments on commit 2f3f6fc

Please sign in to comment.