1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 23:42:54 +08:00
osu-lazer/osu.Game/Online/Multiplayer/StatefulMultiplayerClient.cs

539 lines
18 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2020-12-18 19:50:54 +08:00
// See the LICENCE file in the repository root for full licence text.
#nullable enable
using System;
using System.Collections.Generic;
2020-12-18 19:50:54 +08:00
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-12-20 17:19:41 +08:00
using osu.Framework.Extensions.ObjectExtensions;
2020-12-18 19:50:54 +08:00
using osu.Framework.Graphics;
using osu.Framework.Logging;
2020-12-18 19:50:54 +08:00
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Extensions;
2020-12-18 19:50:54 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms;
using osu.Game.Online.Rooms.RoomStatuses;
2020-12-18 19:50:54 +08:00
using osu.Game.Rulesets;
using osu.Game.Users;
using osu.Game.Utils;
2020-12-25 12:38:11 +08:00
namespace osu.Game.Online.Multiplayer
2020-12-18 19:50:54 +08:00
{
public abstract class StatefulMultiplayerClient : Component, IMultiplayerClient, IMultiplayerRoomServer
{
/// <summary>
/// Invoked when any change occurs to the multiplayer room.
/// </summary>
public event Action? RoomUpdated;
2020-12-18 19:50:54 +08:00
/// <summary>
/// Invoked when the multiplayer server requests the current beatmap to be loaded into play.
/// </summary>
public event Action? LoadRequested;
/// <summary>
/// Invoked when the multiplayer server requests gameplay to be started.
/// </summary>
public event Action? MatchStarted;
/// <summary>
/// Invoked when the multiplayer server has finished collating results.
/// </summary>
public event Action? ResultsReady;
/// <summary>
/// Whether the <see cref="StatefulMultiplayerClient"/> is currently connected.
/// This is NOT thread safe and usage should be scheduled.
2020-12-18 19:50:54 +08:00
/// </summary>
public abstract IBindable<bool> IsConnected { get; }
/// <summary>
/// The joined <see cref="MultiplayerRoom"/>.
/// </summary>
public MultiplayerRoom? Room { get; private set; }
/// <summary>
2020-12-29 13:29:40 +08:00
/// The users in the joined <see cref="Room"/> which are participating in the current gameplay loop.
2020-12-18 19:50:54 +08:00
/// </summary>
public readonly BindableList<int> CurrentMatchPlayingUserIds = new BindableList<int>();
2020-12-18 19:50:54 +08:00
2020-12-29 14:46:22 +08:00
/// <summary>
/// The <see cref="MultiplayerRoomUser"/> corresponding to the local player, if available.
/// </summary>
public MultiplayerRoomUser? LocalUser => Room?.Users.SingleOrDefault(u => u.User?.Id == api.LocalUser.Value.Id);
2020-12-30 23:29:19 +08:00
/// <summary>
/// Whether the <see cref="LocalUser"/> is the host in <see cref="Room"/>.
/// </summary>
public bool IsHost
{
get
{
var localUser = LocalUser;
return localUser != null && Room?.Host != null && localUser.Equals(Room.Host);
}
}
2020-12-18 19:50:54 +08:00
[Resolved]
private UserLookupCache userLookupCache { get; set; } = null!;
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved]
private RulesetStore rulesets { get; set; } = null!;
private Room? apiRoom;
2020-12-20 23:41:14 +08:00
// Todo: This is temporary, until the multiplayer server returns the item id on match start or otherwise.
private int playlistItemId;
2020-12-18 19:50:54 +08:00
protected StatefulMultiplayerClient()
{
IsConnected.BindValueChanged(connected =>
{
// clean up local room state on server disconnect.
if (!connected.NewValue && Room != null)
{
Logger.Log("Connection to multiplayer server was lost.", LoggingTarget.Runtime, LogLevel.Important);
LeaveRoom().CatchUnobservedExceptions();
}
});
}
2020-12-18 19:50:54 +08:00
/// <summary>
/// Joins the <see cref="MultiplayerRoom"/> for a given API <see cref="Room"/>.
/// </summary>
/// <param name="room">The API <see cref="Room"/>.</param>
public async Task JoinRoom(Room room)
{
2020-12-20 21:51:33 +08:00
if (Room != null)
throw new InvalidOperationException("Cannot join a multiplayer room while already in one.");
2020-12-18 19:50:54 +08:00
Debug.Assert(room.RoomID.Value != null);
apiRoom = room;
playlistItemId = room.Playlist.SingleOrDefault()?.ID ?? 0;
Room = await JoinRoom(room.RoomID.Value.Value);
Debug.Assert(Room != null);
var users = await getRoomUsers();
Debug.Assert(users != null);
await Task.WhenAll(users.Select(PopulateUser));
2020-12-18 19:50:54 +08:00
updateLocalRoomSettings(Room.Settings);
}
/// <summary>
/// Joins the <see cref="MultiplayerRoom"/> with a given ID.
/// </summary>
/// <param name="roomId">The room ID.</param>
/// <returns>The joined <see cref="MultiplayerRoom"/>.</returns>
protected abstract Task<MultiplayerRoom> JoinRoom(long roomId);
public virtual Task LeaveRoom()
{
Scheduler.Add(() =>
{
if (Room == null)
return;
2020-12-18 19:50:54 +08:00
apiRoom = null;
Room = null;
CurrentMatchPlayingUserIds.Clear();
2020-12-18 19:50:54 +08:00
RoomUpdated?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
/// <summary>
/// Change the current <see cref="MultiplayerRoom"/> settings.
/// </summary>
/// <remarks>
2020-12-18 23:17:50 +08:00
/// A room must be joined for this to have any effect.
2020-12-18 19:50:54 +08:00
/// </remarks>
/// <param name="name">The new room name, if any.</param>
/// <param name="item">The new room playlist item, if any.</param>
public Task ChangeSettings(Optional<string> name = default, Optional<PlaylistItem> item = default)
2020-12-18 19:50:54 +08:00
{
if (Room == null)
throw new InvalidOperationException("Must be joined to a match to change settings.");
2020-12-18 19:50:54 +08:00
// A dummy playlist item filled with the current room settings (except mods).
var existingPlaylistItem = new PlaylistItem
{
Beatmap =
{
Value = new BeatmapInfo
{
OnlineBeatmapID = Room.Settings.BeatmapID,
MD5Hash = Room.Settings.BeatmapChecksum
}
},
RulesetID = Room.Settings.RulesetID
};
return ChangeSettings(new MultiplayerRoomSettings
2020-12-18 19:50:54 +08:00
{
Name = name.GetOr(Room.Settings.Name),
BeatmapID = item.GetOr(existingPlaylistItem).BeatmapID,
BeatmapChecksum = item.GetOr(existingPlaylistItem).Beatmap.Value.MD5Hash,
RulesetID = item.GetOr(existingPlaylistItem).RulesetID,
2020-12-20 17:19:41 +08:00
Mods = item.HasValue ? item.Value.AsNonNull().RequiredMods.Select(m => new APIMod(m)).ToList() : Room.Settings.Mods
});
2020-12-18 19:50:54 +08:00
}
/// <summary>
/// Toggles the <see cref="LocalUser"/>'s ready state.
/// </summary>
/// <exception cref="InvalidOperationException">If a toggle of ready state is not valid at this time.</exception>
public async Task ToggleReady()
{
var localUser = LocalUser;
if (localUser == null)
return;
switch (localUser.State)
{
case MultiplayerUserState.Idle:
await ChangeState(MultiplayerUserState.Ready);
return;
case MultiplayerUserState.Ready:
await ChangeState(MultiplayerUserState.Idle);
return;
default:
throw new InvalidOperationException($"Cannot toggle ready when in {localUser.State}");
}
}
2020-12-18 19:50:54 +08:00
public abstract Task TransferHost(int userId);
public abstract Task ChangeSettings(MultiplayerRoomSettings settings);
public abstract Task ChangeState(MultiplayerUserState newState);
public abstract Task ChangeBeatmapAvailability(BeatmapAvailability newBeatmapAvailability);
2020-12-18 19:50:54 +08:00
public abstract Task StartMatch();
Task IMultiplayerClient.RoomStateChanged(MultiplayerRoomState state)
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
Debug.Assert(apiRoom != null);
Room.State = state;
switch (state)
{
case MultiplayerRoomState.Open:
apiRoom.Status.Value = new RoomStatusOpen();
break;
case MultiplayerRoomState.Playing:
apiRoom.Status.Value = new RoomStatusPlaying();
break;
case MultiplayerRoomState.Closed:
apiRoom.Status.Value = new RoomStatusEnded();
break;
}
RoomUpdated?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
async Task IMultiplayerClient.UserJoined(MultiplayerRoomUser user)
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return;
2020-12-18 19:50:54 +08:00
await PopulateUser(user);
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
// for sanity, ensure that there can be no duplicate users in the room user list.
if (Room.Users.Any(existing => existing.UserID == user.UserID))
return;
2020-12-18 19:50:54 +08:00
Room.Users.Add(user);
RoomUpdated?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
}
Task IMultiplayerClient.UserLeft(MultiplayerRoomUser user)
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
Room.Users.Remove(user);
CurrentMatchPlayingUserIds.Remove(user.UserID);
2020-12-18 19:50:54 +08:00
RoomUpdated?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
Task IMultiplayerClient.HostChanged(int userId)
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
Debug.Assert(apiRoom != null);
var user = Room.Users.FirstOrDefault(u => u.UserID == userId);
Room.Host = user;
apiRoom.Host.Value = user?.User;
RoomUpdated?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
Task IMultiplayerClient.SettingsChanged(MultiplayerRoomSettings newSettings)
{
updateLocalRoomSettings(newSettings);
return Task.CompletedTask;
}
Task IMultiplayerClient.UserStateChanged(int userId, MultiplayerUserState state)
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
Room.Users.Single(u => u.UserID == userId).State = state;
updateUserPlayingState(userId, state);
2020-12-18 19:50:54 +08:00
RoomUpdated?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
Task IMultiplayerClient.UserBeatmapAvailabilityChanged(int userId, BeatmapAvailability beatmapAvailability)
{
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
{
var user = Room?.Users.SingleOrDefault(u => u.UserID == userId);
// errors here are not critical - beatmap availability state is mostly for display.
if (user == null)
return;
user.BeatmapAvailability = beatmapAvailability;
RoomUpdated?.Invoke();
}, false);
return Task.CompletedTask;
}
2020-12-18 19:50:54 +08:00
Task IMultiplayerClient.LoadRequested()
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
LoadRequested?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
Task IMultiplayerClient.MatchStarted()
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return Task.CompletedTask;
2020-12-18 19:50:54 +08:00
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
MatchStarted?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
Task IMultiplayerClient.ResultsReady()
{
2020-12-20 21:51:33 +08:00
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
ResultsReady?.Invoke();
}, false);
2020-12-18 19:50:54 +08:00
return Task.CompletedTask;
}
/// <summary>
/// Populates the <see cref="User"/> for a given <see cref="MultiplayerRoomUser"/>.
/// </summary>
/// <param name="multiplayerUser">The <see cref="MultiplayerRoomUser"/> to populate.</param>
protected async Task PopulateUser(MultiplayerRoomUser multiplayerUser) => multiplayerUser.User ??= await userLookupCache.GetUserAsync(multiplayerUser.UserID);
/// <summary>
/// Retrieve a copy of users currently in the joined <see cref="Room"/> in a thread-safe manner.
/// This should be used whenever accessing users from outside of an Update thread context (ie. when not calling <see cref="Drawable.Schedule"/>).
/// </summary>
/// <returns>A copy of users in the current room, or null if unavailable.</returns>
private Task<List<MultiplayerRoomUser>?> getRoomUsers()
{
var tcs = new TaskCompletionSource<List<MultiplayerRoomUser>?>();
// at some point we probably want to replace all these schedule calls with Room.LockForUpdate.
// for now, as this would require quite some consideration due to the number of accesses to the room instance,
// let's just add a manual schedule for the non-scheduled usages instead.
Scheduler.Add(() =>
{
var users = Room?.Users.ToList();
tcs.SetResult(users);
}, false);
return tcs.Task;
}
2020-12-18 19:50:54 +08:00
/// <summary>
/// Updates the local room settings with the given <see cref="MultiplayerRoomSettings"/>.
/// </summary>
/// <remarks>
/// This updates both the joined <see cref="MultiplayerRoom"/> and the respective API <see cref="Room"/>.
/// </remarks>
/// <param name="settings">The new <see cref="MultiplayerRoomSettings"/> to update from.</param>
private void updateLocalRoomSettings(MultiplayerRoomSettings settings)
{
if (Room == null)
return;
Scheduler.Add(() =>
2020-12-18 19:50:54 +08:00
{
if (Room == null)
return;
Debug.Assert(apiRoom != null);
// Update a few properties of the room instantaneously.
2020-12-18 19:50:54 +08:00
Room.Settings = settings;
apiRoom.Name.Value = Room.Settings.Name;
// The playlist update is delayed until an online beatmap lookup (below) succeeds.
// In-order for the client to not display an outdated beatmap, the playlist is forcefully cleared here.
apiRoom.Playlist.Clear();
RoomUpdated?.Invoke();
2020-12-18 19:50:54 +08:00
var req = new GetBeatmapSetRequest(settings.BeatmapID, BeatmapSetLookupType.BeatmapId);
req.Success += res => updatePlaylist(settings, res);
2020-12-18 19:50:54 +08:00
api.Queue(req);
}, false);
}
2020-12-18 19:50:54 +08:00
private void updatePlaylist(MultiplayerRoomSettings settings, APIBeatmapSet onlineSet)
{
if (Room == null || !Room.Settings.Equals(settings))
return;
2020-12-18 19:50:54 +08:00
Debug.Assert(apiRoom != null);
2020-12-18 19:50:54 +08:00
var beatmapSet = onlineSet.ToBeatmapSet(rulesets);
var beatmap = beatmapSet.Beatmaps.Single(b => b.OnlineBeatmapID == settings.BeatmapID);
beatmap.MD5Hash = settings.BeatmapChecksum;
2020-12-18 19:50:54 +08:00
var ruleset = rulesets.GetRuleset(settings.RulesetID).CreateInstance();
var mods = settings.Mods.Select(m => m.ToMod(ruleset));
2020-12-18 19:50:54 +08:00
PlaylistItem playlistItem = new PlaylistItem
{
ID = playlistItemId,
Beatmap = { Value = beatmap },
Ruleset = { Value = ruleset.RulesetInfo },
2020-12-18 19:50:54 +08:00
};
playlistItem.RequiredMods.AddRange(mods);
apiRoom.Playlist.Clear(); // Clearing should be unnecessary, but here for sanity.
apiRoom.Playlist.Add(playlistItem);
2020-12-18 19:50:54 +08:00
}
/// <summary>
/// For the provided user ID, update whether the user is included in <see cref="CurrentMatchPlayingUserIds"/>.
/// </summary>
/// <param name="userId">The user's ID.</param>
/// <param name="state">The new state of the user.</param>
private void updateUserPlayingState(int userId, MultiplayerUserState state)
{
bool wasPlaying = CurrentMatchPlayingUserIds.Contains(userId);
bool isPlaying = state >= MultiplayerUserState.WaitingForLoad && state <= MultiplayerUserState.FinishedPlay;
if (isPlaying == wasPlaying)
return;
if (isPlaying)
CurrentMatchPlayingUserIds.Add(userId);
else
CurrentMatchPlayingUserIds.Remove(userId);
}
2020-12-18 19:50:54 +08:00
}
}