2021-04-01 13:02:32 +00: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.
|
|
|
|
|
2021-12-17 10:26:41 +00:00
|
|
|
using System;
|
2021-04-01 13:02:32 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2022-01-03 08:31:12 +00:00
|
|
|
using osu.Framework.Extensions;
|
2021-05-20 10:27:34 +00:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2021-04-01 13:02:32 +00:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Database;
|
2021-11-04 09:02:44 +00:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-04-01 13:02:32 +00:00
|
|
|
using osu.Game.Online.Spectator;
|
|
|
|
using osu.Game.Replays;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Scoring;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Spectate
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A <see cref="OsuScreen"/> which spectates one or more users.
|
|
|
|
/// </summary>
|
|
|
|
public abstract class SpectatorScreen : OsuScreen
|
|
|
|
{
|
2021-08-10 09:39:20 +00:00
|
|
|
protected IReadOnlyList<int> Users => users;
|
2021-05-03 05:25:52 +00:00
|
|
|
|
2021-08-10 09:39:20 +00:00
|
|
|
private readonly List<int> users = new List<int>();
|
2021-04-01 13:02:32 +00:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private BeatmapManager beatmaps { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private RulesetStore rulesets { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
2021-05-20 06:55:07 +00:00
|
|
|
private SpectatorClient spectatorClient { get; set; }
|
2021-04-01 13:02:32 +00:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private UserLookupCache userLookupCache { get; set; }
|
|
|
|
|
2021-05-20 10:27:34 +00:00
|
|
|
private readonly IBindableDictionary<int, SpectatorState> playingUserStates = new BindableDictionary<int, SpectatorState>();
|
|
|
|
|
2021-11-04 09:02:44 +00:00
|
|
|
private readonly Dictionary<int, APIUser> userMap = new Dictionary<int, APIUser>();
|
2021-10-01 17:08:56 +00:00
|
|
|
private readonly Dictionary<int, SpectatorGameplayState> gameplayStates = new Dictionary<int, SpectatorGameplayState>();
|
2021-04-01 13:02:32 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a new <see cref="SpectatorScreen"/>.
|
|
|
|
/// </summary>
|
2021-08-10 09:39:20 +00:00
|
|
|
/// <param name="users">The users to spectate.</param>
|
|
|
|
protected SpectatorScreen(params int[] users)
|
2021-04-01 13:02:32 +00:00
|
|
|
{
|
2021-08-10 09:39:20 +00:00
|
|
|
this.users.AddRange(users);
|
2021-04-01 13:02:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 10:26:41 +00:00
|
|
|
[Resolved]
|
|
|
|
private RealmContextFactory realmContextFactory { get; set; }
|
|
|
|
|
|
|
|
private IDisposable realmSubscription;
|
|
|
|
|
2021-04-01 13:02:32 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-01-03 08:31:12 +00:00
|
|
|
userLookupCache.GetUsersAsync(users.ToArray()).ContinueWith(task => Schedule(() =>
|
2021-04-16 09:22:22 +00:00
|
|
|
{
|
2022-01-06 13:54:43 +00:00
|
|
|
var foundUsers = task.GetResultSafely();
|
2022-01-03 08:31:12 +00:00
|
|
|
|
|
|
|
foreach (var u in foundUsers)
|
2021-07-05 12:30:24 +00:00
|
|
|
{
|
|
|
|
if (u == null)
|
|
|
|
continue;
|
|
|
|
|
2021-05-20 09:52:20 +00:00
|
|
|
userMap[u.Id] = u;
|
2021-07-05 12:30:24 +00:00
|
|
|
}
|
2021-05-20 09:52:20 +00:00
|
|
|
|
2021-05-20 10:27:34 +00:00
|
|
|
playingUserStates.BindTo(spectatorClient.PlayingUserStates);
|
|
|
|
playingUserStates.BindCollectionChanged(onPlayingUserStatesChanged, true);
|
|
|
|
|
2021-12-17 10:26:41 +00:00
|
|
|
realmSubscription = realmContextFactory.Context
|
|
|
|
.All<BeatmapSetInfo>()
|
|
|
|
.Where(s => !s.DeletePending)
|
|
|
|
.QueryAsyncWithNotifications((items, changes, ___) =>
|
|
|
|
{
|
|
|
|
if (changes?.InsertedIndices == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
foreach (int c in changes.InsertedIndices)
|
|
|
|
beatmapUpdated(items[c]);
|
|
|
|
});
|
2021-04-16 09:22:22 +00:00
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
foreach ((int id, var _) in userMap)
|
2021-05-20 09:52:20 +00:00
|
|
|
spectatorClient.WatchUser(id);
|
2021-04-16 09:22:22 +00:00
|
|
|
}));
|
|
|
|
}
|
2021-04-01 13:02:32 +00:00
|
|
|
|
2021-11-05 09:05:31 +00:00
|
|
|
private void beatmapUpdated(BeatmapSetInfo beatmapSet)
|
2021-04-01 13:02:32 +00:00
|
|
|
{
|
2021-11-05 09:05:31 +00:00
|
|
|
foreach ((int userId, _) in userMap)
|
2021-04-01 13:02:32 +00:00
|
|
|
{
|
2021-05-20 10:27:34 +00:00
|
|
|
if (!playingUserStates.TryGetValue(userId, out var userState))
|
2021-05-20 09:52:20 +00:00
|
|
|
continue;
|
2021-04-19 07:48:55 +00:00
|
|
|
|
2021-11-12 08:45:05 +00:00
|
|
|
if (beatmapSet.Beatmaps.Any(b => b.OnlineID == userState.BeatmapID))
|
2021-05-20 09:52:20 +00:00
|
|
|
updateGameplayState(userId);
|
2021-04-01 13:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 10:27:34 +00:00
|
|
|
private void onPlayingUserStatesChanged(object sender, NotifyDictionaryChangedEventArgs<int, SpectatorState> e)
|
|
|
|
{
|
|
|
|
switch (e.Action)
|
|
|
|
{
|
|
|
|
case NotifyDictionaryChangedAction.Add:
|
2021-10-27 04:04:41 +00:00
|
|
|
foreach ((int userId, var state) in e.NewItems.AsNonNull())
|
2021-05-20 10:27:34 +00:00
|
|
|
onUserStateAdded(userId, state);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NotifyDictionaryChangedAction.Remove:
|
2021-10-27 04:04:41 +00:00
|
|
|
foreach ((int userId, var _) in e.OldItems.AsNonNull())
|
2021-05-20 10:27:34 +00:00
|
|
|
onUserStateRemoved(userId);
|
|
|
|
break;
|
2021-05-20 10:37:43 +00:00
|
|
|
|
|
|
|
case NotifyDictionaryChangedAction.Replace:
|
2021-10-27 04:04:41 +00:00
|
|
|
foreach ((int userId, var _) in e.OldItems.AsNonNull())
|
2021-05-20 10:37:43 +00:00
|
|
|
onUserStateRemoved(userId);
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
foreach ((int userId, var state) in e.NewItems.AsNonNull())
|
2021-05-20 10:37:43 +00:00
|
|
|
onUserStateAdded(userId, state);
|
|
|
|
break;
|
2021-05-20 10:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onUserStateAdded(int userId, SpectatorState state)
|
2021-04-01 13:02:32 +00:00
|
|
|
{
|
|
|
|
if (state.RulesetID == null || state.BeatmapID == null)
|
|
|
|
return;
|
|
|
|
|
2021-05-20 09:52:20 +00:00
|
|
|
if (!userMap.ContainsKey(userId))
|
|
|
|
return;
|
2021-04-01 13:02:32 +00:00
|
|
|
|
2021-05-20 10:27:34 +00:00
|
|
|
Schedule(() => OnUserStateChanged(userId, state));
|
|
|
|
updateGameplayState(userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onUserStateRemoved(int userId)
|
|
|
|
{
|
|
|
|
if (!userMap.ContainsKey(userId))
|
2021-05-20 09:52:20 +00:00
|
|
|
return;
|
2021-04-20 12:17:24 +00:00
|
|
|
|
2021-05-20 10:27:34 +00:00
|
|
|
if (!gameplayStates.TryGetValue(userId, out var gameplayState))
|
|
|
|
return;
|
2021-04-01 13:02:32 +00:00
|
|
|
|
2021-05-20 10:27:34 +00:00
|
|
|
gameplayState.Score.Replay.HasReceivedAllFrames = true;
|
|
|
|
|
|
|
|
gameplayStates.Remove(userId);
|
|
|
|
Schedule(() => EndGameplay(userId));
|
2021-04-01 13:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateGameplayState(int userId)
|
|
|
|
{
|
2021-05-20 09:52:20 +00:00
|
|
|
Debug.Assert(userMap.ContainsKey(userId));
|
2021-04-01 13:02:32 +00:00
|
|
|
|
2021-05-20 09:52:20 +00:00
|
|
|
var user = userMap[userId];
|
2021-05-20 10:27:34 +00:00
|
|
|
var spectatorState = playingUserStates[userId];
|
2021-04-01 13:02:32 +00:00
|
|
|
|
2021-11-24 06:25:49 +00:00
|
|
|
var resolvedRuleset = rulesets.AvailableRulesets.FirstOrDefault(r => r.OnlineID == spectatorState.RulesetID)?.CreateInstance();
|
2021-05-20 09:52:20 +00:00
|
|
|
if (resolvedRuleset == null)
|
|
|
|
return;
|
2021-04-01 13:02:32 +00:00
|
|
|
|
2021-11-12 08:45:05 +00:00
|
|
|
var resolvedBeatmap = beatmaps.QueryBeatmap(b => b.OnlineID == spectatorState.BeatmapID);
|
2021-05-20 09:52:20 +00:00
|
|
|
if (resolvedBeatmap == null)
|
|
|
|
return;
|
2021-04-01 13:02:32 +00:00
|
|
|
|
2021-05-20 09:52:20 +00:00
|
|
|
var score = new Score
|
|
|
|
{
|
|
|
|
ScoreInfo = new ScoreInfo
|
2021-04-01 13:02:32 +00:00
|
|
|
{
|
2021-10-04 08:35:53 +00:00
|
|
|
BeatmapInfo = resolvedBeatmap,
|
2021-05-20 09:52:20 +00:00
|
|
|
User = user,
|
|
|
|
Mods = spectatorState.Mods.Select(m => m.ToMod(resolvedRuleset)).ToArray(),
|
|
|
|
Ruleset = resolvedRuleset.RulesetInfo,
|
|
|
|
},
|
|
|
|
Replay = new Replay { HasReceivedAllFrames = false },
|
|
|
|
};
|
|
|
|
|
2021-10-01 17:08:56 +00:00
|
|
|
var gameplayState = new SpectatorGameplayState(score, resolvedRuleset, beatmaps.GetWorkingBeatmap(resolvedBeatmap));
|
2021-05-20 09:52:20 +00:00
|
|
|
|
|
|
|
gameplayStates[userId] = gameplayState;
|
|
|
|
Schedule(() => StartGameplay(userId, gameplayState));
|
2021-04-01 13:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Invoked when a spectated user's state has changed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user whose state has changed.</param>
|
|
|
|
/// <param name="spectatorState">The new state.</param>
|
|
|
|
protected abstract void OnUserStateChanged(int userId, [NotNull] SpectatorState spectatorState);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Starts gameplay for a user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to start gameplay for.</param>
|
2021-10-01 17:08:56 +00:00
|
|
|
/// <param name="spectatorGameplayState">The gameplay state.</param>
|
|
|
|
protected abstract void StartGameplay(int userId, [NotNull] SpectatorGameplayState spectatorGameplayState);
|
2021-04-01 13:02:32 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Ends gameplay for a user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to end gameplay for.</param>
|
|
|
|
protected abstract void EndGameplay(int userId);
|
|
|
|
|
2021-04-26 12:25:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Stops spectating a user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user to stop spectating.</param>
|
|
|
|
protected void RemoveUser(int userId)
|
|
|
|
{
|
2021-05-20 10:27:34 +00:00
|
|
|
onUserStateRemoved(userId);
|
2021-04-26 12:25:34 +00:00
|
|
|
|
2021-08-10 09:39:20 +00:00
|
|
|
users.Remove(userId);
|
2021-05-20 09:52:20 +00:00
|
|
|
userMap.Remove(userId);
|
2021-04-26 12:25:34 +00:00
|
|
|
|
2021-05-20 09:52:20 +00:00
|
|
|
spectatorClient.StopWatchingUser(userId);
|
2021-04-26 12:25:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 13:02:32 +00:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
if (spectatorClient != null)
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
foreach ((int userId, var _) in userMap)
|
2021-05-20 09:52:20 +00:00
|
|
|
spectatorClient.StopWatchingUser(userId);
|
2021-04-01 13:02:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 10:26:41 +00:00
|
|
|
realmSubscription?.Dispose();
|
2021-04-01 13:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|