<commit_before>// Copyright (c) 2007-2018 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE

using System;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Multiplayer;
using osu.Game.Rulesets;
using osu.Game.Screens.Multi.Lounge.Components;

namespace osu.Game.Screens.Multi
{
    public class RoomManager : PollingComponent, IRoomManager
    {
        public event Action<Room> RoomJoined;

        private readonly BindableCollection<Room> rooms = new BindableCollection<Room>();
        public IBindableCollection<Room> Rooms => rooms;

        private Room currentRoom;

        private FilterCriteria currentFilter = new FilterCriteria();

        [Resolved]
        private APIAccess api { get; set; }

        [Resolved]
        private RulesetStore rulesets { get; set; }

        [Resolved]
        private BeatmapManager beatmaps { get; set; }

        public RoomManager()
        {
            TimeBetweenPolls = 5000;
        }

        protected override void Dispose(bool isDisposing)
        {
            base.Dispose(isDisposing);
            PartRoom();
        }

        public void CreateRoom(Room room)
        {
            room.Host.Value = api.LocalUser;

            var req = new CreateRoomRequest(room);
            req.Success += result => addRoom(room, result);
            req.Failure += exception => Logger.Log($"Failed to create room: {exception}");

            api.Queue(req);
        }

        private JoinRoomRequest currentJoinRoomRequest;

        public void JoinRoom(Room room)
        {
            currentJoinRoomRequest?.Cancel();
            currentJoinRoomRequest = null;

            currentJoinRoomRequest = new JoinRoomRequest(room, api.LocalUser.Value);
            currentJoinRoomRequest.Success += () =>
            {
                currentRoom = room;
                RoomJoined?.Invoke(room);
            };

            currentJoinRoomRequest.Failure += exception => Logger.Log($"Failed to join room: {exception}");

            api.Queue(currentJoinRoomRequest);
        }

        public void PartRoom()
        {
            if (currentRoom == null)
                return;<commit_msg>Display room join errors as notifications<commit_after>// Copyright (c) 2007-2018 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE

using System;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Multiplayer;
using osu.Game.Rulesets;
using osu.Game.Screens.Multi.Lounge.Components;

namespace osu.Game.Screens.Multi
{
    public class RoomManager : PollingComponent, IRoomManager
    {
        public event Action<Room> RoomJoined;

        private readonly BindableCollection<Room> rooms = new BindableCollection<Room>();
        public IBindableCollection<Room> Rooms => rooms;

        private Room currentRoom;

        private FilterCriteria currentFilter = new FilterCriteria();

        [Resolved]
        private APIAccess api { get; set; }

        [Resolved]
        private RulesetStore rulesets { get; set; }

        [Resolved]
        private BeatmapManager beatmaps { get; set; }

        public RoomManager()
        {
            TimeBetweenPolls = 5000;
        }

        protected override void Dispose(bool isDisposing)
        {
            base.Dispose(isDisposing);
            PartRoom();
        }

        public void CreateRoom(Room room)
        {
            room.Host.Value = api.LocalUser;

            var req = new CreateRoomRequest(room);
            req.Success += result => addRoom(room, result);
            req.Failure += exception => Logger.Log($"Failed to create room: {exception}");

            api.Queue(req);
        }

        private JoinRoomRequest currentJoinRoomRequest;

        public void JoinRoom(Room room)
        {
            currentJoinRoomRequest?.Cancel();
            currentJoinRoomRequest = null;

            currentJoinRoomRequest = new JoinRoomRequest(room, api.LocalUser.Value);
            currentJoinRoomRequest.Success += () =>
            {
                currentRoom = room;
                RoomJoined?.Invoke(room);
            };

            currentJoinRoomRequest.Failure += exception => Logger.Log($"Failed to join room: {exception}", level: LogLevel.Important);

            api.Queue(currentJoinRoomRequest);
        }

        public void PartRoom()
        {
            if (currentRoom == null)
                return;
{'fraction_non_alphanumeric': 0.0957683741648107, 'fraction_numerical': 0.004859283255719781, 'mean_word_length': 2.299933199732799, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1197293', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1305, 'n_words': 312}