Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LilaTournament"/> class.
        /// </summary>
        /// <param name="c">The client.</param>
        /// <param name="socket">The socket.</param>
        /// <param name="tournamentData">The tournament data.</param>
        internal LilaTournament(LilaClient c, LilaSocket socket, TournamentData tournamentData)
        {
            tournamentPing = new PPingVersion(0);
            Version        = tournamentData.SocketVersion;

            Client = c;

            this.socket         = socket;
            this.tournamentData = tournamentData;

            socket.OnDisconnect += OnDisconnect;

            socket.AddHandler <MPong>(OnPong);
            socket.AddHandler <MCrowd>(OnCrowd);
            socket.AddHandler <MReload>(OnReload);
            socket.AddHandler <MMessage>(OnMessage);
            socket.AddHandler <MTournamentRedirect>(OnRedirect);
            socket.AddHandler <MTournamentReminder>(OnReminder);
            socket.AddHandler <MResync>(OnResync);
            socket.AddHandler <MChallenges>(OnChallenges);
            socket.AddHandler <MDeployPre>(OnDeployPre);
            socket.AddHandler <MFollowingPlaying>(OnFollowingPlaying);
            socket.AddHandler <MFollowingStoppedPlaying>(OnFollowingStoppedPlaying);
            socket.AddHandler <MFollowingOnlines>(OnFollowingOnline);

            SendPing();
            socket.SchedulePacket(tournamentPing, 1000);
        }
Example #2
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 public void Dispose()
 {
     try
     {
         Pause().Wait(1000);
     }
     finally
     {
         if (socket != null)
         {
             socket.Dispose();
             socket = null;
         }
     }
 }
Example #3
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="finalize"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        public void Dispose(bool finalize)
        {
            _disposing = true;
            if (random != null)
            {
                random.Dispose();
                random = null;
            }

            if (lobbyCon != null)
            {
                lobbyCon.Dispose();
                lobbyCon = null;
            }

            if (challengeCon != null)
            {
                challengeCon.Dispose();
                challengeCon = null;
            }

            if (gameCons != null)
            {
                foreach (KeyValuePair <string, LilaGame> game in gameCons)
                {
                    log.ConditionalDebug("Disconnecting {0}", game.Key);
                    game.Value.Dispose();
                }

                gameCons = null;
            }

            if (tournamentCons != null)
            {
                foreach (KeyValuePair <string, LilaTournament> tournament in tournamentCons)
                {
                    log.ConditionalDebug("Disconnecting {0}", tournament.Key);
                    tournament.Value.Dispose();
                }

                tournamentCons = null;
            }

            if (finalize)
            {
                GC.SuppressFinalize(this);
            }
        }
Example #4
0
        /// <summary>
        /// Joins a tournament.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        internal bool JoinTournament(TournamentData data)
        {
            if (_disposing)
            {
                return(false);
            }

            Uri host = new Uri("wss://socket.lichess.org");

            if (!Uri.TryCreate(string.Format("/tournament/{0}/socket/v2", data.Id), UriKind.Relative, out Uri relative))
            {
                return(false);
            }

            Uri absolute = new Uri(host, relative);

            if (random == null)
            {
                return(false);
            }

            UriBuilder gameBldr = new UriBuilder(absolute)
            {
                Query = string.Format("sri={0}", random.NextSri())
            };

            LilaSocket tournamentCon = new LilaSocket("Tournament-Socket", ResourceType.Thread);

            tournamentCon.AddCookies(lobbyCon.GetCookies());

            if (tournamentCon.Connect(gameBldr.Uri) && !_disposing)
            {
                //Disconnect from lobby
                if (lobbyCon.IsConnected())
                {
                    lobbyCon.Disconnect();
                }

                LilaTournament lilaTournament = new LilaTournament(this, tournamentCon, data);
                tournamentCons.TryAdd(data.Id, lilaTournament);

                Events.FireEventAsync(Events._onTournamentEnter, new TournamentEvent(this, lilaTournament));
                return(true);
            }

            tournamentCon.Dispose();
            return(false);
        }
