fix: harden chest patch, battle calc loading, and double-injection#62
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Three intermittent runtime errors reported in production:
Chests.init | Unexpected token '}'— the chest tracker rewritesItemUse.doItby string-inserting atrackChestcall before theitem_usedsignal. The anchor lookup used a single-quote-onlyindexOfwith no "not found" guard. WhenindexOfreturned-1(anchor absent, e.g. a minified build emitting the signal with double quotes), thesubstrmath collapsed the body toTW_Calc.trackChest(itemId,res);}andevalthrew a misleadingUnexpected token '}'."Battle calc core script is not loaded" —
BattleCalc.initfired a single fire-and-forget$.getScriptwith no error handling, so a transient/failed fetch left the calc unavailable for the whole session, with only "refresh the game" as recovery.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), sobootstrapran twice andTheWestApi.registerthrew on the second run. The register collision was the visible symptom of everything initializing twice.What
Chests
/EventHandler\.signal\(['"]item_used['"]/) so double-quoted builds patch correctly.-1position.Battle calc
Logger.loadingflag.Double injection
src/inject.tsand skip bootstrapping whenwindow.TW_Calcis 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, andnpm run build:devall 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,getInstancebehavior.inject.spec.ts: game-ready and already-injected guards (driven with jasmine's clock).🤖 Generated with Claude Code