Search in sources :

Example 16 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class JudgeManager method submitProblemJudge.

/**
 * @MethodName submitProblemJudge
 * @Description 核心方法 判题通过openfeign调用判题系统服务
 * @Since 2020/10/30
 */
@Transactional(rollbackFor = Exception.class)
public Judge submitProblemJudge(SubmitJudgeDto judgeDto) throws StatusForbiddenException, StatusFailException, StatusNotFoundException, StatusAccessDeniedException, AccessException {
    judgeValidator.validateSubmissionInfo(judgeDto);
    // 需要获取一下该token对应用户的数据
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isContestSubmission = judgeDto.getCid() != 0;
    boolean isTrainingSubmission = judgeDto.getTid() != null && judgeDto.getTid() != 0;
    if (!isContestSubmission && configVo.getDefaultSubmitInterval() > 0) {
        // 非比赛提交有限制限制
        String lockKey = Constants.Account.SUBMIT_NON_CONTEST_LOCK.getCode() + userRolesVo.getUid();
        long count = redisUtils.incr(lockKey, 1);
        if (count > 1) {
            throw new StatusForbiddenException("对不起,您的提交频率过快,请稍后再尝试!");
        }
        redisUtils.expire(lockKey, configVo.getDefaultSubmitInterval());
    }
    HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
    // 将提交先写入数据库,准备调用判题服务器
    Judge judge = new Judge();
    // 默认设置代码为单独自己可见
    judge.setShare(false).setCode(judgeDto.getCode()).setCid(judgeDto.getCid()).setGid(judgeDto.getGid()).setLanguage(judgeDto.getLanguage()).setLength(judgeDto.getCode().length()).setUid(userRolesVo.getUid()).setUsername(userRolesVo.getUsername()).setStatus(// 开始进入判题队列
    Constants.Judge.STATUS_PENDING.getStatus()).setSubmitTime(new Date()).setVersion(0).setIp(IpUtils.getUserIpAddr(request));
    // 如果比赛id不等于0,则说明为比赛提交
    if (isContestSubmission) {
        beforeDispatchInitManager.initContestSubmission(judgeDto.getCid(), judgeDto.getPid(), userRolesVo, judge);
    } else if (isTrainingSubmission) {
        beforeDispatchInitManager.initTrainingSubmission(judgeDto.getTid(), judgeDto.getPid(), userRolesVo, judge);
    } else {
        // 如果不是比赛提交和训练提交
        beforeDispatchInitManager.initCommonSubmission(judgeDto.getPid(), judge);
    }
    // 将提交加入任务队列
    if (judgeDto.getIsRemote()) {
        // 如果是远程oj判题
        remoteJudgeDispatcher.sendTask(judge, judge.getDisplayPid(), isContestSubmission, false);
    } else {
        judgeDispatcher.sendTask(judge, isContestSubmission);
    }
    return judge;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class JudgeManager method getALLCaseResult.

/**
 * @MethodName getJudgeCase
 * @Description 获得指定提交id的测试样例结果,暂不支持查看测试数据,只可看测试点结果,时间,空间,或者IO得分
 * @Since 2020/10/29
 */
@GetMapping("/get-all-case-result")
public List<JudgeCase> getALLCaseResult(Long submitId) throws StatusNotFoundException, StatusForbiddenException {
    Judge judge = judgeEntityService.getById(submitId);
    if (judge == null) {
        throw new StatusNotFoundException("此提交数据不存在!");
    }
    Problem problem = problemEntityService.getById(judge.getPid());
    // 如果该题不支持开放测试点结果查看
    if (!problem.getOpenCaseResult()) {
        return null;
    }
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    QueryWrapper<JudgeCase> wrapper = new QueryWrapper<>();
    if (judge.getCid() == 0) {
        // 非比赛提交
        if (userRolesVo == null) {
            // 没有登录
            wrapper.select("time", "memory", "score", "status", "user_output");
        } else {
            // 是否为超级管理员
            boolean isRoot = SecurityUtils.getSubject().hasRole("root");
            if (!isRoot && !SecurityUtils.getSubject().hasRole("admin") && !SecurityUtils.getSubject().hasRole("problem_admin")) {
                // 不是管理员
                wrapper.select("time", "memory", "score", "status", "user_output");
            }
        }
    } else {
        // 比赛提交
        if (userRolesVo == null) {
            throw new StatusForbiddenException("您还未登录!不可查看比赛提交的测试点详情!");
        }
        // 是否为超级管理员
        boolean isRoot = SecurityUtils.getSubject().hasRole("root");
        if (!isRoot) {
            Contest contest = contestEntityService.getById(judge.getCid());
            // 如果不是比赛管理员 需要受到规则限制
            if (!contest.getUid().equals(userRolesVo.getUid()) || (contest.getIsGroup() && !groupValidator.isGroupRoot(userRolesVo.getUid(), contest.getGid()))) {
                // ACM比赛期间强制禁止查看,比赛管理员除外(赛后恢复正常)
                if (contest.getType().intValue() == Constants.Contest.TYPE_ACM.getCode()) {
                    if (contest.getStatus().intValue() == Constants.Contest.STATUS_RUNNING.getCode()) {
                        return null;
                    }
                } else {
                    // 当前是oi比赛期间 同时处于封榜时间
                    if (contest.getSealRank() && contest.getStatus().intValue() == Constants.Contest.STATUS_RUNNING.getCode() && contest.getSealRankTime().before(new Date())) {
                        return null;
                    }
                }
                wrapper.select("time", "memory", "score", "status", "user_output");
            }
        }
    }
    wrapper.eq("submit_id", submitId);
    if (!problem.getIsRemote()) {
        wrapper.last("order by length(input_data) asc,input_data asc");
    }
    // 当前所有测试点只支持 空间 时间 状态码 IO得分 和错误信息提示查看而已
    return judgeCaseEntityService.list(wrapper);
}
Also used : JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Session(org.apache.shiro.session.Session) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 18 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class JudgeManager method updateSubmission.

/**
 * @MethodName updateSubmission
 * @Description 修改单个提交详情的分享权限
 * @Since 2021/1/2
 */
public void updateSubmission(Judge judge) throws StatusForbiddenException, StatusFailException {
    // 需要获取一下该token对应用户的数据
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    if (!userRolesVo.getUid().equals(judge.getUid())) {
        // 判断该提交是否为当前用户的
        throw new StatusForbiddenException("对不起,您不能修改他人的代码分享权限!");
    }
    Judge judgeInfo = judgeEntityService.getById(judge.getSubmitId());
    if (judgeInfo.getCid() != 0) {
        // 如果是比赛提交,不可分享!
        throw new StatusForbiddenException("对不起,您不能分享比赛题目的提交代码!");
    }
    judgeInfo.setShare(judge.getShare());
    boolean isOk = judgeEntityService.updateById(judgeInfo);
    if (!isOk) {
        throw new StatusFailException("修改代码权限失败!");
    }
}
Also used : Judge(top.hcode.hoj.pojo.entity.judge.Judge) Session(org.apache.shiro.session.Session)

Example 19 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class Dispatcher method checkResult.

private void checkResult(CommonResult<Void> result, Long submitId) {
    Judge judge = new Judge();
    if (result == null) {
        // 调用失败
        judge.setSubmitId(submitId);
        judge.setStatus(Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus());
        judge.setErrorMessage("Failed to connect the judgeServer. Please resubmit this submission again!");
        judgeEntityService.updateById(judge);
    } else {
        if (result.getStatus() != ResultStatus.SUCCESS.getStatus()) {
            // 如果是结果码不是200 说明调用有错误
            // 判为系统错误
            judge.setStatus(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()).setErrorMessage(result.getMsg());
            judgeEntityService.updateById(judge);
        }
    }
}
Also used : Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Example 20 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class RemoteJudgeReceiver method handleJudgeMsg.

@Override
public void handleJudgeMsg(String taskStr, String queueName) {
    JSONObject task = JSONUtil.parseObj(taskStr);
    Judge judge = task.get("judge", Judge.class);
    String token = task.getStr("token");
    String remoteJudgeProblem = task.getStr("remoteJudgeProblem");
    Boolean isHasSubmitIdRemoteReJudge = task.getBool("isHasSubmitIdRemoteReJudge");
    String remoteOJName = remoteJudgeProblem.split("-")[0].toUpperCase();
    dispatchRemoteJudge(judge, token, remoteJudgeProblem, isHasSubmitIdRemoteReJudge, remoteOJName);
}
Also used : JSONObject(cn.hutool.json.JSONObject) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Aggregations

Judge (top.hcode.hoj.pojo.entity.judge.Judge)50 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)29 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)21 Transactional (org.springframework.transaction.annotation.Transactional)18 Problem (top.hcode.hoj.pojo.entity.problem.Problem)18 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)14 Contest (top.hcode.hoj.pojo.entity.contest.Contest)13 Session (org.apache.shiro.session.Session)11 HttpSession (javax.servlet.http.HttpSession)10 ContestRecord (top.hcode.hoj.pojo.entity.contest.ContestRecord)10 JudgeCase (top.hcode.hoj.pojo.entity.judge.JudgeCase)10 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)8 UserAcproblem (top.hcode.hoj.pojo.entity.user.UserAcproblem)8 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)7 java.util (java.util)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 SecurityUtils (org.apache.shiro.SecurityUtils)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 Constants (top.hcode.hoj.utils.Constants)6 JSONObject (cn.hutool.json.JSONObject)4