Example #5
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="finalize"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool finalize)
        {
            lock (disposeLock)
            {
                if (socket != null)
                {
                    socket.Dispose();
                    socket = null;
                }

                if (finalize)
                {
                    GC.SuppressFinalize(this);
                }
            }
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LilaGame"/> class.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <param name="socket">The socket.</param>
        /// <param name="gameData">The game data.</param>
        internal LilaGame(LilaClient c, LilaSocket socket, GameData gameData)
        {
            gamePing = new PPingVersion(0);
            Version  = gameData.Player.Version;
            curClock = new ClockData
            {
                White = gameData.Clock.White,
                Black = gameData.Clock.Black
            };

            Client = c;

            this.socket   = socket;
            this.gameData = gameData;

            socket.OnDisconnect += OnDisconnect;

            socket.AddHandler <MPong>(OnPong);
            socket.AddHandler <MMove>(OnMove);
            socket.AddHandler <MGameMessages>(OnGameEvent);
            socket.AddHandler <MReload>(OnReload);
            socket.AddHandler <MMessage>(OnMessage);
            socket.AddHandler <MEnd>(OnEnd);
            socket.AddHandler <MEndData>(OnEndData);
            socket.AddHandler <MTakebackOffer>(OnTakebackOffer);
            socket.AddHandler <MAck>(OnAck);
            socket.AddHandler <MGone>(OnGone);
            socket.AddHandler <MCrowd>(OnCrowd);
            socket.AddHandler <MClock>(OnClock);
            socket.AddHandler <MChallenges>(OnChallenges);
            socket.AddHandler <MResync>(OnResync);
            socket.AddHandler <MRoundRedirect>(OnRedirect);
            socket.AddHandler <MCheckCount>(OnCheckCount);
            socket.AddHandler <MTournamentReminder>(OnReminder);
            socket.AddHandler <MBerserk>(OnBerserk);
            socket.AddHandler <MClockIncrement>(OnClockInc);
            socket.AddHandler <MTournamentStandings>(OnStandings);
            socket.AddHandler <MDeployPre>(OnDeployPre);

            socket.AddVersionHandler <MVersion>(OnVersion);

            SendPing();
            socket.SchedulePacket(gamePing, 1000);

            random = new Random();
        }
Example #7
0
        /// <summary>
        /// Joins a game.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <returns></returns>
        internal bool JoinGame(GameData game)
        {
            if (_disposing)
            {
                return(false);
            }

            lock (joinLock)
            {
                Uri host = new Uri("wss://socket.lichess.org");
                if (!Uri.TryCreate(game.Url.Socket, UriKind.Relative, out Uri relative))
                {
                    return(false);
                }

                Uri absolute = new Uri(host, relative);
                if (random == null)
                {
                    return(false);
                }

                UriBuilder gameBldr = new UriBuilder(absolute)
                {
                    Query = string.Format("sri={0}", random.NextSri())
                };

                LilaSocket gameCon = new LilaSocket("Game-Socket", ResourceType.Thread);
                gameCon.AddCookies(lobbyCon.GetCookies());
                if (anonymous)
                {
                    gameCon.AddCookies(new Cookie("rk2", game.Url.Socket.Substring(9, 4), "/", "lichess.org")); //Add anoncookie
                }

                if (gameCons.Count == 0 && gameCon.Connect(gameBldr.Uri) && !_disposing)
                {
                    LilaGame lilaGame = new LilaGame(this, gameCon, game);
                    gameCons.TryAdd(game.Url.Socket, lilaGame);

                    Events.FireEventAsync(Events._onJoinGame, new JoinGameEvent(this, lilaGame));
                    return(true);
                }

                gameCon.Dispose();
                return(false);
            }
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LilaClient"/> class.
        /// </summary>
        /// <param name="culture">The culture of the client.</param>
        /// <param name="settings">The settings for the client to use.</param>
        public LilaClient(CultureInfo culture, LilaSettings settings)
        {
            random         = new LilaRandom();
            tournamentCons = new ConcurrentDictionary <string, LilaTournament>();
            gameCons       = new ConcurrentDictionary <string, LilaGame>();

            //#Lobby Connection
            lobbyCon = new LilaSocket("Lobby-Socket", ResourceType.Thread);
            lobbyCon.OnDisconnect += OnLobbyDisconnect;

            //#Challenge Connection
            challengeCon = new LilaSocket("Challenge-Socket", ResourceType.Task);
            challengeCon.OnDisconnect += OnChallengeDisconnect;

            //#Hooks
            joinLock = new object();
            hookLock = new object();
            hooks    = new List <IHook>();

            //#Json
            jsonSettings = new JsonSerializerSettings
            {
                Formatting       = Formatting.None,
                Error            = OnJsonParseError,
                ContractResolver = new PacketResolver()
            };

            //#Packets
            _lobbyPing      = new PP();
            _challengePing  = new PP();
            _challengePing2 = new PPing();
            _gamePing       = new PP();

            //#Lobby Events
            Events = new LilaEvents();
            lobbyCon.AddHandler <MPong>(OnLobbyPong);
            lobbyCon.AddHandler <MServerLatency>(OnLag);
            lobbyCon.AddHandler <MRemovedHooks>(OnHooksRemoved);
            lobbyCon.AddHandler <MNewHook>(OnNewHook);
            lobbyCon.AddHandler <MHookSync>(OnHookSync);
            lobbyCon.AddHandler <MHooks>(OnHooks);
            lobbyCon.AddHandler <MReloadSeeks>(OnReloadSeeks);
            lobbyCon.AddHandler <MRoundRedirect>(OnRedirect);
            lobbyCon.AddHandler <MChallenges>(OnChallenges);
            lobbyCon.AddHandler <MTournaments>(OnTournaments);
            lobbyCon.AddHandler <MSimuls>(OnSimuls);
            lobbyCon.AddHandler <MStreams>(OnStreams);
            lobbyCon.AddHandler <MFeatured>(OnFeatured);
            lobbyCon.AddHandler <MTournamentReminder>(OnReminder);
            lobbyCon.AddHandler <MReloadForum>(OnReloadForum);
            lobbyCon.AddHandler <MReloadTimeline>(OnReloadTimeline);
            lobbyCon.AddHandler <MDeployPre>(OnDeployPre);
            lobbyCon.AddHandler <MFollowingPlaying>(OnFollowingPlaying);
            lobbyCon.AddHandler <MFollowingStoppedPlaying>(OnFollowingStoppedPlaying);
            lobbyCon.AddHandler <MFollowingOnlines>(OnFollowingOnline);

            challengeCon.AddHandler <MPong>(OnChallengePong);
            challengeCon.AddHandler <MChallenges>(OnChallenges);
            challengeCon.AddHandler <MReload>(OnChallengeReload);

            //#Scheduled packets
            lobbyCon.SchedulePacket(_lobbyPing, 1000);
            challengeCon.SchedulePacket(_challengePing, 1000);
            challengeCon.SchedulePacket(_challengePing2, 2000);

            if (culture == null)
            {
                Culture = CultureInfo.CurrentCulture;
            }
            else
            {
                Culture = culture;
            }

            if (settings == null)
            {
                lilaSettings = new LilaSettings();
            }
            else
            {
                lilaSettings = settings;
            }
        }