예제 #1
0
        private async Task RankFeedTimer(CancellationToken token)
        {
            var watch = Stopwatch.StartNew();

            while (!token.IsCancellationRequested)
            {
                try
                {
                    var belgiumRankFeed = new CountryRankFeed(discordSocketClient, "BE");
                    var gbRankFeed      = new CountryRankFeed(discordSocketClient, "GB");
                    var au_nzRankFeed   = new CountryRankFeed(discordSocketClient, "AU", "NZ");
                    var dkRankFeed      = new CountryRankFeed(discordSocketClient, "DK");

                    Console.WriteLine("Updating news feed in 60 sec");
                    await Task.Delay(60000 - (int)(watch.ElapsedMilliseconds % 1000), token);

                    Console.WriteLine("Startin NL feed...");
                    try
                    {
                        var stopwatch = new Stopwatch();
                        stopwatch.Start();
                        await DutchRankFeed.DutchRankingFeed(discordSocketClient);

                        stopwatch.Stop();
                        Console.WriteLine("NL Feed updatetime: " + stopwatch.Elapsed);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("News Feed Crashed" + ex + "NL");
                        _logger.Log(Logger.LogCode.error, "NL Feed Crashed \n\n" + ex);
                    }

                    Console.WriteLine("Ending NL feed...");

                    await Task.Delay(20000 - (int)(watch.ElapsedMilliseconds % 1000), token);

                    Console.WriteLine("Startin DK feed...");
                    try
                    {
                        await dkRankFeed.SendFeedInCountryDiscord(511188151968989197, 680415200108871784);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("News Feed Crashed" + ex + "DK");
                    }

                    await Task.Delay(20000 - (int)(watch.ElapsedMilliseconds % 1000), token);

                    Console.WriteLine("Startin AU_NZ feed...");
                    try
                    {
                        await au_nzRankFeed.SendFeedInCountryDiscord(471250128615899136, 550387948294766611);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("News Feed Crashed" + ex + "AU_NZ");
                    }

                    Console.WriteLine("Ending AU_NZ feed...");

                    await Task.Delay(20000 - (int)(watch.ElapsedMilliseconds % 1000), token);

                    Console.WriteLine("Startin GB feed...");
                    try
                    {
                        await gbRankFeed.SendFeedInCountryDiscord(483482746824687616, 535187354122584070);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("News Feed Crashed" + ex + "GB");
                    }

                    Console.WriteLine("Ending GB feed...");

                    await Task.Delay(20000 - (int)(watch.ElapsedMilliseconds % 1000), token);

                    Console.WriteLine("Startin BE feed...");
                    try
                    {
                        await belgiumRankFeed.SendFeedInCountryDiscord(561207570669371402, 634091663526199307);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("News Feed Crashed" + ex + "BE");
                    }

                    Console.WriteLine("Ending BE feed...");


                    Console.WriteLine("News Feed Updated");
                }
                catch (Exception ex)
                {
                    _logger.Log(Logger.LogCode.error, ex.ToString());
                }

                Console.WriteLine("Task token: " + token.IsCancellationRequested);
            }
        }
        public static async Task UpdateNLAsync(DiscordSocketClient discord, SocketMessage message)
        {
            var embed = await message.Channel.SendMessageAsync("Starting Roles update");

            await Task.Delay(1000);

            Console.WriteLine("Starting updating roles from linked NL accounts");
            var    accounts          = new Dictionary <string, object>();
            string DutchAccountsPath = "../../../DutchAccounts.txt";

            accounts = JsonExtension.GetJsonData(DutchAccountsPath);

            if (accounts == null || accounts.Count == 0)
            {
                accounts = new Dictionary <string, object>();
            }

            var client = new HttpClient();

            var LoadingSpaceCount    = 0;
            var spaceCount           = accounts.Count;
            var accountsProcessCount = 0;

            string staticUrl = "https://scoresaber.com/u/";

            foreach (var account in accounts)
            {
                string url  = staticUrl + account.Value.ToString();
                int    rank = 0;

                string html = await client.GetStringAsync(url);

                var doc = new HtmlDocument();
                doc.LoadHtml(html);
                try
                {
                    string rankUnfixed = doc.DocumentNode.SelectSingleNode("//a[@href='/global?country=nl']").InnerText;
                    rank = int.Parse(rankUnfixed.Replace("(", "").Replace(")", "").Replace("#", "").Replace(",", "").Trim());
                }
                catch
                {
                    var Logger = new Logger(discord);
                    Logger.Log(Logger.LogCode.debug, "Cant get rank info from: " + url);
                }

                try
                {
                    if (rank == 0)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Koos Rankloos", discord);

                        continue;
                    }

                    if (rank == 1)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Nummer 1", discord);
                    }
                    else if (rank <= 3)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 3", discord);
                    }
                    else if (rank <= 10)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 10", discord);
                    }
                    else if (rank <= 25)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 25", discord);
                    }
                    else if (rank <= 50)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 50", discord);
                    }
                    else if (rank <= 100)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 100", discord);
                    }
                    else if (rank <= 250)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 250", discord);
                    }
                    else if (rank <= 500)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 500", discord);
                    }
                    else if (rank > 500)
                    {
                        await DutchRankFeed.GiveRole(account.Value.ToString(), "Top 501+", discord);
                    }
                }
                catch
                {
                    Console.WriteLine("Delete " + account.Value.ToString() + "He left the discord");
                }

                await embed.ModifyAsync(x =>
                                        x.Content = "Loading... \n*" + accountsProcessCount + "* ||  " + GiveSpaces(accountsProcessCount).Item1 + "||" + GiveSpaces(accountsProcessCount).Item2 + "*" + accounts.Count.ToString() + "*");

                accountsProcessCount += 1;


                (string, string) GiveSpaces(int count)
                {
                    var l = "";
                    var s = "";

                    for (var i = 0; i < count; i++)
                    {
                        l += " ";
                    }

                    for (var i = 0; i < accounts.Count - count; i++)
                    {
                        s += " ";
                    }

                    return(l, s);
                }
            }

            client.Dispose();

            Console.WriteLine("Done updating accounts NL");
        }