Search in sources :

Example 16 with Problem

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

the class AtCoderProblemStrategy method getProblemInfo.

@Override
public RemoteProblemInfo getProblemInfo(String problemId, String author) throws Exception {
    problemId = problemId.toLowerCase();
    boolean isMatch = ReUtil.isMatch("[a-z]+[0-9]+_[a-z]+", problemId);
    if (!isMatch) {
        throw new IllegalArgumentException("AtCoder: Incorrect problem id format! Must be like `abc110_a`");
    }
    String contestId = problemId.split("_")[0];
    String body = HttpUtil.get(getProblemUrl(problemId, contestId));
    Pattern pattern = Pattern.compile("Time Limit: (\\d+) sec / Memory Limit: (\\d+) MB");
    Matcher matcher = pattern.matcher(body);
    Validate.isTrue(matcher.find());
    String timeLimit = matcher.group(1).trim();
    String memoryLimit = matcher.group(2).trim();
    String title = ReUtil.get("<title>[\\s\\S]*? - ([\\s\\S]*?)</title>", body, 1);
    Problem problem = new Problem();
    problem.setProblemId(getJudgeName() + "-" + problemId).setAuthor(author).setTitle(title).setType(0).setTimeLimit(Integer.parseInt(timeLimit) * 1000).setMemoryLimit(Integer.parseInt(memoryLimit)).setIsRemote(true).setSource(getProblemSource(problemId, contestId)).setAuth(1).setOpenCaseResult(false).setIsRemoveEndBlank(false).setIsGroup(false).setDifficulty(// 默认为中等
    1);
    if (body.contains("Problem Statement")) {
        String desc = ReUtil.get("<h3>Problem Statement</h3>([\\s\\S]*?)</section>[\\s\\S]*?</div>", body, 1);
        desc = desc.replaceAll("<var>", "\\$").replaceAll("</var>", "\\$");
        desc = desc.replaceAll("<pre>", "<pre style=\"padding:9px!important;background-color: #f5f5f5!important\">");
        desc = desc.replaceAll("src=\"/img", "src=\"" + HOST + "/img");
        StringBuilder sb = new StringBuilder();
        String rawInput = ReUtil.get("<h3>Input</h3>([\\s\\S]*?)</section>[\\s\\S]*?</div>", body, 1);
        sb.append(rawInput);
        String constrains = ReUtil.get("<h3>Constraints</h3>([\\s\\S]*?)</section>[\\s\\S]*?</div>", body, 1);
        sb.append(constrains);
        String input = sb.toString().replaceAll("<var>", "\\$").replaceAll("</var>", "\\$");
        input = input.replaceAll("<pre>", "<pre style=\"padding:9px!important;background-color: #f5f5f5!important\">");
        String rawOutput = ReUtil.get("<h3>Output</h3>([\\s\\S]*?)</section>[\\s\\S]*?</div>", body, 1);
        String output = rawOutput.replaceAll("<var>", "\\$").replaceAll("</var>", "\\$");
        output = output.replaceAll("<pre>", "<pre style=\"padding:9px!important;background-color: #f5f5f5!important\">");
        List<String> sampleInput = ReUtil.findAll("<h3>Sample Input \\d+</h3><pre>([\\s\\S]*?)</pre>[\\s\\S]*?</section>[\\s\\S]*?</div>", body, 1);
        List<String> sampleOutput = ReUtil.findAll("<h3>Sample Output \\d+</h3><pre>([\\s\\S]*?)</pre>[\\s\\S]*?</section>[\\s\\S]*?</div>", body, 1);
        StringBuilder examples = new StringBuilder();
        for (int i = 0; i < sampleInput.size() && i < sampleOutput.size(); i++) {
            examples.append("<input>");
            String exampleInput = sampleInput.get(i).trim();
            examples.append(exampleInput).append("</input>");
            examples.append("<output>");
            String exampleOutput = sampleOutput.get(i).trim();
            examples.append(exampleOutput).append("</output>");
        }
        problem.setInput(input.trim()).setOutput(output.trim()).setDescription(desc.trim()).setExamples(examples.toString());
    } else {
        org.jsoup.nodes.Element element = Jsoup.parse(body).getElementById("task-statement");
        String desc = element.html();
        desc = desc.replaceAll("src=\"/img", "src=\"https://atcoder.jp/img");
        desc = desc.replaceAll("<pre>", "<pre style=\"padding:9px!important;background-color: #f5f5f5!important\">");
        desc = desc.replaceAll("<var>", "\\$").replaceAll("</var>", "\\$");
        desc = desc.replaceAll("<hr>", "");
        problem.setDescription(desc);
    }
    return new RemoteProblemInfo().setProblem(problem).setTagList(null).setLangIdList(null).setRemoteOJ(Constants.RemoteOJ.ATCODER);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Problem(top.hcode.hoj.pojo.entity.problem.Problem)

Example 17 with Problem

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

the class HDUProblemStrategy method getProblemInfo.

/**
 * @param problemId String的原因是因为某些题库题号不是纯数字
 * @param author    导入该题目的管理员用户名
 * @return 返回Problem对象
 * @throws Exception
 */
@Override
public RemoteProblemInfo getProblemInfo(String problemId, String author) throws Exception {
    // 验证题号是否符合规范
    Assert.isTrue(problemId.matches("[1-9]\\d*"), "HDU题号格式错误!");
    Problem info = new Problem();
    String url = HOST + String.format(PROBLEM_URL, problemId);
    Connection connection = JsoupUtils.getConnectionFromUrl(url, null, null);
    Document document = JsoupUtils.getDocument(connection, null);
    String html = document.html();
    info.setProblemId(JUDGE_NAME + "-" + problemId);
    info.setTitle(ReUtil.get("color:#1A5CC8\">([\\s\\S]*?)</h1>", html, 1).trim());
    info.setTimeLimit(Integer.parseInt(ReUtil.get("(\\d*) MS", html, 1)));
    info.setMemoryLimit(Integer.parseInt(ReUtil.get("/(\\d*) K", html, 1)) / 1024);
    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));
    StringBuilder sb = new StringBuilder("<input>");
    sb.append(ReUtil.get(">Sample Input</div><div .*?,monospace;\">([\\s\\S]*?)</div></pre>", html, 1));
    sb.append("</input><output>");
    sb.append(ReUtil.get(">Sample Output</div><div .*?monospace;\">([\\s\\S]*?)(<div style=.*?</div><i style=.*?</i>)*?</div></pre>", html, 1)).append("</output>");
    info.setExamples(sb.toString());
    info.setHint(ReUtil.get("<i>Hint</i></div>([\\s\\S]*?)</div><i .*?<br><[^<>]*?panel_title[^<>]*?>", html, 1));
    info.setIsRemote(true);
    info.setSource(String.format("<a style='color:#1A5CC8' href='https://acm.hdu.edu.cn/showproblem.php?pid=%s'>%s</a>", problemId, JUDGE_NAME + "-" + problemId));
    info.setType(0).setAuth(1).setAuthor(author).setOpenCaseResult(false).setIsRemoveEndBlank(false).setIsGroup(false).setDifficulty(// 默认为简单
    1);
    return new RemoteProblemInfo().setProblem(info).setTagList(null).setRemoteOJ(Constants.RemoteOJ.HDU);
}
Also used : Connection(org.jsoup.Connection) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Document(org.jsoup.nodes.Document)

