Ejemplo n.º 1
0
 /// <summary>
 /// Changes lobby rules asynchronously
 /// </summary>
 /// <param name="language">Language (optional)</param>
 /// <param name="isLobbyPublic">Is lobby public (optional)</param>
 /// <param name="maximalPlayerCount">Maximal player count (optional)</param>
 /// <param name="drawingTime">Drawing time (optional)</param>
 /// <param name="roundCount">Round count (optional)</param>
 /// <param name="customWords">Custom words (optional)</param>
 /// <param name="customWordsChance">Custom words chance (optional)</param>
 /// <param name="isVotekickingEnabled">Is votekicking enabled (optional)</param>
 /// <param name="clientsPerIPLimit">Clients per IP limit (optional)</param>
 /// <returns>Task</returns>
 public async Task ChangeLobbyRulesAsync(ELanguage? language = null, bool? isLobbyPublic = null, uint? maximalPlayerCount = null, ulong? drawingTime = null, uint? roundCount = null, IReadOnlyList<string> customWords = null, uint? customWordsChance = null, bool? isVotekickingEnabled = null, uint? clientsPerIPLimit = null)
 {
     if ((language != null) && (language == ELanguage.Invalid))
     {
         throw new ArgumentException("Language can't be invalid.", nameof(language));
     }
     if ((maximalPlayerCount != null) && (maximalPlayerCount < 1U))
     {
         throw new ArgumentException("Maximal player count can't be smaller than one.", nameof(maximalPlayerCount));
     }
     if ((drawingTime != null) && (drawingTime < 1U))
     {
         throw new ArgumentException("Drawing time can't be smaller than one.", nameof(drawingTime));
     }
     if ((customWords != null) && !Protection.IsValid(customWords))
     {
         throw new ArgumentException("Custom words can't contain null.", nameof(customWords));
     }
     if ((clientsPerIPLimit != null) && (clientsPerIPLimit < 1U))
     {
         throw new ArgumentException("Clients per IP limit can't be smaller than one.", nameof(clientsPerIPLimit));
     }
     bool are_changes_specified = false;
     StringBuilder parameters_string_builder = new StringBuilder();
     if ((language != null) && (language != ELanguage.Invalid))
     {
         are_changes_specified = true;
         parameters_string_builder.Append($"language={ Uri.EscapeUriString(Naming.GetLanguageString(language.Value)) }");
     }
     if (isLobbyPublic != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"public={ Uri.EscapeUriString(isLobbyPublic.Value.ToString().ToLower()) }");
     }
     if (maximalPlayerCount != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"max_players={ Uri.EscapeUriString(maximalPlayerCount.Value.ToString()) }");
     }
     if (drawingTime != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"drawing_time={ Uri.EscapeUriString(drawingTime.Value.ToString()) }");
     }
     if (roundCount != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"rounds={ Uri.EscapeUriString(roundCount.Value.ToString()) }");
     }
     if (customWords != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"custom_words=");
         bool is_first = true;
         foreach (string custom_word in customWords)
         {
             if (is_first)
             {
                 is_first = false;
             }
             else
             {
                 parameters_string_builder.Append(',');
             }
             parameters_string_builder.Append(Uri.EscapeUriString(roundCount.Value.ToString()));
         }
     }
     if (customWordsChance != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"custom_words_chance={ Uri.EscapeUriString(customWordsChance.Value.ToString()) }");
     }
     if (isVotekickingEnabled != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"enable_votekick={ Uri.EscapeUriString(isVotekickingEnabled.Value.ToString().ToLower()) }");
     }
     if (clientsPerIPLimit != null)
     {
         if (are_changes_specified)
         {
             parameters_string_builder.Append("&");
         }
         else
         {
             are_changes_specified = true;
         }
         parameters_string_builder.Append($"clients_per_ip_limit={ Uri.EscapeUriString(clientsPerIPLimit.Value.ToString()) }");
     }
     if (are_changes_specified)
     {
         bool is_successfull = await SendHTTPPATCHAsync(new Uri(HTTPHostURI, $"/v1/lobby?{ parameters_string_builder }"));
         if (!is_successfull && IsUsingSecureProtocols && IsAllowedToUseInsecureConnections)
         {
             await SendHTTPPATCHAsync(new Uri(InsecureHTTPHostURI, $"/v1/lobby?{ parameters_string_builder }"));
         }
     }
     parameters_string_builder.Clear();
 }