Skip to content

fix: harden chest patch, battle calc loading, and double-injection#62

Merged
timzatko merged 3 commits into
mainfrom
fix/chest-anchor-and-battle-calc-load
Jun 29, 2026
Merged

fix: harden chest patch, battle calc loading, and double-injection#62
timzatko merged 3 commits into
mainfrom
fix/chest-anchor-and-battle-calc-load

Conversation

@timzatko

@timzatko timzatko commented Jun 28, 2026

Copy link
Copy Markdown
Member

Why

Three intermittent runtime errors reported in production:

  1. Chests.init | Unexpected token '}' — the chest tracker rewrites ItemUse.doIt by string-inserting a trackChest call before the item_used signal. The anchor lookup used a single-quote-only indexOf with no "not found" guard. When indexOf returned -1 (anchor absent, e.g. a minified build emitting the signal with double quotes), the substr math collapsed the body to TW_Calc.trackChest(itemId,res);} and eval threw a misleading Unexpected token '}'.

  2. "Battle calc core script is not loaded"BattleCalc.init fired a single fire-and-forget $.getScript with no error handling, so a transient/failed fetch left the calc unavailable for the whole session, with only "refresh the game" as recovery.

  3. Initializer.registerApi | given parameter ('tw-calc') is already registered! — the script could be injected more than once into the same page (re-injection or a same-origin iframe), so bootstrap ran twice and TheWestApi.register threw on the second run. The register collision was the visible symptom of everything initializing twice.

What

Chests

  • Quote-agnostic anchor via regex (/EventHandler\.signal\(['"]item_used['"]/) so double-quoted builds patch correctly.
  • Guard the not-found case: throw a clear, trackable error instead of building broken code from a -1 position.

Battle calc

  • Retry the core-script load up to 3 times with a backoff, logging each failure via Logger.
  • Guard against concurrent/duplicate loads with a loading flag.
  • When a view finds the calc unavailable, kick off a background reload so reopening the window recovers it without a full game refresh.

Double injection

  • Extract the injection logic into src/inject.ts and skip bootstrapping when window.TW_Calc is already set, making injection idempotent (fixes the root cause, not just the API-register symptom).

Tests

New specs for all three areas (none existed before) — 14 new tests, full suite green (npm test → 18/18). tsc, eslint, and npm run build:dev all clean.

  • chests.spec.ts: single/double-quote anchor patching + missing-anchor error path.
  • battle-calc.spec.ts: load on init, retry-with-logging, in-progress/already-available guards, getInstance behavior.
  • inject.spec.ts: game-ready and already-injected guards (driven with jasmine's clock).

🤖 Generated with Claude Code

timzatko and others added 3 commits June 29, 2026 00:06
The chest tracker rewrites ItemUse.doIt by string-inserting a trackChest
call before the item_used signal. The anchor lookup used a single-quote-only
indexOf and had no guard for "not found": when indexOf returned -1, the substr
math collapsed the body to `TW_Calc.trackChest(itemId,res);}` and eval threw a
misleading `Unexpected token '}'`.

Use a quote-agnostic regex (some minified builds emit the signal with double
quotes) and fail loudly with a clear, trackable error instead of evaling broken
code from a -1 position.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
BattleCalc.init fired a single fire-and-forget $.getScript with no error
handling, so a transient/failed fetch left the calc unavailable for the whole
session with only "refresh the game" as recovery.

- Retry the load up to 3 times with a backoff and log each failure via Logger.
- Guard against concurrent/duplicate loads with a loading flag.
- When a view finds the calc unavailable, kick off a background reload so
  reopening the window recovers it without a full game refresh.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- Chests: patches with single- and double-quoted item_used anchors, and
  tracks an error (leaving the handler untouched) when the anchor is missing.
- BattleCalc: loads on init, retries up to 3 times with logging on failure,
  is a no-op while loading or already available, and getInstance throws/returns
  per availability.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@timzatko timzatko merged commit 854b4ce into main Jun 29, 2026
5 checks passed
@timzatko timzatko changed the title fix: harden chest handler patch and battle calc core loading fix: harden chest patch, battle calc loading, and double-injection Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant