2022-07-04 16:18:33 +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-07-14 14:18:12 +08:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2025-02-04 16:54:03 +08:00
|
|
|
using System.Threading;
|
2022-07-04 16:18:33 +08:00
|
|
|
using System.Threading.Tasks;
|
2025-01-17 15:01:11 +08:00
|
|
|
using osu.Framework.Allocation;
|
2023-12-07 01:24:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2022-07-04 16:18:33 +08:00
|
|
|
using osu.Framework.Graphics;
|
2025-01-17 15:01:11 +08:00
|
|
|
using osu.Game.Online.API;
|
2025-02-04 16:54:03 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2023-12-07 01:24:31 +08:00
|
|
|
using osu.Game.Users;
|
2022-07-04 16:18:33 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Metadata
|
|
|
|
{
|
|
|
|
public abstract partial class MetadataClient : Component, IMetadataClient, IMetadataServer
|
|
|
|
{
|
2023-12-07 01:24:31 +08:00
|
|
|
public abstract IBindable<bool> IsConnected { get; }
|
|
|
|
|
2025-01-17 15:01:11 +08:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; } = null!;
|
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
#region Beatmap metadata updates
|
2022-07-04 16:18:33 +08:00
|
|
|
|
2022-07-05 20:42:35 +08:00
|
|
|
public abstract Task<BeatmapUpdates> GetChangesSince(int queueId);
|
2022-07-14 14:18:12 +08:00
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
public abstract Task BeatmapSetsUpdated(BeatmapUpdates updates);
|
|
|
|
|
|
|
|
public event Action<int[]>? ChangedBeatmapSetsArrived;
|
2022-07-14 14:18:12 +08:00
|
|
|
|
|
|
|
protected Task ProcessChanges(int[] beatmapSetIDs)
|
|
|
|
{
|
|
|
|
ChangedBeatmapSetsArrived?.Invoke(beatmapSetIDs.Distinct().ToArray());
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2023-12-07 01:24:31 +08:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region User presence updates
|
|
|
|
|
2025-01-17 14:59:25 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The <see cref="UserPresence"/> information about the current user.
|
|
|
|
/// </summary>
|
2025-01-17 15:25:48 +08:00
|
|
|
public abstract UserPresence LocalUserPresence { get; }
|
2025-01-17 14:59:25 +08:00
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Dictionary keyed by user ID containing all of the <see cref="UserPresence"/> information about currently online users received from the server.
|
|
|
|
/// </summary>
|
2025-01-17 15:25:48 +08:00
|
|
|
public abstract IBindableDictionary<int, UserPresence> UserPresences { get; }
|
2023-12-07 01:24:31 +08:00
|
|
|
|
2025-01-09 16:31:01 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Dictionary keyed by user ID containing all of the <see cref="UserPresence"/> information about currently online friends received from the server.
|
|
|
|
/// </summary>
|
2025-01-17 15:25:48 +08:00
|
|
|
public abstract IBindableDictionary<int, UserPresence> FriendPresences { get; }
|
2025-01-09 16:31:01 +08:00
|
|
|
|
2025-01-15 15:53:55 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Attempts to retrieve the presence of a user.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userId">The user ID.</param>
|
|
|
|
/// <returns>The user presence, or null if not available or the user's offline.</returns>
|
|
|
|
public UserPresence? GetPresence(int userId)
|
|
|
|
{
|
2025-01-17 15:01:11 +08:00
|
|
|
if (userId == api.LocalUser.Value.OnlineID)
|
2025-01-17 16:32:39 +08:00
|
|
|
return LocalUserPresence;
|
2025-01-17 15:01:11 +08:00
|
|
|
|
2025-01-17 16:32:39 +08:00
|
|
|
if (FriendPresences.TryGetValue(userId, out UserPresence presence))
|
2025-01-15 15:53:55 +08:00
|
|
|
return presence;
|
|
|
|
|
2025-01-17 16:32:39 +08:00
|
|
|
if (UserPresences.TryGetValue(userId, out presence))
|
2025-01-15 15:53:55 +08:00
|
|
|
return presence;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
public abstract Task UpdateActivity(UserActivity? activity);
|
|
|
|
|
|
|
|
public abstract Task UpdateStatus(UserStatus? status);
|
|
|
|
|
2025-02-04 16:54:03 +08:00
|
|
|
private int userPresenceWatchCount;
|
|
|
|
|
|
|
|
protected bool IsWatchingUserPresence
|
|
|
|
=> Interlocked.CompareExchange(ref userPresenceWatchCount, userPresenceWatchCount, userPresenceWatchCount) > 0;
|
|
|
|
|
2025-02-05 17:24:07 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Signals to the server that we want to begin receiving status updates for all users.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An <see cref="IDisposable"/> which will end the session when disposed.</returns>
|
|
|
|
public IDisposable BeginWatchingUserPresence() => new UserPresenceWatchToken(this);
|
2025-02-04 16:54:03 +08:00
|
|
|
|
|
|
|
Task IMetadataServer.BeginWatchingUserPresence()
|
|
|
|
{
|
|
|
|
if (Interlocked.Increment(ref userPresenceWatchCount) == 1)
|
|
|
|
return BeginWatchingUserPresenceInternal();
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2023-12-07 01:24:31 +08:00
|
|
|
|
2025-02-04 16:54:03 +08:00
|
|
|
Task IMetadataServer.EndWatchingUserPresence()
|
|
|
|
{
|
|
|
|
if (Interlocked.Decrement(ref userPresenceWatchCount) == 0)
|
|
|
|
return EndWatchingUserPresenceInternal();
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract Task BeginWatchingUserPresenceInternal();
|
|
|
|
|
|
|
|
protected abstract Task EndWatchingUserPresenceInternal();
|
2023-12-07 01:24:31 +08:00
|
|
|
|
|
|
|
public abstract Task UserPresenceUpdated(int userId, UserPresence? presence);
|
|
|
|
|
2025-01-09 16:31:01 +08:00
|
|
|
public abstract Task FriendPresenceUpdated(int userId, UserPresence? presence);
|
|
|
|
|
2025-02-04 16:54:03 +08:00
|
|
|
private class UserPresenceWatchToken : IDisposable
|
|
|
|
{
|
|
|
|
private readonly IMetadataServer server;
|
|
|
|
private bool isDisposed;
|
|
|
|
|
|
|
|
public UserPresenceWatchToken(IMetadataServer server)
|
|
|
|
{
|
|
|
|
this.server = server;
|
|
|
|
server.BeginWatchingUserPresence().FireAndForget();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (isDisposed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
server.EndWatchingUserPresence().FireAndForget();
|
|
|
|
isDisposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
#endregion
|
|
|
|
|
2024-05-17 16:32:39 +08:00
|
|
|
#region Daily Challenge
|
|
|
|
|
|
|
|
public abstract IBindable<DailyChallengeInfo?> DailyChallengeInfo { get; }
|
|
|
|
|
|
|
|
public abstract Task DailyChallengeUpdated(DailyChallengeInfo? info);
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2024-06-27 16:44:28 +08:00
|
|
|
#region Multiplayer room watching
|
|
|
|
|
|
|
|
public abstract Task<MultiplayerPlaylistItemStats[]> BeginWatchingMultiplayerRoom(long id);
|
|
|
|
|
|
|
|
public abstract Task EndWatchingMultiplayerRoom(long id);
|
|
|
|
|
|
|
|
public event Action<MultiplayerRoomScoreSetEvent>? MultiplayerRoomScoreSet;
|
|
|
|
|
|
|
|
Task IMetadataClient.MultiplayerRoomScoreSet(MultiplayerRoomScoreSetEvent roomScoreSetEvent)
|
|
|
|
{
|
|
|
|
if (MultiplayerRoomScoreSet != null)
|
|
|
|
Schedule(MultiplayerRoomScoreSet, roomScoreSetEvent);
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2023-12-07 01:24:31 +08:00
|
|
|
#region Disconnection handling
|
|
|
|
|
|
|
|
public event Action? Disconnecting;
|
|
|
|
|
|
|
|
public virtual Task DisconnectRequested()
|
|
|
|
{
|
|
|
|
Schedule(() => Disconnecting?.Invoke());
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2022-07-04 16:18:33 +08:00
|
|
|
}
|
|
|
|
}
|