public static async Task SelectiveUpdate(Clan clan, Changes changes, List <DiscordMessage> modifyMessages, Action <List <DiscordMessage>, TimeSpan, Changes> modifyMethod) { await Read(clan); int size = (int)MathF.Ceiling((float)changes.addedUsers.Count / 5); TimeSpan[] durations = new TimeSpan[size]; var workingList = clan.members.BungieUsers; int index = 0; foreach (User addedUser in changes.addedUsers) { var startTime = DateTime.UtcNow; var workingUser = new User(); if (!string.IsNullOrEmpty(addedUser.BungieProfile)) { var a = await BungieTools.GetBungieUserID(addedUser.BungieProfile); workingUser.BungieID = a.ToString(); if (!string.IsNullOrEmpty(workingUser.BungieID)) { string bungieProfile = addedUser.BungieProfile; string bungieID = workingUser.BungieID; UserStatus.StatusEnum userStatus = addedUser.UserStatus; GeneralUser bungieUser = await BungieTools.GetBungieUser(Convert.ToInt64(bungieID)); string steamID = SteamTools.GetSteamID(bungieProfile); string steamProfile = "https://steamcommunity.com/profiles/" + steamID; string bungieName = bungieUser.displayName; string steamName = SteamTools.GetSteamUserName(steamID); string discordID = addedUser.DiscordID; string userClanTag = addedUser.ClanTag; workingUser = new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, userClanTag); workingList.Add(workingUser); } else { List <string> extraColumns = new List <string>(); string bungieProfile = addedUser.BungieProfile; string bungieID = "N/A"; string steamID = "N/A"; string steamProfile = "N/A"; string bungieName = "N/A"; string steamName = "N/A"; string discordID = addedUser.DiscordID; if (addedUser.BungieID != "N/A" || addedUser.BungieID != "") { bungieID = addedUser.BungieID; } if (addedUser.SteamID != "N/A" || addedUser.SteamID != "") { steamID = addedUser.SteamID; } if (addedUser.SteamProfile != "N/A" || addedUser.SteamProfile != "") { steamProfile = addedUser.SteamProfile; } if (addedUser.BungieName != "N/A" || addedUser.BungieName != "") { bungieName = addedUser.BungieName; } if (addedUser.SteamName != "N/A" || addedUser.SteamName != "") { steamName = addedUser.SteamName; } if (addedUser.ExtraColumns != null) { extraColumns = addedUser.ExtraColumns; } UserStatus.StatusEnum userStatus = addedUser.UserStatus; if (addedUser.ExtraColumns != null) { extraColumns = addedUser.ExtraColumns; } workingList.Add(new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, clan.details.Tag, extraColumns)); } } durations[index % durations.Length] = DateTime.UtcNow - startTime; if (index > durations.Length - 1) { var averageTime = TimeSpan.FromMilliseconds(durations.Average(t => t.TotalMilliseconds)); var usersLeft = changes.addedUsers.Count - (index + 1); if (modifyMessages != null && modifyMethod != null) { modifyMethod.Invoke(modifyMessages, averageTime * usersLeft, changes); } } index++; } for (int i = 0; i < changes.updatedUsers.Count; i++) { index = workingList.FindIndex(t => t.BungieProfile == changes.updatedUsers[i].BungieProfile); workingList[index] = changes.updatedUsers[i]; } changes.removedUsers.ForEach(removedUser => { index = workingList.FindIndex(t => t.BungieProfile == removedUser.BungieProfile); workingList.RemoveAt(index); }); workingList.RemoveAll(t => string.IsNullOrEmpty(t.BungieProfile)); workingList = workingList.DistinctBy(t => t.BungieProfile).ToList(); workingList = workingList.OrderBy(t => t.SteamName).ToList(); clan.members.BungieUsers = workingList; Write(clan); Clans.SaveClanMembers(clan); }
public static async Task BulkUpdate(Clan clan, List <DiscordMessage> modifyMessages, Action <List <DiscordMessage>, TimeSpan> modifyMethod, bool skipRead = false) { TimeSpan[] durations = new TimeSpan[5]; if (!skipRead) { await Read(clan); } //ShowLoading("processing..."); var _ = clan.members.SpreadsheetUsers; List <User> workingList = new List <User>(); int index = 0; foreach (User user in _) { var startTime = DateTime.UtcNow; var workingUser = new User(); if (!string.IsNullOrEmpty(user.BungieProfile)) { long?a = await BungieTools.GetBungieUserID(user.BungieProfile); workingUser.BungieID = a.ToString(); if (!string.IsNullOrEmpty(workingUser.BungieID)) { List <string> extraColumns = new List <string>(); string bungieProfile = user.BungieProfile; string bungieID = workingUser.BungieID; GeneralUser bungieUser = await BungieTools.GetBungieUser(Convert.ToInt64(bungieID)); string steamID = SteamTools.GetSteamID(bungieProfile); string steamName = SteamTools.GetSteamUserName(steamID); string steamProfile = "https://steamcommunity.com/profiles/" + steamID; string bungieName = bungieUser.displayName; string discordID = user.DiscordID; UserStatus.StatusEnum userStatus = user.UserStatus; if (user.ExtraColumns != null) { extraColumns = user.ExtraColumns; } if (steamName == null || steamID == null) { steamName = "N/A"; steamProfile = "N/A"; } workingList.Add(new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, clan.details.Tag, extraColumns)); } else { List <string> extraColumns = new List <string>(); string bungieProfile = user.BungieProfile; string bungieID = "N/A"; string steamID = "N/A"; string steamProfile = "N/A"; string bungieName = "N/A"; string steamName = "N/A"; string discordID = user.DiscordID; UserStatus.StatusEnum userStatus = user.UserStatus; if (user.BungieID != "N/A" || user.BungieID != null) { bungieID = user.BungieID; } if (user.SteamID != "N/A" || user.SteamID != null) { steamID = user.SteamID; } if (user.SteamProfile != "N/A" || user.SteamProfile != null) { steamProfile = user.SteamProfile; } if (user.BungieName != "N/A" || user.BungieName != null) { bungieName = user.BungieName; } if (user.SteamName != "N/A" || user.SteamName != null) { steamName = user.SteamName; } if (user.ExtraColumns != null) { extraColumns = user.ExtraColumns; } workingList.Add(new User(bungieProfile, bungieName, bungieID, steamProfile, steamID, steamName, discordID, userStatus, clan.details.Tag, extraColumns)); } } durations[index % durations.Length] = DateTime.UtcNow - startTime; if (index > durations.Length - 1) { var averageTime = TimeSpan.FromMilliseconds(durations.Average(t => t.TotalMilliseconds)); var usersLeft = _.Count - (index + 1); if (modifyMessages != null && modifyMethod != null) { modifyMethod.Invoke(modifyMessages, averageTime * usersLeft); } } index++; } workingList.RemoveAll(t => string.IsNullOrEmpty(t.BungieProfile)); workingList = workingList.DistinctBy(t => t.BungieProfile).ToList(); workingList = workingList.OrderBy(t => t.SteamName).ToList(); clan.members.BungieUsers = workingList; Write(clan); Clans.SaveClanMembers(clan); }