/// <summary> /// Connects the lobby internally /// </summary> private void ConnectLobbyInternal() { if (_disposing) { return; } Uri host = new Uri("wss://socket.lichess.org"); Uri relative = new Uri(LilaRoutes.LobbySocket, UriKind.Relative); Uri absolute = new Uri(host, relative); UriBuilder lobbyBldr = new UriBuilder(absolute) { Query = string.Format("sri={0}", LobbySri = random.NextSri()) }; //int port = LilaPing.PingServer(9025, 9029, 1); //lobbyBldr.Port = port == -1 ? 9025 : port; if (lobbyCon != null && lobbyCon.Connect(lobbyBldr.Uri)) { Events.FireEventAsync(Events._onConnect, new ConnectEvent(this)); lobbyCon.Send(new PHookIn()); lobbyCon.Send(new PServerLatency(true)); } }
/// <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); }
/// <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); } }
/// <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); }