// TODO finalize and test this public EmissaryResult SaveCurrentlyEquippedAsLoadout(ulong discordId, string loadoutName) { EmissaryResult currentlyEquippedResult = CurrentlyEquipped(discordId); if (!currentlyEquippedResult.Success) { return(currentlyEquippedResult); } Loadout loadoutToSave = JsonConvert.DeserializeObject <Loadout>(currentlyEquippedResult.Message); return(SaveLoadout(discordId, loadoutToSave, loadoutName)); }
public void AddOrUpdateLoadout(Loadout loadout) { Loadout existingLoadout = dbContext.Loadouts.Where(l => l.LoadoutName == loadout.LoadoutName && l.DiscordId == loadout.DiscordId && l.DestinyCharacterId == loadout.DestinyCharacterId).AsQueryable().FirstOrDefault(); if (existingLoadout == null) { dbContext.Loadouts.Add(loadout); } else { dbContext.Entry(existingLoadout).CurrentValues.SetValues(loadout); } dbContext.SaveChanges(); }
// TODO should this be private? idk public EmissaryResult SaveLoadout(ulong discordId, Loadout loadout, string loadoutName) { loadout.DiscordId = discordId; loadout.LoadoutName = loadoutName; try { int maxLoadoutLimit = 25; if (emissaryDao.GetAllLoadoutsForUser(discordId).Count >= maxLoadoutLimit) { return(EmissaryResult.FromError($"you've reached the max loadout limit ({maxLoadoutLimit}). please delete or overwrite an existing loadouts in order to save this loadout")); } emissaryDao.AddOrUpdateLoadout(loadout); return(EmissaryResult.FromSuccess(JsonConvert.SerializeObject(loadout))); } catch (Exception e) { return(EmissaryResult.FromError(e.Message)); } }
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}\"")); }
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))); }
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); }
public Loadout GetLoadout(ulong discordId, long destinyCharacterId, string loadoutName) { Loadout foundLoadout = dbContext.Loadouts.Find(discordId, destinyCharacterId, loadoutName); return(foundLoadout); }