The Battle Pass Manager is a class that manages the progression and rewards of a battle pass. It provides functionality to track the current level, total XP earned, and emit events when certain conditions are met. The manager extends the EventEmitter class from the node:events
module to handle event emission and listening.
To use the Battle Pass Manager, you need to have Node.js installed on your system. You can then import the required modules and classes into your project.
npm install battlepass
First, import the necessary classes and types into your project:
import {
BattlePassManager,
BattlePassEvents,
BattlePassReward,
} from "./BattlePassManager";
To create a new instance of the BattlePassManager class, provide an array of levels in the battle pass. Each level should have an associated XP threshold and optional rewards.
const levels = [
{ xp: 100, rewards: [{ name: "Reward 1" }] },
{ xp: 200, rewards: [{ name: "Reward 2" }] },
{ xp: 300, rewards: [{ name: "Reward 3" }] },
// Add more levels as needed
];
const battlePass = new BattlePassManager(levels);
You can also specify the current level and total XP earned as optional parameters:
const battlePass = new BattlePassManager(levels, 2, 150);
The BattlePassManager class extends the EventEmitter class, allowing you to listen for events emitted by the manager.
To listen to any battle pass event, use the onBattlepassEvent
method. Provide the event name and a callback function to handle the event.
battlePass.onBattlepassEvent("levelUp", (reward: BattlePassReward) => {
console.log(`Level up! Reached level ${reward.level}`);
console.log("Unlocked rewards:", reward.rewards);
});
battlePass.onBattlepassEvent("maxLevel", () => {
console.log(
"Congratulations! You reached the maximum level in the battle pass."
);
});
The BattlePassManager class provides convenience methods to listen to specific events, such as "levelUp" and "maxLevel".
To listen specifically to the "levelUp" event, use the onLevelUp
method. Provide a callback function that receives the reward object (BattlePassReward
) as an argument.
battlePass.onLevelUp((reward: BattlePassReward) => {
console.log(`Level up! Reached level ${reward.level}`);
console.log("Unlocked rewards:", reward.rewards);
});
To listen specifically to the "maxLevel" event, use the onMaxLevel
method. Provide a callback function that does not receive any arguments.
battlePass.onMaxLevel(() => {
console.log(
"Congratulations! You reached the maximum level in the battle pass."
);
});
You can add XP to the battle pass using the addXp
method. Provide the amount of XP to add as a parameter.
battlePass.addXp(50);
The addXp
method increments the total XP earned and checks if the added XP causes the player to level up. If a level up occurs, the "levelUp" event is emitted with the corresponding reward information. If the player reaches the maximum level, the "maxLevel" event is emitted.
The BattlePassManager
emits the following events:
Emitted when the player levels up in the battle pass.
battlePass.on("levelUp", (reward: BattlePassReward) => {
// Handle level up event
});
The event callback function receives the reward object (BattlePassReward
) as an argument. The reward object contains the level number and an array of rewards for that level.
Emitted when the player reaches the maximum level in the battle pass.
battlePass.on("maxLevel", () => {
// Handle max level event
});
The event callback function does not receive any arguments.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
The Battle Pass Manager is released under the MIT License.