From 3b06bbd9cfeaac4f062ebe9ea773f7253ff97092 Mon Sep 17 00:00:00 2001 From: Chung Nguyen Thanh Date: Tue, 29 Oct 2024 23:31:47 +0700 Subject: [PATCH] Update updateLeaderboard.js --- .github/updateLeaderboard.js | 56 +++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/.github/updateLeaderboard.js b/.github/updateLeaderboard.js index 2f06a38..e1ad29c 100644 --- a/.github/updateLeaderboard.js +++ b/.github/updateLeaderboard.js @@ -46,31 +46,29 @@ module.exports = async ({ github, context }) => { const readmePath = 'README.md'; let readme = fs.readFileSync(readmePath, 'utf8'); -// Update Recent Plays -const recentPlaysSection = /[\s\S]*?/.exec(readme); -if (recentPlaysSection) { - let recentPlaysContent = recentPlaysSection[0]; - - // Lấy danh sách các hàng hiện tại - const recentPlaysRows = recentPlaysContent - .split('\n') - .filter(row => row.startsWith('|') && !row.includes('Date | Player | Message | Score ') && !row.includes('|-------|--------|---------|------|')); - - // Thêm mục mới lên trên cùng - recentPlaysRows.unshift(newEntry); // Thêm vào đầu danh sách - - // Giới hạn số lượng người chơi tối đa là 20 - if (recentPlaysRows.length > 20) { - recentPlaysRows.pop(); // Xoá người chơi cuối cùng nếu đã đủ 20 người - } + // Update Recent Plays + const recentPlaysSection = /[\s\S]*?/.exec(readme); + if (recentPlaysSection) { + let recentPlaysContent = recentPlaysSection[0]; + + + const recentPlaysRows = recentPlaysContent + .split('\n') + .filter(row => row.startsWith('|') && !row.includes('Date | Player | Message | Score ') && !row.includes('|-------|--------|---------|------|')); - // Cập nhật bảng - const updatedRecentPlays = `\n| Date | Player | Message | Score |\n|-------|--------|---------|------|\n${recentPlaysRows.join('\n')}\n`; - readme = readme.replace(recentPlaysSection[0], updatedRecentPlays); -} + + recentPlaysRows.unshift(newEntry); + + if (recentPlaysRows.length > 20) { + recentPlaysRows.pop(); + } + const updatedRecentPlays = `\n| Date | Player | Message | Score |\n|-------|--------|---------|------|\n${recentPlaysRows.join('\n')}\n`; + readme = readme.replace(recentPlaysSection[0], updatedRecentPlays); + } + // Update Leaderboard const leaderboardSection = /[\s\S]*?/.exec(readme); if (leaderboardSection) { @@ -84,7 +82,19 @@ if (recentPlaysSection) { leaderboardRows.sort((a, b) => { const scoreA = a.match(/^\| (\d+) \|/); const scoreB = b.match(/^\| (\d+) \|/); - return (scoreB ? parseInt(scoreB[1]) : 0) - (scoreA ? parseInt(scoreA[1]) : 0); + const dateA = a.match(/\| ([^|]+) \| ([^|]+) \| ([^|]+) \| ([^|]+) \|/)[4].trim(); + const dateB = b.match(/\| ([^|]+) \| ([^|]+) \| ([^|]+) \| ([^|]+) \|/)[4].trim(); + + + // So sánh theo điểm trước + if (scoreB && scoreA) { + const scoreDiff = parseInt(scoreB[1]) - parseInt(scoreA[1]); + if (scoreDiff !== 0) { + return scoreDiff; + } + } + + return dateA.localeCompare(dateB); }); if (leaderboardRows.length > 20) leaderboardRows.pop(); @@ -92,7 +102,7 @@ if (recentPlaysSection) { const updatedLeaderboard = `\n| Score | Player | Message | Date |\n|-------|--------|---------|------|\n${leaderboardRows.join('\n')}\n`; readme = readme.replace(leaderboardSection[0], updatedLeaderboard); } - + fs.writeFileSync(readmePath, readme, 'utf8'); console.log('README.md updated successfully.'); };