Search in sources :

Example 1 with CommonResult

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, "获取成功");
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) CommonResult(top.hcode.hoj.common.result.CommonResult) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 2 with CommonResult

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("提交失败!");
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) CommonResult(top.hcode.hoj.common.result.CommonResult) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 3 with CommonResult

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, "获取最新判题数据成功!");
}
Also used : java.util(java.util) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingRecordServiceImpl(top.hcode.hoj.service.training.impl.TrainingRecordServiceImpl) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) JudgeVo(top.hcode.hoj.pojo.vo.JudgeVo) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) RemoteJudgeDispatcher(top.hcode.hoj.judge.remote.RemoteJudgeDispatcher) Autowired(org.springframework.beans.factory.annotation.Autowired) RedisUtils(top.hcode.hoj.utils.RedisUtils) CommonResult(top.hcode.hoj.common.result.CommonResult) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Value(org.springframework.beans.factory.annotation.Value) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) HttpServletRequest(javax.servlet.http.HttpServletRequest) ProblemService(top.hcode.hoj.service.problem.ProblemService) JudgeCaseServiceImpl(top.hcode.hoj.service.judge.impl.JudgeCaseServiceImpl) ContestRecordServiceImpl(top.hcode.hoj.service.contest.impl.ContestRecordServiceImpl) JudgeServiceImpl(top.hcode.hoj.service.judge.impl.JudgeServiceImpl) JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) JudgeDispatcher(top.hcode.hoj.judge.self.JudgeDispatcher) HttpSession(javax.servlet.http.HttpSession) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) IpUtils(top.hcode.hoj.utils.IpUtils) Contest(top.hcode.hoj.pojo.entity.contest.Contest) SubmitIdListDto(top.hcode.hoj.pojo.dto.SubmitIdListDto) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) CollectionUtils(org.springframework.util.CollectionUtils) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ToJudgeDto(top.hcode.hoj.pojo.dto.ToJudgeDto) ContestServiceImpl(top.hcode.hoj.service.contest.impl.ContestServiceImpl) Constants(top.hcode.hoj.utils.Constants) UserAcproblemServiceImpl(top.hcode.hoj.service.user.impl.UserAcproblemServiceImpl) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) IPage(com.baomidou.mybatisplus.core.metadata.IPage) SecurityUtils(org.apache.shiro.SecurityUtils) Transactional(org.springframework.transaction.annotation.Transactional) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Example 4 with CommonResult

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, "代码提交成功!");
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) CommonResult(top.hcode.hoj.common.result.CommonResult) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with CommonResult

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, "获取最新判题数据成功!");
}
Also used : java.util(java.util) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingRecordServiceImpl(top.hcode.hoj.service.training.impl.TrainingRecordServiceImpl) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) JudgeVo(top.hcode.hoj.pojo.vo.JudgeVo) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) RemoteJudgeDispatcher(top.hcode.hoj.judge.remote.RemoteJudgeDispatcher) Autowired(org.springframework.beans.factory.annotation.Autowired) RedisUtils(top.hcode.hoj.utils.RedisUtils) CommonResult(top.hcode.hoj.common.result.CommonResult) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Value(org.springframework.beans.factory.annotation.Value) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) HttpServletRequest(javax.servlet.http.HttpServletRequest) ProblemService(top.hcode.hoj.service.problem.ProblemService) JudgeCaseServiceImpl(top.hcode.hoj.service.judge.impl.JudgeCaseServiceImpl) ContestRecordServiceImpl(top.hcode.hoj.service.contest.impl.ContestRecordServiceImpl) JudgeServiceImpl(top.hcode.hoj.service.judge.impl.JudgeServiceImpl) JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) JudgeDispatcher(top.hcode.hoj.judge.self.JudgeDispatcher) HttpSession(javax.servlet.http.HttpSession) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) IpUtils(top.hcode.hoj.utils.IpUtils) Contest(top.hcode.hoj.pojo.entity.contest.Contest) SubmitIdListDto(top.hcode.hoj.pojo.dto.SubmitIdListDto) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) CollectionUtils(org.springframework.util.CollectionUtils) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ToJudgeDto(top.hcode.hoj.pojo.dto.ToJudgeDto) ContestServiceImpl(top.hcode.hoj.service.contest.impl.ContestServiceImpl) Constants(top.hcode.hoj.utils.Constants) UserAcproblemServiceImpl(top.hcode.hoj.service.user.impl.UserAcproblemServiceImpl) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) IPage(com.baomidou.mybatisplus.core.metadata.IPage) SecurityUtils(org.apache.shiro.SecurityUtils) Transactional(org.springframework.transaction.annotation.Transactional) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Aggregations

CommonResult (top.hcode.hoj.common.result.CommonResult)18 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)13 HttpSession (javax.servlet.http.HttpSession)10 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)7 Transactional (org.springframework.transaction.annotation.Transactional)5 Contest (top.hcode.hoj.pojo.entity.contest.Contest)4 Problem (top.hcode.hoj.pojo.entity.problem.Problem)4 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)4 IPage (com.baomidou.mybatisplus.core.metadata.IPage)3 java.util (java.util)3 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 org.springframework.web.bind.annotation (org.springframework.web.bind.annotation)3 ContestRecord (top.hcode.hoj.pojo.entity.contest.ContestRecord)3 Judge (top.hcode.hoj.pojo.entity.judge.Judge)3 Constants (top.hcode.hoj.utils.Constants)3 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 SecurityUtils (org.apache.shiro.SecurityUtils)2 Value (org.springframework.beans.factory.annotation.Value)2