Exemple #1
0
        /// <summary>
        /// Creates a seek.
        /// </summary>
        /// <param name="time">The time.</param>
        /// <param name="increment">The increment.</param>
        /// <param name="ratingLow">The rating low.</param>
        /// <param name="ratingHigh">The rating high.</param>
        /// <returns></returns>
        public async Task <SeekResponse> CreateSeek(int time, int increment, int ratingLow, int ratingHigh)
        {
            //TODO: Change for anonymous player requests

            LilaRequest setupSeek = new LilaRequest(new Uri(string.Format("/setup/hook/{0}", LobbySri), UriKind.Relative));

            setupSeek.Cookies.Add(lobbyCon.GetCookies());

            double timeMinutes = time / 60000.0;
            string ratingRange = string.Format("{0}-{1}", ratingLow, ratingHigh);

            string[] keys   = new string[] { "variant", "timeMode", "time", "increment", "days", "mode", "ratingRange", "color" };
            object[] values = new object[] { 1, 1, timeMinutes, increment, 2, 1, ratingRange, "white" };

            LilaResponse res = await setupSeek.Post(LilaRequest.ContentType.Any, keys, values);

            if (res == null || !res.CheckStatus(HttpStatusCode.OK | HttpStatusCode.SeeOther))
            {
                return(null);
            }

            SeekResponse sr = res.ReadJson <SeekResponse>();

            return(sr);
        }
Exemple #2
0
        /// <summary>
        /// Called when seeks are reloaded.
        /// </summary>
        /// <param name="ws">The websocket.</param>
        /// <param name="message">The message.</param>
        private void OnReloadSeeks(WebSocketBase ws, MReloadSeeks message)
        {
            LilaRequest lr = new LilaRequest(new Uri(LilaRoutes.LobbySeeks, UriKind.Relative), Culture);

            lr.Cookies.Add(lobbyCon.GetCookies());
            lr.Get(LilaRequest.ContentType.Json).ContinueWith(SetHooks);
        }
Exemple #3
0
        /// <summary>
        /// Logs in as anonymous player.
        /// </summary>
        public void Login()
        {
            anonymous = true;
            LilaRequest         req    = new LilaRequest(new Uri(LilaRoutes.Lobby, UriKind.Relative), Culture);
            Task <LilaResponse> result = req.Get(LilaRequest.ContentType.Html);

            result.ContinueWith(HandleAuth);
        }
Exemple #4
0
        /// <summary>
        /// Pauses or withdraws the client from the tournament.
        /// </summary>
        public async Task <bool> Pause()
        {
            LilaRequest joinReq = new LilaRequest(new Uri(string.Format("/tournament/{0}/withdraw", tournamentData.Id), UriKind.Relative));

            joinReq.Cookies.Add(socket.GetCookies());

            //TODO: Add password
            //p is password
            string       str      = "{\"p\":null}";
            LilaResponse pauseRes = await joinReq.Post(LilaRequest.ContentType.Json, str);

            bool success = pauseRes != null && pauseRes.CheckStatus(HttpStatusCode.OK | HttpStatusCode.SeeOther);

            return(success);
        }
Exemple #5
0
        /// <summary>
        /// Gets the round.
        /// </summary>
        /// <param name="location">The location.</param>
        /// <returns></returns>
        private async Task <Round> GetRound(string location)
        {
            if (_disposing)
            {
                return(null);
            }

            LilaRequest roundReq = new LilaRequest(new Uri(location, UriKind.Relative), Culture);

            roundReq.Cookies.Add(lobbyCon.GetCookies());
            if (anonymous)
            {
                roundReq.Cookies.Add(new Cookie("rk2", location.Substring(9), "/", "lichess.org")); //add anoncookie
            }

            LilaResponse roundRes = await roundReq.Get(LilaRequest.ContentType.Html);

            if (roundRes == null)
            {
                log.Error("Failed to join round at {0}", location);
                return(null);
            }

            string html = await roundRes.ReadAsync();

            const string id = "LichessRound.boot(";

            int roundBootIndex = html.IndexOf(id);

            if (roundBootIndex == -1)
            {
                return(null);
            }

            string json = StringEngine.GetInside(html, roundBootIndex + id.Length);

            if (json == null)
            {
                log.Error("Failed to parse game json data.");
                return(null);
            }

            return(JsonConvert.DeserializeObject <Round>(json, jsonSettings));
        }
