use of top.hcode.hoj.common.result.CommonResult in project HOJ by HimitZH.
the class ContestController method getContestAnnouncement.
/**
* @MethodName getContestAnnouncement
* @Params * @param null
* @Description 获得比赛的通知列表
* @Return CommonResult
* @Since 2020/10/28
*/
@GetMapping("/get-contest-announcement")
@RequiresAuthentication
public CommonResult getContestAnnouncement(@RequestParam(value = "cid", required = true) Long cid, @RequestParam(value = "limit", required = false) Integer limit, @RequestParam(value = "currentPage", required = false) Integer currentPage, HttpServletRequest request) {
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 获取本场比赛的状态
Contest contest = contestService.getById(cid);
// 超级管理员或者该比赛的创建者,则为比赛管理者
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
/**
* 需要对该比赛做判断,是否处于开始或结束状态才可以获取公告,同时若是私有赛需要判断是否已注册(比赛管理员包括超级管理员可以直接获取)
*/
CommonResult commonResult = contestService.checkContestAuth(contest, userRolesVo, isRoot);
if (commonResult != null) {
return commonResult;
}
if (currentPage == null || currentPage < 1)
currentPage = 1;
if (limit == null || limit < 1)
limit = 10;
IPage<AnnouncementVo> contestAnnouncementList = announcementService.getContestAnnouncement(cid, true, limit, currentPage);
if (contestAnnouncementList.getTotal() == 0) {
return CommonResult.successResponse(contestAnnouncementList, "暂无数据");
} else {
return CommonResult.successResponse(contestAnnouncementList, "获取成功");
}
}
use of top.hcode.hoj.common.result.CommonResult in project HOJ by HimitZH.
the class ContestController method submitPrintText.
/**
* @param contestPrintDto
* @param request
* @MethodName submitPrintText
* @Description 提交比赛文本打印内容
* @Return
* @Since 2021/9/20
*/
@PostMapping("/submit-print-text")
@RequiresAuthentication
public CommonResult submitPrintText(@RequestBody ContestPrintDto contestPrintDto, HttpServletRequest request) {
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 获取本场比赛的状态
Contest contest = contestService.getById(contestPrintDto.getCid());
// 超级管理员或者该比赛的创建者,则为比赛管理者
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
/**
* 需要对该比赛做判断,是否处于开始或结束状态才可以提交打印内容,同时若是私有赛需要判断是否已注册(比赛管理员包括超级管理员可以直接获取)
*/
CommonResult commonResult = contestService.checkContestAuth(contest, userRolesVo, isRoot);
if (commonResult != null) {
return commonResult;
}
String lockKey = Constants.Account.CONTEST_ADD_PRINT_LOCK.getCode() + userRolesVo.getUid();
if (redisUtils.hasKey(lockKey)) {
long expire = redisUtils.getExpire(lockKey);
return CommonResult.errorResponse("提交打印功能限制,请在" + expire + "秒后再进行提交!", CommonResult.STATUS_FORBIDDEN);
} else {
redisUtils.set(lockKey, 1, 30);
}
boolean result = contestPrintService.saveOrUpdate(new ContestPrint().setCid(contestPrintDto.getCid()).setContent(contestPrintDto.getContent()).setUsername(userRolesVo.getUsername()).setRealname(userRolesVo.getRealname()));
if (result) {
return CommonResult.successResponse(null, "提交成功,请等待工作人员打印!");
} else {
return CommonResult.errorResponse("提交失败!");
}
}
use of top.hcode.hoj.common.result.CommonResult in project HOJ by HimitZH.
the class JudgeController method checkCommonJudgeResult.
/**
* @MethodName checkJudgeResult
* @Params * @param null
* @Description 对提交列表状态为Pending和Judging的提交进行更新检查
* @Return
* @Since 2021/1/3
*/
@RequestMapping(value = "/check-submissions-status", method = RequestMethod.POST)
public CommonResult checkCommonJudgeResult(@RequestBody SubmitIdListDto submitIdListDto) {
List<Long> submitIds = submitIdListDto.getSubmitIds();
if (CollectionUtils.isEmpty(submitIds)) {
return CommonResult.successResponse(new HashMap<>(), "查询的提交id列表为空!");
}
QueryWrapper<Judge> queryWrapper = new QueryWrapper<>();
// lambada表达式过滤掉code
queryWrapper.select(Judge.class, info -> !info.getColumn().equals("code")).in("submit_id", submitIds);
List<Judge> judgeList = judgeService.list(queryWrapper);
HashMap<Long, Object> result = new HashMap<>();
for (Judge judge : judgeList) {
judge.setCode(null);
judge.setErrorMessage(null);
judge.setVjudgeUsername(null);
judge.setVjudgeSubmitId(null);
judge.setVjudgePassword(null);
result.put(judge.getSubmitId(), judge);
}
return CommonResult.successResponse(result, "获取最新判题数据成功!");
}
use of top.hcode.hoj.common.result.CommonResult in project HOJ by HimitZH.
the class JudgeController method submitProblemJudge.
/**
* @MethodName submitProblemJudge
* @Params * @param null
* @Description 核心方法 判题通过openfeign调用判题系统服务
* @Return CommonResult
* @Since 2020/10/30
*/
@RequiresAuthentication
@RequiresPermissions("submit")
@RequestMapping(value = "/submit-problem-judge", method = RequestMethod.POST)
@Transactional(rollbackFor = Exception.class)
public CommonResult submitProblemJudge(@RequestBody ToJudgeDto judgeDto, HttpServletRequest request) {
CommonResult checkResult = judgeService.checkSubmissionInfo(judgeDto);
if (checkResult != null) {
return checkResult;
}
// 需要获取一下该token对应用户的数据
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isContestSubmission = judgeDto.getCid() != 0;
boolean isTrainingSubmission = judgeDto.getTid() != null && judgeDto.getTid() != 0;
if (!isContestSubmission) {
// 非比赛提交限制8秒提交一次
String lockKey = Constants.Account.SUBMIT_NON_CONTEST_LOCK.getCode() + userRolesVo.getUid();
long count = redisUtils.incr(lockKey, 1);
if (count > 1) {
return CommonResult.errorResponse("对不起,您的提交频率过快,请稍后再尝试!", CommonResult.STATUS_FORBIDDEN);
}
redisUtils.expire(lockKey, 8);
}
// 将提交先写入数据库,准备调用判题服务器
Judge judge = new Judge();
// 默认设置代码为单独自己可见
judge.setShare(false).setCode(judgeDto.getCode()).setCid(judgeDto.getCid()).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));
CommonResult result = null;
// 如果比赛id不等于0,则说明为比赛提交
if (isContestSubmission) {
result = contestRecordService.submitContestProblem(judgeDto, userRolesVo, judge);
} else if (isTrainingSubmission) {
result = trainingRecordService.submitTrainingProblem(judgeDto, userRolesVo, judge);
} else {
// 如果不是比赛提交和训练提交
result = judgeService.submitProblem(judgeDto, judge);
}
if (result != null) {
return result;
}
// 将提交加入任务队列
if (judgeDto.getIsRemote()) {
// 如果是远程oj判题
remoteJudgeDispatcher.sendTask(judge, judgeToken, judge.getDisplayPid(), isContestSubmission, false);
} else {
judgeDispatcher.sendTask(judge, judgeToken, isContestSubmission);
}
return CommonResult.successResponse(judge, "代码提交成功!");
}
use of top.hcode.hoj.common.result.CommonResult in project HOJ by HimitZH.
the class JudgeController method checkContestJudgeResult.
/**
* @MethodName checkContestJudgeResult
* @Params * @param submitIdListDto
* @Description 需要检查是否为封榜,是否可以查询结果,避免有人恶意查询
* @Return
* @Since 2021/6/11
*/
@RequestMapping(value = "/check-contest-submissions-status", method = RequestMethod.POST)
@RequiresAuthentication
public CommonResult checkContestJudgeResult(@RequestBody SubmitIdListDto submitIdListDto, HttpServletRequest request) {
if (submitIdListDto.getCid() == null) {
return CommonResult.errorResponse("查询比赛ID不能为空");
}
if (CollectionUtils.isEmpty(submitIdListDto.getSubmitIds())) {
return CommonResult.successResponse(new HashMap<>(), "查询的提交id列表为空!");
}
HttpSession session = request.getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 是否为超级管理员
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
Contest contest = contestService.getById(submitIdListDto.getCid());
boolean isContestAdmin = isRoot || userRolesVo.getUid().equals(contest.getUid());
// 如果是封榜时间且不是比赛管理员和超级管理员
boolean isSealRank = contestService.isSealRank(userRolesVo.getUid(), contest, true, isRoot);
QueryWrapper<Judge> queryWrapper = new QueryWrapper<>();
// lambada表达式过滤掉code
queryWrapper.select(Judge.class, info -> !info.getColumn().equals("code")).in("submit_id", submitIdListDto.getSubmitIds()).eq("cid", submitIdListDto.getCid()).between(isSealRank, "submit_time", contest.getStartTime(), contest.getSealRankTime());
List<Judge> judgeList = judgeService.list(queryWrapper);
HashMap<Long, Object> result = new HashMap<>();
for (Judge judge : judgeList) {
judge.setCode(null);
judge.setDisplayPid(null);
judge.setErrorMessage(null);
judge.setVjudgeUsername(null);
judge.setVjudgeSubmitId(null);
judge.setVjudgePassword(null);
if (!judge.getUid().equals(userRolesVo.getUid()) && !isContestAdmin) {
judge.setTime(null);
judge.setMemory(null);
judge.setLength(null);
}
result.put(judge.getSubmitId(), judge);
}
return CommonResult.successResponse(result, "获取最新判题数据成功!");
}
Aggregations