Example #1
0
        public void LoginWithPassTest(string userScreenName, string pass, bool shouldSuccess)
        {
            var client = new AtCoderClient();

            client.LoginAsync(userScreenName, pass).Wait();
            Assert.AreEqual(userScreenName == client.LoggedInUserScreenName, shouldSuccess);
        }
Example #2
0
        public void GetUpcomingContestsAsyncTest()
        {
            var client   = new AtCoderClient();
            var contests = client.GetUpcomingContestsAsync().Result;

            Assert.IsTrue(contests.All(x => DateTime.Now.ToUniversalTime() < x.StartTime));
        }
Example #3
0
        public void LoginWithSessionTest(string revelSession, string userScreenName)
        {
            var client = new AtCoderClient();

            client.LoginAsync(revelSession).Wait();
            Assert.AreEqual(userScreenName, client.LoggedInUserScreenName);
        }
Example #4
0
        public static async Task <ContestInfo[]> GetUpcomingContest([ActivityTrigger] object obj, ILogger log)
        {
            var client = new AtCoderClient();
            var res    = (await client.GetUpcomingContestsAsync()).ToArray();

            //var res = await client.GetUpcomingContestsAsync();
            log.LogInformation($"upcoming contest fetched.\n{string.Join("\n", res.Length)}");
            return(res);
        }
Example #5
0
        public void GetResultsAsyncTest(string contestScreenName, string topScreenName)
        {
            var client = new AtCoderClient();

            client.LoginAsync(Configuration["RevelSession"]).Wait();
            var results = client.GetReslutsAsync(contestScreenName).Result;

            Assert.AreEqual(topScreenName, results[0].UserScreenName);
        }
Example #6
0
        public async Task <MinimalStandings> GetAsync(string contestScreenName)
        {
            AtCoderClient client = new AtCoderClient();
            await client.LoginAsync(Secrets.GetSecret("AtCoderCSRFToken"));

            var res = await client.GetStandingsAsync(contestScreenName);

            if (res is null)
            {
                return(null);
            }
            return((MinimalStandings)res);
        }
Example #7
0
        public void GetPastContestsAsyncTest()
        {
            var client        = new AtCoderClient();
            var page1contests = client.GetPastContestsAsync(1).Result.ToArray();
            var page2contests = client.GetPastContestsAsync(2).Result.ToArray();
            var lastTime      = DateTime.Now.ToUniversalTime();

            foreach (var item in page1contests.Concat(page2contests))
            {
                if (lastTime < item.StartTime)
                {
                    Assert.Fail();
                }
                lastTime = item.StartTime;
            }
        }
Example #8
0
        public void GetCompetitionHistoryAsyncTest(string userScreenName, int?firstInnerPerformance)
        {
            var firstHistory = new AtCoderClient().GetCompetitionHistoryAsync(userScreenName).Result?.FirstOrDefault();

            Assert.AreEqual(firstInnerPerformance, firstHistory?.InnerPerformance);
        }
Example #9
0
        public static async Task <bool> Run([ActivityTrigger] string contestScreenName, ILogger log)
        {
            var startTime = DateTime.Now;

            log.LogInformation("start updating {0}", contestScreenName);
            var ghClient = GitHubUtil.Client;
            var acClient = new AtCoderClient();

            var session = Secrets.GetSecret("AtCoderCSRFToken");
            await acClient.LoginAsync(session);

            var standings = await acClient.GetStandingsAsync(contestScreenName);

            var jsonPath = $"aperfs/{contestScreenName}.json";

            bool found;
            RepositoryContent content;

            try
            {
                content = (await ghClient.Repository.Content.GetAllContents(GitHubUtil.Owner, GitHubUtil.Repo, jsonPath)).First();
                found   = true;
            }
            catch (NotFoundException)
            {
                content = null;
                found   = false;
            }
            var dic = content is null ? new Dictionary <string, double>() : JsonSerializer.Deserialize <Dictionary <string, double> >(content.Content);

            log.LogInformation("start crawling.");
            bool abort = false;

            foreach (var standingData in standings.StandingsData)
            {
                if ((DateTime.Now - startTime).TotalMinutes >= 8)
                {
                    log.LogWarning("time limit is nearing. abort.");
                    abort = true;
                    break;
                }
                if (standingData.UserIsDeleted ||
                    standingData.Competitions == 0 ||
                    !standingData.IsRated ||
                    dic.ContainsKey(standingData.UserScreenName))
                {
                    continue;
                }
                var history = await acClient.GetCompetitionHistoryAsync(standingData.UserScreenName);

                var beforeContestPerfs = history?.TakeWhile(x => x.ContestScreenName.Split('.', 2).First() != contestScreenName)?.Where(x => x.IsRated)?.Select(x => x.InnerPerformance)?.ToArray();
                var aperf = Rating.CalcAPerf(beforeContestPerfs);
                if (aperf is null)
                {
                    log.LogWarning($"aperf is null. screenName: {standingData.UserScreenName}");
                    continue;
                }
                dic.Add(standingData.UserScreenName, aperf.Value);
                await Task.Delay(100);
            }
            log.LogInformation("end crawling.");

            var options = new JsonSerializerOptions();

            options.Converters.Add(new TruncateDoubleConverter());
            var json = JsonSerializer.Serialize(dic, options);

            if (!found)
            {
                var request = new CreateFileRequest($"add aperfs data of {contestScreenName}", json)
                {
                    Committer = GitHubUtil.Comitter
                };
                await ghClient.Repository.Content.CreateFile(GitHubUtil.Owner, GitHubUtil.Repo, jsonPath, request);
            }
            else
            {
                var request = new UpdateFileRequest($"update aperfs data of {contestScreenName}", json, content.Sha)
                {
                    Committer = GitHubUtil.Comitter
                };
                await ghClient.Repository.Content.UpdateFile(GitHubUtil.Owner, GitHubUtil.Repo, jsonPath, request);
            }

            return(abort || !standings.Fixed);
        }