跳转到主要内容

使用 SortedMap 的排行榜系统

创建一个游戏排行榜,按分数顺序维护玩家。

实现

import { SortedMap } from '@msnkr/data-structures';

interface PlayerScore {
username: string;
timestamp: Date;
}

// 按分数排序(越高越好)
const leaderboard = new SortedMap<number, PlayerScore>({
comparator: (a, b) => b - a, // 降序
});

// 添加玩家分数
leaderboard.set(100, { username: 'alice', timestamp: new Date() });
leaderboard.set(250, { username: 'bob', timestamp: new Date() });
leaderboard.set(180, { username: 'charlie', timestamp: new Date() });

// 获取排名第一的玩家
const [topScore, topPlayer] = leaderboard.firstEntry();
console.log(`${topPlayer.username}${topScore} 分领先!`);
// 输出:"bob 以 250 分领先!"

// 显示前 3 名
let rank = 1;
for (const [score, player] of leaderboard) {
console.log(`#${rank}${player.username} - ${score}`);
if (rank++ === 3) break;
}

输出

#1:bob - 250 分
#2:charlie - 180 分
#3:alice - 100 分

更新玩家分数

// 更新分数(删除旧的,添加新的)
leaderboard.delete(100); // 删除 alice 的旧分数
leaderboard.set(300, { username: 'alice', timestamp: new Date() }); // 新的最高分!

另请参阅