From 586df479f68b8c8d297de2b3fd45594c4597f1b8 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 5 Dec 2022 17:14:43 +0800 Subject: [PATCH 1/2] refactor(web/problem): difficulty --- web/app/controllers/list.php | 4 +- web/app/controllers/problem.php | 2 +- web/app/controllers/problem_data_manage.php | 31 --------- web/app/controllers/problem_set.php | 41 ++++++----- web/app/controllers/problem_solutions.php | 2 +- .../controllers/problem_statement_manage.php | 4 +- web/app/controllers/problem_statistics.php | 2 +- web/app/models/UOJProblem.php | 68 ++++++++++++------- 8 files changed, 75 insertions(+), 79 deletions(-) diff --git a/web/app/controllers/list.php b/web/app/controllers/list.php index 5728a43..ad1fcf7 100644 --- a/web/app/controllers/list.php +++ b/web/app/controllers/list.php @@ -54,7 +54,7 @@ function getProblemTR($info) { ) ); } - $html .= HTML::tag('td', [], UOJProblem::getDifficultyHTML($problem->info['difficulty'])); + $html .= HTML::tag('td', [], $problem->getDifficultyHTML()); $html .= HTML::tag('td', [], ClickZans::getCntBlock($problem->info['zan'])); $html .= HTML::tag_end('tr'); return $html; @@ -66,7 +66,7 @@ $header .= '' . UOJLocale::get('problems::problem') . ''; if (isset($_COOKIE['show_submit_mode'])) { $header .= '' . UOJLocale::get('problems::ac ratio') . ''; } -$header .= '' . UOJLocale::get('problems::difficulty') . ''; +$header .= '' . UOJLocale::get('problems::difficulty') . ''; $header .= '' . UOJLocale::get('appraisal') . ''; $header .= ''; diff --git a/web/app/controllers/problem.php b/web/app/controllers/problem.php index 0bb6425..8759c0d 100644 --- a/web/app/controllers/problem.php +++ b/web/app/controllers/problem.php @@ -390,7 +390,7 @@ if (UOJContest::cur()) {
  • 难度 - + getDifficultyHTML() ?>
  • diff --git a/web/app/controllers/problem_data_manage.php b/web/app/controllers/problem_data_manage.php index 60412b6..6e5696f 100644 --- a/web/app/controllers/problem_data_manage.php +++ b/web/app/controllers/problem_data_manage.php @@ -553,29 +553,6 @@ $solution_view_type_form->handle = function () { }; $solution_view_type_form->submit_button_config['class_str'] = 'btn btn-warning d-block w-100 mt-2'; -$difficulty_form = new UOJBs4Form('difficulty'); -$difficulty_form->addVInput( - 'difficulty', - 'text', - '难度系数', - $problem_extra_config['difficulty'], - function ($str) { - if (!is_numeric($str)) { - return '难度系数必须是一个数字'; - } - return ''; - }, - null -); -$difficulty_form->handle = function () { - global $problem, $problem_extra_config; - $config = $problem_extra_config; - $config['difficulty'] = $_POST['difficulty'] + 0; - $esc_config = DB::escape(json_encode($config)); - DB::query("update problems set extra_config = '$esc_config' where id = '{$problem['id']}'"); -}; -$difficulty_form->submit_button_config['class_str'] = 'btn btn-warning d-block w-100 mt-2'; - if ($problem['hackable']) { $test_std_form = new UOJBs4Form('test_std'); $test_std_form->handle = function () use ($problem, $data_disp) { @@ -643,7 +620,6 @@ if ($problem['hackable']) { $hackable_form->runAtServer(); $view_type_form->runAtServer(); $solution_view_type_form->runAtServer(); -$difficulty_form->runAtServer(); $data_form->runAtServer(); $clear_data_form->runAtServer(); $rejudge_form->runAtServer(); @@ -803,13 +779,6 @@ $info_form->runAtServer();
    - -
    - - -
    diff --git a/web/app/controllers/problem_set.php b/web/app/controllers/problem_set.php index e557a03..ae5d647 100644 --- a/web/app/controllers/problem_set.php +++ b/web/app/controllers/problem_set.php @@ -128,7 +128,7 @@ function getProblemTR($info) { ) ); } - $html .= HTML::tag('td', [], UOJProblem::getDifficultyHTML($problem->info['difficulty'])); + $html .= HTML::tag('td', [], $problem->getDifficultyHTML()); $html .= HTML::tag('td', [], ClickZans::getCntBlock($problem->info['zan'])); $html .= HTML::tag_end('tr'); return $html; @@ -177,8 +177,12 @@ if (Auth::check() && isset($_GET['my'])) { $cond['problems.uploader'] = Auth::id(); } -if (isset($_GET['difficulty']) && $_GET['difficulty']) { - $cond['problems.difficulty'] = $_GET['difficulty']; +if (isset($_GET['min_difficulty']) && $_GET['min_difficulty']) { + $cond[] = ['problems.difficulty', '>=', $_GET['min_difficulty']]; +} + +if (isset($_GET['max_difficulty']) && $_GET['max_difficulty']) { + $cond[] = ['problems.difficulty', '<=', $_GET['max_difficulty']]; } if (empty($cond)) { @@ -191,7 +195,7 @@ $header .= '' . UOJLocale::get('problems::problem') . ''; if (isset($_COOKIE['show_submit_mode'])) { $header .= '' . UOJLocale::get('problems::ac ratio') . ''; } -$header .= '' . UOJLocale::get('problems::difficulty') . ''; +$header .= '' . UOJLocale::get('problems::difficulty') . ''; $header .= '' . UOJLocale::get('appraisal') . ''; $header .= ''; @@ -247,8 +251,7 @@ $pag = new Paginator([
    -
    - +

    @@ -335,8 +338,7 @@ $pag = new Paginator([ -

    -
    - -
    - + +
    +
    + 题目难度 +
    +
    +
    + + ~ + + +
    diff --git a/web/app/controllers/problem_solutions.php b/web/app/controllers/problem_solutions.php index 71ef636..8e5be69 100644 --- a/web/app/controllers/problem_solutions.php +++ b/web/app/controllers/problem_solutions.php @@ -253,7 +253,7 @@ $pag = new Paginator($pag_config);
  • 难度 - + getDifficultyHTML() ?>
  • diff --git a/web/app/controllers/problem_statement_manage.php b/web/app/controllers/problem_statement_manage.php index 3abf647..ab2dfa5 100644 --- a/web/app/controllers/problem_statement_manage.php +++ b/web/app/controllers/problem_statement_manage.php @@ -65,7 +65,7 @@ $problem_editor->runAtServer(); $difficulty_form = new UOJForm('difficulty'); $difficulty_form->addSelect('difficulty', [ - 'options' => UOJProblem::$difficulty, + 'options' => array_combine(UOJProblem::$difficulty, UOJProblem::$difficulty), 'default_value' => UOJProblem::info('difficulty'), ]); $difficulty_form->handle = function () { @@ -183,4 +183,4 @@ $difficulty_form->runAtServer(); - + \ No newline at end of file diff --git a/web/app/controllers/problem_statistics.php b/web/app/controllers/problem_statistics.php index d87c49b..93d6f12 100644 --- a/web/app/controllers/problem_statistics.php +++ b/web/app/controllers/problem_statistics.php @@ -325,7 +325,7 @@ $submissions_sort_by_choice = !isset($_COOKIE['submissions-sort-by-code-length']
  • 难度 - + getDifficultyHTML() ?>
  • diff --git a/web/app/models/UOJProblem.php b/web/app/models/UOJProblem.php index 2dfaa02..b101f32 100644 --- a/web/app/models/UOJProblem.php +++ b/web/app/models/UOJProblem.php @@ -9,25 +9,45 @@ class UOJProblem { use UOJArticleTrait; public static array $difficulty = [ - -1 => '暂无评定', - 1 => '入门', - 2 => '普及-', - 3 => '普及/提高-', - 4 => '普及+/提高', - 6 => '提高+/省选-', - 8 => '省选/NOI-', - 10 => 'NOI/NOI+/CTSC', + 800, + 1000, + 1200, + 1400, + 1600, + 1800, + 1900, + 2000, + 2100, + 2200, + 2300, + 2400, + 2500, + 2700, + 2900, + 3100, + 3300, + 3500, ]; public static array $difficulty_color = [ - -1 => '#bfbfbf', - 1 => '#fe4c61', - 2 => '#f39c11', - 3 => '#ffc116', - 4 => '#52c41a', - 6 => '#3498db', - 8 => '#9d3dcf', - 10 => '#0e1d69', + 800 => '#008000', + 1000 => '#008000', + 1200 => '#00c0c0', + 1400 => '#00c0c0', + 1600 => '#0000ff', + 1800 => '#0000ff', + 1900 => '#0000ff', + 2000 => '#c0c000', + 2100 => '#c0c000', + 2200 => '#c0c000', + 2300 => '#c0c000', + 2400 => '#ff8000', + 2500 => '#ff8000', + 2700 => '#ff8000', + 2900 => '#ff0000', + 3100 => '#ff0000', + 3300 => '#aa0000', + 3500 => '#aa0000', ]; public static function query($id) { @@ -78,14 +98,6 @@ class UOJProblem { return isSuperUser($user) || UOJUser::checkPermission($user, 'problems.create'); } - public static function getDifficultyHTML($difficulty = -1) { - $difficulty = (int)$difficulty; - $difficulty_text = self::$difficulty[$difficulty] ?: self::$difficulty[-1]; - $difficulty_color = self::$difficulty_color[$difficulty] ?: self::$difficulty_color[-1]; - - return HTML::tag('span', ['class' => 'uoj-difficulty', 'style' => "color: $difficulty_color"], $difficulty_text); - } - public function __construct($info) { $this->info = $info; } @@ -128,6 +140,14 @@ class UOJProblem { return UOJUser::getLink($this->info['uploader'] ?: "root"); } + public function getDifficultyHTML() { + $difficulty = (int)$this->info['difficulty']; + $difficulty_text = in_array($difficulty, static::$difficulty) ? $difficulty : '-'; + $difficulty_color = in_array($difficulty, static::$difficulty) ? static::$difficulty_color[$difficulty] : '#7e7e7e'; + + return HTML::tag('span', ['class' => 'uoj-difficulty', 'style' => "color: $difficulty_color"], $difficulty_text); + } + public function findInContests() { $res = DB::selectAll([ "select contest_id from contests_problems", From 83ffb7257757fe8bd1ef98fe8fe18399723ad385 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 5 Dec 2022 17:31:22 +0800 Subject: [PATCH 2/2] chore(web): add upgrader for #21 --- .../upgrade/21_problem_difficulty/upgrade.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 web/app/upgrade/21_problem_difficulty/upgrade.php diff --git a/web/app/upgrade/21_problem_difficulty/upgrade.php b/web/app/upgrade/21_problem_difficulty/upgrade.php new file mode 100644 index 0000000..14d706a --- /dev/null +++ b/web/app/upgrade/21_problem_difficulty/upgrade.php @@ -0,0 +1,43 @@ +getExtraConfig(); + + if (isset($extra_config['difficulty'])) { + $old_difficulty = (float)$extra_config['difficulty']; + + $difficulty = (int)(3.0 * $old_difficulty + 5) * 100; + $difficulty = (function ($search, $arr) { + $closest = null; + foreach ($arr as $item) { + if ($closest === null || abs($search - $closest) > abs($item - $search)) { + $closest = $item; + } + } + return $closest; + })($difficulty, UOJProblem::$difficulty); + } + + DB::update([ + "update problems", + "set", [ + "difficulty" => $difficulty, + ], + "where", [ + "id" => $problem->info['id'], + ] + ]); + + echo "Problem: {$problem->info['id']} ({$difficulty})\n"; + } + } +};