Ejemplo n.º 1
0
        public async Task SyncRankRolesAndData()
        {
            // Note: we currently do not take access locks for the data structure.
            // We only touch it in a read-only way, so it should be okay.
            // This function should only be called at the initialization time anyway.
            int updates   = 0;
            int preserved = 0;

            foreach ((var discordID, var uplayID) in _data.DiscordUplay)
            {
                foreach (var guild in guilds.byID.Values)
                {
                    SocketGuildUser player = guild._socket.Users.FirstOrDefault(x => x.Id == discordID);
                    if (player == null)
                    {
                        continue;
                    }

                    List <string> allRoles    = player.Roles.Select(x => x.Name).ToList();
                    Rank          guessedRank = Ranking.GuessRank(allRoles);
                    Rank          queriedRank = await _data.QueryRank(discordID);

                    if (!guessedRank.Equals(queriedRank))
                    {
                        try
                        {
                            Console.WriteLine($"Guessed and queried rank of user {player.Username} on guild {guild.GetName()} do not match, guessed: ({guessedRank.FullPrint()},{guessedRank.level}), queried: ({queriedRank.FullPrint()},{queriedRank.level})");
                            await UpdateRoles(discordID, queriedRank);
                        }
                        catch (RankParsingException)
                        {
                            Console.WriteLine($"Failed to fix mismatch for Discord user {player.Username}");
                        }
                        updates++;
                    }
                    else
                    {
                        preserved++;
                    }
                }
                // TODO: Possibly erase from the DB if the user IS null.
            }
            System.Console.WriteLine($"Bootstrap: {updates} players have their roles updated, {preserved} have the same roles.");
            roleInitComplete = true;
        }