2020-10-28 16:19:14 +08:00
|
|
|
// 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-01-25 23:21:54 +08:00
|
|
|
using System.Diagnostics;
|
2020-10-28 16:19:14 +08:00
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2022-01-03 16:31:12 +08:00
|
|
|
using osu.Framework.Extensions;
|
2020-10-28 16:19:14 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Screens;
|
2020-11-06 15:38:57 +08:00
|
|
|
using osu.Game.Database;
|
2020-10-28 16:19:14 +08:00
|
|
|
using osu.Game.Online.API;
|
2021-11-04 17:02:44 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2020-10-28 16:19:14 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
2020-12-25 23:50:00 +08:00
|
|
|
using osu.Game.Screens.OnlinePlay.Match.Components;
|
2020-10-28 16:19:14 +08:00
|
|
|
using osu.Game.Screens.Play;
|
|
|
|
using osu.Game.Users;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Dashboard
|
|
|
|
{
|
|
|
|
internal class CurrentlyPlayingDisplay : CompositeDrawable
|
|
|
|
{
|
2022-01-25 23:21:54 +08:00
|
|
|
private readonly IBindableDictionary<int, SpectatorState> playingUserStates = new BindableDictionary<int, SpectatorState>();
|
2020-10-28 16:19:14 +08:00
|
|
|
|
2020-11-02 20:09:47 +08:00
|
|
|
private FillFlowContainer<PlayingUserPanel> userFlow;
|
2020-10-28 16:19:14 +08:00
|
|
|
|
|
|
|
[Resolved]
|
2021-05-20 14:55:07 +08:00
|
|
|
private SpectatorClient spectatorClient { get; set; }
|
2020-10-28 16:19:14 +08:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
2020-11-02 20:09:47 +08:00
|
|
|
InternalChild = userFlow = new FillFlowContainer<PlayingUserPanel>
|
2020-10-28 16:19:14 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
2020-11-06 12:08:07 +08:00
|
|
|
Padding = new MarginPadding(10),
|
2020-10-28 16:19:14 +08:00
|
|
|
Spacing = new Vector2(10),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-06 15:38:57 +08:00
|
|
|
[Resolved]
|
|
|
|
private UserLookupCache users { get; set; }
|
|
|
|
|
2020-10-28 16:19:14 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-01-25 23:21:54 +08:00
|
|
|
playingUserStates.BindTo(spectatorClient.PlayingUserStates);
|
|
|
|
playingUserStates.BindCollectionChanged(onUsersChanged, true);
|
2020-11-09 11:22:54 +08:00
|
|
|
}
|
2020-11-06 11:54:08 +08:00
|
|
|
|
2022-01-25 23:21:54 +08:00
|
|
|
private void onUsersChanged(object sender, NotifyDictionaryChangedEventArgs<int, SpectatorState> e) => Schedule(() =>
|
2020-11-09 11:22:54 +08:00
|
|
|
{
|
|
|
|
switch (e.Action)
|
|
|
|
{
|
2022-01-25 23:21:54 +08:00
|
|
|
case NotifyDictionaryChangedAction.Add:
|
|
|
|
Debug.Assert(e.NewItems != null);
|
|
|
|
|
|
|
|
foreach ((int userId, _) in e.NewItems)
|
2020-11-09 11:22:54 +08:00
|
|
|
{
|
2022-01-25 23:21:54 +08:00
|
|
|
users.GetUserAsync(userId).ContinueWith(task =>
|
2020-10-28 16:19:14 +08:00
|
|
|
{
|
2022-01-06 21:54:43 +08:00
|
|
|
var user = task.GetResultSafely();
|
2022-01-03 16:31:12 +08:00
|
|
|
|
|
|
|
if (user == null) return;
|
2020-11-09 11:22:54 +08:00
|
|
|
|
|
|
|
Schedule(() =>
|
2020-10-28 16:19:14 +08:00
|
|
|
{
|
2020-11-09 11:22:54 +08:00
|
|
|
// user may no longer be playing.
|
2022-01-25 23:21:54 +08:00
|
|
|
if (!playingUserStates.ContainsKey(user.Id))
|
2020-11-06 16:55:29 +08:00
|
|
|
return;
|
|
|
|
|
2022-01-03 16:31:12 +08:00
|
|
|
userFlow.Add(createUserPanel(user));
|
2020-11-06 15:38:57 +08:00
|
|
|
});
|
2020-11-09 11:22:54 +08:00
|
|
|
});
|
|
|
|
}
|
2020-10-28 16:19:14 +08:00
|
|
|
|
2020-11-09 11:22:54 +08:00
|
|
|
break;
|
2020-10-28 16:19:14 +08:00
|
|
|
|
2022-01-25 23:21:54 +08:00
|
|
|
case NotifyDictionaryChangedAction.Remove:
|
|
|
|
Debug.Assert(e.OldItems != null);
|
2020-10-28 16:19:14 +08:00
|
|
|
|
2022-01-25 23:21:54 +08:00
|
|
|
foreach ((int userId, _) in e.OldItems)
|
|
|
|
userFlow.FirstOrDefault(card => card.User.Id == userId)?.Expire();
|
2020-11-09 11:22:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2020-10-28 16:19:14 +08:00
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
private PlayingUserPanel createUserPanel(APIUser user) =>
|
2020-11-02 20:09:47 +08:00
|
|
|
new PlayingUserPanel(user).With(panel =>
|
2020-10-28 16:19:14 +08:00
|
|
|
{
|
|
|
|
panel.Anchor = Anchor.TopCentre;
|
|
|
|
panel.Origin = Anchor.TopCentre;
|
|
|
|
});
|
2020-11-02 20:09:47 +08:00
|
|
|
|
|
|
|
private class PlayingUserPanel : CompositeDrawable
|
|
|
|
{
|
2021-11-04 17:02:44 +08:00
|
|
|
public readonly APIUser User;
|
2020-11-02 20:09:47 +08:00
|
|
|
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
private OsuGame game { get; set; }
|
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
public PlayingUserPanel(APIUser user)
|
2020-11-02 20:09:47 +08:00
|
|
|
{
|
|
|
|
User = user;
|
|
|
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2020-11-10 11:33:07 +08:00
|
|
|
}
|
2020-11-02 20:09:47 +08:00
|
|
|
|
2020-11-10 11:33:07 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(IAPIProvider api)
|
|
|
|
{
|
2020-11-02 20:09:47 +08:00
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
new FillFlowContainer
|
|
|
|
{
|
2020-11-04 02:03:48 +08:00
|
|
|
AutoSizeAxes = Axes.Y,
|
2020-11-02 20:09:47 +08:00
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Spacing = new Vector2(2),
|
2020-11-04 02:03:48 +08:00
|
|
|
Width = 290,
|
2020-11-02 20:09:47 +08:00
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2020-11-10 11:33:07 +08:00
|
|
|
new UserGridPanel(User)
|
2020-11-02 20:09:47 +08:00
|
|
|
{
|
2020-11-04 02:03:48 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
2020-11-04 02:12:03 +08:00
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.TopCentre,
|
2020-11-02 20:09:47 +08:00
|
|
|
},
|
2020-11-10 11:33:07 +08:00
|
|
|
new PurpleTriangleButton
|
2020-11-02 20:09:47 +08:00
|
|
|
{
|
2020-11-04 02:03:48 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
2020-11-02 20:09:47 +08:00
|
|
|
Text = "Watch",
|
2020-11-04 02:12:03 +08:00
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.TopCentre,
|
2021-04-01 21:08:52 +08:00
|
|
|
Action = () => game?.PerformFromScreen(s => s.Push(new SoloSpectator(User))),
|
2020-11-10 11:33:07 +08:00
|
|
|
Enabled = { Value = User.Id != api.LocalUser.Value.Id }
|
2020-11-02 20:09:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-10-28 16:19:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|