2020-12-18 19:50:54 +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.
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
using System;
|
|
|
|
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.Game.Beatmaps;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
2020-12-20 17:30:00 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2020-12-18 19:50:54 +08:00
|
|
|
using osu.Game.Online.Multiplayer;
|
|
|
|
using osu.Game.Online.Multiplayer.RoomStatuses;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Users;
|
|
|
|
using osu.Game.Utils;
|
|
|
|
|
|
|
|
namespace osu.Game.Online.RealtimeMultiplayer
|
|
|
|
{
|
|
|
|
public abstract class StatefulMultiplayerClient : Component, IMultiplayerClient, IMultiplayerRoomServer
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Invoked when any change occurs to the multiplayer room.
|
|
|
|
/// </summary>
|
|
|
|
public event Action? RoomChanged;
|
|
|
|
|
|
|
|
/// <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.
|
|
|
|
/// </summary>
|
|
|
|
public abstract IBindable<bool> IsConnected { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The joined <see cref="MultiplayerRoom"/>.
|
|
|
|
/// </summary>
|
|
|
|
public MultiplayerRoom? Room { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The users currently in gameplay.
|
|
|
|
/// </summary>
|
|
|
|
public readonly BindableList<int> PlayingUsers = new BindableList<int>();
|
|
|
|
|
|
|
|
[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
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
|
|
foreach (var user in Room.Users)
|
|
|
|
await PopulateUser(user);
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
|
|
apiRoom = null;
|
|
|
|
Room = null;
|
|
|
|
|
|
|
|
Schedule(() => RoomChanged?.Invoke());
|
|
|
|
|
|
|
|
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 void ChangeSettings(Optional<string> name = default, Optional<PlaylistItem> item = default)
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
2020-12-20 17:19:41 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public abstract Task TransferHost(int userId);
|
|
|
|
|
|
|
|
public abstract Task ChangeSettings(MultiplayerRoomSettings settings);
|
|
|
|
|
|
|
|
public abstract Task ChangeState(MultiplayerUserState newState);
|
|
|
|
|
|
|
|
public abstract Task StartMatch();
|
|
|
|
|
|
|
|
Task IMultiplayerClient.RoomStateChanged(MultiplayerRoomState state)
|
|
|
|
{
|
2020-12-20 21:51:33 +08:00
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
2020-12-18 19:50:54 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomChanged?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Room.Users.Add(user);
|
|
|
|
|
|
|
|
RoomChanged?.Invoke();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Task IMultiplayerClient.UserLeft(MultiplayerRoomUser user)
|
|
|
|
{
|
2020-12-20 21:51:33 +08:00
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
2020-12-18 19:50:54 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Room.Users.Remove(user);
|
|
|
|
PlayingUsers.Remove(user.UserID);
|
|
|
|
|
|
|
|
RoomChanged?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task IMultiplayerClient.HostChanged(int userId)
|
|
|
|
{
|
2020-12-20 21:51:33 +08:00
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
2020-12-18 19:50:54 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
RoomChanged?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-12-18 19:50:54 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Room.Users.Single(u => u.UserID == userId).State = state;
|
|
|
|
|
|
|
|
if (state != MultiplayerUserState.Playing)
|
|
|
|
PlayingUsers.Remove(userId);
|
|
|
|
|
|
|
|
RoomChanged?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task IMultiplayerClient.LoadRequested()
|
|
|
|
{
|
2020-12-20 21:51:33 +08:00
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
2020-12-18 19:50:54 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LoadRequested?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
2020-12-20 21:53:07 +08:00
|
|
|
PlayingUsers.AddRange(Room.Users.Where(u => u.State == MultiplayerUserState.Playing).Select(u => u.UserID));
|
2020-12-18 19:50:54 +08:00
|
|
|
|
|
|
|
MatchStarted?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task IMultiplayerClient.ResultsReady()
|
|
|
|
{
|
2020-12-20 21:51:33 +08:00
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
2020-12-18 19:50:54 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ResultsReady?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
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>
|
|
|
|
/// 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;
|
|
|
|
|
2020-12-20 17:25:54 +08:00
|
|
|
// Update a few properties of the room instantaneously.
|
2020-12-18 19:50:54 +08:00
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Debug.Assert(apiRoom != null);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
RoomChanged?.Invoke();
|
|
|
|
});
|
|
|
|
|
|
|
|
var req = new GetBeatmapSetRequest(settings.BeatmapID, BeatmapSetLookupType.BeatmapId);
|
2020-12-20 17:30:00 +08:00
|
|
|
req.Success += res => updatePlaylist(settings, res);
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-20 17:30:00 +08:00
|
|
|
api.Queue(req);
|
|
|
|
}
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-20 17:30:00 +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
|
|
|
|
2020-12-20 17:30:00 +08:00
|
|
|
Debug.Assert(apiRoom != null);
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-20 17:30:00 +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
|
|
|
|
2020-12-20 17:30:00 +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
|
|
|
|
2020-12-20 17:30:00 +08:00
|
|
|
PlaylistItem playlistItem = new PlaylistItem
|
|
|
|
{
|
|
|
|
ID = playlistItemId,
|
|
|
|
Beatmap = { Value = beatmap },
|
|
|
|
Ruleset = { Value = ruleset.RulesetInfo },
|
2020-12-18 19:50:54 +08:00
|
|
|
};
|
|
|
|
|
2020-12-20 17:30:00 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|