feat(problem): display remote problem url

This commit is contained in:
Baoshuo Ren 2023-01-19 11:49:15 +08:00
parent 4523109085
commit 73f5298911
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
2 changed files with 19 additions and 5 deletions

View File

@ -392,6 +392,7 @@ class UOJProblem {
} }
$remote_oj = $this->getExtraConfig('remote_online_judge'); $remote_oj = $this->getExtraConfig('remote_online_judge');
$remote_id = $this->getExtraConfig('remote_problem_id');
if (!$remote_oj || !array_key_exists($remote_oj, UOJRemoteProblem::$providers)) { if (!$remote_oj || !array_key_exists($remote_oj, UOJRemoteProblem::$providers)) {
return 'Error'; return 'Error';
@ -399,7 +400,10 @@ class UOJProblem {
$provider = UOJRemoteProblem::$providers[$remote_oj]; $provider = UOJRemoteProblem::$providers[$remote_oj];
return HTML::tag('a', ['href' => $provider['url'], 'target' => '_blank'], $provider['name']); return HTML::tag('a', [
'href' => UOJRemoteProblem::getProblemRemoteUrl($remote_oj, $remote_id),
'target' => '_blank'
], $provider['name']);
} }
public function getDifficultyHTML() { public function getDifficultyHTML() {

View File

@ -6,7 +6,6 @@ class UOJRemoteProblem {
'name' => 'Codeforces', 'name' => 'Codeforces',
'short_name' => 'CF', 'short_name' => 'CF',
'url' => 'https://codeforces.com', 'url' => 'https://codeforces.com',
'host' => 'https://codeforces.com',
'not_exists_texts' => [ 'not_exists_texts' => [
'<th>Actions</th>', '<th>Actions</th>',
'Statement is not available on English language', 'Statement is not available on English language',
@ -16,16 +15,19 @@ class UOJRemoteProblem {
], ],
]; ];
static function getCodeforcesProblemUrl($id) {
return static::$providers['codeforces']['url'] . '/problemset/problem/' . preg_replace_callback('/([1-9][0-9]{0,5})([A-Z][1-9]?)/', fn ($matches) => $matches[1] . '/' . $matches[2], $id);
}
// 传入 ID 需确保有效 // 传入 ID 需确保有效
static function getCodeforcesProblemBasicInfo($id) { static function getCodeforcesProblemBasicInfo($id) {
$curl = new Curl(); $curl = new Curl();
$curl->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36 S2OJ/3.1.0'); $curl->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.0.0 Safari/537.36 S2OJ/3.1.0');
$remote_provider = static::$providers['codeforces']; $remote_provider = static::$providers['codeforces'];
$url_id = preg_replace_callback('/([1-9][0-9]{0,5})([A-Z][1-9]?)/', fn ($matches) => $matches[1] . '/' . $matches[2], $id);
$html = retry_loop(function () use ($curl, $url_id, $remote_provider) { $html = retry_loop(function () use ($curl, $id) {
$curl->get($remote_provider['host'] . '/problemset/problem/' . $url_id); $curl->get(static::getCodeforcesProblemUrl($id));
if ($curl->error) { if ($curl->error) {
return false; return false;
@ -126,6 +128,14 @@ class UOJRemoteProblem {
]; ];
} }
public static function getProblemRemoteUrl($oj, $id) {
if ($oj === 'codeforces') {
return static::getCodeforcesProblemUrl($id);
}
return null;
}
public static function getProblemBasicInfo($oj, $id) { public static function getProblemBasicInfo($oj, $id) {
if ($oj === 'codeforces') { if ($oj === 'codeforces') {
return static::getCodeforcesProblemBasicInfo($id); return static::getCodeforcesProblemBasicInfo($id);