Пример #1
0
        public EmissaryResult ListLoadouts(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);
            IList <Loadout> loadouts           = emissaryDao.GetAllLoadoutsForUser(discordId).Where(l => l.DestinyCharacterId == destinyCharacterId).ToList();

            return(EmissaryResult.FromSuccess(JsonConvert.SerializeObject(loadouts)));
        }
Пример #2
0
        public EmissaryResult DeleteLoadout(ulong discordId, string loadoutName)
        {
            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);

            loadoutName = loadoutName.Trim();
            Loadout foundLoadout = emissaryDao.GetLoadout(discordId, destinyCharacterId, loadoutName);

            if (foundLoadout == null)
            {
                return(EmissaryResult.FromError($"loadout not found. use `$list` to view all of your saved loadouts"));
            }
            emissaryDao.RemoveLoadout(discordId, destinyCharacterId, loadoutName);
            return(EmissaryResult.FromSuccess($"successfully deleted loadout \"{loadoutName}\""));
        }
Пример #3
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)));
        }
Пример #4
0
        public EmissaryResult EquipLoadout(ulong discordId, string loadoutName)
        {
            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);

            loadoutName = loadoutName.Trim();
            Loadout loadout = emissaryDao.GetLoadout(discordId, destinyCharacterId, loadoutName);

            IList <DestinyItem> itemsToEquip = loadout.Items;

            foreach (DestinyItem item in itemsToEquip.ToList())
            {
                if (item.TierTypeName == "Exotic")
                {
                    itemsToEquip.RemoveAt(itemsToEquip.IndexOf(item));
                    itemsToEquip.Add(item);
                }
            }
            IList <long> itemInstanceIds = itemsToEquip.Select(item => item.ItemInstanceId).ToList();

            string bungieAccessToken = authorizationService.GetAccessToken(discordId);

            if (bungieAccessToken == null)
            {
                RequestAuthorizationEvent?.Invoke(discordId);
                return(EmissaryResult.FromError("i need access to your bungie account to do this. please check your DMs for instructions"));
            }
            EquipItemsRequest equipRequest = new EquipItemsRequest(bungieAccessToken, user.DestinyMembershipType, destinyCharacterId, itemInstanceIds);

            EquipItemsResponse equipResponse;

            try {
                equipResponse = bungieApiService.EquipItems(equipRequest);
            } catch (BungieApiException e) {
                string errorMessage = e.Message;
                if (errorMessage.Contains("Unauthorized"))
                {
                    RequestAuthorizationEvent?.Invoke(discordId);
                    errorMessage += " please check your DMs for instructions";
                }
                return(EmissaryResult.FromError(errorMessage));
            }

            // TODO equip exotics last.
            // otherwise we might get the DestinyItemUniqueEquipRestricted error, which is annoying
            // because i have to tell the user to try again or do a thread.sleep(0.1sec) which is a big waste.
            // instead, adjust the order of the equip request so that exotics are very last. that way the
            // purple legendaries are equipped first (replacing any exotics in that slot)
            // and we won't have an exotic equip limit error.

            EmissaryResult result;

            if (equipResponse.EquipResults.All(equipResult => equipResult.EquipStatus == BungiePlatformErrorCodes.Success))
            {
                result = EmissaryResult.FromSuccess(JsonConvert.SerializeObject(loadout));
            }
            else
            {
                string errorMessage = "some items could not be equipped.";
                foreach (EquipItemResult equipResult in equipResponse.EquipResults)
                {
                    if (equipResult.EquipStatus != BungiePlatformErrorCodes.Success)
                    {
                        errorMessage += $"\n{GetErrorDescriptionHelpMessageForEquipFail(equipResult.EquipStatus)}";
                    }
                }
                result = EmissaryResult.FromError(errorMessage);
            }
            return(result);
        }