2020-12-23 18:27:15 +08:00
|
|
|
// 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;
|
2020-12-26 09:25:16 +08:00
|
|
|
using System.Collections.Generic;
|
2020-12-18 19:50:54 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
2020-12-26 09:25:16 +08:00
|
|
|
using System.Threading;
|
2020-12-18 19:50:54 +08:00
|
|
|
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;
|
2020-12-23 15:19:03 +08:00
|
|
|
using osu.Framework.Logging;
|
2020-12-18 19:50:54 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Database;
|
2020-12-23 16:10:34 +08:00
|
|
|
using osu.Game.Extensions;
|
2020-12-18 19:50:54 +08:00
|
|
|
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-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>
|
2020-12-26 09:48:55 +08:00
|
|
|
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.
|
|
|
|
/// </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>
|
2020-12-29 13:27:33 +08:00
|
|
|
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
|
|
|
|
2020-12-23 14:49:22 +08:00
|
|
|
protected StatefulMultiplayerClient()
|
|
|
|
{
|
|
|
|
IsConnected.BindValueChanged(connected =>
|
|
|
|
{
|
|
|
|
// clean up local room state on server disconnect.
|
2020-12-29 14:19:52 +08:00
|
|
|
if (!connected.NewValue && Room != null)
|
2020-12-23 15:19:03 +08:00
|
|
|
{
|
|
|
|
Logger.Log("Connection to multiplayer server was lost.", LoggingTarget.Runtime, LogLevel.Important);
|
2020-12-23 16:14:58 +08:00
|
|
|
LeaveRoom().CatchUnobservedExceptions();
|
2020-12-23 15:19:03 +08:00
|
|
|
}
|
2020-12-23 14:49:22 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2020-12-26 09:25:16 +08:00
|
|
|
var users = getRoomUsers();
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-26 09:25:16 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-26 09:25:16 +08:00
|
|
|
apiRoom = null;
|
|
|
|
Room = null;
|
2020-12-29 13:27:33 +08:00
|
|
|
CurrentMatchPlayingUserIds.Clear();
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
RoomUpdated?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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>
|
2020-12-22 15:50:30 +08:00
|
|
|
public Task ChangeSettings(Optional<string> name = default, Optional<PlaylistItem> item = default)
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
2020-12-22 15:50:30 +08:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2020-12-22 15:50:30 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-29 16:09:47 +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>
|
2020-12-31 01:00:57 +08:00
|
|
|
public async Task ToggleReady()
|
2020-12-29 16:09:47 +08:00
|
|
|
{
|
|
|
|
var localUser = LocalUser;
|
|
|
|
|
|
|
|
if (localUser == null)
|
2020-12-31 01:00:57 +08:00
|
|
|
return;
|
2020-12-29 16:09:47 +08:00
|
|
|
|
|
|
|
switch (localUser.State)
|
|
|
|
{
|
|
|
|
case MultiplayerUserState.Idle:
|
|
|
|
await ChangeState(MultiplayerUserState.Ready);
|
2020-12-31 01:00:57 +08:00
|
|
|
return;
|
2020-12-29 16:09:47 +08:00
|
|
|
|
|
|
|
case MultiplayerUserState.Ready:
|
|
|
|
await ChangeState(MultiplayerUserState.Idle);
|
2020-12-31 01:00:57 +08:00
|
|
|
return;
|
2020-12-29 16:09:47 +08:00
|
|
|
|
|
|
|
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 StartMatch();
|
|
|
|
|
|
|
|
Task IMultiplayerClient.RoomStateChanged(MultiplayerRoomState state)
|
|
|
|
{
|
2020-12-20 21:51:33 +08:00
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
RoomUpdated?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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);
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
2020-12-24 03:34:19 +08:00
|
|
|
// 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);
|
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
RoomUpdated?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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;
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Room.Users.Remove(user);
|
2020-12-29 13:27:33 +08:00
|
|
|
CurrentMatchPlayingUserIds.Remove(user.UserID);
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
RoomUpdated?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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;
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
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;
|
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
RoomUpdated?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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;
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Room.Users.Single(u => u.UserID == userId).State = state;
|
|
|
|
|
2020-12-29 13:25:08 +08:00
|
|
|
updateUserPlayingState(userId, state);
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
RoomUpdated?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, false);
|
2020-12-18 19:50:54 +08:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task IMultiplayerClient.LoadRequested()
|
|
|
|
{
|
2020-12-20 21:51:33 +08:00
|
|
|
if (Room == null)
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LoadRequested?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MatchStarted?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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;
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ResultsReady?.Invoke();
|
2020-12-26 18:58:25 +08:00
|
|
|
}, 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);
|
|
|
|
|
2020-12-26 09:25:16 +08:00
|
|
|
/// <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 List<MultiplayerRoomUser>? getRoomUsers()
|
|
|
|
{
|
|
|
|
List<MultiplayerRoomUser>? users = null;
|
|
|
|
|
|
|
|
ManualResetEventSlim resetEvent = new ManualResetEventSlim();
|
|
|
|
|
|
|
|
// 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,
|
2020-12-26 17:59:14 +08:00
|
|
|
// let's just add a manual schedule for the non-scheduled usages instead.
|
|
|
|
Scheduler.Add(() =>
|
2020-12-26 09:25:16 +08:00
|
|
|
{
|
|
|
|
users = Room?.Users.ToList();
|
|
|
|
resetEvent.Set();
|
2020-12-26 17:59:14 +08:00
|
|
|
}, false);
|
2020-12-26 09:25:16 +08:00
|
|
|
|
|
|
|
resetEvent.Wait(100);
|
|
|
|
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-12-26 18:58:25 +08:00
|
|
|
Scheduler.Add(() =>
|
2020-12-18 19:50:54 +08:00
|
|
|
{
|
|
|
|
if (Room == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Debug.Assert(apiRoom != null);
|
|
|
|
|
2020-12-23 21:39:14 +08:00
|
|
|
// 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();
|
|
|
|
|
2020-12-26 09:48:55 +08:00
|
|
|
RoomUpdated?.Invoke();
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-23 21:39:14 +08:00
|
|
|
var req = new GetBeatmapSetRequest(settings.BeatmapID, BeatmapSetLookupType.BeatmapId);
|
|
|
|
req.Success += res => updatePlaylist(settings, res);
|
2020-12-18 19:50:54 +08:00
|
|
|
|
2020-12-23 21:39:14 +08:00
|
|
|
api.Queue(req);
|
2020-12-26 18:58:25 +08:00
|
|
|
}, false);
|
2020-12-20 17:30:00 +08:00
|
|
|
}
|
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
|
|
|
}
|
2020-12-29 02:11:44 +08:00
|
|
|
|
2020-12-29 13:25:08 +08:00
|
|
|
/// <summary>
|
2020-12-29 13:27:33 +08:00
|
|
|
/// For the provided user ID, update whether the user is included in <see cref="CurrentMatchPlayingUserIds"/>.
|
2020-12-29 13:25:08 +08:00
|
|
|
/// </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)
|
2020-12-29 02:11:44 +08:00
|
|
|
{
|
2020-12-29 13:27:33 +08:00
|
|
|
bool wasPlaying = CurrentMatchPlayingUserIds.Contains(userId);
|
2020-12-29 13:25:08 +08:00
|
|
|
bool isPlaying = state >= MultiplayerUserState.WaitingForLoad && state <= MultiplayerUserState.FinishedPlay;
|
2020-12-29 02:11:44 +08:00
|
|
|
|
2020-12-29 13:25:08 +08:00
|
|
|
if (isPlaying == wasPlaying)
|
|
|
|
return;
|
2020-12-29 02:11:44 +08:00
|
|
|
|
2020-12-29 13:25:08 +08:00
|
|
|
if (isPlaying)
|
2020-12-29 13:27:33 +08:00
|
|
|
CurrentMatchPlayingUserIds.Add(userId);
|
2020-12-29 13:25:08 +08:00
|
|
|
else
|
2020-12-29 13:27:33 +08:00
|
|
|
CurrentMatchPlayingUserIds.Remove(userId);
|
2020-12-29 02:11:44 +08:00
|
|
|
}
|
2020-12-18 19:50:54 +08:00
|
|
|
}
|
|
|
|
}
|