Search in sources :

Example 1 with RemoteJudgeRes

use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes in project HOJ by HimitZH.

the class HduJudge method result.

@Override
public RemoteJudgeRes result() {
    RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
    String url = HOST + String.format(QUERY_URL, remoteJudgeDTO.getSubmitId());
    HttpRequest request = HttpUtil.createGet(url).cookie(remoteJudgeDTO.getCookies()).addHeaders(headers);
    HttpResponse response = request.execute();
    // 1提交时间 2结果 3执行时间 4执行空间 5代码长度
    // 一般情况下 代码长度和提交时间不需要,想要也行,自行添加
    Pattern pattern = Pattern.compile(">" + remoteJudgeDTO.getSubmitId() + "</td><td>[\\s\\S]*?</td><td>([\\s\\S]*?)</td><td>[\\s\\S]*?</td><td>(\\d*?)MS</td><td>(\\d*?)K</td>");
    Matcher matcher = pattern.matcher(response.body());
    // 找到时
    Validate.isTrue(matcher.find());
    String rawStatus = matcher.group(1).replaceAll("<[\\s\\S]*?>", "").trim();
    Constants.Judge judgeStatus = statusTypeMap.getOrDefault(rawStatus, Constants.Judge.STATUS_PENDING);
    RemoteJudgeRes remoteJudgeRes = RemoteJudgeRes.builder().status(judgeStatus.getStatus()).build();
    if (judgeStatus == Constants.Judge.STATUS_PENDING) {
        return remoteJudgeRes;
    }
    // 获取其他信息
    String executionTime = matcher.group(2);
    remoteJudgeRes.setTime(Integer.parseInt(executionTime));
    String executionMemory = matcher.group(3);
    remoteJudgeRes.setMemory(Integer.parseInt(executionMemory));
    // 如果CE了,则还需要获得错误信息
    if (judgeStatus == Constants.Judge.STATUS_COMPILE_ERROR) {
        request.setUrl(HOST + String.format(ERROR_URL, remoteJudgeDTO.getSubmitId()));
        String CEHtml = request.execute().body();
        String compilationErrorInfo = ReUtil.get("<pre>([\\s\\S]*?)</pre>", CEHtml, 1);
        remoteJudgeRes.setErrorInfo(HtmlUtil.unescape(compilationErrorInfo));
    }
    return remoteJudgeRes;
}
Also used : RemoteJudgeRes(top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes) RemoteJudgeDTO(top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Constants(top.hcode.hoj.util.Constants)

Example 2 with RemoteJudgeRes

use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes in project HOJ by HimitZH.

the class AtCoderJudge method result.

@Override
public RemoteJudgeRes result() {
    RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
    String url = HOST + String.format(SUBMISSION_RESULT_URL, remoteJudgeDTO.getContestId(), remoteJudgeDTO.getSubmitId());
    String body = HttpUtil.get(url);
    String status = ReUtil.get("<th>Status</th>[\\s\\S]*?<td id=\"judge-status\" class=\"[\\s\\S]*?\"><span [\\s\\S]*?>([\\s\\S]*?)</span></td>", body, 1);
    Constants.Judge judgeStatus = statusMap.get(status);
    if (judgeStatus == Constants.Judge.STATUS_JUDGING || judgeStatus == Constants.Judge.STATUS_PENDING) {
        return RemoteJudgeRes.builder().status(judgeStatus.getStatus()).build();
    }
    String time = ReUtil.get("<th>Exec Time</th>[\\s\\S]*?<td [\\s\\S]*?>([\\s\\S]*?) ms</td>", body, 1);
    String memory = ReUtil.get("<th>Memory</th>[\\s\\S]*?<td [\\s\\S]*?>([\\s\\S]*?) KB</td>", body, 1);
    RemoteJudgeRes remoteJudgeRes = RemoteJudgeRes.builder().status(judgeStatus.getStatus()).time(time == null ? null : Integer.parseInt(time)).memory(memory == null ? null : Integer.parseInt(memory)).build();
    if (judgeStatus == Constants.Judge.STATUS_COMPILE_ERROR) {
        String CEInfo = ReUtil.get("<h4>Compile Error</h4>[\\s\\S]*?<pre>([\\s\\S]*?)</pre>", body, 1);
        remoteJudgeRes.setErrorInfo(HtmlUtil.unescape(CEInfo));
    }
    return remoteJudgeRes;
}
Also used : RemoteJudgeRes(top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes) RemoteJudgeDTO(top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO) Constants(top.hcode.hoj.util.Constants)

Example 3 with RemoteJudgeRes

use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes in project HOJ by HimitZH.

the class CodeForcesJudge method result.

@Override
public RemoteJudgeRes result() {
    // 清除当前线程的cookies缓存
    HttpRequest.getCookieManager().getCookieStore().removeAll();
    RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
    if (remoteJudgeDTO.getCookies() == null) {
        login();
    }
    HttpRequest homeRequest = HttpUtil.createGet(HOST + MY_SUBMISSION);
    homeRequest.cookie(remoteJudgeDTO.getCookies());
    HttpResponse homeResponse = homeRequest.execute();
    String csrfToken = ReUtil.get("data-csrf='(\\w+)'", homeResponse.body(), 1);
    HttpRequest httpRequest = HttpUtil.createPost(HOST + CE_INFO_URL).cookie(remoteJudgeDTO.getCookies()).timeout(30000);
    httpRequest.form(MapUtil.builder(new HashMap<String, Object>()).put("csrf_token", csrfToken).put("submissionId", remoteJudgeDTO.getSubmitId()).map());
    HttpResponse httpResponse = httpRequest.execute();
    RemoteJudgeRes remoteJudgeRes = RemoteJudgeRes.builder().status(Constants.Judge.STATUS_JUDGING.getStatus()).build();
    if (httpResponse.getStatus() == 200) {
        JSONObject submissionInfoJson = JSONUtil.parseObj(httpResponse.body());
        String compilationError = submissionInfoJson.getStr("compilationError");
        if ("true".equals(compilationError)) {
            remoteJudgeRes.setMemory(0).setTime(0).setStatus(Constants.Judge.STATUS_COMPILE_ERROR.getStatus());
            String CEMsg = submissionInfoJson.getStr("checkerStdoutAndStderr#1");
            if (StringUtils.isEmpty(CEMsg)) {
                remoteJudgeRes.setErrorInfo("Oops! Because Codeforces does not provide compilation details, it is unable to provide the reason for compilation failure!");
            } else {
                remoteJudgeRes.setErrorInfo(CEMsg);
            }
            return remoteJudgeRes;
        }
        Integer testcaseNum = remoteJudgeDTO.getTestcaseNum();
        Integer maxTime = remoteJudgeDTO.getMaxTime();
        Integer maxMemory = remoteJudgeDTO.getMaxMemory();
        if (testcaseNum == null) {
            testcaseNum = 1;
            maxTime = 0;
            maxMemory = 0;
        }
        List<JudgeCase> judgeCaseList = new ArrayList<>();
        String testCountStr = submissionInfoJson.getStr("testCount");
        int testCount = Integer.parseInt(testCountStr);
        for (; testcaseNum <= testCount; testcaseNum++) {
            String verdict = submissionInfoJson.getStr("verdict#" + testcaseNum);
            if (StringUtils.isEmpty(verdict)) {
                continue;
            }
            Constants.Judge judgeRes = statusMap.get(verdict);
            Integer time = Integer.parseInt(submissionInfoJson.getStr("timeConsumed#" + testcaseNum));
            Integer memory = Integer.parseInt(submissionInfoJson.getStr("memoryConsumed#" + testcaseNum)) / 1024;
            String msg = submissionInfoJson.getStr("checkerStdoutAndStderr#" + testcaseNum);
            judgeCaseList.add(new JudgeCase().setSubmitId(remoteJudgeDTO.getJudgeId()).setPid(remoteJudgeDTO.getPid()).setUid(remoteJudgeDTO.getUid()).setTime(time).setMemory(memory).setStatus(judgeRes.getStatus()).setUserOutput(msg));
            if (time > maxTime) {
                maxTime = time;
            }
            if (memory > maxMemory) {
                maxMemory = memory;
            }
        }
        remoteJudgeDTO.setTestcaseNum(testcaseNum);
        remoteJudgeDTO.setMaxMemory(maxMemory);
        remoteJudgeDTO.setMaxTime(maxTime);
        remoteJudgeRes.setJudgeCaseList(judgeCaseList);
        if ("true".equals(submissionInfoJson.getStr("waiting"))) {
            return remoteJudgeRes;
        }
        Constants.Judge finalJudgeRes = statusMap.get(submissionInfoJson.getStr("verdict#" + testCount));
        remoteJudgeRes.setStatus(finalJudgeRes.getStatus()).setTime(maxTime).setMemory(maxMemory);
        return remoteJudgeRes;
    } else {
        remoteJudgeRes.setStatus(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()).setMemory(0).setTime(0).setErrorInfo("Oops! Error in obtaining the judging result. The status code returned by the interface is " + httpResponse.getStatus() + ".");
        return remoteJudgeRes;
    }
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) HttpResponse(cn.hutool.http.HttpResponse) Constants(top.hcode.hoj.util.Constants) RemoteJudgeRes(top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes) RemoteJudgeDTO(top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO) JSONObject(cn.hutool.json.JSONObject) JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) JSONObject(cn.hutool.json.JSONObject)

