Search in sources :

Example 66 with Problem

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

the class BeforeDispatchInitManager method initContestSubmission.

@Transactional(rollbackFor = Exception.class)
public void initContestSubmission(Long cid, String displayId, UserRolesVo userRolesVo, Judge judge) throws StatusNotFoundException, StatusForbiddenException {
    // 首先判断一下比赛的状态是否是正在进行,结束状态都不能提交,比赛前比赛管理员可以提交
    Contest contest = contestEntityService.getById(cid);
    if (contest == null) {
        throw new StatusNotFoundException("对不起,该比赛不存在!");
    }
    if (contest.getStatus().intValue() == Constants.Contest.STATUS_ENDED.getCode()) {
        throw new StatusForbiddenException("比赛已结束,不可再提交!");
    }
    // 是否为超级管理员或者该比赛的创建者,则为比赛管理者
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    if (!isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), contest.getGid()))) {
        if (contest.getStatus().intValue() == Constants.Contest.STATUS_SCHEDULED.getCode()) {
            throw new StatusForbiddenException("比赛未开始,不可提交!");
        }
        // 需要检查是否有权限在当前比赛进行提交
        contestValidator.validateJudgeAuth(contest, userRolesVo.getUid());
        // 需要校验当前比赛是否为保护或私有比赛,同时是否开启账号规则限制,如果有,需要对当前用户的用户名进行验证
        if (contest.getOpenAccountLimit() && !contestValidator.validateAccountRule(contest.getAccountLimitRule(), userRolesVo.getUsername())) {
            throw new StatusForbiddenException("对不起!本次比赛只允许符合特定账号规则的用户参赛!");
        }
    }
    // 查询获取对应的pid和cpid
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    contestProblemQueryWrapper.eq("cid", cid).eq("display_id", displayId);
    ContestProblem contestProblem = contestProblemEntityService.getOne(contestProblemQueryWrapper, false);
    judge.setCpid(contestProblem.getId()).setPid(contestProblem.getPid()).setGid(contest.getGid());
    Problem problem = problemEntityService.getById(contestProblem.getPid());
    if (problem.getAuth() == 2) {
        throw new StatusForbiddenException("错误!当前题目已被隐藏,不可提交!");
    }
    judge.setDisplayPid(problem.getProblemId());
    // 将新提交数据插入数据库
    judgeEntityService.save(judge);
    // 同时初始化写入contest_record表
    ContestRecord contestRecord = new ContestRecord();
    contestRecord.setDisplayId(displayId).setCpid(contestProblem.getId()).setSubmitId(judge.getSubmitId()).setPid(judge.getPid()).setUsername(userRolesVo.getUsername()).setRealname(userRolesVo.getRealname()).setUid(userRolesVo.getUid()).setCid(judge.getCid()).setSubmitTime(judge.getSubmitTime());
    if (contest.getStatus().intValue() == Constants.Contest.STATUS_SCHEDULED.getCode()) {
        contestRecord.setTime(0L);
    } else {
        // 设置比赛开始时间到提交时间之间的秒数
        contestRecord.setTime(DateUtil.between(contest.getStartTime(), judge.getSubmitTime(), DateUnit.SECOND));
    }
    contestRecordEntityService.save(contestRecord);
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Contest(top.hcode.hoj.pojo.entity.contest.Contest) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Transactional(org.springframework.transaction.annotation.Transactional)

Example 67 with Problem

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

the class BeforeDispatchInitManager method initCommonSubmission.

public void initCommonSubmission(String problemId, Judge judge) throws StatusForbiddenException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
    problemQueryWrapper.eq("problem_id", problemId);
    Problem problem = problemEntityService.getOne(problemQueryWrapper, false);
    if (problem.getAuth() == 2) {
        throw new StatusForbiddenException("错误!当前题目不可提交!");
    }
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    if (problem.getIsGroup()) {
        if (!isRoot && !groupValidator.isGroupMember(userRolesVo.getUid(), problem.getGid())) {
            throw new StatusForbiddenException("对不起,您并非该题目所属的团队内成员,无权进行提交!");
        }
    }
    judge.setCpid(0L).setPid(problem.getId()).setDisplayPid(problem.getProblemId());
    // 将新提交数据插入数据库
    judgeEntityService.save(judge);
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Session(org.apache.shiro.session.Session)

Example 68 with Problem

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

the class DataBackupApplicationTests method Test7.

