Skip to content

Commit

Permalink
Update updateLeaderboard.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunhThanhDe authored Oct 29, 2024
1 parent aa237b6 commit 3b06bbd
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions .github/updateLeaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,29 @@ module.exports = async ({ github, context }) => {
const readmePath = 'README.md';
let readme = fs.readFileSync(readmePath, 'utf8');

// Update Recent Plays
const recentPlaysSection = /<!-- Recent Plays -->[\s\S]*?<!-- \/Recent Plays -->/.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 = /<!-- Recent Plays -->[\s\S]*?<!-- \/Recent Plays -->/.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 = `<!-- Recent Plays -->\n| Date | Player | Message | Score |\n|-------|--------|---------|------|\n${recentPlaysRows.join('\n')}\n<!-- /Recent Plays -->`;
readme = readme.replace(recentPlaysSection[0], updatedRecentPlays);
}

recentPlaysRows.unshift(newEntry);


if (recentPlaysRows.length > 20) {
recentPlaysRows.pop();
}


const updatedRecentPlays = `<!-- Recent Plays -->\n| Date | Player | Message | Score |\n|-------|--------|---------|------|\n${recentPlaysRows.join('\n')}\n<!-- /Recent Plays -->`;
readme = readme.replace(recentPlaysSection[0], updatedRecentPlays);
}

// Update Leaderboard
const leaderboardSection = /<!-- Leaderboard -->[\s\S]*?<!-- \/Leaderboard -->/.exec(readme);
if (leaderboardSection) {
Expand All @@ -84,15 +82,27 @@ 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();

const updatedLeaderboard = `<!-- Leaderboard -->\n| Score | Player | Message | Date |\n|-------|--------|---------|------|\n${leaderboardRows.join('\n')}\n<!-- /Leaderboard -->`;
readme = readme.replace(leaderboardSection[0], updatedLeaderboard);
}

fs.writeFileSync(readmePath, readme, 'utf8');
console.log('README.md updated successfully.');
};

0 comments on commit 3b06bbd

Please sign in to comment.