Make `Room.UserScore` non-bindable

This commit is contained in:
Dan Balasescu 2024-11-13 22:39:01 +09:00
parent c4f8fd1832
commit dc5337d771
No known key found for this signature in database
4 changed files with 45 additions and 24 deletions

View File

@ -200,6 +200,15 @@ public TimeSpan AutoStartDuration
set => SetField(ref autoStartDuration, (ushort)value.TotalSeconds);
}
/// <summary>
/// Provides some extra scoring statistics for the local user in the room.
/// </summary>
public PlaylistAggregateScore? UserScore
{
get => userScore;
set => SetField(ref userScore, value);
}
/// <summary>
/// Represents the current item selected within the room.
/// </summary>
@ -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<PlaylistItem> Playlist = new BindableList<PlaylistItem>();
[Cached]
[JsonProperty("current_user_score")]
public readonly Bindable<PlaylistAggregateScore> UserScore = new Bindable<PlaylistAggregateScore>();
[Cached]
[JsonProperty("recent_participants")]
public readonly BindableList<APIUser> RecentParticipants = new BindableList<APIUser>();
@ -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;

View File

@ -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)

View File

@ -19,8 +19,5 @@ public partial class OnlinePlayComposite : CompositeDrawable
[Resolved(typeof(Room))]
protected BindableList<APIUser> RecentParticipants { get; private set; } = null!;
[Resolved(typeof(Room))]
public Bindable<PlaylistAggregateScore> UserScore { get; private set; } = null!;
}
}

View File

@ -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<PlaylistAggregateScore> userScore { get; set; } = null!;
[Resolved]
private IBindable<WorkingBeatmap> 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;
}
}
}