/// <summary> /// Creates a new lobby with URIs asynchronously /// </summary> /// <param name="httpHostURI">HTTP host URI</param> /// <param name="webSocketHostURI">WebSocket host URI</param> /// <param name="isConnectionSecure">Is connection secure</param> /// <param name="username">Username</param> /// <param name="language">Language</param> /// <param name="isLobbyPublic">Is lobby public</param> /// <param name="maximalPlayerCount">Maximal player count</param> /// <param name="drawingTime">Drawing time</param> /// <param name="roundCount">Round count</param> /// <param name="customWordsString">Custom words string</param> /// <param name="customWordsChance">Custom words chance</param> /// <param name="isVotekickingEnabled">Is votekicking enabled</param> /// <param name="clientsPerIPLimit">Clients per IP limit</param> /// <returns>Lobby task</returns> private async Task<ILobby> CreateLobbyWithURIsAsync(Uri httpHostURI, Uri webSocketHostURI, bool isConnectionSecure, string username, ELanguage language, bool isLobbyPublic, uint maximalPlayerCount, ulong drawingTime, uint roundCount, string customWordsString, uint customWordsChance, bool isVotekickingEnabled, uint clientsPerIPLimit) { ILobby ret = null; ResponseWithUserSessionCookie<CreateLobbyResponseData> response_with_user_session_cookie = await SendHTTPPostRequestAsync<CreateLobbyResponseData>(new Uri(httpHostURI, "/v1/lobby"), new Dictionary<string, string> { { "username", username }, { "language", Naming.GetLanguageString(language) }, { "public", isLobbyPublic.ToString().ToLower() }, { "max_players", maximalPlayerCount.ToString() }, { "drawing_time", drawingTime.ToString() }, { "rounds", roundCount.ToString() }, { "custom_words", customWordsString }, { "custom_words_chance", customWordsChance.ToString() }, { "enable_votekick", isVotekickingEnabled.ToString().ToLower() }, { "clients_per_ip_limit", clientsPerIPLimit.ToString() } }); CreateLobbyResponseData response = response_with_user_session_cookie.Response; if ((response != null) && response.IsValid) { ClientWebSocket client_web_socket = new ClientWebSocket(); client_web_socket.Options.Cookies = cookieContainer; await client_web_socket.ConnectAsync(new Uri(webSocketHostURI, $"/v1/ws?lobby_id={ Uri.EscapeUriString(response.LobbyID) }"), default); if (client_web_socket.State == WebSocketState.Open) { ret = new Lobby ( client_web_socket, isConnectionSecure, response.LobbyID, response.MinimalDrawingTime, response.MaximalDrawingTime, response.MinimalRoundCount, response.MaximalRoundCount, response.MinimalMaximalPlayerCount, response.MaximalMaximalPlayerCount, response.MinimalClientsPerIPLimit, response.MaximalClientsPerIPLimit, response.MaximalPlayerCount, response.CurrentMaximalRoundCount, response.IsLobbyPublic, response.IsVotekickingEnabled, response.CustomWordsChance, response.AllowedClientsPerIPCount, response.DrawingBoardBaseWidth, response.DrawingBoardBaseHeight, response.MinimalBrushSize, response.MaximalBrushSize, response.SuggestedBrushSizes, (Color)response.CanvasColor ); } else { client_web_socket.Dispose(); } } return ret; }
/// <summary> /// Enters a lobby with URIs asynchronously /// </summary> /// <param name="httpHostURI">HTTP host URI</param> /// <param name="webSocketHostURI">WebSocket host URI</param> /// <param name="isConnectionSecure">Is connection secure</param> /// <param name="lobbyID">Lobby ID</param> /// <param name="username">Username</param> /// <returns>Lobby task</returns> private async Task<ILobby> EnterLobbyWithURIsAsync(Uri httpHostURI, Uri webSocketHostURI, bool isConnectionSecure, string lobbyID, string username) { ILobby ret = null; ResponseWithUserSessionCookie<EnterLobbyResponseData> response_with_user_session_cookie = await SendHTTPPostRequestAsync<EnterLobbyResponseData>(new Uri(httpHostURI, $"/v1/lobby/player?lobby_id={ Uri.EscapeUriString(lobbyID) }"), new Dictionary<string, string> { { "lobby_id", lobbyID }, { "username", username } }); EnterLobbyResponseData response = response_with_user_session_cookie.Response; if ((response != null) && response.IsValid) { ClientWebSocket client_web_socket = new ClientWebSocket(); client_web_socket.Options.Cookies = cookieContainer; await client_web_socket.ConnectAsync(new Uri(webSocketHostURI, $"/v1/ws?lobby_id={ Uri.EscapeUriString(response.LobbyID) }"), default); if (client_web_socket.State == WebSocketState.Open) { ret = new Lobby ( client_web_socket, isConnectionSecure, response.LobbyID, response.MinimalDrawingTime, response.MaximalDrawingTime, response.MinimalRoundCount, response.MaximalRoundCount, response.MinimalMaximalPlayerCount, response.MaximalMaximalPlayerCount, response.MinimalClientsPerIPLimit, response.MaximalClientsPerIPLimit, response.MaximalPlayerCount, response.CurrentMaximalRoundCount, response.IsLobbyPublic, response.IsVotekickingEnabled, response.CustomWordsChance, response.AllowedClientsPerIPCount, response.DrawingBoardBaseWidth, response.DrawingBoardBaseHeight, response.MinimalBrushSize, response.MaximalBrushSize, response.SuggestedBrushSizes, (Color)response.CanvasColor ); } else { client_web_socket.Dispose(); } } return ret; }
/// <summary> /// Sends a HTTP POST request asynchronously /// </summary> /// <param name="requestURI">Request URI</param> /// <param name="parameters">Parameters</param> /// <returns>Response if successful, otherwise "default"</returns> private async Task<ResponseWithUserSessionCookie<T>> SendHTTPPostRequestAsync<T>(Uri requestURI, IReadOnlyDictionary<string, string> parameters) where T : IResponseData { ResponseWithUserSessionCookie<T> ret = default; string user_session_cookie = string.Empty; try { using (HttpResponseMessage response = await httpClient.PostAsync(requestURI, new FormUrlEncodedContent(parameters))) { if (response.StatusCode == HttpStatusCode.OK) { ret = new ResponseWithUserSessionCookie<T>(JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync()), user_session_cookie); } else { Console.Error.WriteLine(await response.Content.ReadAsStringAsync()); } } } catch (Exception e) { Console.Error.WriteLine(e); } return ret; }