2020-12-19 00:15:41 +09: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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Bindables;
|
2021-12-10 17:22:24 +09:00
|
|
|
using osu.Framework.Development;
|
2020-12-19 00:15:41 +09:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Logging;
|
2020-12-25 13:38:11 +09:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-19 00:15:41 +09:00
|
|
|
|
2020-12-25 16:50:00 +01:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Components
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2025-01-23 17:18:01 +09:00
|
|
|
// Todo: This class should be inlined into the lounge.
|
2021-08-13 17:39:09 +09:00
|
|
|
public partial class RoomManager : Component, IRoomManager
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2024-11-13 16:28:39 +09:00
|
|
|
public event Action? RoomsUpdated;
|
2020-12-19 00:15:41 +09:00
|
|
|
|
|
|
|
private readonly BindableList<Room> rooms = new BindableList<Room>();
|
|
|
|
|
|
|
|
public IBindableList<Room> Rooms => rooms;
|
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
public RoomManager()
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
}
|
|
|
|
|
2021-02-16 19:29:40 +09:00
|
|
|
private readonly HashSet<long> ignoredRooms = new HashSet<long>();
|
2020-12-19 00:15:41 +09:00
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
public void AddOrUpdateRoom(Room room)
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2021-12-10 17:22:24 +09:00
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
2024-11-13 16:28:39 +09:00
|
|
|
Debug.Assert(room.RoomID != null);
|
2021-08-13 17:39:09 +09:00
|
|
|
|
2024-11-13 16:28:39 +09:00
|
|
|
if (ignoredRooms.Contains(room.RoomID.Value))
|
2020-12-20 23:05:17 +09:00
|
|
|
return;
|
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
try
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2024-11-13 16:28:39 +09:00
|
|
|
var existing = rooms.FirstOrDefault(e => e.RoomID == room.RoomID);
|
2021-08-16 13:04:06 +09:00
|
|
|
if (existing == null)
|
|
|
|
rooms.Add(room);
|
|
|
|
else
|
|
|
|
existing.CopyFrom(room);
|
2020-12-19 00:15:41 +09:00
|
|
|
}
|
2021-08-13 17:39:09 +09:00
|
|
|
catch (Exception ex)
|
2020-12-19 00:15:41 +09:00
|
|
|
{
|
2024-11-13 16:55:18 +09:00
|
|
|
Logger.Error(ex, $"Failed to update room: {room.Name}.");
|
2020-12-19 00:15:41 +09:00
|
|
|
|
2024-11-13 16:28:39 +09:00
|
|
|
ignoredRooms.Add(room.RoomID.Value);
|
2021-08-13 17:39:09 +09:00
|
|
|
rooms.Remove(room);
|
2020-12-19 00:15:41 +09:00
|
|
|
}
|
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
notifyRoomsUpdated();
|
2020-12-19 00:15:41 +09:00
|
|
|
}
|
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
public void RemoveRoom(Room room)
|
|
|
|
{
|
2021-12-10 17:22:24 +09:00
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
rooms.Remove(room);
|
|
|
|
notifyRoomsUpdated();
|
|
|
|
}
|
2020-12-19 02:02:04 +09:00
|
|
|
|
2021-08-13 17:39:09 +09:00
|
|
|
public void ClearRooms()
|
2020-12-19 01:01:09 +09:00
|
|
|
{
|
2021-12-10 17:22:24 +09:00
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
|
|
|
|
2020-12-19 01:01:09 +09:00
|
|
|
rooms.Clear();
|
2021-08-13 17:39:09 +09:00
|
|
|
notifyRoomsUpdated();
|
2020-12-19 01:01:09 +09:00
|
|
|
}
|
|
|
|
|
2021-10-14 17:52:19 +09:00
|
|
|
private void notifyRoomsUpdated()
|
|
|
|
{
|
|
|
|
Scheduler.AddOnce(invokeRoomsUpdated);
|
|
|
|
|
|
|
|
void invokeRoomsUpdated() => RoomsUpdated?.Invoke();
|
|
|
|
}
|
2020-12-19 00:15:41 +09:00
|
|
|
}
|
|
|
|
}
|