Example 4 with RemoteJudgeRes

use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes in project HOJ by HimitZH.

the class HDUJudge method result.

@Override
public RemoteJudgeRes result() {
    RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
    String url = HOST + String.format(QUERY_URL, remoteJudgeDTO.getSubmitId());
    HttpRequest request = HttpUtil.createGet(url).cookie(remoteJudgeDTO.getCookies()).addHeaders(headers);
    HttpResponse response = request.execute();
    // 1提交时间 2结果 3执行时间 4执行空间 5代码长度
    // 一般情况下 代码长度和提交时间不需要,想要也行,自行添加
    Pattern pattern = Pattern.compile(">" + remoteJudgeDTO.getSubmitId() + "</td><td>[\\s\\S]*?</td><td>([\\s\\S]*?)</td><td>[\\s\\S]*?</td><td>(\\d*?)MS</td><td>(\\d*?)K</td>");
    Matcher matcher = pattern.matcher(response.body());
    // 找到时
    Validate.isTrue(matcher.find());
    String rawStatus = matcher.group(1).replaceAll("<[\\s\\S]*?>", "").trim();
    Constants.Judge judgeStatus = statusTypeMap.getOrDefault(rawStatus, Constants.Judge.STATUS_PENDING);
    RemoteJudgeRes remoteJudgeRes = RemoteJudgeRes.builder().status(judgeStatus.getStatus()).build();
    if (judgeStatus == Constants.Judge.STATUS_PENDING) {
        return remoteJudgeRes;
    }
    // 获取其他信息
    String executionTime = matcher.group(2);
    remoteJudgeRes.setTime(Integer.parseInt(executionTime));
    String executionMemory = matcher.group(3);
    remoteJudgeRes.setMemory(Integer.parseInt(executionMemory));
    // 如果CE了,则还需要获得错误信息
    if (judgeStatus == Constants.Judge.STATUS_COMPILE_ERROR) {
        request.setUrl(HOST + String.format(ERROR_URL, remoteJudgeDTO.getSubmitId()));
        String CEHtml = request.execute().body();
        String compilationErrorInfo = ReUtil.get("<pre>([\\s\\S]*?)</pre>", CEHtml, 1);
        remoteJudgeRes.setErrorInfo(HtmlUtil.unescape(compilationErrorInfo));
    }
    return remoteJudgeRes;
}
Also used : RemoteJudgeRes(top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes) RemoteJudgeDTO(top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Constants(top.hcode.hoj.util.Constants)

Example 5 with RemoteJudgeRes

use of top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes in project HOJ by HimitZH.

the class POJJudge method result.

@Override
public RemoteJudgeRes result() {
    RemoteJudgeDTO remoteJudgeDTO = getRemoteJudgeDTO();
    List<HttpCookie> cookies = remoteJudgeDTO.getCookies();
    Long submitId = remoteJudgeDTO.getSubmitId();
    if (CollectionUtils.isEmpty(cookies)) {
        login();
        cookies = remoteJudgeDTO.getCookies();
    }
    String url = HOST + String.format(QUERY_URL, submitId);
    HttpRequest request = HttpUtil.createGet(url).cookie(cookies).addHeaders(headers);
    HttpResponse response = request.execute();
    if (!response.body().contains("<b>Result:</b>")) {
        log.error(submitId + " error:{}", response.body());
    }
    String statusStr = ReUtil.get("<b>Result:</b>(.+?)</td>", response.body(), 1).replaceAll("<.*?>", "").trim();
    Constants.Judge judgeStatus = statusMap.get(statusStr);
    if (judgeStatus == null) {
        return RemoteJudgeRes.builder().status(Constants.Judge.STATUS_PENDING.getStatus()).build();
    }
    RemoteJudgeRes remoteJudgeRes = RemoteJudgeRes.builder().status(judgeStatus.getStatus()).build();
    // 如果CE了,需要获得错误信息
    if (judgeStatus == Constants.Judge.STATUS_COMPILE_ERROR) {
        request.setUrl(HOST + String.format(ERROR_URL, submitId));
        String CEHtml = request.execute().body();
        String compilationErrorInfo = ReUtil.get("<pre>([\\s\\S]*?)</pre>", CEHtml, 1);
        remoteJudgeRes.setErrorInfo(HtmlUtil.unescape(compilationErrorInfo));
    } else {
        // 如果不是CE,获取其他信息
        String executionMemory = ReUtil.get("<b>Memory:</b> ([-\\d]+)", response.body(), 1);
        remoteJudgeRes.setMemory(executionMemory == null ? null : Integer.parseInt(executionMemory));
        String executionTime = ReUtil.get("<b>Time:</b> ([-\\d]+)", response.body(), 1);
        remoteJudgeRes.setTime(executionTime == null ? null : Integer.parseInt(executionTime));
    }
    return remoteJudgeRes;
}
Also used : RemoteJudgeRes(top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes) RemoteJudgeDTO(top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO) Constants(top.hcode.hoj.util.Constants) HttpCookie(java.net.HttpCookie)

Aggregations

RemoteJudgeDTO (top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO)8 RemoteJudgeRes (top.hcode.hoj.remoteJudge.entity.RemoteJudgeRes)8 Constants (top.hcode.hoj.util.Constants)7 HttpRequest (cn.hutool.http.HttpRequest)3 HttpResponse (cn.hutool.http.HttpResponse)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 JSONObject (cn.hutool.json.JSONObject)2 HttpCookie (java.net.HttpCookie)2 JSONArray (cn.hutool.json.JSONArray)1 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Judge (top.hcode.hoj.pojo.entity.judge.Judge)1 JudgeCase (top.hcode.hoj.pojo.entity.judge.JudgeCase)1