I listen to “registerLeaderboardReset” callback after creating the leaderboard. The leaderboard gets reset based on the CRON timer but the callback is not called. Sharing the code below. Please guide if I have done any logical error.
function InitModule(_ctx: nkruntime.Context, logger: nkruntime.Logger, _nk: nkruntime.Nakama, initializer: nkruntime.Initializer)
{
logger.info(`Hello World!............................`);
//rest of the code.
function createLeaderboard()
{
let id = WEEKLY_LEADERBOARD;
let authoritative = true;
let sort = nkruntime.SortOrder.DESCENDING;
let operator = nkruntime.Operator.INCREMENTAL;
let reset = '0 0 * * 0'; // Resets every sunday at midnight
try
{
_nk.leaderboardCreate(id, authoritative, sort, operator, reset);
logger.info("createLeaderboard success......... ")
// Register leaderboard reset event
initializer.registerLeaderboardReset(leaderboardResetCallback);
} catch (error)
{
// Handle error
logger.info("createLeaderboard failed......... ")
}
}
createLeaderboard();
}
function leaderboardResetCallback(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, leaderboard: nkruntime.Leaderboard)
{
logger.info(`Leaderboard ${leaderboard.id} has been reset.`);
try
{
// Fetch the leaderboard records
let results = nk.leaderboardRecordsList(leaderboard.id, [], 1, undefined);
if (results && results.records && results.records.length > 0)
{
let topRecord = results.records[0];
let topPlayerId = topRecord.ownerId;
// Reward the top player with 2000 coins
let changeset = {coins: 2000};
nk.walletUpdate(topPlayerId, changeset, {}, true);
logger.info(`Top player ${topPlayerId} rewarded with 2000 coins.`);
//Reward notification to the winner
{
// Send a persistent message to the top player
let content = {reward: 2000, message: "Congratulations! You've been rewarded with 2000 coins for being the top player."};
let subject = "Weekly Leaderboard Reward";
let senderId = ""; // Can be an empty string or a specific sender ID
// Send the notification with the correct content type
nk.notificationSend(topPlayerId, subject, content, NOTIFICATION_MSG_WEEKLY_LEADERBOARD_REWARD, senderId, true);
logger.info(`Notification sent to top player ${topPlayerId}.`);
}
} else
{
logger.info("No players in the leaderboard.");
}
} catch (error)
{
logger.error(`Failed to fetch leaderboard records or update wallet: ${error}`);
}
}