This class does not extends Leaderboard
. This class generates the appropiate Leaderboard
instance for each period cycle.
Each cycle (a unique Leaderboard) is identified by a CycleKey
(a string).
Every time you want to interact with the leaderboard, you need to retrieve the appropiate based on the current time and cycle function. When entering a new cycle, you'll receive the new leaderboard right away. Stale (and active) leaderboards can be retrieved with getExistingKeys
.
client
: Redis connection objectbaseKey
: string prefix for all the leaderboardsoptions
: PeriodicLeaderboardOptions configurationleaderboardOptions
: LeaderboardOptions underlying leaderboard optionscycle
: PeriodicLeaderboardCycle = CycleFunction | DefaultCycles: time cycleDefaultCycles
: default cycles
Allowed values:minute
hourly
daily
weekly
: cut is Saturday-Sundaymonthly
yearly
CycleFunction
:(time: Date) =>
CycleKey takes a time and retruns the appropiateCycleKey
for that time (internally the suffix for the Redis key).
The key returned must be appropiate for local time (not UTC).
See EXAMPLES.md for examples.
now
?: NowFunction: function to evaluate the current time.
If not provided, a function returning the local time will be used
const plb = new PeriodicLeaderboard(client, "plb:test", {
leaderboardOptions: {
sortPolicy: 'high-to-low',
updatePolicy: 'replace'
},
cycle: 'monthly'
});
-
getKey(time: Date)
: CycleKey get the cycle key at a specified date and timetime
: Date the time
-
getKeyNow()
: CycleKey get the current leaderboard based on the time returned bynow()
-
getExistingKeys()
: Promise<CycleKey[]> find all the active cycle keys in the database.
Use this function sparsely, it usesSCAN
over the whole database to find matches.
⚠️ I recommend having periodic leaderboards on a database index other than the main one if you plan to call this function a lot.O(N)
where N is the number of keys in the Redis database
-
getLeaderboard(key: CycleKey)
: Leaderboard get the leaderboard for the provided cycle keykey
: CycleKey cycle key
-
getLeaderboardAt(time?: Date)
: Leaderboard get the leaderboard at the specified date and timetime
?: Date the time
If
time
is not provided, it will use the time returned bynow()
-
getLeaderboardNow()
: Leaderboard get the current leaderboard based on the time returned bynow()