npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

agoragames-leaderboard

v2.1.0

Published

Leaderboards backed by Redis in JavaScript

Downloads

11

Readme

leaderboard

Leaderboards backed by Redis in JavaScript.

Builds off ideas proposed in http://www.agoragames.com/blog/2011/01/01/creating-high-score-tables-leaderboards-using-redis/.

Installation

npm install agoragames-leaderboard

Make sure your redis server is running! Redis configuration is outside the scope of this README, but check out the Redis documentation.

Usage

All methods take a callback as the final argument. The callback is non-optional for methods that return data.

Creating a leaderboard

Create a new leaderboard or attach to an existing leaderboard named 'highscores':

highscores = new Leaderboard('highscores')

Ranking members in the leaderboard

Add members to your leaderboard using rankMember:

for index in [1..50]
  highscores.rankMember("member_#{index}", index, "Optional member data for member #{index}", (reply) -> )

You can call rankMember with the same member and the leaderboard will be updated automatically.

Get some information about your leaderboard:

highscores.totalMembers((numMembers) -> ...)

highscores.totalPages(Leaderboard.DEFAULT_PAGE_SIZE, (totalPages) -> ...)

Get some information about a specific member(s) in the leaderboard:

highscores.scoreFor('member_5', (memberScore) -> ...)

highscores.rankFor('member_5', (memberRank) -> ...)

Retrieving members from the leaderboard

Get page 1 in the leaderboard:

highscores.leaders(1, {'withMemberData': false}, (leaders) -> ...)

Get an "Around Me" leaderboard page for a given member, which pulls members above and below the given member:

highscores.aroundMe('member_25', {'withMemberData': false}, (leaders) -> ...)

Get rank and score for an arbitrary list of members (e.g. friends) from the leaderboard:

highscores.rankedInList(['member_5', 'member_17', 'member_1'], {'withMemberData': false}, (leaders) -> ...)

Retrieve members from the leaderboard in a given score range:

highscores.membersFromScoreRange(10, 15, {'withMemberData': false}, (leaders) -> ...)

Retrieve a single member from the leaderboard at a given position:

highscores.memberAt(4, {'withMemberData': false}, (member) -> ...)

Retrieve a range of members from the leaderboard within a given rank range:

highscores.membersFromRankRange(5, 9, {'withMemberData': false}, (leaders) -> ...)

Conditionally rank a member in the leaderboard

You can pass a function to the rankMemberIf method to conditionally rank a member in the leaderboard. The function is passed the following 5 parameters:

  • member: Member name.
  • currentScore: Current score for the member in the leaderboard. This property is currently supplied when calling the rankMemberIf method.
  • score: Member score.
  • memberData: Optional member data.
  • options: Leaderboard options, e.g. 'reverse': Value of reverse option
highscoreCheck = (member, currentScore, score, memberData, leaderboardOptions) ->
  return true if !currentScore?
  return true if score > currentScore
  false

highscores.rankMemberIf(highscoreCheck, 'david', 1337, null, 'Optional member data', (reply) -> ...)

Optional member data notes

If you use optional member data, the use of the removeMembersInScoreRange or removeMembersOutsideRank methods will leave data around in the member data hash. This is because the internal Redis method, zremrangebyscore, only returns the number of items removed. It does not return the members that it removed.

Leaderboard request options

You can pass various options to the calls leaders, allLeaders, aroundMe, membersFromScoreRange, membersFromRankRange and rankedInList. Valid options are:

  • withMemberData - true or false to return the optional member data.
  • pageSize - An integer value to change the page size for that call.
  • membersOnly - true or false to return only the members without their score and rank.
  • sortBy - Valid values for sortBy are score and rank.

Ranking a member across multiple leaderboards

highscores.rankMemberAcross(['highscores', 'more_highscores'], 'david', 50000, { 'member_name': 'david' }, (reply) -> ...)

Alternate leaderboard types

The leaderboard library offers 3 styles of ranking. This is only an issue for members with the same score in a leaderboard.

Default: The Leaderboard class uses the default Redis sorted set ordering, whereby different members having the same score are ordered lexicographically. As per the Redis documentation on Redis sorted sets, "The lexicographic ordering used is binary, it compares strings as array of bytes."

Tie ranking: The TieRankingLeaderboard subclass of Leaderboard allows you to define a leaderboard where members with the same score are given the same rank. For example, members in a leaderboard with the associated scores would have the ranks of:

| member     | score | rank |
-----------------------------
| member_1   | 50    | 1    |
| member_2   | 50    | 1    |
| member_3   | 30    | 2    |
| member_4   | 30    | 2    |
| member_5   | 10    | 3    |

The TieRankingLeaderboard accepts one additional option, tiesNamespace (default: ties), when initializing a new instance of this class. Please note that in its current implementation, the TieRankingLeaderboard class uses an additional sorted set to rank the scores, so please keep this in mind when you are doing any capacity planning for Redis with respect to memory usage.

Competition ranking: The CompetitionRankingLeaderboard subclass of Leaderboard allows you to define a leaderboard where members with the same score will have the same rank, and then a gap is left in the ranking numbers. For example, members in a leaderboard with the associated scores would have the ranks of:

| member     | score | rank |
-----------------------------
| member_1   | 50    | 1    |
| member_2   | 50    | 1    |
| member_3   | 30    | 3    |
| member_4   | 30    | 3    |
| member_5   | 10    | 5    |

Performance Metrics

You can view performance metrics for the leaderboard library at the original Ruby library's page.

Ports

The following ports have been made of the leaderboard gem.

Officially supported:

  • JavaScript: https://github.com/agoragames/leaderboard-coffeescript
  • Python: https://github.com/agoragames/leaderboard-python
  • Ruby: https://github.com/agoragames/leaderboard

Unofficially supported (they need some feature parity love):

  • Java: https://github.com/agoragames/java-leaderboard
  • PHP: https://github.com/agoragames/php-leaderboard
  • Scala: https://github.com/agoragames/scala-leaderboard

Contributing to leaderboard

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Makefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2012-2014 David Czarnecki. See LICENSE.txt for further details.