Beispiel #1
0
        public void RemoveUser(ulong discordId)
        {
            EmissaryUser user = dbContext.Users.Find(discordId);

            if (user != null)
            {
                dbContext.Users.Remove(user);
                dbContext.SaveChanges();
            }
        }
Beispiel #2
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)));
        }
        public void AddOrUpdateUser(EmissaryUser user)
        {
            EmissaryUser existingUser = dbContext.Users.Where(u => u.DiscordId == user.DiscordId).FirstOrDefault();

            if (existingUser == null)
            {
                dbContext.Users.Add(user);
            }
            else
            {
                dbContext.Entry(existingUser).CurrentValues.SetValues(user);
            }
            dbContext.SaveChanges();
        }
Beispiel #4
0
        public EmissaryResult RegisterOrReauthorize(ulong discordId, string authCode)
        {
            EmissaryUser existingUser = emissaryDao.GetUserByDiscordId(discordId);

            if (existingUser != null)
            {
                // OAuthRequest refreshOAuthRequest = new OAuthRequest(authCode);
                // OAuthResponse refreshOAuthResponse = bungieApiService.GetOAuthAccessToken(refreshOAuthRequest);
                // if (string.IsNullOrWhiteSpace(refreshOAuthResponse.AccessToken)) {
                //     return EmissaryResult.FromError($"api request failed. {refreshOAuthResponse.ErrorType} ({refreshOAuthResponse.ErrorDescription})");
                // }
                OAuthResponse refreshOAuthResponse = authorizationService.AuthorizeUser(discordId, authCode);
                if (!string.IsNullOrWhiteSpace(refreshOAuthResponse.ErrorType))
                {
                    return(EmissaryResult.FromError($"unable to authorize user (discordId {discordId}). {refreshOAuthResponse.ErrorType}: {refreshOAuthResponse.ErrorDescription}"));
                }
                return(EmissaryResult.FromSuccess("successfully authorized user"));
            }
            EmissaryUser newUser = new EmissaryUser();

            newUser.DiscordId = discordId;
            OAuthResponse oauthResponse = authorizationService.AuthorizeUser(discordId, authCode);

            // OAuthRequest oauthRequest = new OAuthRequest(authCode);
            // OAuthResponse oauthResponse = bungieApiService.GetOAuthAccessToken(oauthRequest);
            if (string.IsNullOrWhiteSpace(oauthResponse.AccessToken))
            {
                return(EmissaryResult.FromError($"api request failed. {oauthResponse.ErrorType} ({oauthResponse.ErrorDescription})"));
            }
            BungieAccessToken newUsersAccessToken = new BungieAccessToken(discordId, oauthResponse.AccessToken, oauthResponse.RefreshToken, oauthResponse.AccessTokenExpiresInSeconds, oauthResponse.RefreshTokenExpiresInSeconds, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow);

            emissaryDao.AddOrUpdateAccessToken(newUsersAccessToken);
            UserMembershipsRequest  membershipsRequest  = new UserMembershipsRequest(newUsersAccessToken.AccessToken);
            UserMembershipsResponse membershipsResponse = bungieApiService.GetMembershipsForUser(membershipsRequest);

            DestinyMembership mainMembership = membershipsResponse.DestinyMemberships[0];

            if (membershipsResponse.DestinyMemberships.Count > 1)
            {
                int crossSaveOverrideType = membershipsResponse.DestinyMemberships[0].CrossSaveOverride;
                mainMembership = membershipsResponse.DestinyMemberships.Where(membership => membership.MembershipType == crossSaveOverrideType).FirstOrDefault();
            }
            newUser.DestinyMembershipType = mainMembership.MembershipType;
            newUser.DestinyProfileId      = mainMembership.DestinyProfileId;

            emissaryDao.AddOrUpdateUser(newUser);
            return(EmissaryResult.FromSuccess(""));
        }
Beispiel #5
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}\""));
        }
Beispiel #6
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)));
        }
Beispiel #7
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);
        }