Exemple #6
0
        /// <summary>
        /// Challenges a player.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="form">The form.</param>
        /// <returns></returns>
        public async Task <bool> ChallengePlayer(string username, GameForm form)
        {
            if (_disposing || challengeCon == null)
            {
                return(false);
            }

            LilaRequest setupFriend = new LilaRequest(new Uri(string.Format("/setup/friend?user={0}", username.ToLower()), UriKind.Relative), Culture);

            setupFriend.Cookies.Add(lobbyCon.GetCookies());

            string[] keys   = new string[] { "variant", "fen", "timeMode", "time", "increment", "days", "mode", "color" };
            object[] values = new object[] { form.Variant, form.Fen, form.TimeMode, form.Time, form.Increment, form.Days, form.Mode, form.Color };

            LilaResponse res = await setupFriend.Post(LilaRequest.ContentType.Any, keys, values);

            if (res == null || !res.CheckStatus(HttpStatusCode.OK | HttpStatusCode.SeeOther))
            {
                return(false);
            }

            string loc = (challengeLocation = res.GetHeader("Location")).Trim('/');

            Uri host     = new Uri("wss://socket.lichess.org");
            Uri relative = new Uri(string.Format("/challenge/{0}/socket/v2", loc), UriKind.Relative);
            Uri absolute = new Uri(host, relative);

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

            //int port = LilaPing.PingServer(9025, 9029, 1);
            //challengeBldr.Port = port == -1 ? 9025 : port;

            challengeCon.AddCookies(lobbyCon.GetCookies());
            if (challengeCon.Connect(challengeBldr.Uri))
            {
                log.ConditionalDebug("Connected to challenge socket. Awaiting reload message.");
                return(true);
            }

            return(false);
        }
Exemple #7
0
        /// <summary>
        /// Joins the tournament (starts playing).
        /// </summary>
        public async Task <bool> Join()
        {
            LilaRequest joinReq = new LilaRequest(new Uri(string.Format("/tournament/{0}/join", tournamentData.Id), UriKind.Relative));

            joinReq.Cookies.Add(socket.GetCookies());

            //p is password
            string       str     = "{\"p\":null}";
            LilaResponse joinRes = await joinReq.Post(LilaRequest.ContentType.Json, str);

            bool success = joinRes != null && joinRes.CheckStatus(HttpStatusCode.OK | HttpStatusCode.SeeOther);

            if (success)
            {
                Client.Events.FireEventAsync(Client.Events._onTournamentJoin, new TournamentEvent(Client, this));
                return(true);
            }

            return(false);
        }
Exemple #8
0
        /// <summary>
        /// Gets the tournament.
        /// </summary>
        /// <param name="tournamentId">The tournament identifier.</param>
        /// <returns></returns>
        private async Task <Tournament> GetTournament(string tournamentId)
        {
            if (_disposing)
            {
                return(null);
            }

            LilaRequest tournamentReq = new LilaRequest(new Uri(string.Format("/tournament/{0}", tournamentId), UriKind.Relative), Culture);

            tournamentReq.Cookies.Add(lobbyCon.GetCookies());

            LilaResponse roundRes = await tournamentReq.Get(LilaRequest.ContentType.Html);

            if (roundRes == null)
            {
                log.Error("Failed to join tournament at {0}", tournamentId);
                return(null);
            }

            string html = await roundRes.ReadAsync();

            const string id = "lichess.tournament = ";

            int roundBootIndex = html.IndexOf(id);

            if (roundBootIndex == -1)
            {
                return(null);
            }

            string json = StringEngine.GetInside(html, roundBootIndex + id.Length);

            if (json == null)
            {
                log.Error("Failed to parse tournament json data.");
                return(null);
            }

            return(JsonConvert.DeserializeObject <Tournament>(json, jsonSettings));
        }
Exemple #9
0
        /// <summary>
        /// Logs in as the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        public void Login(string username, string password)
        {
            if (_disposing)
            {
                return;
            }

            anonymous = false;
            LilaRequest         hpReq     = new LilaRequest(new Uri(LilaRoutes.Lobby, UriKind.Relative), Culture);
            Task <LilaResponse> hpResTask = hpReq.Get(LilaRequest.ContentType.Any);

            hpResTask.Wait();

            LilaRequest req = new LilaRequest(new Uri(LilaRoutes.Login, UriKind.Relative), Culture);

            if (hpResTask.Result != null)
            {
                req.Cookies.Add(hpResTask.Result.GetCookies());
            }

            Task <LilaResponse> result = req.Post(LilaRequest.ContentType.Html, new string[] { "username", "password" }, new string[] { username, password });

            result.ContinueWith(HandleAuth);
        }