-
Notifications
You must be signed in to change notification settings - Fork 0
fix: auto-start matching game on Play Again #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,7 +48,6 @@ function GamesXBlockMatchingInit(runtime, element, pages, matching_key) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| function refreshGame() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| MatchInit = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $.ajax({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| type: 'GET', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| url: runtime.handlerUrl(element, 'refresh_game'), | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -61,9 +60,22 @@ function GamesXBlockMatchingInit(runtime, element, pages, matching_key) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| var scriptContent = decoderScript.text(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| decoderScript.remove(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| eval(scriptContent); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof MatchingInit === 'function') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| MatchingInit(runtime, element); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| var match = scriptContent.match(/function\s+(\w+)\s*\(/); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (match) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| var initFuncName = match[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| eval(scriptContent); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof window[initFuncName] === 'function') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| window[initFuncName](runtime, element); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| setTimeout(function() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $('.matching-start-button', element).click(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $('.matching-start-button', element).click(); | |
| const $startButtons = $('.matching-start-button', element); | |
| if ($startButtons.length === 1) { | |
| $startButtons.click(); | |
| } else if ($startButtons.length > 1) { | |
| console.warn('Expected a single .matching-start-button but found', $startButtons.length); | |
| $startButtons.first().click(); | |
| } else { | |
| console.warn('No .matching-start-button found for auto-start; game will not be started automatically.'); | |
| } |
Copilot
AI
Jan 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardcoded 100ms delay for triggering the start button click is a magic number that lacks documentation. This arbitrary timeout could cause race conditions if the initialization takes longer than 100ms on slower devices or network conditions. Consider either documenting why 100ms is sufficient, or implementing a more robust mechanism such as waiting for a specific initialization event or checking for element readiness before triggering the click.
| setTimeout(function() { | |
| $('.matching-start-button', element).click(); | |
| }, 100); | |
| // Automatically start the game once the start button is available. | |
| var $startButton = $('.matching-start-button', element); | |
| if ($startButton.length) { | |
| $startButton.click(); | |
| } else { | |
| // Poll briefly for the button in case initialization is asynchronous. | |
| var attempts = 0; | |
| var maxAttempts = 20; // ~2 seconds with 100ms interval | |
| var pollInterval = 100; | |
| var pollId = setInterval(function() { | |
| var $btn = $('.matching-start-button', element); | |
| if ($btn.length) { | |
| clearInterval(pollId); | |
| $btn.click(); | |
| } else if (++attempts >= maxAttempts) { | |
| clearInterval(pollId); | |
| console.warn('Matching start button not found after waiting for initialization.'); | |
| } | |
| }, pollInterval); | |
| } |
Copilot
AI
Jan 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Falling back to a full page reload on initialization failure may result in poor user experience and potential infinite reload loops if the underlying issue persists. The error handling should provide more user-friendly feedback or retry mechanisms rather than silently reloading the page, which could disorient users and lose their context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex pattern used to extract the function name is too simplistic and may fail in edge cases. For example, it won't handle function names with leading/trailing whitespace variations, comments within the function declaration, or multiline function declarations. Consider using a more robust pattern or adding validation to ensure the extracted function name matches expected patterns.