@Test
public void Test7() throws IOException {
    String JUDGE_NAME = "CF";
    String HOST = "https://codeforces.com";
    String PROBLEM_URL = "/problemset/problem/%s/%s";
    String problemId = "750A";
    String contestId = ReUtil.get("([0-9]+)[A-Z]{1}[0-9]{0,1}", problemId, 1);
    String problemNum = ReUtil.get("[0-9]+([A-Z]{1}[0-9]{0,1})", problemId, 1);
    String url = HOST + String.format(PROBLEM_URL, contestId, problemNum);
    Connection connection = JsoupUtils.getConnectionFromUrl(url, null, null);
    Document document = JsoupUtils.getDocument(connection, null);
    String html = document.html();
    Problem info = new Problem();
    info.setProblemId(JUDGE_NAME + "-" + problemId);
    info.setTitle(ReUtil.get("<div class=\"title\">\\s*" + problemNum + "\\. ([\\s\\S]*?)</div>", html, 1).trim());
    info.setTimeLimit(1000 * Integer.parseInt(ReUtil.get("</div>([\\d\\.]+) (seconds?|s)\\s*</div>", html, 1)));
    info.setMemoryLimit(Integer.parseInt(ReUtil.get("</div>(\\d+) (megabytes|MB)\\s*</div>", html, 1)));
    String tmpDesc = ReUtil.get("standard output\\s*</div></div><div>([\\s\\S]*?)</div><div class=\"input-specification", html, 1);
    if (StringUtils.isEmpty(tmpDesc)) {
        tmpDesc = "<div>" + ReUtil.get("(<div class=\"input-file\">[\\s\\S]*?)</div><div class=\"input-specification", html, 1);
    }
    info.setDescription(tmpDesc.replaceAll("src=\"../../", "src=\"" + HOST + "/"));
    info.setInput(ReUtil.get("<div class=\"section-title\">\\s*Input\\s*</div>([\\s\\S]*?)</div><div class=\"output-specification\">", html, 1));
    info.setOutput(ReUtil.get("<div class=\"section-title\">\\s*Output\\s*</div>([\\s\\S]*?)</div><div class=\"sample-tests\">", html, 1));
    List<String> inputExampleList = ReUtil.findAll(Pattern.compile("<div class=\"input\"><div class=\"title\">Input</div><pre>([\\s\\S]*?)</pre></div>"), html, 1);
    List<String> outputExampleList = ReUtil.findAll(Pattern.compile("<div class=\"output\"><div class=\"title\">Output</div><pre>([\\s\\S]*?)</pre></div>"), html, 1);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < inputExampleList.size() && i < outputExampleList.size(); i++) {
        sb.append("<input>");
        sb.append(inputExampleList.get(i).replace("<br>", "")).append("</input>");
        sb.append("<output>");
        sb.append(outputExampleList.get(i).replace("<br>", "")).append("</output>");
    }
    info.setExamples(sb.toString());
    System.out.println(info.getExamples());
    info.setHint(ReUtil.get("<div class=\"section-title\">\\s*Note\\s*</div>([\\s\\S]*?)</div></div>", html, 1));
    info.setIsRemote(true);
    info.setSource(String.format("<p>Problem:<a style='color:#1A5CC8' href='https://codeforces.com/problemset/problem/%s/%s'>%s</a></p><p>" + "Contest:" + ReUtil.get("(<a[^<>]+/contest/\\d+\">.+?</a>)", html, 1).replace("/contest", HOST + "/contest").replace("color: black", "color: #009688;") + "</p>", contestId, problemNum, JUDGE_NAME + "-" + problemId));
    List<String> all = ReUtil.findAll(Pattern.compile("<span class=\"tag-box\" style=\"font-size:1\\.2rem;\" title=\"[\\s\\S]*?\">([\\s\\S]*?)</span>"), html, 1);
}
Also used : Connection(org.jsoup.Connection) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Document(org.jsoup.nodes.Document) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 69 with Problem

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

the class DataBackupApplicationTests method Test6.

@Test
public void Test6() throws IOException {
    String JUDGE_NAME = "HDU";
    String HOST = "http://acm.hdu.edu.cn";
    String PROBLEM_URL = "/showproblem.php?pid=%s";
    Problem info = new Problem();
    String url = HOST + String.format(PROBLEM_URL, 1016);
    Connection connection = JsoupUtils.getConnectionFromUrl(url, null, null);
    Document document = JsoupUtils.getDocument(connection, null);
    String html = document.html();
    info.setDescription(ReUtil.get(">Problem Description</div> <div class=.*?>([\\s\\S]*?)</div>", html, 1).replaceAll("src=\"../../", "src=\"" + HOST + "/"));
    info.setInput(ReUtil.get(">Input</div> <div class=.*?>([\\s\\S]*?)</div>", html, 1));
    info.setOutput(ReUtil.get(">Output</div> <div class=.*?>([\\s\\S]*?)</div>", html, 1));
    info.setIsRemote(true);
    System.out.println(info.getDescription());
    System.out.println(info.getInput());
    System.out.println(info.getOutput());
}
Also used : Connection(org.jsoup.Connection) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Document(org.jsoup.nodes.Document) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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