Пример #1
0
 /// <summary>
 /// Checks if the specified game is currently supported
 /// </summary>
 /// <param name="game">Game to check</param>
 private void CheckGame(SupportedBattlelogGame game)
 {
     if (Enum.IsDefined(typeof(SupportedBattlelogGame), game) == false)
     {
         throw new NotSupportedException($"The game { game } ({ (int)game }) is currently not supported!");
     }
 }
Пример #2
0
        /// <summary>
        /// Removes the reservation for the given server
        /// </summary>
        /// <param name="gameId">GameId of server</param>
        /// <param name="game">Game to which the gameId belongs to</param>
        public async Task LeaveServerAsync(long gameId, SupportedBattlelogGame game)
        {
            // Check that the specified game is really supported
            this.CheckGame(game);

            // Check that a session was created
            this.CheckSession();

            using (HttpClient client = this.CreateHttpClient())
            {
                // Create the full url
                string url = $"http://battlelog.battlefield.com/{ game.ToString().ToLower() }/launcher/mpleavegameserver/1/{ this._personaId }/{ gameId }";

                // Create the data that should be posted to the given url
                FormUrlEncodedContent data = new FormUrlEncodedContent(new Dictionary <string, string>
                {
                    { "post-check-sum", this._postChecksum },
                });

                // Remove reservation
                _ = await client.PostAsync(url, data);
            }
        }
Пример #3
0
        /// <summary>
        /// Reserves a slot on the given server
        /// </summary>
        /// <param name="gameId">GameId of server</param>
        /// <param name="game">Game to which the gameId belongs to</param>
        public async Task <string> EnterServerAsync(long gameId, SupportedBattlelogGame game)
        {
            string result;

            // Check that the specified game is really supported
            this.CheckGame(game);

            // Check that a session was created
            this.CheckSession();

            using (HttpClient client = this.CreateHttpClient())
            {
                // Create the full url
                string url = $"http://battlelog.battlefield.com/{ game.ToString().ToLower() }/launcher/reserveslotbygameid/1/{ this._personaId }/{ gameId }/1//0";

                // Create the data that should be posted to the given url
                FormUrlEncodedContent data = new FormUrlEncodedContent(new Dictionary <string, string>
                {
                    { "post-check-sum", this._postChecksum },
                });

                // Reserve a slot on the server
                HttpResponseMessage response = await client.PostAsync(url, data);

                // Read the response body
                // It contains some info about the join state
                string content = await response.Content.ReadAsStringAsync();

                // Read the state from response
                string joinState = ((JsonConvert.DeserializeObject(content) as JObject)?["data"]?["joinState"] as JValue)?.Value?.ToString();

                // Set the state (or if there is no the whole content) as result
                result = string.IsNullOrWhiteSpace(joinState) ? content : joinState;
            }

            return(result);
        }