StatisticsModule can save and retrieve players’ statistics and rank players based on their stats. Stats are saved in the database and are often ranked to create leaderboards. For testing purposes, you can use BlueDragon’s /leaderboard command to view the top players for each statistic.
StatRecorders
To provide maximum flexibility, statistics are recorded by separate stat recorders, which are automatically registered when StatisticsModule is initialized. For example, to start recording kills, all you have to do is use the module like this:
Here is a full list of recorders included with BlueDragon’s common library:
PLAYER_KILLS: When a player kills another player, adds one to the attacker’s “kills” statistic.
PLAYER_DEATHS_ALL: When a player dies by any means, adds one to their “deaths” statistic.
PLAYER_DEATHS_BY_PLAYER: When a player dies to another, adds one to their “deaths_by_player” statistic.
KILLS_AND_DEATHS: Combines PLAYER_KILLS, PLAYER_DEATHS_ALL, and PLAYER_DEATHS_BY_PLAYER.
WINS_AND_LOSSES: Records a “wins” and a “losses” statistic when a winner is declared.
ALL: Combines all of the above recorders.
Custom Statistics
It’s also easy to record custom statistics using your own recorder. As a simple example, the PLAYER_KILLS recorder is defined like this:
By changing the event type and the “_kills” suffix, you can record any type of event as it happens.
You can also combine multiple existing recorders using the MultiStatisticRecorder:
Saving Stats
StatisticsModule provides several helper functions for storing stats in the database. All statistics are key-value pairs, where the values are stored as doubles for each player. The key should uniquely identify a type of statistic (e.g. WackyMaze kills), not a specific player.
By convention, keys for game-specific statistics start with game_<game name>_. You can easily create an accurate prefix using the helper method:
Ranking
StatisticsModule also includes a function to retrieve the best players for a given statistic, ranked in order. Note that this is a blocking operation.
Usage
Import the module:
Use the module in your game’s initialize function:
Now, statistics will be saved to the database based on the given recorders.