1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-21 23:36:10 +08:00

Make Room.Host non-bindable

This commit is contained in:
Dan Balasescu 2024-11-13 17:32:32 +09:00
parent 6160df1586
commit 8694f7e1cc
No known key found for this signature in database
12 changed files with 80 additions and 52 deletions

View File

@ -195,7 +195,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
private DrawableRoom createLoungeRoom(Room room) private DrawableRoom createLoungeRoom(Room room)
{ {
room.Host.Value ??= new APIUser { Username = "peppy", Id = 2 }; room.Host ??= new APIUser { Username = "peppy", Id = 2 };
if (room.RecentParticipants.Count == 0) if (room.RecentParticipants.Count == 0)
{ {

View File

@ -26,17 +26,14 @@ namespace osu.Game.Tests.Visual.Multiplayer
SelectedRoom.Value = new Room SelectedRoom.Value = new Room
{ {
Name = "test room", Name = "test room",
Host = Host = new APIUser
{ {
Value = new APIUser Id = 2,
{ Username = "peppy",
Id = 2,
Username = "peppy",
}
} }
}; };
Child = list = new DrawableRoomParticipantsList Child = list = new DrawableRoomParticipantsList(SelectedRoom.Value)
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,

View File

@ -61,8 +61,8 @@ namespace osu.Game.Tests.Visual.Playlists
setupAndCreateRoom(room => setupAndCreateRoom(room =>
{ {
room.Name = "my awesome room"; room.Name = "my awesome room";
room.Host.Value = API.LocalUser.Value; room.Host = API.LocalUser.Value;
room.RecentParticipants.Add(room.Host.Value); room.RecentParticipants.Add(room.Host);
room.EndDate.Value = DateTimeOffset.Now.AddMinutes(5); room.EndDate.Value = DateTimeOffset.Now.AddMinutes(5);
room.Playlist.Add(new PlaylistItem(importedBeatmap.Beatmaps.First()) room.Playlist.Add(new PlaylistItem(importedBeatmap.Beatmaps.First())
{ {
@ -85,8 +85,8 @@ namespace osu.Game.Tests.Visual.Playlists
{ {
room.Name = "my awesome room"; room.Name = "my awesome room";
room.MaxAttempts.Value = 5; room.MaxAttempts.Value = 5;
room.Host.Value = API.LocalUser.Value; room.Host = API.LocalUser.Value;
room.RecentParticipants.Add(room.Host.Value); room.RecentParticipants.Add(room.Host);
room.EndDate.Value = DateTimeOffset.Now.AddMinutes(5); room.EndDate.Value = DateTimeOffset.Now.AddMinutes(5);
room.Playlist.Add(new PlaylistItem(importedBeatmap.Beatmaps.First()) room.Playlist.Add(new PlaylistItem(importedBeatmap.Beatmaps.First())
{ {
@ -103,7 +103,7 @@ namespace osu.Game.Tests.Visual.Playlists
setupAndCreateRoom(room => setupAndCreateRoom(room =>
{ {
room.Name = "my awesome room"; room.Name = "my awesome room";
room.Host.Value = API.LocalUser.Value; room.Host = API.LocalUser.Value;
room.Playlist.Add(new PlaylistItem(importedBeatmap.Beatmaps.First()) room.Playlist.Add(new PlaylistItem(importedBeatmap.Beatmaps.First())
{ {
RulesetID = new OsuRuleset().RulesetInfo.OnlineID RulesetID = new OsuRuleset().RulesetInfo.OnlineID
@ -151,7 +151,7 @@ namespace osu.Game.Tests.Visual.Playlists
setupAndCreateRoom(room => setupAndCreateRoom(room =>
{ {
room.Name = "my awesome room"; room.Name = "my awesome room";
room.Host.Value = API.LocalUser.Value; room.Host = API.LocalUser.Value;
room.Playlist.Add(new PlaylistItem(new BeatmapInfo room.Playlist.Add(new PlaylistItem(new BeatmapInfo
{ {
MD5Hash = realHash, MD5Hash = realHash,

View File

@ -528,7 +528,7 @@ namespace osu.Game.Online.Multiplayer
var user = Room.Users.FirstOrDefault(u => u.UserID == userId); var user = Room.Users.FirstOrDefault(u => u.UserID == userId);
Room.Host = user; Room.Host = user;
APIRoom.Host.Value = user?.User; APIRoom.Host = user?.User;
RoomUpdated?.Invoke(); RoomUpdated?.Invoke();
}, false); }, false);

View File

@ -39,6 +39,12 @@ namespace osu.Game.Online.Rooms
set => SetField(ref name, value); set => SetField(ref name, value);
} }
public APIUser? Host
{
get => host;
set => SetField(ref host, value);
}
/// <summary> /// <summary>
/// Represents the current item selected within the room. /// Represents the current item selected within the room.
/// </summary> /// </summary>
@ -57,13 +63,12 @@ namespace osu.Game.Online.Rooms
[JsonProperty("name")] [JsonProperty("name")]
private string name = string.Empty; private string name = string.Empty;
[JsonProperty("host")]
private APIUser? host;
[JsonProperty("current_playlist_item")] [JsonProperty("current_playlist_item")]
private PlaylistItem? currentPlaylistItem; private PlaylistItem? currentPlaylistItem;
[Cached]
[JsonProperty("host")]
public readonly Bindable<APIUser?> Host = new Bindable<APIUser?>();
[Cached] [Cached]
[JsonProperty("playlist")] [JsonProperty("playlist")]
public readonly BindableList<PlaylistItem> Playlist = new BindableList<PlaylistItem>(); public readonly BindableList<PlaylistItem> Playlist = new BindableList<PlaylistItem>();
@ -217,8 +222,8 @@ namespace osu.Game.Online.Rooms
Category.Value = other.Category.Value; Category.Value = other.Category.Value;
if (other.Host.Value != null && Host.Value?.Id != other.Host.Value.Id) if (other.Host != null && Host?.Id != other.Host.Id)
Host.Value = other.Host.Value; Host = other.Host;
ChannelId.Value = other.ChannelId.Value; ChannelId.Value = other.ChannelId.Value;
Status.Value = other.Status.Value; Status.Value = other.Status.Value;

View File

@ -42,7 +42,7 @@ namespace osu.Game.Screens.OnlinePlay.Components
public virtual void CreateRoom(Room room, Action<Room>? onSuccess = null, Action<string>? onError = null) public virtual void CreateRoom(Room room, Action<Room>? onSuccess = null, Action<string>? onError = null)
{ {
room.Host.Value = api.LocalUser.Value; room.Host = api.LocalUser.Value;
var req = new CreateRoomRequest(room); var req = new CreateRoomRequest(room);

View File

@ -224,7 +224,7 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components
Children = new Drawable[] Children = new Drawable[]
{ {
ButtonsContainer, ButtonsContainer,
drawableRoomParticipantsList = new DrawableRoomParticipantsList drawableRoomParticipantsList = new DrawableRoomParticipantsList(Room)
{ {
Anchor = Anchor.CentreRight, Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight, Origin = Anchor.CentreRight,

View File

@ -1,13 +1,11 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System.Collections.Specialized; using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
@ -16,31 +14,33 @@ using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Rooms;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Users.Drawables; using osu.Game.Users.Drawables;
using osuTK; using osuTK;
using Container = osu.Framework.Graphics.Containers.Container;
namespace osu.Game.Screens.OnlinePlay.Lounge.Components namespace osu.Game.Screens.OnlinePlay.Lounge.Components
{ {
public partial class DrawableRoomParticipantsList : OnlinePlayComposite public partial class DrawableRoomParticipantsList : OnlinePlayComposite
{ {
public const float SHEAR_WIDTH = 12f; public const float SHEAR_WIDTH = 12f;
private const float avatar_size = 36; private const float avatar_size = 36;
private const float height = 60f; private const float height = 60f;
private static readonly Vector2 shear = new Vector2(SHEAR_WIDTH / height, 0); private static readonly Vector2 shear = new Vector2(SHEAR_WIDTH / height, 0);
private FillFlowContainer<CircularAvatar> avatarFlow; private readonly Room room;
private CircularAvatar hostAvatar; private FillFlowContainer<CircularAvatar> avatarFlow = null!;
private LinkFlowContainer hostText; private CircularAvatar hostAvatar = null!;
private HiddenUserCount hiddenUsers; private LinkFlowContainer hostText = null!;
private OsuSpriteText totalCount; private HiddenUserCount hiddenUsers = null!;
private OsuSpriteText totalCount = null!;
public DrawableRoomParticipantsList() public DrawableRoomParticipantsList(Room room)
{ {
this.room = room;
AutoSizeAxes = Axes.X; AutoSizeAxes = Axes.X;
Height = height; Height = height;
} }
@ -172,7 +172,8 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components
totalCount.Text = ParticipantCount.Value.ToString(); totalCount.Text = ParticipantCount.Value.ToString();
}, true); }, true);
Host.BindValueChanged(onHostChanged, true); room.PropertyChanged += onRoomPropertyChanged;
updateRoomHost();
} }
private int numberOfCircles = 4; private int numberOfCircles = 4;
@ -199,7 +200,7 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components
} }
} }
private void onParticipantsChanged(object sender, NotifyCollectionChangedEventArgs e) private void onParticipantsChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
switch (e.Action) switch (e.Action)
{ {
@ -269,21 +270,33 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components
} }
} }
private void onHostChanged(ValueChangedEvent<APIUser> host) private void onRoomPropertyChanged(object? sender, PropertyChangedEventArgs e)
{ {
hostAvatar.User = host.NewValue; if (e.PropertyName == nameof(Room.Host))
updateRoomHost();
}
private void updateRoomHost()
{
hostAvatar.User = room.Host;
hostText.Clear(); hostText.Clear();
if (host.NewValue != null) if (room.Host != null)
{ {
hostText.AddText("hosted by "); hostText.AddText("hosted by ");
hostText.AddUserLink(host.NewValue); hostText.AddUserLink(room.Host);
} }
} }
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
room.PropertyChanged -= onRoomPropertyChanged;
}
private partial class CircularAvatar : CompositeDrawable private partial class CircularAvatar : CompositeDrawable
{ {
public APIUser User public APIUser? User
{ {
get => avatar.User; get => avatar.User;
set => avatar.User = value; set => avatar.User = value;

View File

@ -2,12 +2,12 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.ComponentModel;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Beatmaps.Drawables; using osu.Game.Beatmaps.Drawables;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
using osu.Game.Resources.Localisation.Web; using osu.Game.Resources.Localisation.Web;
using osu.Game.Screens.OnlinePlay.Lounge.Components; using osu.Game.Screens.OnlinePlay.Lounge.Components;
@ -30,7 +30,6 @@ namespace osu.Game.Screens.OnlinePlay.Match
private IAPIProvider api { get; set; } = null!; private IAPIProvider api { get; set; } = null!;
private readonly BindableWithCurrent<PlaylistItem?> selectedItem = new BindableWithCurrent<PlaylistItem?>(); private readonly BindableWithCurrent<PlaylistItem?> selectedItem = new BindableWithCurrent<PlaylistItem?>();
private readonly IBindable<APIUser?> host = new Bindable<APIUser?>();
private readonly bool allowEdit; private readonly bool allowEdit;
private Drawable? editButton; private Drawable? editButton;
@ -40,7 +39,6 @@ namespace osu.Game.Screens.OnlinePlay.Match
this.allowEdit = allowEdit; this.allowEdit = allowEdit;
base.SelectedItem.BindTo(SelectedItem); base.SelectedItem.BindTo(SelectedItem);
host.BindTo(room.Host);
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -62,13 +60,31 @@ namespace osu.Game.Screens.OnlinePlay.Match
{ {
base.LoadComplete(); base.LoadComplete();
Room.PropertyChanged += onRoomPropertyChanged;
updateRoomHost();
}
private void onRoomPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Room.Host))
updateRoomHost();
}
private void updateRoomHost()
{
if (editButton != null) if (editButton != null)
host.BindValueChanged(h => editButton.Alpha = h.NewValue?.Equals(api.LocalUser.Value) == true ? 1 : 0, true); editButton.Alpha = Room.Host?.Equals(api.LocalUser.Value) == true ? 1 : 0;
} }
protected override UpdateableBeatmapBackgroundSprite CreateBackground() => base.CreateBackground().With(d => protected override UpdateableBeatmapBackgroundSprite CreateBackground() => base.CreateBackground().With(d =>
{ {
d.BackgroundLoadDelay = 0; d.BackgroundLoadDelay = 0;
}); });
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Room.PropertyChanged -= onRoomPropertyChanged;
}
} }
} }

View File

@ -16,9 +16,6 @@ namespace osu.Game.Screens.OnlinePlay
/// </summary> /// </summary>
public partial class OnlinePlayComposite : CompositeDrawable public partial class OnlinePlayComposite : CompositeDrawable
{ {
[Resolved(typeof(Room))]
protected Bindable<APIUser> Host { get; private set; } = null!;
[Resolved(typeof(Room))] [Resolved(typeof(Room))]
protected Bindable<RoomStatus> Status { get; private set; } = null!; protected Bindable<RoomStatus> Status { get; private set; } = null!;

View File

@ -33,7 +33,7 @@ namespace osu.Game.Tests.Visual.OnlinePlay
{ {
RoomID = -currentRoomId, RoomID = -currentRoomId,
Name = $@"Room {currentRoomId}", Name = $@"Room {currentRoomId}",
Host = { Value = new APIUser { Username = @"Host" } }, Host = new APIUser { Username = @"Host" },
EndDate = { Value = DateTimeOffset.Now + TimeSpan.FromSeconds(10) }, EndDate = { Value = DateTimeOffset.Now + TimeSpan.FromSeconds(10) },
Category = { Value = withSpotlightRooms && i % 2 == 0 ? RoomCategory.Spotlight : RoomCategory.Normal }, Category = { Value = withSpotlightRooms && i % 2 == 0 ? RoomCategory.Spotlight : RoomCategory.Normal },
}; };

View File

@ -262,12 +262,12 @@ namespace osu.Game.Tests.Visual.OnlinePlay
public void AddServerSideRoom(Room room, APIUser host) public void AddServerSideRoom(Room room, APIUser host)
{ {
room.RoomID ??= currentRoomId++; room.RoomID ??= currentRoomId++;
room.Host.Value = host; room.Host = host;
for (int i = 0; i < room.Playlist.Count; i++) for (int i = 0; i < room.Playlist.Count; i++)
{ {
room.Playlist[i].ID = currentPlaylistItemId++; room.Playlist[i].ID = currentPlaylistItemId++;
room.Playlist[i].OwnerID = room.Host.Value.OnlineID; room.Playlist[i].OwnerID = room.Host.OnlineID;
} }
serverSideRooms.Add(room); serverSideRooms.Add(room);