Search in sources :

Example 1 with ContestProblem

use of top.hcode.hoj.pojo.entity.contest.ContestProblem in project HOJ by HimitZH.

the class ContestRecordServiceImpl method submitContestProblem.

@Override
@Transactional(rollbackFor = Exception.class)
public CommonResult submitContestProblem(ToJudgeDto judgeDto, UserRolesVo userRolesVo, Judge judge) {
    // 首先判断一下比赛的状态是否是正在进行,结束状态都不能提交,比赛前比赛管理员可以提交
    Contest contest = contestService.getById(judgeDto.getCid());
    if (contest == null) {
        return CommonResult.errorResponse("对不起,该比赛不存在!");
    }
    if (contest.getStatus().intValue() == Constants.Contest.STATUS_ENDED.getCode()) {
        return CommonResult.errorResponse("比赛已结束,不可再提交!");
    }
    // 是否为超级管理员或者该比赛的创建者,则为比赛管理者
    boolean root = SecurityUtils.getSubject().hasRole("root");
    if (!root && !contest.getUid().equals(userRolesVo.getUid())) {
        if (contest.getStatus().intValue() == Constants.Contest.STATUS_SCHEDULED.getCode()) {
            return CommonResult.errorResponse("比赛未开始,不可提交!");
        }
        // 需要检查是否有权限在当前比赛进行提交
        CommonResult checkResult = contestService.checkJudgeAuth(contest, userRolesVo.getUid());
        if (checkResult != null) {
            return checkResult;
        }
        if (contest.getAuth().equals(Constants.Contest.AUTH_PROTECT.getCode()) && contest.getOpenAccountLimit() && !contestService.checkAccountRule(contest.getAccountLimitRule(), userRolesVo.getUsername())) {
            return CommonResult.errorResponse("对不起!本次比赛只允许特定账号规则的用户参赛!", CommonResult.STATUS_ACCESS_DENIED);
        }
    }
    // 查询获取对应的pid和cpid
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    contestProblemQueryWrapper.eq("cid", judgeDto.getCid()).eq("display_id", judgeDto.getPid());
    ContestProblem contestProblem = contestProblemMapper.selectOne(contestProblemQueryWrapper);
    judge.setCpid(contestProblem.getId()).setPid(contestProblem.getPid());
    Problem problem = problemService.getById(contestProblem.getPid());
    if (problem.getAuth() == 2) {
        return CommonResult.errorResponse("错误!当前题目不可提交!", CommonResult.STATUS_FORBIDDEN);
    }
    judge.setDisplayPid(problem.getProblemId());
    // 将新提交数据插入数据库
    judgeMapper.insert(judge);
    // 同时初始化写入contest_record表
    ContestRecord contestRecord = new ContestRecord();
    contestRecord.setDisplayId(judgeDto.getPid()).setCpid(contestProblem.getId()).setSubmitId(judge.getSubmitId()).setPid(judge.getPid()).setUsername(userRolesVo.getUsername()).setRealname(userRolesVo.getRealname()).setUid(userRolesVo.getUid()).setCid(judge.getCid()).setSubmitTime(judge.getSubmitTime());
    if (contest.getStatus().intValue() == Constants.Contest.STATUS_SCHEDULED.getCode()) {
        contestRecord.setTime(0L);
    } else {
        // 设置比赛开始时间到提交时间之间的秒数
        contestRecord.setTime(DateUtil.between(contest.getStartTime(), judge.getSubmitTime(), DateUnit.SECOND));
    }
    contestRecordMapper.insert(contestRecord);
    return null;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) CommonResult(top.hcode.hoj.common.result.CommonResult) Problem(top.hcode.hoj.pojo.entity.problem.Problem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Contest(top.hcode.hoj.pojo.entity.contest.Contest) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ContestProblem

use of top.hcode.hoj.pojo.entity.contest.ContestProblem in project HOJ by HimitZH.

the class AdminContestController method importContestRemoteOJProblem.

@GetMapping("/import-remote-oj-problem")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
@Transactional(rollbackFor = Exception.class)
public CommonResult importContestRemoteOJProblem(@RequestParam("name") String name, @RequestParam("problemId") String problemId, @RequestParam("cid") Long cid, @RequestParam("displayId") String displayId, HttpServletRequest request) {
    QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("problem_id", name.toUpperCase() + "-" + problemId);
    Problem problem = problemService.getOne(queryWrapper, false);
    // 如果该题目不存在,需要先导入
    if (problem == null) {
        HttpSession session = request.getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        try {
            ProblemStrategy.RemoteProblemInfo otherOJProblemInfo = problemService.getOtherOJProblemInfo(name.toUpperCase(), problemId, userRolesVo.getUsername());
            if (otherOJProblemInfo != null) {
                problem = problemService.adminAddOtherOJProblem(otherOJProblemInfo, name);
                if (problem == null) {
                    return CommonResult.errorResponse("导入新题目失败!请重新尝试!");
                }
            } else {
                return CommonResult.errorResponse("导入新题目失败!原因:可能是与该OJ链接超时或题号格式错误!");
            }
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    Problem finalProblem = problem;
    contestProblemQueryWrapper.eq("cid", cid).and(wrapper -> wrapper.eq("pid", finalProblem.getId()).or().eq("display_id", displayId));
    ContestProblem contestProblem = contestProblemService.getOne(contestProblemQueryWrapper, false);
    if (contestProblem != null) {
        return CommonResult.errorResponse("添加失败,该题目已添加或者题目的比赛展示ID已存在!", CommonResult.STATUS_FAIL);
    }
    // 比赛中题目显示默认为原标题
    String displayName = problem.getTitle();
    // 修改成比赛题目
    boolean updateProblem = problemService.saveOrUpdate(problem.setAuth(3));
    boolean result = contestProblemService.saveOrUpdate(new ContestProblem().setCid(cid).setPid(problem.getId()).setDisplayTitle(displayName).setDisplayId(displayId));
    if (result && updateProblem) {
        // 添加成功
        return CommonResult.successResponse(null, "添加成功!");
    } else {
        return CommonResult.errorResponse("添加失败", CommonResult.STATUS_FAIL);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) ProblemStrategy(top.hcode.hoj.crawler.problem.ProblemStrategy) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ContestProblem

use of top.hcode.hoj.pojo.entity.contest.ContestProblem in project HOJ by HimitZH.

the class AdminContestController method addProblemFromPublic.

@PostMapping("/add-problem-from-public")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
public CommonResult addProblemFromPublic(@RequestBody HashMap<String, String> params) {
    String pidStr = params.get("pid");
    String cidStr = params.get("cid");
    String displayId = params.get("displayId");
    if (StringUtils.isEmpty(pidStr) || StringUtils.isEmpty(cidStr) || StringUtils.isEmpty(displayId)) {
        return CommonResult.errorResponse("参数错误!", CommonResult.STATUS_FAIL);
    }
    Long pid = Long.valueOf(pidStr);
    Long cid = Long.valueOf(cidStr);
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    contestProblemQueryWrapper.eq("cid", cid).and(wrapper -> wrapper.eq("pid", pid).or().eq("display_id", displayId));
    ContestProblem contestProblem = contestProblemService.getOne(contestProblemQueryWrapper, false);
    if (contestProblem != null) {
        return CommonResult.errorResponse("添加失败,该题目已添加或者题目的比赛展示ID已存在!", CommonResult.STATUS_FAIL);
    }
    // 比赛中题目显示默认为原标题
    Problem problem = problemService.getById(pid);
    String displayName = problem.getTitle();
    // 修改成比赛题目
    boolean updateProblem = problemService.saveOrUpdate(problem.setAuth(3));
    boolean result = contestProblemService.saveOrUpdate(new ContestProblem().setCid(cid).setPid(pid).setDisplayTitle(displayName).setDisplayId(displayId));
    if (result && updateProblem) {
        // 添加成功
        return CommonResult.successResponse(null, "添加成功!");
    } else {
        return CommonResult.errorResponse("添加失败", CommonResult.STATUS_FAIL);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Problem(top.hcode.hoj.pojo.entity.problem.Problem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Example 4 with ContestProblem

use of top.hcode.hoj.pojo.entity.contest.ContestProblem in project HOJ by HimitZH.

the class GroupContestProblemManager method addProblemFromGroup.

@Transactional(rollbackFor = Exception.class)
public void addProblemFromGroup(String problemId, Long cid, String displayId) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Contest contest = contestEntityService.getById(cid);
    if (contest == null) {
        throw new StatusNotFoundException("该训练不存在!");
    }
    Long gid = contest.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUid().equals(contest.getUid()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
    problemQueryWrapper.eq("problem_id", problemId).eq("gid", gid);
    Problem problem = problemEntityService.getOne(problemQueryWrapper);
    if (problem == null) {
        throw new StatusNotFoundException("该题目不存在或不是团队题目!");
    }
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    contestProblemQueryWrapper.eq("cid", cid).and(wrapper -> wrapper.eq("pid", problem.getId()).or().eq("display_id", displayId));
    ContestProblem contestProblem = contestProblemEntityService.getOne(contestProblemQueryWrapper);
    if (contestProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的比赛展示ID已存在!");
    }
    ContestProblem newCProblem = new ContestProblem();
    String displayName = problem.getTitle();
    boolean updateProblem = problemEntityService.saveOrUpdate(problem.setAuth(3));
    boolean isOk = contestProblemEntityService.saveOrUpdate(newCProblem.setCid(cid).setPid(problem.getId()).setDisplayTitle(displayName).setDisplayId(displayId));
    if (!isOk || !updateProblem) {
        throw new StatusFailException("添加失败");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ContestProblem

use of top.hcode.hoj.pojo.entity.contest.ContestProblem in project HOJ by HimitZH.

the class GroupContestProblemManager method addProblemFromPublic.

public void addProblemFromPublic(ContestProblemDto contestProblemDto) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Long pid = contestProblemDto.getPid();
    Problem problem = problemEntityService.getById(pid);
    if (problem == null || problem.getAuth() != 1 || problem.getIsGroup()) {
        throw new StatusNotFoundException("该题目不存在或已被隐藏!");
    }
    Long cid = contestProblemDto.getCid();
    Contest contest = contestEntityService.getById(cid);
    if (contest == null) {
        throw new StatusNotFoundException("该比赛不存在!");
    }
    Long gid = contest.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUid().equals(contest.getUid()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    String displayId = contestProblemDto.getDisplayId();
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    contestProblemQueryWrapper.eq("cid", cid).and(wrapper -> wrapper.eq("pid", pid).or().eq("display_id", displayId));
    ContestProblem contestProblem = contestProblemEntityService.getOne(contestProblemQueryWrapper, false);
    if (contestProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的比赛展示ID已存在!");
    }
    String displayName = problem.getTitle();
    ContestProblem newCProblem = new ContestProblem();
    boolean isOk = contestProblemEntityService.saveOrUpdate(newCProblem.setCid(cid).setPid(pid).setDisplayTitle(displayName).setDisplayId(displayId));
    if (!isOk) {
        throw new StatusFailException("添加失败");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Session(org.apache.shiro.session.Session)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)18 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)18 Contest (top.hcode.hoj.pojo.entity.contest.Contest)11 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)11 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)10 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)10 Session (org.apache.shiro.session.Session)8 Problem (top.hcode.hoj.pojo.entity.problem.Problem)8 Transactional (org.springframework.transaction.annotation.Transactional)7 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)6 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)5 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)4 FileUtil (cn.hutool.core.io.FileUtil)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 SecurityUtils (org.apache.shiro.SecurityUtils)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 ProblemStrategy (top.hcode.hoj.crawler.problem.ProblemStrategy)3 Group (top.hcode.hoj.pojo.entity.group.Group)3 Judge (top.hcode.hoj.pojo.entity.judge.Judge)3