-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add WebSocket messages for beatmap data and user activity #38071
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
Open
tsunyoku
wants to merge
23
commits into
ppy:master
Choose a base branch
from
tsunyoku:add-basic-websocket-data
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
76cc776
Add beatmap and user activity websocket messages
tsunyoku 213ef13
Move to STJ
tsunyoku 3bbb302
`beatmapset_id`
tsunyoku 379796c
Round values in `BeatmapDifficulty` too
tsunyoku 84b814a
Move serialisation & broadcasting onto its own task
tsunyoku 719e4c7
Remove unnecessary inline comment
tsunyoku ec999de
Repurpose beatmap message to wider "player state" message - include m…
tsunyoku d0e0783
Add `MaxCombo`
tsunyoku ca18df5
Rename types for clarity
tsunyoku 83e3d54
Add `Serializable` property
tsunyoku b74c92e
Move to data source API
tsunyoku 9c2e976
Fix accidental port change
tsunyoku 99d3f1a
Reorganise
tsunyoku 46767cb
Make `WebSocketDataSource` a component and use DI where possible
tsunyoku 07f56b2
Add separate `WebSocketMod`
tsunyoku 8d398e6
Add data to user activity message
tsunyoku ba67015
Move models to separate namespace
tsunyoku 2982db6
Add maximum PP to beatmap
tsunyoku 96bdb7e
Add lengths
tsunyoku 2d1cc31
Add object count
tsunyoku 1f88e15
Rename player state to beatmap state
tsunyoku 123172a
Remove unuseful user activity data
tsunyoku 03e9dcd
Expose user ID for watching replay activity
tsunyoku 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 was deleted.
Oops, something went wrong.
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
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
131 changes: 131 additions & 0 deletions
131
osu.Game/IPC/DataSources/PlayerStateWebSocketDataSource.cs
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,131 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using osu.Framework.Allocation; | ||
| using osu.Framework.Bindables; | ||
| using osu.Framework.Threading; | ||
| using osu.Game.Beatmaps; | ||
| using osu.Game.Configuration; | ||
| using osu.Game.IPC.Messages; | ||
| using osu.Game.Online.API; | ||
| using osu.Game.Online.Multiplayer; | ||
| using osu.Game.Rulesets; | ||
| using osu.Game.Rulesets.Mods; | ||
| using osu.Game.Utils; | ||
|
|
||
| namespace osu.Game.IPC.DataSources | ||
| { | ||
| public partial class PlayerStateWebSocketDataSource : WebSocketDataSource | ||
| { | ||
| [Resolved] | ||
| private Bindable<WorkingBeatmap> workingBeatmap { get; set; } = null!; | ||
|
|
||
| [Resolved] | ||
| private IBindable<RulesetInfo> rulesetInfo { get; set; } = null!; | ||
|
|
||
| [Resolved] | ||
| private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!; | ||
|
|
||
| [Resolved] | ||
| private BeatmapDifficultyCache difficultyCache { get; set; } = null!; | ||
|
|
||
| private ModSettingChangeTracker? modSettingChangeTracker; | ||
| private ScheduledDelegate? debouncedModSettingsChange; | ||
|
|
||
| public PlayerStateWebSocketDataSource(IWebSocketProvider provider) | ||
| : base(provider) { } | ||
|
|
||
| protected override void LoadComplete() | ||
| { | ||
| base.LoadComplete(); | ||
|
|
||
| workingBeatmap.BindValueChanged(val => | ||
| { | ||
| if (val.NewValue.BeatmapInfo.OnlineID == val.OldValue.BeatmapInfo.OnlineID) | ||
| return; | ||
|
|
||
| updatePlayerState().FireAndForget(); | ||
| }); | ||
|
|
||
| rulesetInfo.BindValueChanged(val => | ||
| { | ||
| if (val.NewValue.Equals(val.OldValue)) | ||
| return; | ||
|
|
||
| updatePlayerState().FireAndForget(); | ||
| }); | ||
|
|
||
| mods.BindValueChanged(val => | ||
| { | ||
| if (val.OldValue.SequenceEqual(val.NewValue, ReferenceEqualityComparer.Instance)) | ||
| return; | ||
|
|
||
| modSettingChangeTracker?.Dispose(); | ||
|
|
||
| updatePlayerState().FireAndForget(); | ||
|
|
||
| modSettingChangeTracker = new ModSettingChangeTracker(mods.Value); | ||
| modSettingChangeTracker.SettingChanged += _ => | ||
| { | ||
| debouncedModSettingsChange?.Cancel(); | ||
| debouncedModSettingsChange = Scheduler.AddDelayed(() => updatePlayerState().FireAndForget(), 100); | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| private async Task updatePlayerState() | ||
| { | ||
| if (workingBeatmap.Value is DummyWorkingBeatmap) | ||
| return; | ||
|
|
||
| double rate = ModUtils.CalculateRateWithMods(mods.Value); | ||
|
|
||
| var ruleset = rulesetInfo.Value.CreateInstance(); | ||
| var adjustedDifficulty = ruleset.GetAdjustedDisplayDifficulty(workingBeatmap.Value.BeatmapInfo, mods.Value); | ||
|
|
||
| var starDifficulty = await difficultyCache.GetDifficultyAsync(workingBeatmap.Value.BeatmapInfo, rulesetInfo.Value, mods.Value).ConfigureAwait(false); | ||
|
|
||
| var msg = new PlayerStateWebSocketMessage | ||
| { | ||
| Beatmap = new WebSocketBeatmap | ||
| { | ||
| BeatmapId = workingBeatmap.Value.BeatmapInfo.OnlineID, | ||
| BeatmapSetId = workingBeatmap.Value.BeatmapSetInfo.OnlineID, | ||
| BeatmapHash = workingBeatmap.Value.BeatmapInfo.OnlineMD5Hash, | ||
| Metadata = new WebSocketBeatmapMetadata | ||
| { | ||
| Artist = workingBeatmap.Value.BeatmapInfo.Metadata.Artist, | ||
| ArtistUnicode = workingBeatmap.Value.BeatmapInfo.Metadata.ArtistUnicode, | ||
| Title = workingBeatmap.Value.BeatmapInfo.Metadata.Title, | ||
| TitleUnicode = workingBeatmap.Value.BeatmapInfo.Metadata.TitleUnicode, | ||
| Author = workingBeatmap.Value.BeatmapInfo.Metadata.Author.Username, | ||
| Source = workingBeatmap.Value.BeatmapInfo.Metadata.Source, | ||
| Tags = workingBeatmap.Value.BeatmapInfo.Metadata.Tags, | ||
| UserTags = workingBeatmap.Value.BeatmapInfo.Metadata.UserTags.ToArray(), | ||
| }, | ||
| Difficulty = new WebSocketBeatmapDifficulty | ||
| { | ||
| ApproachRate = Math.Round(adjustedDifficulty.ApproachRate, 2), | ||
| CircleSize = Math.Round(adjustedDifficulty.CircleSize, 2), | ||
| DrainRate = Math.Round(adjustedDifficulty.DrainRate, 2), | ||
| OverallDifficulty = Math.Round(adjustedDifficulty.OverallDifficulty, 2), | ||
| }, | ||
| DifficultyName = workingBeatmap.Value.BeatmapInfo.DifficultyName, | ||
| RulesetId = workingBeatmap.Value.BeatmapInfo.Ruleset.OnlineID, | ||
| BPM = FormatUtils.RoundBPM(workingBeatmap.Value.BeatmapInfo.BPM, rate), | ||
| StarRating = starDifficulty?.Stars.FloorToDecimalDigits(2) ?? workingBeatmap.Value.BeatmapInfo.StarRating.FloorToDecimalDigits(2), | ||
| MaxCombo = starDifficulty?.MaxCombo ?? 0, | ||
| Status = workingBeatmap.Value.BeatmapInfo.Status, | ||
| }, | ||
| RulesetId = rulesetInfo.Value.OnlineID, | ||
| Mods = mods.Value.Select(m => new APIMod(m)).ToArray(), | ||
| }; | ||
|
|
||
| BroadcastMessage(msg); | ||
| } | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
osu.Game/IPC/DataSources/UserActivityWebSocketDataSource.cs
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,45 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using osu.Framework.Allocation; | ||
| using osu.Framework.Bindables; | ||
| using osu.Game.Configuration; | ||
| using osu.Game.IPC.Messages; | ||
| using osu.Game.Users; | ||
|
|
||
| namespace osu.Game.IPC.DataSources | ||
| { | ||
| public partial class UserActivityWebSocketDataSource : WebSocketDataSource | ||
| { | ||
| private readonly Bindable<UserActivity?> userActivity = new Bindable<UserActivity?>(); | ||
|
|
||
| public UserActivityWebSocketDataSource(IWebSocketProvider provider) | ||
| : base(provider) { } | ||
|
|
||
| [BackgroundDependencyLoader] | ||
| private void load(SessionStatics sessionStatics) | ||
| { | ||
| sessionStatics.BindWith(Static.UserOnlineActivity, userActivity); | ||
| } | ||
|
|
||
| protected override void LoadComplete() | ||
| { | ||
| base.LoadComplete(); | ||
|
|
||
| userActivity.BindValueChanged(onUserActivityChange); | ||
| } | ||
|
|
||
| private void onUserActivityChange(ValueChangedEvent<UserActivity?> change) | ||
| { | ||
| if (change.NewValue == null) | ||
| return; | ||
|
|
||
| var msg = new UserActivityWebSocketMessage | ||
| { | ||
| Status = change.NewValue.GetType().Name, | ||
|
bdach marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| BroadcastMessage(msg); | ||
| } | ||
| } | ||
| } | ||
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,14 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using osu.Framework.Allocation; | ||
|
|
||
| namespace osu.Game.IPC | ||
| { | ||
| [Cached] | ||
| public interface IWebSocketProvider | ||
| { | ||
| void Register(WebSocketDataSource dataSource); | ||
| void Unregister(WebSocketDataSource dataSource); | ||
| } | ||
| } |
8 changes: 5 additions & 3 deletions
8
...sktop/IPC/Messages/OsuWebSocketMessage.cs → osu.Game/IPC/Messages/OsuWebSocketMessage.cs
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
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,20 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
| using osu.Game.Online.API; | ||
|
|
||
| namespace osu.Game.IPC.Messages | ||
| { | ||
| public class PlayerStateWebSocketMessage : OsuWebSocketMessage | ||
| { | ||
| [JsonPropertyName("beatmap")] | ||
| public required WebSocketBeatmap Beatmap { get; init; } | ||
|
|
||
| [JsonPropertyName("ruleset_id")] | ||
| public required int RulesetId { get; init; } | ||
|
|
||
| [JsonPropertyName("mods")] | ||
| public required APIMod[] Mods { get; init; } | ||
| } | ||
| } |
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,13 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace osu.Game.IPC.Messages | ||
| { | ||
| public class UserActivityWebSocketMessage : OsuWebSocketMessage | ||
| { | ||
| [JsonPropertyName("status")] | ||
| public required string Status { get; init; } | ||
| } | ||
| } |
Oops, something went wrong.
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.