Search in sources :

Example 1 with TrainingProblem

use of top.hcode.hoj.pojo.entity.training.TrainingProblem in project HOJ by HimitZH.

the class AdminTrainingController method importTrainingRemoteOJProblem.

@GetMapping("/import-remote-oj-problem")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
@Transactional(rollbackFor = Exception.class)
public CommonResult importTrainingRemoteOJProblem(@RequestParam("name") String name, @RequestParam("problemId") String problemId, @RequestParam("tid") Long tid, 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<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 = trainingProblemService.getOne(trainingProblemQueryWrapper, false);
    if (trainingProblem != null) {
        return CommonResult.errorResponse("添加失败,该题目已添加或者题目的训练展示ID已存在!", CommonResult.STATUS_FAIL);
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean result = trainingProblemService.saveOrUpdate(newTProblem.setTid(tid).setPid(problem.getId()).setDisplayId(problem.getProblemId()));
    if (result) {
        // 添加成功
        trainingRegisterService.syncAlreadyRegisterUserRecord(tid, problem.getId(), newTProblem.getId());
        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) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ProblemStrategy(top.hcode.hoj.crawler.problem.ProblemStrategy) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with TrainingProblem

use of top.hcode.hoj.pojo.entity.training.TrainingProblem in project HOJ by HimitZH.

the class AdminTrainingController 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 tidStr = params.get("tid");
    String displayId = params.get("displayId");
    if (StringUtils.isEmpty(pidStr) || StringUtils.isEmpty(tidStr) || StringUtils.isEmpty(displayId)) {
        return CommonResult.errorResponse("参数错误!", CommonResult.STATUS_FAIL);
    }
    Long pid = Long.valueOf(pidStr);
    Long tid = Long.valueOf(tidStr);
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    trainingProblemQueryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", pid).or().eq("display_id", displayId));
    TrainingProblem trainingProblem = trainingProblemService.getOne(trainingProblemQueryWrapper, false);
    if (trainingProblem != null) {
        return CommonResult.errorResponse("添加失败,该题目已添加或者题目的训练展示ID已存在!", CommonResult.STATUS_FAIL);
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean result = trainingProblemService.saveOrUpdate(newTProblem.setTid(tid).setPid(pid).setDisplayId(displayId));
    if (result) {
        // 添加成功
        trainingRegisterService.syncAlreadyRegisterUserRecord(tid, pid, newTProblem.getId());
        return CommonResult.successResponse(null, "添加成功!");
    } else {
        return CommonResult.errorResponse("添加失败", CommonResult.STATUS_FAIL);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Example 3 with TrainingProblem

use of top.hcode.hoj.pojo.entity.training.TrainingProblem in project HOJ by HimitZH.

the class TrainingRecordServiceImpl method syncAllUserProblemRecord.

private void syncAllUserProblemRecord(Long tid) {
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    trainingProblemQueryWrapper.eq("tid", tid);
    List<TrainingProblem> trainingProblemList = trainingProblemService.list(trainingProblemQueryWrapper);
    if (trainingProblemList.size() == 0) {
        return;
    }
    List<Long> pidList = new ArrayList<>();
    HashMap<Long, Long> pidMapTPid = new HashMap<>();
    for (TrainingProblem trainingProblem : trainingProblemList) {
        pidList.add(trainingProblem.getPid());
        pidMapTPid.put(trainingProblem.getPid(), trainingProblem.getId());
    }
    List<String> uidList = trainingRegisterService.getAlreadyRegisterUidList(tid);
    if (uidList.size() == 0) {
        return;
    }
    QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
    judgeQueryWrapper.in("pid", pidList).eq("cid", 0).eq("status", // 只同步ac的提交
    Constants.Judge.STATUS_ACCEPTED.getStatus()).in("uid", uidList);
    List<Judge> judgeList = judgeService.list(judgeQueryWrapper);
    saveBatchNewRecordByJudgeList(judgeList, tid, null, pidMapTPid);
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Example 4 with TrainingProblem

use of top.hcode.hoj.pojo.entity.training.TrainingProblem in project HOJ by HimitZH.

the class TrainingRecordServiceImpl method syncUserSubmissionToRecordByTid.

@Override
@Async
public void syncUserSubmissionToRecordByTid(Long tid, String uid) {
    QueryWrapper<TrainingProblem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tid", tid);
    List<TrainingProblem> trainingProblemList = trainingProblemService.list(queryWrapper);
    List<Long> pidList = new ArrayList<>();
    HashMap<Long, Long> pidMapTPid = new HashMap<>();
    for (TrainingProblem trainingProblem : trainingProblemList) {
        pidList.add(trainingProblem.getPid());
        pidMapTPid.put(trainingProblem.getPid(), trainingProblem.getId());
    }
    if (!CollectionUtils.isEmpty(pidList)) {
        QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
        judgeQueryWrapper.in("pid", pidList).eq("cid", 0).eq("status", // 只同步ac的提交
        Constants.Judge.STATUS_ACCEPTED.getStatus()).eq("uid", uid);
        List<Judge> judgeList = judgeService.list(judgeQueryWrapper);
        saveBatchNewRecordByJudgeList(judgeList, tid, null, pidMapTPid);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Async(org.springframework.scheduling.annotation.Async)

Example 5 with TrainingProblem

use of top.hcode.hoj.pojo.entity.training.TrainingProblem in project HOJ by HimitZH.

the class BeforeDispatchInitManager method initTrainingSubmission.

@Transactional(rollbackFor = Exception.class)
public void initTrainingSubmission(Long tid, String displayId, UserRolesVo userRolesVo, Judge judge) throws StatusForbiddenException, StatusFailException, StatusAccessDeniedException {
    Training training = trainingEntityService.getById(tid);
    if (training == null || !training.getStatus()) {
        throw new StatusFailException("该训练不存在或不允许显示!");
    }
    trainingValidator.validateTrainingAuth(training, userRolesVo);
    // 查询获取对应的pid和cpid
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    trainingProblemQueryWrapper.eq("tid", tid).eq("display_id", displayId);
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(trainingProblemQueryWrapper);
    judge.setPid(trainingProblem.getPid());
    Problem problem = problemEntityService.getById(trainingProblem.getPid());
    if (problem.getAuth() == 2) {
        throw new StatusForbiddenException("错误!当前题目不可提交!");
    }
    judge.setDisplayPid(problem.getProblemId()).setGid(training.getGid());
    // 将新提交数据插入数据库
    judgeEntityService.save(judge);
    // 非私有训练不记录
    if (!training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        return;
    }
    TrainingRecord trainingRecord = new TrainingRecord();
    trainingRecord.setPid(problem.getId()).setTid(tid).setTpid(trainingProblem.getId()).setSubmitId(judge.getSubmitId()).setUid(userRolesVo.getUid());
    trainingRecordEntityService.save(trainingRecord);
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRecord(top.hcode.hoj.pojo.entity.training.TrainingRecord) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)10 TrainingProblem (top.hcode.hoj.pojo.entity.training.TrainingProblem)10 Judge (top.hcode.hoj.pojo.entity.judge.Judge)4 Problem (top.hcode.hoj.pojo.entity.problem.Problem)4 Training (top.hcode.hoj.pojo.entity.training.Training)4 Transactional (org.springframework.transaction.annotation.Transactional)3 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)3 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)2 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)2 Async (org.springframework.scheduling.annotation.Async)2 ProblemStrategy (top.hcode.hoj.crawler.problem.ProblemStrategy)2 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)2 TrainingRecord (top.hcode.hoj.pojo.entity.training.TrainingRecord)2 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)2 HttpSession (javax.servlet.http.HttpSession)1 Session (org.apache.shiro.session.Session)1 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)1