use of top.hcode.hoj.pojo.entity.training.Training 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);
}
use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.
the class AdminTrainingManager method changeTrainingStatus.
public void changeTrainingStatus(Long tid, String author, Boolean status) throws StatusForbiddenException, StatusFailException {
// 获取当前登录的用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 是否为超级管理员
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
// 只有超级管理员和训练拥有者才能操作
if (!isRoot && !userRolesVo.getUsername().equals(author)) {
throw new StatusForbiddenException("对不起,你无权限操作!");
}
boolean isOk = trainingEntityService.saveOrUpdate(new Training().setId(tid).setStatus(status));
if (!isOk) {
throw new StatusFailException("修改失败");
}
}
use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.
the class AdminTrainingManager method addTraining.
@Transactional(rollbackFor = Exception.class)
public void addTraining(TrainingDto trainingDto) throws StatusFailException {
Training training = trainingDto.getTraining();
trainingEntityService.save(training);
TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
if (trainingCategory.getId() == null) {
try {
trainingCategoryEntityService.save(trainingCategory);
} catch (Exception ignored) {
QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", trainingCategory.getName());
trainingCategory = trainingCategoryEntityService.getOne(queryWrapper, false);
}
}
boolean isOk = mappingTrainingCategoryEntityService.save(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
if (!isOk) {
throw new StatusFailException("添加失败!");
}
}
use of top.hcode.hoj.pojo.entity.training.Training 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("添加失败!");
}
}
use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.
the class TrainingRecordServiceImpl method submitTrainingProblem.
@Override
@Transactional(rollbackFor = Exception.class)
public CommonResult submitTrainingProblem(ToJudgeDto judgeDto, UserRolesVo userRolesVo, Judge judge) {
Training training = trainingService.getById(judgeDto.getTid());
if (training == null || !training.getStatus()) {
return CommonResult.errorResponse("该训练不存在或不允许显示!");
}
CommonResult result = trainingRegisterService.checkTrainingAuth(training, userRolesVo);
if (result != null) {
return result;
}
// 查询获取对应的pid和cpid
QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
trainingProblemQueryWrapper.eq("tid", judgeDto.getTid()).eq("display_id", judgeDto.getPid());
TrainingProblem trainingProblem = trainingProblemService.getOne(trainingProblemQueryWrapper);
judge.setPid(trainingProblem.getPid());
Problem problem = problemService.getById(trainingProblem.getPid());
if (problem.getAuth() == 2) {
return CommonResult.errorResponse("错误!当前题目不可提交!", CommonResult.STATUS_FORBIDDEN);
}
judge.setDisplayPid(problem.getProblemId());
// 将新提交数据插入数据库
judgeService.saveOrUpdate(judge);
// 非私有训练不记录
if (!training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
return null;
}
TrainingRecord trainingRecord = new TrainingRecord();
trainingRecord.setPid(problem.getId()).setTid(judgeDto.getTid()).setTpid(trainingProblem.getId()).setSubmitId(judge.getSubmitId()).setUid(userRolesVo.getUid());
trainingRecordMapper.insert(trainingRecord);
return null;
}
Aggregations