示例#1
0
        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);
        }
示例#2
0
        public static async Task Read(Clan clan)
        {
            // Define requestRead parameters.
            String spreadsheetId = ConfigValues.SpreadsheetID;
            String range         = clan.details.SpreadsheetRange;

            SpreadsheetsResource.ValuesResource.GetRequest requestRead =
                service.Spreadsheets.Values.Get(spreadsheetId, range);

            // Prints the names and majors of students in a sample spreadsheet:
            // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
            ValueRange response = requestRead.Execute();
            IList <IList <Object> > spreadsheetData = response.Values;
            var  workingList     = new List <User>();
            bool forceBulkUpdate = false;

            if (spreadsheetData == null)
            {
                var _       = new User();
                var members = await BungieTools.GetClanInfoCards(clan);

                var validMembers   = members.validMembers;
                var invalidMembers = members.invalidMembers;
                validMembers.ForEach(async member =>
                {
                    _ = new User(BungieTools.GetBungieProfileLink(member), null, null, null, null, null, null, UserStatus.StatusEnum.ok, clan.details.Tag);
                    workingList.Add(_);
                });
                invalidMembers.ForEach(async member =>
                {
                    _ = new User(BungieTools.GetBungieProfileLink(member), null, null, null, null, null, null, UserStatus.StatusEnum.ok, clan.details.Tag);
                    workingList.Add(_);
                });
                Log.Information("Spreadsheet for " + clan.details.Name + " is empty, generating (will take 10 minutes)");
                forceBulkUpdate = true;
            }
            else
            {
                for (int i = 0; i < spreadsheetData.Count; i++)
                {
                    var user = new User();
                    //if (spreadsheetData[i] != null && spreadsheetData[i].Count > 0 && !string.IsNullOrEmpty(spreadsheetData[i][0].ToString()))
                    //{
                    //    switch (spreadsheetData[i].Count)
                    //    {
                    //        case 0:
                    //            break;
                    //        case 1:
                    //            user = new User(spreadsheetData[i][0].ToString(), null, null, null, null, null, null, UserStatus.ok, clan.details.Tag);
                    //            workingList.Add(user);
                    //            break;
                    //        case 2:
                    //            user = new User(spreadsheetData[i][0].ToString(), spreadsheetData[i][1].ToString(), null, null, null, null, null, UserStatus.ok, clan.details.Tag);
                    //            workingList.Add(user);
                    //            break;
                    //        case 3:
                    //            user = new User(spreadsheetData[i][0].ToString(), spreadsheetData[i][1].ToString(), null, spreadsheetData[i][2].ToString(), null, null, null, UserStatus.ok, clan.details.Tag);
                    //            workingList.Add(user);
                    //            break;
                    //        case 4:
                    //            user = new User(spreadsheetData[i][0].ToString(), spreadsheetData[i][1].ToString(), null, spreadsheetData[i][2].ToString(), null, spreadsheetData[i][3].ToString(), null, UserStatus.ok, clan.details.Tag);
                    //            workingList.Add(user);
                    //            break;
                    //        case 5:
                    //            user = new User(spreadsheetData[i][0].ToString(), spreadsheetData[i][1].ToString(), null, spreadsheetData[i][2].ToString(), null, spreadsheetData[i][3].ToString(), spreadsheetData[i][4].ToString(), UserStatus.ok, clan.details.Tag);
                    //            workingList.Add(user);
                    //            break;
                    //        case 6:
                    //            user = new User(spreadsheetData[i][0].ToString(), spreadsheetData[i][1].ToString(), null, spreadsheetData[i][2].ToString(), null, spreadsheetData[i][3].ToString(), spreadsheetData[i][4].ToString(), Enum.Parse<UserStatus>(spreadsheetData[i][5].ToString().ToLower()), clan.details.Tag);
                    //            workingList.Add(user);
                    //            break;
                    //        default:
                    //            List<string> extraColumns = new List<string>();
                    //            for (int index = 6; index < spreadsheetData[i].Count; index++)
                    //            {
                    //                extraColumns.Add(spreadsheetData[i][index].ToString());
                    //            }
                    //            var a = spreadsheetData[i];
                    //            var b = a[5].ToString().ToLower();
                    //            UserStatus d = UserStatus.ok;
                    //            var c = Enum.TryParse(b, false, out d);
                    //            user = new User(spreadsheetData[i][0].ToString(), spreadsheetData[i][1].ToString(), null, spreadsheetData[i][2].ToString(), null, spreadsheetData[i][3].ToString(), spreadsheetData[i][4].ToString(), d, clan.details.Tag, extraColumns);
                    //            workingList.Add(user);
                    //            break;
                    //    }
                    //}
                    if (spreadsheetData[i] != null && spreadsheetData[i].Count > 0 && !string.IsNullOrEmpty(spreadsheetData[i][0].ToString()))
                    {
                        var count = spreadsheetData[i].Count;
                        if (count > 0)
                        {
                            user.BungieProfile = spreadsheetData[i][0].ToString();
                        }
                        if (count > 1)
                        {
                            user.BungieName = spreadsheetData[i][1].ToString();
                        }
                        if (count > 2)
                        {
                            user.SteamProfile = spreadsheetData[i][2].ToString();
                        }
                        if (count > 3)
                        {
                            user.SteamName = spreadsheetData[i][3].ToString();
                        }
                        if (count > 4)
                        {
                            user.DiscordID = spreadsheetData[i][4].ToString();
                        }
                        if (count > 5)
                        {
                            user.UserStatus = UserStatus.ToEnum(spreadsheetData[i][5].ToString());
                        }
                        if (count > 6)
                        {
                            List <string> extraColumns = new List <string>();
                            for (int index = 6; index < spreadsheetData[i].Count; index++)
                            {
                                extraColumns.Add(spreadsheetData[i][index].ToString());
                            }
                            user.ExtraColumns = extraColumns;
                        }
                        workingList.Add(user);
                    }
                }
            }

            clan.members.SpreadsheetUsers = workingList;
            if (clan.members.BungieUsers == null || clan.members.BungieUsers.Count == 0)
            {
                forceBulkUpdate = true;
                Log.Information("User details for " + clan.details.Name + " is empty, generating (will take 10 minutes)");
                //Console.WriteLine("User details for " + clan.clanName + " is empty, generating (will take 10 minutes)");
            }

            if (forceBulkUpdate)
            {
                await BulkUpdate(clan, null, null, true);
            }
        }
示例#3
0
        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);
        }