From 6712a687972c2fcc347846e5f021a3187342f797 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 25 Dec 2018 11:45:50 +0900 Subject: [PATCH] Create IRoomManager interface, add test for RoomsContainer --- .../Visual/TestCaseLoungeRoomsContainer.cs | 86 +++++++++++++++++++ osu.Game/Screens/Multi/IRoomManager.cs | 46 ++++++++++ .../Multi/Lounge/Components/RoomsContainer.cs | 4 +- osu.Game/Screens/Multi/Lounge/LoungeScreen.cs | 2 +- .../Match/Components/RoomSettingsOverlay.cs | 2 +- osu.Game/Screens/Multi/Match/MatchScreen.cs | 2 +- osu.Game/Screens/Multi/Multiplayer.cs | 2 +- osu.Game/Screens/Multi/RoomManager.cs | 4 +- 8 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs create mode 100644 osu.Game/Screens/Multi/IRoomManager.cs diff --git a/osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs b/osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs new file mode 100644 index 0000000000..c6279767a7 --- /dev/null +++ b/osu.Game.Tests/Visual/TestCaseLoungeRoomsContainer.cs @@ -0,0 +1,86 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using System.Collections.Generic; +using osu.Framework.Allocation; +using osu.Framework.Configuration; +using osu.Framework.Graphics; +using osu.Game.Graphics; +using osu.Game.Online.Multiplayer; +using osu.Game.Screens.Multi; +using osu.Game.Screens.Multi.Lounge.Components; +using osu.Game.Users; +using osuTK.Graphics; + +namespace osu.Game.Tests.Visual +{ + public class TestCaseLoungeRoomsContainer : OsuTestCase + { + public override IReadOnlyList RequiredTypes => new[] + { + typeof(RoomsContainer), + typeof(DrawableRoom) + }; + + [Cached(Type = typeof(IRoomManager))] + private TestRoomManager roomManager = new TestRoomManager(); + + public TestCaseLoungeRoomsContainer() + { + RoomsContainer rooms; + + Child = rooms = new RoomsContainer + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Width = 0.5f, + JoinRequested = joinRequested + }; + + int roomId = 0; + + AddStep("Add room", () => roomManager.Rooms.Add(new Room + { + Name = { Value = $"Room {++roomId}"}, + Host = { Value = new User { Username = "Host" } }, + EndDate = { Value = DateTimeOffset.Now + TimeSpan.FromSeconds(10) } + })); + + AddStep("Remove selected", () => + { + if (rooms.SelectedRoom.Value != null) + roomManager.Rooms.Remove(rooms.SelectedRoom.Value); + }); + } + + private void joinRequested(Room room) => room.Status.Value = new JoinedRoomStatus(); + + private class TestRoomManager : IRoomManager + { + public event Action OpenRequested; + + public readonly BindableCollection Rooms = new BindableCollection(); + IBindableCollection IRoomManager.Rooms => Rooms; + + public void CreateRoom(Room room) => Rooms.Add(room); + + public void JoinRoom(Room room) => OpenRequested?.Invoke(room); + + public void PartRoom() + { + } + + public void Filter(FilterCriteria criteria) + { + } + } + + private class JoinedRoomStatus : RoomStatus + { + public override string Message => "Joined"; + + public override Color4 GetAppropriateColour(OsuColour colours) => colours.Yellow; + } + } +} diff --git a/osu.Game/Screens/Multi/IRoomManager.cs b/osu.Game/Screens/Multi/IRoomManager.cs new file mode 100644 index 0000000000..f09f360ea2 --- /dev/null +++ b/osu.Game/Screens/Multi/IRoomManager.cs @@ -0,0 +1,46 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using osu.Framework.Configuration; +using osu.Game.Online.Multiplayer; +using osu.Game.Screens.Multi.Lounge.Components; + +namespace osu.Game.Screens.Multi +{ + public interface IRoomManager + { + /// + /// Invoked when this requests a to be opened. + /// + event Action OpenRequested; + + /// + /// All the active s. + /// + IBindableCollection Rooms { get; } + + /// + /// Creates a new . + /// + /// The to create. + void CreateRoom(Room room); + + /// + /// Joins a . + /// + /// The to join. must be populated. + void JoinRoom(Room room); + + /// + /// Parts the currently-joined . + /// + void PartRoom(); + + /// + /// Queries for s matching a new . + /// + /// The to match. + void Filter(FilterCriteria criteria); + } +} diff --git a/osu.Game/Screens/Multi/Lounge/Components/RoomsContainer.cs b/osu.Game/Screens/Multi/Lounge/Components/RoomsContainer.cs index b17ddca21d..6fc544ec80 100644 --- a/osu.Game/Screens/Multi/Lounge/Components/RoomsContainer.cs +++ b/osu.Game/Screens/Multi/Lounge/Components/RoomsContainer.cs @@ -27,7 +27,7 @@ namespace osu.Game.Screens.Multi.Lounge.Components private readonly FillFlowContainer roomFlow; [Resolved] - private RoomManager manager { get; set; } + private IRoomManager roomManager { get; set; } public RoomsContainer() { @@ -46,7 +46,7 @@ namespace osu.Game.Screens.Multi.Lounge.Components [BackgroundDependencyLoader] private void load() { - rooms.BindTo(manager.Rooms); + rooms.BindTo(roomManager.Rooms); rooms.ItemsAdded += addRooms; rooms.ItemsRemoved += removeRooms; diff --git a/osu.Game/Screens/Multi/Lounge/LoungeScreen.cs b/osu.Game/Screens/Multi/Lounge/LoungeScreen.cs index 8be7afad8b..82d593033f 100644 --- a/osu.Game/Screens/Multi/Lounge/LoungeScreen.cs +++ b/osu.Game/Screens/Multi/Lounge/LoungeScreen.cs @@ -23,7 +23,7 @@ namespace osu.Game.Screens.Multi.Lounge private readonly Action pushGameplayScreen; [Resolved(CanBeNull = true)] - private RoomManager roomManager { get; set; } + private IRoomManager roomManager { get; set; } public override string Title => "Lounge"; diff --git a/osu.Game/Screens/Multi/Match/Components/RoomSettingsOverlay.cs b/osu.Game/Screens/Multi/Match/Components/RoomSettingsOverlay.cs index db29722025..797894e93c 100644 --- a/osu.Game/Screens/Multi/Match/Components/RoomSettingsOverlay.cs +++ b/osu.Game/Screens/Multi/Match/Components/RoomSettingsOverlay.cs @@ -40,7 +40,7 @@ namespace osu.Game.Screens.Multi.Match.Components private readonly Room room; [Resolved(CanBeNull = true)] - private RoomManager manager { get; set; } + private IRoomManager manager { get; set; } public RoomSettingsOverlay(Room room) { diff --git a/osu.Game/Screens/Multi/Match/MatchScreen.cs b/osu.Game/Screens/Multi/Match/MatchScreen.cs index 927b786ab5..dbbdbb8c54 100644 --- a/osu.Game/Screens/Multi/Match/MatchScreen.cs +++ b/osu.Game/Screens/Multi/Match/MatchScreen.cs @@ -40,7 +40,7 @@ namespace osu.Game.Screens.Multi.Match private OsuGame game { get; set; } [Resolved(CanBeNull = true)] - private RoomManager manager { get; set; } + private IRoomManager manager { get; set; } public MatchScreen(Room room, Action pushGameplayScreen) { diff --git a/osu.Game/Screens/Multi/Multiplayer.cs b/osu.Game/Screens/Multi/Multiplayer.cs index 116d4137d5..7a8f9f3948 100644 --- a/osu.Game/Screens/Multi/Multiplayer.cs +++ b/osu.Game/Screens/Multi/Multiplayer.cs @@ -31,7 +31,7 @@ namespace osu.Game.Screens.Multi private OsuScreen currentScreen; - [Cached] + [Cached(Type = typeof(IRoomManager))] private RoomManager roomManager; public Multiplayer() diff --git a/osu.Game/Screens/Multi/RoomManager.cs b/osu.Game/Screens/Multi/RoomManager.cs index 17c3229b1a..4bd9307bfb 100644 --- a/osu.Game/Screens/Multi/RoomManager.cs +++ b/osu.Game/Screens/Multi/RoomManager.cs @@ -17,12 +17,12 @@ using osu.Game.Screens.Multi.Lounge.Components; namespace osu.Game.Screens.Multi { - public class RoomManager : PollingComponent + public class RoomManager : PollingComponent, IRoomManager { public event Action OpenRequested; - public IBindableCollection Rooms => rooms; private readonly BindableCollection rooms = new BindableCollection(); + public IBindableCollection Rooms => rooms; private Room currentRoom;