-
Notifications
You must be signed in to change notification settings - Fork 418
Don't allow pausing online multiplayer games #8478
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| { | ||
| "pause/pause": "Pause", | ||
| "pause/play": "Resume" | ||
| "pause/play": "Resume", | ||
| "pause/cannot-pause": "This project cannot be paused" | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| let hasOnlineFeatures; | ||
|
|
||
| /** Guesses whether the project has cloud variable features */ | ||
| const scanForOnlineFeatures = (vm, console, msg, element) => { | ||
| // Well, only if it actually has cloud variables :P | ||
| if (!vm.runtime.hasCloudData()) { | ||
| hasOnlineFeatures = false; | ||
| return; | ||
| } | ||
|
|
||
| const allBlocks = vm.runtime.targets.flatMap((target) => Object.values(target.blocks._blocks)); | ||
| // Map the names and values of cloud variables to an object | ||
| const cloudVariables = {}; | ||
| const globalVariables = vm.runtime.getTargetForStage().variables; | ||
| for (let id of Object.keys(globalVariables)) { | ||
| if (globalVariables[id].isCloud) { | ||
| cloudVariables[globalVariables[id].name] = globalVariables[id].value; | ||
| } | ||
| } | ||
|
|
||
| const names = Object.keys(cloudVariables); | ||
| const values = Object.values(cloudVariables); | ||
|
|
||
| const varsHaveInvalidNumbers = values.some((i) => String(Number(i)) !== i); | ||
| const areVarNamesSequential = names.some((i) => i.endsWith("2")) && names.some((i) => i.endsWith("3")); | ||
| const usesUsernameBlock = allBlocks.some((block) => block.opcode === "sensing_username"); | ||
|
|
||
| if (varsHaveInvalidNumbers && areVarNamesSequential && usesUsernameBlock) { | ||
| // It's definitely online | ||
| hasOnlineFeatures = true; | ||
| disablePauseButton(console, msg, element); | ||
| } else if (varsHaveInvalidNumbers || areVarNamesSequential || usesUsernameBlock) { | ||
| // It might be online, we'll analyze during runtime | ||
| threshold = 5; | ||
| console.log("This Cloud project has the following characteristics:", { | ||
| varsHaveInvalidNumbers, | ||
| areVarNamesSequential, | ||
| usesUsernameBlock, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| let interactions = 0; | ||
| let threshold = 10; | ||
| let timeout; | ||
| /** Tracks incoming and outgoing data for cloud variables */ | ||
| const onDataTransferred = (value) => { | ||
| const hasInvalidNumbers = String(Number(value)) !== value; | ||
| if (hasInvalidNumbers) interactions++; | ||
| clearTimeout(timeout); | ||
| if (interactions < threshold) { | ||
| // Reset counter after 1.2s | ||
| timeout = setTimeout(() => (interactions = 0), 1200); | ||
| } else { | ||
| hasOnlineFeatures = true; | ||
| } | ||
| }; | ||
|
|
||
| function disablePauseButton(console, msg, pauseBtnElement) { | ||
| console.log("Online features detected - pause disabled."); | ||
| pauseBtnElement.classList.add("disabled"); | ||
| pauseBtnElement.title = msg("cannot-pause"); | ||
| } | ||
|
|
||
| export const getHasOnlineFeatures = () => hasOnlineFeatures; | ||
| export const checkForOnlineFeatures = async (addon, console, msg, element) => { | ||
| await addon.tab.redux.waitForState((state) => state.scratchGui.projectState.loadingState.startsWith("SHOWING")); | ||
| scanForOnlineFeatures(addon.tab.traps.vm, console, msg, element); | ||
| if (hasOnlineFeatures === undefined) { | ||
| // Watch for cloud variable updates | ||
| const originalSend = addon.tab.traps.vm.runtime.ioDevices.cloud.provider.connection.send; | ||
| addon.tab.traps.vm.runtime.ioDevices.cloud.provider.connection.send = function (data) { | ||
| originalSend.call(this, data); | ||
| const json = JSON.parse(data); | ||
| if (!hasOnlineFeatures && json.name) { | ||
| onDataTransferred(json.value); | ||
| if (hasOnlineFeatures) disablePauseButton(console, msg, element); | ||
| } | ||
| }; | ||
| const originalOnMessage = addon.tab.traps.vm.runtime.ioDevices.cloud.provider.connection.onmessage; | ||
| addon.tab.traps.vm.runtime.ioDevices.cloud.provider.connection.onmessage = function (message) { | ||
| originalOnMessage.call(this, message); | ||
| const json = JSON.parse(message.data.split("\n")[0]); | ||
| if (!hasOnlineFeatures && json.name) { | ||
| onDataTransferred(json.value); | ||
| if (hasOnlineFeatures) disablePauseButton(console, msg, element); | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,7 @@ | |
| .pause-btn:hover { | ||
| background-color: hsla(260, 60%, 60%, 0.15); | ||
| } | ||
|
|
||
| .pause-btn.disabled { | ||
| opacity: 0.5; | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
this condition might trigger false positives if a cloud variable's value is an actual number instead of a numeral string? (because a string is never strictly equal to a number). not sure if it's intentional or not though
Uh oh!
There was an error while loading. Please reload this page.
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.
Isn't the strict inequality sign is on the outside of the String() constructor?
And this function takes in a string as an argument, so it's a string-to-string comparison. …That is, unless Cloud variable value messages aren't always a string. I think they are, but I'll have to check that.
Uh oh!
There was an error while loading. Please reload this page.
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.
if a project sets a cloud variable to a number itself (not receiving a change) then it is a number