From 1ebcda02d6151118cd1f250ea482b70ef79f98df Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 14 Jan 2023 18:24:03 +0800 Subject: [PATCH 1/2] feat(web/contest): username color --- web/app/libs/uoj-contest-lib.php | 27 +++++++++++---- web/app/models/UOJUser.php | 54 ++++++++++++++++++----------- web/app/views/contest-standings.php | 2 +- web/js/uoj.js | 23 +++++++----- 4 files changed, 68 insertions(+), 38 deletions(-) diff --git a/web/app/libs/uoj-contest-lib.php b/web/app/libs/uoj-contest-lib.php index 1b53904..f360bf0 100644 --- a/web/app/libs/uoj-contest-lib.php +++ b/web/app/libs/uoj-contest-lib.php @@ -87,14 +87,23 @@ function queryContestData($contest, $config = []) { $people = []; if ($contest['extra_config']['individual_or_team'] == 'individual') { - $people = DB::selectAll([ - "select contests_registrants.username, user_info.realname from contests_registrants", + $res = DB::selectAll([ + "select contests_registrants.username, user_info.realname, user_info.extra, user_info.usergroup from contests_registrants", "inner join user_info on contests_registrants.username = user_info.username", "where", [ "contest_id" => $contest['id'], "has_participated" => 1 ] ], DB::NUM); + foreach ($res as $row) { + $extra = json_decode($row[2], true); + $people[] = [ + $row[0], + trim(HTML::escape($row[1])), + null, + UOJUser::getUserColor2($row[3], $extra['username_color']), + ]; + } } elseif ($contest['extra_config']['individual_or_team'] == 'team') { $res = DB::selectAll([ "select user_info.username, null, user_info.extra from contests_registrants, user_info", @@ -106,11 +115,15 @@ function queryContestData($contest, $config = []) { ], DB::NUM); foreach ($res as $row) { $extra = json_decode($row[2], true); - $row[2] = [ - 'team_name' => $extra['acm']['team_name'], - 'members' => $extra['acm']['members'] + $people[] = [ + $row[0], + null, + [ + 'team_name' => $extra['acm']['team_name'], + 'members' => $extra['acm']['members'], + ], + null, ]; - $people[] = $row; } } @@ -373,7 +386,7 @@ function calcStandings($contest, $contest_data, &$score, &$standings, $cfg = []) } } - // standings: rank => score, penalty, [username, realname], virtual_rank, ?review + // standings: rank => score, penalty, [username, realname, null|array, null|color], virtual_rank, ?review $standings = []; foreach ($contest_data['people'] as $person) { $cur = array(0, 0, $person); diff --git a/web/app/models/UOJUser.php b/web/app/models/UOJUser.php index 508305d..388b724 100644 --- a/web/app/models/UOJUser.php +++ b/web/app/models/UOJUser.php @@ -210,6 +210,33 @@ class UOJUser { } } + public static function getUserColor($user) { + $extra = UOJUser::getExtra($user); + + return UOJUser::getUserColor2($user['usergroup'], $extra['username_color']); + } + + public static function getUserColor2($usergroup, $custom_color = null) { + if ($usergroup == 'B') { + return '#996600'; + } + + if ($usergroup == 'T') { + return '#707070'; + } + + if ($usergroup == 'S') { + return $custom_color ?: '#9d3dcf'; + } + + // 前管理员设置颜色为紫色、红色的,颜色改为蓝色 + if ($custom_color == '#9d3dcf' || $custom_color == '#fe4c61') { + return '#0d6efd'; + } + + return $custom_color ?: '#0d6efd'; + } + public static function getLink($user) { if (is_string($user)) { $info = UOJUser::query($user); @@ -221,36 +248,21 @@ class UOJUser { } } - $extra = UOJUser::getExtra($user); $realname = $user['realname']; - $color = '#0d6efd'; - - if ($user['usergroup'] === 'B') { - $color = '#996600'; - } else if (isTmpUser($user)) { - $color = '#707070'; - } else { - if (isSuperUser($user)) { - $color = '#9d3dcf'; - } - - $color = $extra['username_color']; - - // 前管理员设置颜色为紫色、红色的,颜色改为蓝色 - if (($color === '#9d3dcf' || $color === '#fe4c61') && !isSuperUser($user)) { - $color = '#0d6efd'; - } - } if ($user['usertype'] == 'teacher') { $realname .= '老师'; } + if (!Auth::check()) { + $realname = ''; + } + return HTML::tag('span', [ 'class' => 'uoj-username', + 'data-color' => UOJUser::getUserColor($user), // 未登录不可查看真实姓名 - 'data-realname' => Auth::check() ? trim(HTML::escape($realname)) : '', - 'data-color' => $color, + 'data-realname' => trim(HTML::escape($realname)), ], $user['username']); } diff --git a/web/app/views/contest-standings.php b/web/app/views/contest-standings.php index 35ee895..b5240f8 100644 --- a/web/app/views/contest-standings.php +++ b/web/app/views/contest-standings.php @@ -35,7 +35,7 @@ function(row) { var col_tr = ''; col_tr += '' + row[3] + ''; - col_tr += '' + getUserLink(row[2][0], row[2][1]) + ''; + col_tr += '' + getUserLink(row[2][0], row[2][1], row[2][3]) + ''; col_tr += '' + '
' + row[0] + '
' + '
' + getPenaltyTimeStr(row[1]) + '
'; for (var i = 0; i < problems.length; i++) { col = score[row[2][0]][i]; diff --git a/web/js/uoj.js b/web/js/uoj.js index 267f608..000af78 100644 --- a/web/js/uoj.js +++ b/web/js/uoj.js @@ -118,46 +118,51 @@ function getColOfScore(score) { } } -function getUserLink(username, realname) { +function getUserLink(username, realname, color) { if (!username) { return ''; } var text = username; + var style = ''; if (username.charAt(0) == '@') { username = username.substr(1); } if (realname) { text = text + ' (' + realname + ')'; } - return '' + text + ''; + if (color) { + style += 'color: ' + color + ';'; + } + return '' + text + ''; } -function getUserSpan(username, realname) { +function getUserSpan(username, realname, color) { if (!username) { return ''; } var text = username; + var style = ''; if (username.charAt(0) == '@') { username = username.substr(1); } if (realname) { text = text + ' (' + realname + ')'; } - return '' + text + ''; + if (color) { + style += 'color: ' + color + ';'; + } + return '' + text + ''; } function replaceWithHighlightUsername() { var username = $(this).text(); var realname = $(this).data("realname"); var color = $(this).data("color"); - var new_elem_inner = ''; if ($(this).data("link") != 0) { - new_elem_inner = getUserLink(username, realname); + $(this).replaceWith(getUserLink(username, realname, color)); } else { - new_elem_inner = getUserSpan(username, realname); + $(this).replaceWith(getUserSpan(username, realname, color)); } - - $(this).replaceWith($(new_elem_inner).css('color', color)); } $.fn.uoj_honor = function() { From 4e5d2dee4974c897d472f76e003577127ef5d432 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 14 Jan 2023 18:24:36 +0800 Subject: [PATCH 2/2] feat(web/blog): username color --- web/app/controllers/subdomain/blog/blog.php | 4 +++- web/js/uoj.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/web/app/controllers/subdomain/blog/blog.php b/web/app/controllers/subdomain/blog/blog.php index 5150047..9399add 100644 --- a/web/app/controllers/subdomain/blog/blog.php +++ b/web/app/controllers/subdomain/blog/blog.php @@ -201,7 +201,9 @@ $comments_pag = new Paginator([ "order by id" ]); foreach ($replies as $idx => $reply) { - $replies[$idx]['poster_realname'] = UOJUser::query($reply['poster'])['realname']; + $reply_user = UOJUser::query($reply['poster']); + $replies[$idx]['poster_realname'] = $reply_user['realname']; + $replies[$idx]['poster_username_color'] = UOJUser::getUserColor($reply_user); $replies[$idx]['content'] = getCommentContentToDisplay($reply); } $replies_json = json_encode($replies); diff --git a/web/js/uoj.js b/web/js/uoj.js index 000af78..3b209f3 100644 --- a/web/js/uoj.js +++ b/web/js/uoj.js @@ -1092,7 +1092,7 @@ function showCommentReplies(id, replies) { function(reply) { return $('').append( $('').append( - $('
' + getUserLink(reply.poster, reply.poster_realname) + ':' + reply.content + '
') + $('
' + getUserLink(reply.poster, reply.poster_realname, reply.poster_username_color) + ':' + reply.content + '
') ).append( $('