Example 18 with Problem

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

the class POJProblemStrategy method getProblemInfo.

@Override
public RemoteProblemInfo getProblemInfo(String problemId, String author) throws Exception {
    // 验证题号是否符合规范
    Assert.isTrue(problemId.matches("[1-9]\\d*"), "POJ题号格式错误!");
    Problem info = new Problem();
    String url = HOST + String.format(PROBLEM_URL, problemId);
    Connection connection = JsoupUtils.getConnectionFromUrl(url, null, null);
    Document document = JsoupUtils.getDocument(connection, null);
    String html = document.html();
    html = html.replaceAll("<br>", "\n");
    info.setProblemId(JUDGE_NAME + "-" + problemId);
    info.setTitle(ReUtil.get("<title>\\d{3,} -- ([\\s\\S]*?)</title>", html, 1).trim());
    info.setTimeLimit(Integer.parseInt(ReUtil.get("<b>Time Limit:</b> (\\d{3,})MS</td>", html, 1)));
    info.setMemoryLimit(Integer.parseInt(ReUtil.get("<b>Memory Limit:</b> (\\d{2,})K</td>", html, 1)) / 1024);
    info.setDescription(ReUtil.get("<p class=\"pst\">Description</p><div class=.*?>([\\s\\S]*?)</div><p class=\"pst\">", html, 1).replaceAll("src=\"[../]*", "src=\"" + HOST + "/"));
    info.setInput(ReUtil.get("<p class=\"pst\">Input</p><div class=.*?>([\\s\\S]*?)</div><p class=\"pst\">", html, 1));
    info.setOutput(ReUtil.get("<p class=\"pst\">Output</p><div class=.*?>([\\s\\S]*?)</div><p class=\"pst\">", html, 1));
    StringBuilder sb = new StringBuilder("<input>");
    sb.append(ReUtil.get("<p class=\"pst\">Sample Input</p><pre class=.*?>([\\s\\S]*?)</pre><p class=\"pst\">", html, 1));
    sb.append("</input><output>");
    sb.append(ReUtil.get("<p class=\"pst\">Sample Output</p><pre class=.*?>([\\s\\S]*?)</pre><p class=\"pst\">", html, 1)).append("</output>");
    info.setExamples(sb.toString());
    info.setHint(ReUtil.get("<p class=.*?>Hint</p><div class=.*?>([\\s\\S]*?)</div><p class=\"pst\">", html, 1));
    info.setIsRemote(true);
    info.setSource(String.format("<a style='color:#1A5CC8' href='http://poj.org/problem?id=%s'>%s</a>", problemId, JUDGE_NAME + "-" + problemId));
    info.setType(0).setAuth(1).setAuthor(author).setOpenCaseResult(false).setIsRemoveEndBlank(false).setIsGroup(false).setDifficulty(// 默认为简单
    1);
    return new RemoteProblemInfo().setProblem(info).setTagList(null).setRemoteOJ(Constants.RemoteOJ.POJ);
}
Also used : Connection(org.jsoup.Connection) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Document(org.jsoup.nodes.Document)

