增加CF的检测id机制

This commit is contained in:
Himit_ZH 2021-11-26 20:45:18 +08:00
parent c09f419526
commit 5b9dc30b64
2 changed files with 23 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@ -24,8 +25,8 @@ import java.util.concurrent.TimeUnit;
@Component
public class RemoteJudgeReceiver {
@Autowired
@Lazy
private RemoteJudgeDispatcher remoteJudgeDispatcher;
@Autowired

View File

@ -82,9 +82,16 @@ public class CodeForcesJudge implements RemoteJudgeStrategy {
contestId = ReUtil.get("([0-9]+)[A-Z]{1}[0-9]{0,1}", problemId, 1);
problemNum = ReUtil.get("[0-9]+([A-Z]{1}[0-9]{0,1})", problemId, 1);
}
long nowTime = DateUtil.currentSeconds();
submitCode(contestId, problemNum, getLanguage(language), userCode);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 获取提交的题目id
Long maxRunId = getMaxRunId(username, contestId, problemNum, problemId);
Long maxRunId = getMaxRunId(username, contestId, problemNum, problemId, nowTime);
return MapUtil.builder(new HashMap<String, Object>())
.put("runId", maxRunId)
@ -93,10 +100,10 @@ public class CodeForcesJudge implements RemoteJudgeStrategy {
}
@SuppressWarnings("unchecked")
private Long getMaxRunId(String username, String contestNum, String problemNum, String problemId) {
private Long getMaxRunId(String username, String contestNum, String problemNum, String problemId, long nowTime) {
int retryNum = 0;
// 防止cf的nginx限制访问频率重试10
while (retryNum != 10) {
// 防止cf的nginx限制访问频率重试5
while (retryNum != 5) {
HttpResponse httpResponse = getSubmissionResult(username, 10);
if (httpResponse.getStatus() == 200) {
try {
@ -104,6 +111,10 @@ public class CodeForcesJudge implements RemoteJudgeStrategy {
List<Map<String, Object>> results = (List<Map<String, Object>>) json.get("result");
for (Map<String, Object> result : results) {
Long runId = Long.valueOf(result.get("id").toString());
long creationTimeSeconds = Long.parseLong(result.get("creationTimeSeconds").toString());
if (creationTimeSeconds < nowTime && retryNum < 4) {
continue;
}
Map<String, Object> problem = (Map<String, Object>) result.get("problem");
if (contestNum.equals(problem.get("contestId").toString()) &&
problemNum.equals(problem.get("index").toString())) {
@ -116,18 +127,18 @@ public class CodeForcesJudge implements RemoteJudgeStrategy {
return -1L;
}
}
try {
TimeUnit.MILLISECONDS.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
retryNum++;
}
return -1L;
}
// CF的这个接口有每两秒的访问限制所以需要加锁保证2秒内只有一次查询
// CF的这个接口有每两秒的访问限制所以需要加锁保证只有一次查询
public static synchronized HttpResponse getSubmissionResult(String username, Integer count) {
try {
TimeUnit.MILLISECONDS.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
String url = HOST + String.format(SUBMISSION_RESULT_URL, username, count);
return HttpUtil.createGet(url)
.timeout(30000)