1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 09:22:54 +08:00
osu-lazer/osu.Game/Overlays/Dashboard/CurrentlyOnlineDisplay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

293 lines
10 KiB
C#
Raw Normal View History

// 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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Collections.Generic;
2022-02-02 22:19:43 +08:00
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Framework.Screens;
2020-11-06 15:38:57 +08:00
using osu.Game.Database;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Metadata;
using osu.Game.Online.Spectator;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Screens;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Screens.Play;
using osu.Game.Users;
using osuTK;
namespace osu.Game.Overlays.Dashboard
{
internal partial class CurrentlyOnlineDisplay : CompositeDrawable
{
private const float search_textbox_height = 40;
private const float padding = 10;
2022-02-02 22:19:43 +08:00
private readonly IBindableList<int> playingUsers = new BindableList<int>();
private readonly IBindableDictionary<int, UserPresence> onlineUsers = new BindableDictionary<int, UserPresence>();
private readonly Dictionary<int, OnlineUserPanel> userPanels = new Dictionary<int, OnlineUserPanel>();
private SearchContainer<OnlineUserPanel> userFlow;
private BasicSearchTextBox searchTextBox;
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private SpectatorClient spectatorClient { get; set; }
[Resolved]
private MetadataClient metadataClient { get; set; }
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2022-05-27 22:38:54 +08:00
InternalChildren = new Drawable[]
{
2022-05-27 22:38:54 +08:00
new Box
{
RelativeSizeAxes = Axes.X,
Height = padding * 2 + search_textbox_height,
2022-05-27 22:38:54 +08:00
Colour = colourProvider.Background4,
},
new Container<BasicSearchTextBox>
{
2022-05-27 22:38:54 +08:00
RelativeSizeAxes = Axes.X,
Padding = new MarginPadding { Horizontal = WaveOverlayContainer.HORIZONTAL_PADDING, Vertical = padding },
Child = searchTextBox = new BasicSearchTextBox
2022-05-27 22:38:54 +08:00
{
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Height = search_textbox_height,
ReleaseFocusOnCommit = false,
HoldFocus = true,
PlaceholderText = HomeStrings.SearchPlaceholder,
2022-05-27 22:38:54 +08:00
},
},
userFlow = new SearchContainer<OnlineUserPanel>
2022-05-27 22:38:54 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10),
2022-05-27 22:38:54 +08:00
Padding = new MarginPadding
{
Top = padding * 3 + search_textbox_height,
Bottom = padding,
Right = padding,
Left = padding,
2022-05-27 22:38:54 +08:00
},
},
};
searchTextBox.Current.ValueChanged += text => userFlow.SearchTerm = text.NewValue;
}
2020-11-06 15:38:57 +08:00
[Resolved]
private UserLookupCache users { get; set; }
protected override void LoadComplete()
{
base.LoadComplete();
onlineUsers.BindTo(metadataClient.UserStates);
onlineUsers.BindCollectionChanged(onUserUpdated, true);
2022-02-02 22:19:43 +08:00
playingUsers.BindTo(spectatorClient.PlayingUsers);
playingUsers.BindCollectionChanged(onPlayingUsersChanged, true);
2020-11-09 11:22:54 +08:00
}
protected override void OnFocus(FocusEvent e)
{
base.OnFocus(e);
searchTextBox.TakeFocus();
}
private void onUserUpdated(object sender, NotifyDictionaryChangedEventArgs<int, UserPresence> e) => Schedule(() =>
2020-11-09 11:22:54 +08:00
{
switch (e.Action)
{
case NotifyDictionaryChangedAction.Add:
Debug.Assert(e.NewItems != null);
foreach (var kvp in e.NewItems)
2022-05-27 22:38:54 +08:00
{
int userId = kvp.Key;
2022-05-27 22:38:54 +08:00
users.GetUserAsync(userId).ContinueWith(task =>
{
APIUser user = task.GetResultSafely();
2022-05-27 22:38:54 +08:00
if (user == null)
return;
Schedule(() =>
{
// explicitly refetch the user's status.
// things may have changed in between the time of scheduling and the time of actual execution.
if (onlineUsers.TryGetValue(userId, out var updatedStatus))
{
user.Activity.Value = updatedStatus.Activity;
user.Status.Value = updatedStatus.Status;
}
userFlow.Add(userPanels[userId] = createUserPanel(user));
2022-05-27 22:38:54 +08:00
});
});
}
2020-11-09 11:22:54 +08:00
break;
case NotifyDictionaryChangedAction.Replace:
Debug.Assert(e.NewItems != null);
foreach (var kvp in e.NewItems)
{
if (userPanels.TryGetValue(kvp.Key, out var panel))
{
panel.User.Activity.Value = kvp.Value.Activity;
panel.User.Status.Value = kvp.Value.Status;
}
}
break;
case NotifyDictionaryChangedAction.Remove:
Debug.Assert(e.OldItems != null);
foreach (var kvp in e.OldItems)
{
int userId = kvp.Key;
if (userPanels.Remove(userId, out var userPanel))
userPanel.Expire();
}
break;
}
});
private void onPlayingUsersChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Debug.Assert(e.NewItems != null);
foreach (int userId in e.NewItems)
{
if (userPanels.TryGetValue(userId, out var panel))
panel.CanSpectate.Value = userId != api.LocalUser.Value.Id;
}
break;
2022-02-02 22:19:43 +08:00
case NotifyCollectionChangedAction.Remove:
Debug.Assert(e.OldItems != null);
2022-02-02 22:19:43 +08:00
foreach (int userId in e.OldItems)
{
if (userPanels.TryGetValue(userId, out var panel))
panel.CanSpectate.Value = false;
}
2020-11-09 11:22:54 +08:00
break;
}
}
private OnlineUserPanel createUserPanel(APIUser user) =>
new OnlineUserPanel(user).With(panel =>
{
panel.Anchor = Anchor.TopCentre;
panel.Origin = Anchor.TopCentre;
panel.CanSpectate.Value = playingUsers.Contains(user.Id);
});
2020-11-02 20:09:47 +08:00
public partial class OnlineUserPanel : CompositeDrawable, IFilterable
2020-11-02 20:09:47 +08:00
{
public readonly APIUser User;
public BindableBool CanSpectate { get; } = new BindableBool();
public IEnumerable<LocalisableString> FilterTerms { get; }
2020-11-02 20:09:47 +08:00
[Resolved(canBeNull: true)]
private IPerformFromScreenRunner performer { get; set; }
2022-05-27 22:38:54 +08:00
public bool FilteringActive { set; get; }
public bool MatchingFilter
{
set
{
if (value)
Show();
else
Hide();
}
}
public OnlineUserPanel(APIUser user)
2020-11-02 20:09:47 +08:00
{
User = user;
2022-05-27 22:38:54 +08:00
FilterTerms = new LocalisableString[] { User.Username };
2020-11-02 20:09:47 +08:00
AutoSizeAxes = Axes.Both;
}
2020-11-02 20:09:47 +08:00
[BackgroundDependencyLoader]
private void load()
{
2020-11-02 20:09:47 +08:00
InternalChildren = new Drawable[]
{
new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
2020-11-02 20:09:47 +08:00
Direction = FillDirection.Vertical,
Spacing = new Vector2(2),
Width = 290,
2020-11-02 20:09:47 +08:00
Children = new Drawable[]
{
new UserGridPanel(User)
2020-11-02 20:09:47 +08:00
{
RelativeSizeAxes = Axes.X,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
// this is SHOCKING
Activity = { BindTarget = User.Activity },
Status = { BindTarget = User.Status },
2020-11-02 20:09:47 +08:00
},
new PurpleRoundedButton
2020-11-02 20:09:47 +08:00
{
RelativeSizeAxes = Axes.X,
Text = "Spectate",
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Action = () => performer?.PerformFromScreen(s => s.Push(new SoloSpectatorScreen(User))),
Enabled = { BindTarget = CanSpectate }
2020-11-02 20:09:47 +08:00
}
}
},
};
}
}
}
}