Esempio n. 1
0
        public async Task <StatisticsListModel> StatisticsList([FromBody] StatisticsListQueryModel model)
        {
            int?resultType = null;

            if (!string.IsNullOrEmpty(model.Result) && model.Result != "All")
            {
                if (Enum.TryParse <ResultCode>(model.Result.Trim(), true, out var type))
                {
                    resultType = (int?)type;
                }
            }

            string?queryUserId = null;

            if (!string.IsNullOrEmpty(model.UserName))
            {
                var queryUser = await userManager.FindByNameAsync(model.UserName);

                queryUserId = queryUser?.Id ?? "-1";
            }

            var judges = await judgeService.QueryJudgesAsync(queryUserId, model.GroupId, model.ContestId, model.ProblemId, resultType);

            IQueryable <Judge> query = judges;

            var ret = new StatisticsListModel();

            if (model.RequireTotalCount)
            {
                ret.TotalCount = await query.Select(i => i.Id) /*.Cacheable()*/.CountAsync();
            }

            query = query.OrderByDescending(i => i.Id);

            if (model.StartId != 0)
            {
                query = query.Where(i => i.Id <= model.StartId);
            }
            else
            {
                query = query.Skip(model.Start);
            }

            ret.Statistics = await query.Take(model.Count).Select(i => new StatisticsListModel.StatisticsListItemModel
            {
                ContestId   = i.ContestId,
                GroupId     = i.GroupId,
                ProblemId   = i.ProblemId,
                ResultId    = i.Id,
                ResultType  = i.ResultType,
                Time        = i.JudgeTime,
                UserId      = i.UserId,
                UserName    = i.UserInfo.UserName,
                ProblemName = i.Problem.Name,
                Language    = i.Language,
                Score       = i.FullScore
            }) /*.Cacheable()*/.ToListAsync();

            return(ret);
        }