Example 19 with Problem

use of top.hcode.hoj.pojo.entity.problem.Problem 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)

Example 20 with Problem

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

the class GroupContestProblemManager method addProblemFromGroup.

@Transactional(rollbackFor = Exception.class)
public void addProblemFromGroup(String problemId, Long cid, String displayId) throws StatusNotFoundException, StatusForbiddenException, StatusFailException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    Contest contest = contestEntityService.getById(cid);
    if (contest == null) {
        throw new StatusNotFoundException("该训练不存在!");
    }
    Long gid = contest.getGid();
    Group group = groupEntityService.getById(gid);
    if (group == null || group.getStatus() == 1 && !isRoot) {
        throw new StatusNotFoundException("该团队不存在或已被封禁!");
    }
    if (!userRolesVo.getUid().equals(contest.getUid()) && !isRoot && !groupValidator.isGroupRoot(userRolesVo.getUid(), gid)) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    QueryWrapper<Problem> problemQueryWrapper = new QueryWrapper<>();
    problemQueryWrapper.eq("problem_id", problemId).eq("gid", gid);
    Problem problem = problemEntityService.getOne(problemQueryWrapper);
    if (problem == null) {
        throw new StatusNotFoundException("该题目不存在或不是团队题目!");
    }
    QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
    contestProblemQueryWrapper.eq("cid", cid).and(wrapper -> wrapper.eq("pid", problem.getId()).or().eq("display_id", displayId));
    ContestProblem contestProblem = contestProblemEntityService.getOne(contestProblemQueryWrapper);
    if (contestProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的比赛展示ID已存在!");
    }
    ContestProblem newCProblem = new ContestProblem();
    String displayName = problem.getTitle();
    boolean updateProblem = problemEntityService.saveOrUpdate(problem.setAuth(3));
    boolean isOk = contestProblemEntityService.saveOrUpdate(newCProblem.setCid(cid).setPid(problem.getId()).setDisplayTitle(displayName).setDisplayId(displayId));
    if (!isOk || !updateProblem) {
        throw new StatusFailException("添加失败");
    }
}
Also used : Group(top.hcode.hoj.pojo.entity.group.Group) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusNotFoundException(top.hcode.hoj.common.exception.StatusNotFoundException) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

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