예제 #1
0
        public CharacterEquipmentResponse GetCharacterEquipment(CharacterEquipmentRequest equipmentRequest)
        {
            HttpRequestMessage request = new HttpRequestMessage();

            request.Method     = HttpMethod.Get;
            request.RequestUri = new Uri($"https://www.bungie.net/Platform/Destiny2/{equipmentRequest.DestinyMembershipType}/Profile/{equipmentRequest.DestinyProfileId}/Character/{equipmentRequest.DestinyCharacterId}/?components=205");
            request.Headers.Add("X-API-KEY", config["Bungie:ApiKey"]);
            HttpResponseMessage response = httpClient.SendAsync(request).Result;

            if (response.IsSuccessStatusCode)
            {
                string json = response.Content.ReadAsStringAsync().Result;
                CharacterEquipmentResponse equipmentResponse = JsonConvert.DeserializeObject <CharacterEquipmentResponse>(json);
                if (equipmentResponse == null || equipmentResponse.Items == null || equipmentResponse.Items.Count <= 0)
                {
                    throw new BungieApiException("character not found: Account exists but could not get equipment for the given character.");
                }
                return(equipmentResponse);
            }
            else if (response.StatusCode == HttpStatusCode.BadRequest || response.StatusCode == HttpStatusCode.InternalServerError)
            {
                dynamic errorResponse = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                throw new BungieApiException($"{errorResponse.ErrorStatus}: {errorResponse.Message}");
            }
            else
            {
                throw new NotImplementedException();
            }
        }
예제 #2
0
        public EmissaryResult CurrentlyEquipped(ulong discordId)
        {
            EmissaryUser user = emissaryDao.GetUserByDiscordId(discordId);

            if (user == null)
            {
                RequestAuthorizationEvent?.Invoke(discordId);
                return(EmissaryResult.FromError("i need access to your bungie account to do this. please check your DMs for instructions"));
            }
            long destinyCharacterId = GetMostRecentlyPlayedCharacterId(user.DestinyMembershipType, user.DestinyProfileId);
            CharacterEquipmentRequest  equipmentRequest  = new CharacterEquipmentRequest(user.DestinyMembershipType, user.DestinyProfileId, destinyCharacterId);
            CharacterEquipmentResponse equipmentResponse = bungieApiService.GetCharacterEquipment(equipmentRequest);
            Loadout currentlyEquipped = new Loadout();

            currentlyEquipped.DiscordId          = user.DiscordId;
            currentlyEquipped.DestinyCharacterId = destinyCharacterId;
            currentlyEquipped.LoadoutName        = "unsaved loadout";
            currentlyEquipped.Items = equipmentResponse.Items
                                      .Select(genericItem => CreateDestinyItemFromGenericItem(genericItem))
                                      .Where(item => ItemIsWeaponOrArmor(item))
                                      .ToList();
            IList <Loadout> savedLoadouts = emissaryDao.GetAllLoadoutsForUser(discordId);

            if (savedLoadouts != null && savedLoadouts.Count > 0)
            {
                foreach (Loadout savedLoadout in savedLoadouts)
                {
                    bool loadoutsAreEqual = currentlyEquipped.DestinyCharacterId == savedLoadout.DestinyCharacterId &&
                                            currentlyEquipped.Items.Count == savedLoadout.Items.Count &&
                                            currentlyEquipped.Items.All(savedLoadout.Items.Contains);
                    if (loadoutsAreEqual)
                    {
                        currentlyEquipped.LoadoutName = savedLoadout.LoadoutName;
                        break;
                    }
                }
            }
            return(EmissaryResult.FromSuccess(JsonConvert.SerializeObject(currentlyEquipped)));
        }