Search in sources :

Example 26 with Problem

use of top.hcode.hoj.pojo.entity.problem.Problem in project HOJ by HimitZH.

the class GroupTrainingProblemManager method addProblemFromGroup.

@Transactional(rollbackFor = Exception.class)
public void addProblemFromGroup(String problemId, Long tid) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Training training = trainingEntityService.getById(tid);
    if (training == null) {
        throw new StatusNotFoundException("该训练不存在!");
    }
    Long gid = training.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUsername().equals(training.getAuthor()) && !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<TrainingProblem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", problem.getId()).or().eq("display_id", problem.getProblemId()));
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(queryWrapper);
    if (trainingProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的训练展示ID已存在!");
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean isOk = trainingProblemEntityService.save(newTProblem.setTid(tid).setPid(problem.getId()).setDisplayId(problem.getProblemId()));
    if (isOk) {
        UpdateWrapper<Training> trainingUpdateWrapper = new UpdateWrapper<>();
        trainingUpdateWrapper.set("gmt_modified", new Date()).eq("id", tid);
        trainingEntityService.update(trainingUpdateWrapper);
        adminTrainingRecordManager.syncAlreadyRegisterUserRecord(tid, problem.getId(), newTProblem.getId());
    } else {
        throw new StatusFailException("添加失败!");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) Date(java.util.Date) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 27 with Problem

use of top.hcode.hoj.pojo.entity.problem.Problem in project HOJ by HimitZH.

the class AdminTrainingProblemManager method importTrainingRemoteOJProblem.

public void importTrainingRemoteOJProblem(String name, String problemId, Long tid) throws StatusFailException {
    QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("problem_id", name.toUpperCase() + "-" + problemId);
    Problem problem = problemEntityService.getOne(queryWrapper, false);
    // 如果该题目不存在,需要先导入
    if (problem == null) {
        Session session = SecurityUtils.getSubject().getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        try {
            ProblemStrategy.RemoteProblemInfo otherOJProblemInfo = remoteProblemManager.getOtherOJProblemInfo(name.toUpperCase(), problemId, userRolesVo.getUsername());
            if (otherOJProblemInfo != null) {
                problem = remoteProblemManager.adminAddOtherOJProblem(otherOJProblemInfo, name);
                if (problem == null) {
                    throw new StatusFailException("导入新题目失败!请重新尝试!");
                }
            } else {
                throw new StatusFailException("导入新题目失败!原因:可能是与该OJ链接超时或题号格式错误!");
            }
        } catch (Exception e) {
            throw new StatusFailException(e.getMessage());
        }
    }
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    Problem finalProblem = problem;
    trainingProblemQueryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", finalProblem.getId()).or().eq("display_id", finalProblem.getProblemId()));
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(trainingProblemQueryWrapper, false);
    if (trainingProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的训练展示ID已存在!");
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean isOk = trainingProblemEntityService.saveOrUpdate(newTProblem.setTid(tid).setPid(problem.getId()).setDisplayId(problem.getProblemId()));
    if (isOk) {
        // 添加成功
        // 更新训练最近更新时间
        UpdateWrapper<Training> trainingUpdateWrapper = new UpdateWrapper<>();
        trainingUpdateWrapper.set("gmt_modified", new Date()).eq("id", tid);
        trainingEntityService.update(trainingUpdateWrapper);
        // 异步地同步用户对该题目的提交数据
        adminTrainingRecordManager.syncAlreadyRegisterUserRecord(tid, problem.getId(), newTProblem.getId());
    } else {
        throw new StatusFailException("添加失败!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Training(top.hcode.hoj.pojo.entity.training.Training) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ProblemStrategy(top.hcode.hoj.crawler.problem.ProblemStrategy) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session)

Example 28 with Problem

use of top.hcode.hoj.pojo.entity.problem.Problem in project HOJ by HimitZH.

the class ImportQDUOJProblemManager method QDOJProblemToProblemVo.

private QDOJProblemDto QDOJProblemToProblemVo(JSONObject problemJson) {
    QDOJProblemDto qdojProblemDto = new QDOJProblemDto();
    List<String> tags = (List<String>) problemJson.get("tags");
    qdojProblemDto.setTags(tags.stream().map(UnicodeUtil::toString).collect(Collectors.toList()));
    qdojProblemDto.setLanguages(Arrays.asList("C", "C With O2", "C++", "C++ With O2", "Java", "Python3", "Python2", "Golang", "C#"));
    Object spj = problemJson.getObj("spj");
    boolean isSpj = !JSONUtil.isNull(spj);
    qdojProblemDto.setIsSpj(isSpj);
    Problem problem = new Problem();
    if (isSpj) {
        JSONObject spjJson = JSONUtil.parseObj(spj);
        problem.setSpjCode(spjJson.getStr("code")).setSpjLanguage(spjJson.getStr("language"));
    }
    problem.setAuth(1).setIsGroup(false).setIsUploadCase(true).setSource(problemJson.getStr("source", null)).setDifficulty(1).setProblemId(problemJson.getStr("display_id")).setIsRemoveEndBlank(true).setOpenCaseResult(true).setCodeShare(false).setType(problemJson.getStr("rule_type").equals("ACM") ? 0 : 1).setTitle(problemJson.getStr("title")).setDescription(UnicodeUtil.toString(problemJson.getJSONObject("description").getStr("value"))).setInput(UnicodeUtil.toString(problemJson.getJSONObject("input_description").getStr("value"))).setOutput(UnicodeUtil.toString(problemJson.getJSONObject("output_description").getStr("value"))).setHint(UnicodeUtil.toString(problemJson.getJSONObject("hint").getStr("value"))).setTimeLimit(problemJson.getInt("time_limit")).setMemoryLimit(problemJson.getInt("memory_limit"));
    JSONArray samples = problemJson.getJSONArray("samples");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < samples.size(); i++) {
        JSONObject sample = (JSONObject) samples.get(i);
        String input = sample.getStr("input");
        String output = sample.getStr("output");
        sb.append("<input>").append(input).append("</input>");
        sb.append("<output>").append(output).append("</output>");
    }
    problem.setExamples(sb.toString());
    int sumScore = 0;
    JSONArray testcaseList = problemJson.getJSONArray("test_case_score");
    List<ProblemCase> problemSamples = new LinkedList<>();
    for (int i = 0; i < testcaseList.size(); i++) {
        JSONObject testcase = (JSONObject) testcaseList.get(i);
        String input = testcase.getStr("input_name");
        String output = testcase.getStr("output_name");
        Integer score = testcase.getInt("score", null);
        problemSamples.add(new ProblemCase().setInput(input).setOutput(output).setScore(score));
        if (score != null) {
            sumScore += score;
        }
    }
    problem.setIsRemote(false);
    problem.setIoScore(sumScore);
    qdojProblemDto.setSamples(problemSamples);
    qdojProblemDto.setProblem(problem);
    return qdojProblemDto;
}
Also used : JSONArray(cn.hutool.json.JSONArray) QDOJProblemDto(top.hcode.hoj.pojo.dto.QDOJProblemDto) ProblemCase(top.hcode.hoj.pojo.entity.problem.ProblemCase) UnicodeUtil(cn.hutool.core.text.UnicodeUtil) LinkedList(java.util.LinkedList) JSONObject(cn.hutool.json.JSONObject) LinkedList(java.util.LinkedList) List(java.util.List) JSONObject(cn.hutool.json.JSONObject) Problem(top.hcode.hoj.pojo.entity.problem.Problem)

Example 29 with Problem

use of top.hcode.hoj.pojo.entity.problem.Problem in project HOJ by HimitZH.

the class JudgeManager method submitProblemTestJudge.

public String submitProblemTestJudge(TestJudgeDto testJudgeDto) throws AccessException, StatusFailException, StatusForbiddenException, StatusSystemErrorException {
    judgeValidator.validateTestJudgeInfo(testJudgeDto);
    // 需要获取一下该token对应用户的数据
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    String lockKey = Constants.Account.TEST_JUDGE_LOCK.getCode() + userRolesVo.getUid();
    long count = redisUtils.incr(lockKey, 1);
    if (count > 1) {
        throw new StatusForbiddenException("对不起,您使用在线调试过于频繁,请稍后再尝试!");
    }
    redisUtils.expire(lockKey, 3);
    Problem problem = problemEntityService.getById(testJudgeDto.getPid());
    if (problem == null) {
        throw new StatusFailException("当前题目不存在!不支持在线调试!");
    }
    String uniqueKey = "TEST_JUDGE_" + IdUtil.simpleUUID();
    TestJudgeReq testJudgeReq = new TestJudgeReq();
    testJudgeReq.setMemoryLimit(problem.getMemoryLimit()).setTimeLimit(problem.getTimeLimit()).setStackLimit(problem.getStackLimit()).setCode(testJudgeDto.getCode()).setLanguage(testJudgeDto.getLanguage()).setUniqueKey(uniqueKey).setExpectedOutput(testJudgeDto.getExpectedOutput()).setTestCaseInput(testJudgeDto.getUserInput()).setProblemJudgeMode(problem.getJudgeMode()).setIsRemoveEndBlank(problem.getIsRemoveEndBlank() || problem.getIsRemote());
    String userExtraFile = problem.getUserExtraFile();
    if (!StringUtils.isEmpty(userExtraFile)) {
        testJudgeReq.setExtraFile((HashMap<String, String>) JSONUtil.toBean(userExtraFile, Map.class));
    }
    judgeDispatcher.sendTestJudgeTask(testJudgeReq);
    redisUtils.set(uniqueKey, TestJudgeRes.builder().status(Constants.Judge.STATUS_PENDING.getStatus()).build(), 10 * 60);
    return uniqueKey;
}
Also used : Problem(top.hcode.hoj.pojo.entity.problem.Problem) Session(org.apache.shiro.session.Session)

Example 30 with Problem

use of top.hcode.hoj.pojo.entity.problem.Problem 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)

Aggregations

Problem (top.hcode.hoj.pojo.entity.problem.Problem)69 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)46 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)35 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)26 Session (org.apache.shiro.session.Session)25 Transactional (org.springframework.transaction.annotation.Transactional)24 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)22 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)19 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)15 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)15 Judge (top.hcode.hoj.pojo.entity.judge.Judge)15 HttpSession (javax.servlet.http.HttpSession)13 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)13 Group (top.hcode.hoj.pojo.entity.group.Group)13 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)12 LinkedList (java.util.LinkedList)9 Contest (top.hcode.hoj.pojo.entity.contest.Contest)9 ContestRecord (top.hcode.hoj.pojo.entity.contest.ContestRecord)8 Tag (top.hcode.hoj.pojo.entity.problem.Tag)7 JudgeCase (top.hcode.hoj.pojo.entity.judge.JudgeCase)6