1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 20:47:51 +08:00

Merge branch 'master' into patch/11310

This commit is contained in:
Bartłomiej Dach 2020-12-26 13:19:13 +01:00 committed by GitHub
commit a1af749b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 43 deletions

View File

@ -4,8 +4,10 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
@ -31,7 +33,7 @@ namespace osu.Game.Online.Multiplayer
/// <summary>
/// Invoked when any change occurs to the multiplayer room.
/// </summary>
public event Action? RoomChanged;
public event Action? RoomUpdated;
/// <summary>
/// Invoked when the multiplayer server requests the current beatmap to be loaded into play.
@ -108,8 +110,9 @@ namespace osu.Game.Online.Multiplayer
Debug.Assert(Room != null);
foreach (var user in Room.Users)
await PopulateUser(user);
var users = getRoomUsers();
await Task.WhenAll(users.Select(PopulateUser));
updateLocalRoomSettings(Room.Settings);
}
@ -123,13 +126,16 @@ namespace osu.Game.Online.Multiplayer
public virtual Task LeaveRoom()
{
if (Room == null)
return Task.CompletedTask;
Scheduler.Add(() =>
{
if (Room == null)
return;
apiRoom = null;
Room = null;
apiRoom = null;
Room = null;
Schedule(() => RoomChanged?.Invoke());
RoomUpdated?.Invoke();
}, false);
return Task.CompletedTask;
}
@ -184,7 +190,7 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return Task.CompletedTask;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
@ -208,8 +214,8 @@ namespace osu.Game.Online.Multiplayer
break;
}
RoomChanged?.Invoke();
});
RoomUpdated?.Invoke();
}, false);
return Task.CompletedTask;
}
@ -221,7 +227,7 @@ namespace osu.Game.Online.Multiplayer
await PopulateUser(user);
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
@ -232,8 +238,8 @@ namespace osu.Game.Online.Multiplayer
Room.Users.Add(user);
RoomChanged?.Invoke();
});
RoomUpdated?.Invoke();
}, false);
}
Task IMultiplayerClient.UserLeft(MultiplayerRoomUser user)
@ -241,7 +247,7 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return Task.CompletedTask;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
@ -249,8 +255,8 @@ namespace osu.Game.Online.Multiplayer
Room.Users.Remove(user);
PlayingUsers.Remove(user.UserID);
RoomChanged?.Invoke();
});
RoomUpdated?.Invoke();
}, false);
return Task.CompletedTask;
}
@ -260,7 +266,7 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return Task.CompletedTask;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
@ -272,8 +278,8 @@ namespace osu.Game.Online.Multiplayer
Room.Host = user;
apiRoom.Host.Value = user?.User;
RoomChanged?.Invoke();
});
RoomUpdated?.Invoke();
}, false);
return Task.CompletedTask;
}
@ -289,7 +295,7 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return Task.CompletedTask;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
@ -299,8 +305,8 @@ namespace osu.Game.Online.Multiplayer
if (state != MultiplayerUserState.Playing)
PlayingUsers.Remove(userId);
RoomChanged?.Invoke();
});
RoomUpdated?.Invoke();
}, false);
return Task.CompletedTask;
}
@ -310,13 +316,13 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return Task.CompletedTask;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
LoadRequested?.Invoke();
});
}, false);
return Task.CompletedTask;
}
@ -326,7 +332,7 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return Task.CompletedTask;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
@ -334,7 +340,7 @@ namespace osu.Game.Online.Multiplayer
PlayingUsers.AddRange(Room.Users.Where(u => u.State == MultiplayerUserState.Playing).Select(u => u.UserID));
MatchStarted?.Invoke();
});
}, false);
return Task.CompletedTask;
}
@ -344,13 +350,13 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return Task.CompletedTask;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
ResultsReady?.Invoke();
});
}, false);
return Task.CompletedTask;
}
@ -361,6 +367,31 @@ namespace osu.Game.Online.Multiplayer
/// <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 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,
// let's just add a manual schedule for the non-scheduled usages instead.
Scheduler.Add(() =>
{
users = Room?.Users.ToList();
resetEvent.Set();
}, false);
resetEvent.Wait(100);
return users;
}
/// <summary>
/// Updates the local room settings with the given <see cref="MultiplayerRoomSettings"/>.
/// </summary>
@ -373,7 +404,7 @@ namespace osu.Game.Online.Multiplayer
if (Room == null)
return;
Schedule(() =>
Scheduler.Add(() =>
{
if (Room == null)
return;
@ -388,13 +419,13 @@ namespace osu.Game.Online.Multiplayer
// In-order for the client to not display an outdated beatmap, the playlist is forcefully cleared here.
apiRoom.Playlist.Clear();
RoomChanged?.Invoke();
RoomUpdated?.Invoke();
var req = new GetBeatmapSetRequest(settings.BeatmapID, BeatmapSetLookupType.BeatmapId);
req.Success += res => updatePlaylist(settings, res);
api.Queue(req);
});
}, false);
}
private void updatePlaylist(MultiplayerRoomSettings settings, APIBeatmapSet onlineSet)

View File

@ -56,11 +56,13 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
sampleReadyCount = audio.Samples.Get(@"SongSelect/select-difficulty");
}
protected override void OnRoomChanged()
protected override void OnRoomUpdated()
{
base.OnRoomChanged();
base.OnRoomUpdated();
// this method is called on leaving the room, so the local user may not exist in the room any more.
localUser = Room?.Users.SingleOrDefault(u => u.User?.Id == api.LocalUser.Value.Id);
localUser = Room?.Users.Single(u => u.User?.Id == api.LocalUser.Value.Id);
button.Enabled.Value = Client.Room?.State == MultiplayerRoomState.Open;
updateState();
}

View File

@ -19,18 +19,21 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
{
base.LoadComplete();
Client.RoomChanged += OnRoomChanged;
OnRoomChanged();
Client.RoomUpdated += OnRoomUpdated;
OnRoomUpdated();
}
protected virtual void OnRoomChanged()
/// <summary>
/// Invoked when any change occurs to the multiplayer room.
/// </summary>
protected virtual void OnRoomUpdated()
{
}
protected override void Dispose(bool isDisposing)
{
if (Client != null)
Client.RoomChanged -= OnRoomChanged;
Client.RoomUpdated -= OnRoomUpdated;
base.Dispose(isDisposing);
}

View File

@ -135,9 +135,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Participants
};
}
protected override void OnRoomChanged()
protected override void OnRoomUpdated()
{
base.OnRoomChanged();
base.OnRoomUpdated();
if (Room == null)
return;

View File

@ -36,9 +36,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Participants
};
}
protected override void OnRoomChanged()
protected override void OnRoomUpdated()
{
base.OnRoomChanged();
base.OnRoomUpdated();
if (Room == null)
panels.Clear();