Esempio n. 2
0
        public async Task <ProblemListModel> ProblemList([FromBody] ProblemListQueryModel model)
        {
            var userId = userManager.GetUserId(User);

            var ret = new ProblemListModel();

            // use an invalid value when userId is empty or null
            var judges = await judgeService.QueryJudgesAsync(string.IsNullOrEmpty(userId)? "-1" : userId, model.GroupId == 0?null : (int?)model.GroupId, model.ContestId == 0?null : (int?)model.ContestId);

            IQueryable <Problem> problems;

            try
            {
                problems = await(model switch
                {
                    { ContestId: 0, GroupId: 0 } => problemService.QueryProblemAsync(userId),
                    { GroupId: 0 } => problemService.QueryProblemAsync(userId, model.ContestId),
Esempio n. 3
0
        public async Task <ActivityListModel> GetActivities()
        {
            var judges = await judgeService
                         .QueryJudgesAsync(null, 0, 0, 0, (int)ResultCode.Accepted);

            var result = judges
                         .Where(i => (i.ContestId == null && i.GroupId == null && !i.Problem.Hidden) ||
                                (i.ContestId != null && i.GroupId == null && !i.Contest.Hidden) ||
                                (i.GroupId != null && !i.Group.IsPrivate))
                         .Select(i =>
                                 new
            {
                ProblemName = i.Problem.Name,
                UserName    = i.UserInfo.UserName,
                UserId      = i.UserId,
                Time        = i.JudgeTime,
                Title       = "通过题目",
                Content     = $"成功通过了题目 {i.Problem.Name}"
            })
                         .OrderByDescending(i => i.Time);

            var model = new ActivityListModel
            {
                TotalCount = 10
            };

            foreach (var i in result.Take(10))
            {
                model.Activities.Add(new ActivityModel
                {
                    Content  = $"成功通过了题目 {i.ProblemName}",
                    Time     = i.Time,
                    Title    = "通过题目",
                    UserId   = i.UserId,
                    UserName = i.UserName
                });
            }

            return(model);
        }
Esempio n. 4
0
        public async Task <ProblemStatisticsModel> GetUserProblemStatistics(string?userId = null)
        {
            var ret = new ProblemStatisticsModel();

            if (string.IsNullOrEmpty(userId))
            {
                userId = userManager.GetUserId(User);
            }
            var user = await userManager.FindByIdAsync(userId);

            if (userId == null || user == null)
            {
                return(new ProblemStatisticsModel());
            }

            var judges = await judgeService.QueryJudgesAsync(userId);

            ret.SolvedProblems = await judges.Where(i => i.ResultType == (int)ResultCode.Accepted).Select(i => i.ProblemId).Distinct().OrderBy(i => i).ToListAsync();

            ret.TriedProblems = await judges.Where(i => i.ResultType != (int)ResultCode.Accepted).Select(i => i.ProblemId).Distinct().OrderBy(i => i).ToListAsync();

            ret.TriedProblems = ret.TriedProblems.Where(i => !ret.SolvedProblems.Contains(i)).ToList();
            return(ret);
        }
Esempio n. 5
0
        public async Task <RankContestStatisticsModel> GetRankForContest(int contestId, int groupId)
        {
            var user = await userManager.GetUserAsync(User);

            var contest = await contestService.GetContestAsync(contestId);

            if (contest == null)
            {
                throw new NotFoundException("该比赛不存在");
            }
            if (groupId != 0)
            {
                var groups = await groupService.QueryGroupAsync(user?.Id);

                groups = groups.Where(i => i.GroupContestConfig.Any(j => j.ContestId == contestId && j.GroupId == groupId)) /*.Cacheable()*/;
                if (!await groups.AnyAsync())
                {
                    throw new NotFoundException("该比赛不存在或未加入对应小组");
                }
            }

            var config = contest.Config.DeserializeJson <ContestConfig>(false);

            if (!config.ShowRank && !Utils.PrivilegeHelper.IsTeacher(user?.Privilege))
            {
                throw new ForbiddenException("不允许查看排名");
            }

            var judges = await judgeService.QueryJudgesAsync(null,
                                                             groupId == 0?null : (int?)groupId,
                                                             contestId,
                                                             0);

            if (config.AutoStopRank && !Utils.PrivilegeHelper.IsTeacher(user?.Privilege) && DateTime.Now < contest.EndTime)
            {
                var time = contest.EndTime.AddHours(-1);
                judges = judges.Where(i => i.JudgeTime < time);
            }

            var ret = new RankContestStatisticsModel
            {
                ContestId = contestId,
                GroupId   = groupId
            };

            var results = judges.OrderBy(i => i.Id).Select(i => new
            {
                Id          = i.Id,
                ProblemId   = i.ProblemId,
                ProblemName = i.Problem.Name,
                UserId      = i.UserId,
                UserName    = i.UserInfo.UserName,
                Name        = i.UserInfo.Name,
                ResultType  = i.ResultType,
                Time        = i.JudgeTime,
                Score       = i.FullScore
            }) /*.Cacheable()*/;

            var isAccepted = new Dictionary <(string UserId, int ProblemId), bool>();

            foreach (var i in results)
            {
                if (!ret.UserInfos.ContainsKey(i.UserId))
                {
                    ret.UserInfos[i.UserId] = new RankUserInfoModel
                    {
                        UserName = i.UserName,
                        Name     = Utils.PrivilegeHelper.IsTeacher(user?.Privilege) ? i.Name : string.Empty
                    }
                }
                ;
                if (!ret.ProblemInfos.ContainsKey(i.ProblemId))
                {
                    ret.ProblemInfos[i.ProblemId] = new RankProblemInfoModel
                    {
                        ProblemName = i.ProblemName
                    }
                }
                ;
                if (!ret.RankInfos.ContainsKey(i.UserId))
                {
                    ret.RankInfos[i.UserId] = new Dictionary <int, RankContestItemModel>();
                }
                if (!ret.RankInfos[i.UserId].ContainsKey(i.ProblemId))
                {
                    ret.RankInfos[i.UserId][i.ProblemId] = new RankContestItemModel();
                }

                if (config.Type != ContestType.LastSubmit)
                {
                    ret.RankInfos[i.UserId][i.ProblemId].Accepted = ret.RankInfos[i.UserId][i.ProblemId].Accepted || (i.ResultType == (int)ResultCode.Accepted);
                }
                else
                {
                    ret.RankInfos[i.UserId][i.ProblemId].Accepted = ret.RankInfos[i.UserId][i.ProblemId].Accepted && (i.ResultType == (int)ResultCode.Accepted);
                }

                if (!isAccepted.ContainsKey((i.UserId, i.ProblemId)))
                {
                    isAccepted[(i.UserId, i.ProblemId)] = false;
Esempio n. 6
0
        public async Task <SubmitSuccessModel> SubmitSolution([FromBody] SubmitModel model)
        {
            var user = await userManager.GetUserAsync(User);

            var now = DateTime.Now;
            var allowJumpToResult = true;

            if (user.Privilege == 5)
            {
                throw new ForbiddenException("不允许提交,请与管理员联系");
            }

            if (model.GroupId != 0)
            {
                var inGroup = await groupService.IsInGroupAsync(user.Id, model.GroupId);

                if (!inGroup)
                {
                    throw new ForbiddenException("未参加该小组");
                }
            }

            var problem = await problemService.GetProblemAsync(model.ProblemId);

            if (problem == null)
            {
                throw new NotFoundException("该题目不存在");
            }
            var problemConfig = problem.Config.DeserializeJson <ProblemConfig>(false);

            // For older version compatibility
            if (problemConfig.SourceFiles.Count == 0)
            {
                problemConfig.SourceFiles.Add(string.IsNullOrEmpty(problemConfig.SubmitFileName) ? "${random}${extension}" : $"{problemConfig.SubmitFileName}${{extension}}");
            }

            var sources = new List <Source>();

            foreach (var i in model.Content)
            {
                if (problemConfig.CodeSizeLimit != 0 && problemConfig.CodeSizeLimit < Encoding.UTF8.GetByteCount(i.Content))
                {
                    throw new BadRequestException("提交内容长度超出限制");
                }
                if (problemConfig.SourceFiles.Contains(i.FileName))
                {
                    sources.Add(new Source
                    {
                        FileName = i.FileName,
                        Content  = i.Content
                    });
                }
            }

            var useDefaultDisabledConfig = false;

            var langConfig = (await languageService.GetLanguageConfigAsync()).ToList();
            var langs      = problemConfig.Languages?.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? new string[0];

            if (langs.Length == 0)
            {
                langs = langConfig.Select(i => i.Name).ToArray();
            }
            else
            {
                useDefaultDisabledConfig = true;
            }

            if (model.ContestId != 0)
            {
                var contest = await contestService.GetContestAsync(model.ContestId);

                if (contest != null)
                {
                    if (contest.StartTime > now || now > contest.EndTime)
                    {
                        throw new ForbiddenException("当前不允许提交");
                    }
                    if (contest.Hidden && !PrivilegeHelper.IsTeacher(user.Privilege))
                    {
                        throw new NotFoundException("该比赛不存在");
                    }

                    var contestConfig = contest.Config.DeserializeJson <ContestConfig>(false);
                    if (contestConfig.SubmissionLimit != 0)
                    {
                        var judges = await judgeService.QueryJudgesAsync(user.Id,
                                                                         model.GroupId == 0?null : (int?)model.GroupId,
                                                                         model.ContestId,
                                                                         model.ProblemId);

                        if (contestConfig.SubmissionLimit <= await judges.CountAsync())
                        {
                            throw new ForbiddenException("超出提交次数限制");
                        }
                    }
                    if (contestConfig.ResultMode != ResultDisplayMode.Intime)
                    {
                        allowJumpToResult = false;
                    }
                    var contestLangs = contestConfig.Languages?.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
                    if (contestLangs.Length != 0)
                    {
                        langs = langs.Intersect(contestLangs).ToArray();
                        useDefaultDisabledConfig = true;
                    }
                }
            }
            else if (problem.Hidden && !PrivilegeHelper.IsTeacher(user.Privilege))
            {
                throw new NotFoundException("该题目不存在");
            }

            if (!useDefaultDisabledConfig)
            {
                langs = langs.Where(i => langConfig.Any(j => j.Name == i && !j.DisabledByDefault)).ToArray();
            }

            if (!langs.Contains(model.Language))
            {
                throw new ForbiddenException("不允许使用该语言提交");
            }

            user.SubmissionCount++;
            await userManager.UpdateAsync(user);

            var id = await judgeService.QueueJudgeAsync(new Judge
            {
                Content        = sources.SerializeJsonAsString(),
                Language       = model.Language,
                ProblemId      = model.ProblemId,
                ContestId      = model.ContestId == 0 ? null : (int?)model.ContestId,
                GroupId        = model.GroupId == 0 ? null : (int?)model.GroupId,
                UserId         = user.Id,
                Description    = "Online Judge",
                AdditionalInfo = "v2"
            });

            return(new SubmitSuccessModel
            {
                Jump = allowJumpToResult,
                ResultId = id
            });
        }