diff --git a/osu.Game/Online/Rooms/Room.cs b/osu.Game/Online/Rooms/Room.cs index 9d69e791c7..8fb3901a79 100644 --- a/osu.Game/Online/Rooms/Room.cs +++ b/osu.Game/Online/Rooms/Room.cs @@ -200,6 +200,15 @@ public TimeSpan AutoStartDuration set => SetField(ref autoStartDuration, (ushort)value.TotalSeconds); } + /// + /// Provides some extra scoring statistics for the local user in the room. + /// + public PlaylistAggregateScore? UserScore + { + get => userScore; + set => SetField(ref userScore, value); + } + /// /// Represents the current item selected within the room. /// @@ -296,6 +305,9 @@ public RoomAvailability Availability [JsonProperty("auto_start_duration")] private ushort autoStartDuration; + [JsonProperty("current_user_score")] + private PlaylistAggregateScore? userScore; + [JsonProperty("current_playlist_item")] private PlaylistItem? currentPlaylistItem; @@ -312,10 +324,6 @@ public RoomAvailability Availability [JsonProperty("playlist")] public readonly BindableList Playlist = new BindableList(); - [Cached] - [JsonProperty("current_user_score")] - public readonly Bindable UserScore = new Bindable(); - [Cached] [JsonProperty("recent_participants")] public readonly BindableList RecentParticipants = new BindableList(); @@ -345,7 +353,7 @@ public void CopyFrom(Room other) MaxParticipants = other.MaxParticipants; ParticipantCount = other.ParticipantCount; EndDate = other.EndDate; - UserScore.Value = other.UserScore.Value; + UserScore = other.UserScore; QueueMode = other.QueueMode; AutoStartDuration = other.AutoStartDuration; DifficultyRange = other.DifficultyRange; diff --git a/osu.Game/Screens/OnlinePlay/Components/RoomLocalUserInfo.cs b/osu.Game/Screens/OnlinePlay/Components/RoomLocalUserInfo.cs index 974c296399..b3e60d4318 100644 --- a/osu.Game/Screens/OnlinePlay/Components/RoomLocalUserInfo.cs +++ b/osu.Game/Screens/OnlinePlay/Components/RoomLocalUserInfo.cs @@ -47,16 +47,19 @@ protected override void LoadComplete() { base.LoadComplete(); - UserScore.BindValueChanged(_ => updateAttempts()); - room.PropertyChanged += onRoomPropertyChanged; updateAttempts(); } private void onRoomPropertyChanged(object? sender, PropertyChangedEventArgs e) { - if (e.PropertyName == nameof(Room.MaxAttempts)) - updateAttempts(); + switch (e.PropertyName) + { + case nameof(Room.UserScore): + case nameof(Room.MaxAttempts): + updateAttempts(); + break; + } } private void updateAttempts() @@ -65,9 +68,9 @@ private void updateAttempts() { attemptDisplay.Text = $"Maximum attempts: {room.MaxAttempts:N0}"; - if (UserScore.Value != null) + if (room.UserScore != null) { - int remaining = room.MaxAttempts.Value - UserScore.Value.PlaylistItemAttempts.Sum(a => a.Attempts); + int remaining = room.MaxAttempts.Value - room.UserScore.PlaylistItemAttempts.Sum(a => a.Attempts); attemptDisplay.Text += $" ({remaining} remaining)"; if (remaining == 0) diff --git a/osu.Game/Screens/OnlinePlay/OnlinePlayComposite.cs b/osu.Game/Screens/OnlinePlay/OnlinePlayComposite.cs index 66e43c50b7..82fc6d535c 100644 --- a/osu.Game/Screens/OnlinePlay/OnlinePlayComposite.cs +++ b/osu.Game/Screens/OnlinePlay/OnlinePlayComposite.cs @@ -19,8 +19,5 @@ public partial class OnlinePlayComposite : CompositeDrawable [Resolved(typeof(Room))] protected BindableList RecentParticipants { get; private set; } = null!; - - [Resolved(typeof(Room))] - public Bindable UserScore { get; private set; } = null!; } } diff --git a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsReadyButton.cs b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsReadyButton.cs index ca661b041a..a460779ea6 100644 --- a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsReadyButton.cs +++ b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsReadyButton.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.ComponentModel; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; @@ -15,9 +16,6 @@ namespace osu.Game.Screens.OnlinePlay.Playlists { public partial class PlaylistsReadyButton : ReadyButton { - [Resolved(typeof(Room), nameof(Room.UserScore))] - private Bindable userScore { get; set; } = null!; - [Resolved] private IBindable gameBeatmap { get; set; } = null!; @@ -41,15 +39,24 @@ protected override void LoadComplete() { base.LoadComplete(); - userScore.BindValueChanged(aggregate => - { - if (room.MaxAttempts == null) - return; + room.PropertyChanged += onRoomPropertyChanged; + updateRoomUserScore(); + } - int remaining = room.MaxAttempts.Value - aggregate.NewValue.PlaylistItemAttempts.Sum(a => a.Attempts); + private void onRoomPropertyChanged(object? sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(Room.UserScore)) + updateRoomUserScore(); + } - hasRemainingAttempts = remaining > 0; - }); + private void updateRoomUserScore() + { + if (room.MaxAttempts == null || room.UserScore == null) + return; + + int remaining = room.MaxAttempts.Value - room.UserScore.PlaylistItemAttempts.Sum(a => a.Attempts); + + hasRemainingAttempts = remaining > 0; } protected override void Update() @@ -76,5 +83,11 @@ public override LocalisableString TooltipText private bool enoughTimeLeft => // This should probably consider the length of the currently selected item, rather than a constant 30 seconds. room.EndDate != null && DateTimeOffset.UtcNow.AddSeconds(30).AddMilliseconds(gameBeatmap.Value.Track.Length) < room.EndDate; + + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + room.PropertyChanged -= onRoomPropertyChanged; + } } }