From 73f52989115344e06c4dca99daa7d74c4f59df92 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 19 Jan 2023 11:49:15 +0800 Subject: [PATCH] feat(problem): display remote problem url --- web/app/models/UOJProblem.php | 6 +++++- web/app/models/UOJRemoteProblem.php | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/web/app/models/UOJProblem.php b/web/app/models/UOJProblem.php index 3bae2cf..463d623 100644 --- a/web/app/models/UOJProblem.php +++ b/web/app/models/UOJProblem.php @@ -392,6 +392,7 @@ class UOJProblem { } $remote_oj = $this->getExtraConfig('remote_online_judge'); + $remote_id = $this->getExtraConfig('remote_problem_id'); if (!$remote_oj || !array_key_exists($remote_oj, UOJRemoteProblem::$providers)) { return 'Error'; @@ -399,7 +400,10 @@ class UOJProblem { $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() { diff --git a/web/app/models/UOJRemoteProblem.php b/web/app/models/UOJRemoteProblem.php index e4c69f4..6074faa 100644 --- a/web/app/models/UOJRemoteProblem.php +++ b/web/app/models/UOJRemoteProblem.php @@ -6,7 +6,6 @@ class UOJRemoteProblem { 'name' => 'Codeforces', 'short_name' => 'CF', 'url' => 'https://codeforces.com', - 'host' => 'https://codeforces.com', 'not_exists_texts' => [ 'Actions', '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 需确保有效 static function getCodeforcesProblemBasicInfo($id) { $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'); $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) { - $curl->get($remote_provider['host'] . '/problemset/problem/' . $url_id); + $html = retry_loop(function () use ($curl, $id) { + $curl->get(static::getCodeforcesProblemUrl($id)); if ($curl->error) { 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) { if ($oj === 'codeforces') { return static::getCodeforcesProblemBasicInfo($id);