letafterAuthenticateDevice : AfterHookFunction<Session,AuthenticateDeviceRequest>=function(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama,session: nkruntime.Session,request: nkruntime.AuthenticateDeviceRequest){// Define a constant for how many milliseconds are in a day
constmsInADay=1000*60*60*24;// Get previous midnight
constpreviousMidnight=newDate().setHours(0,0,0,0);// Get the user's account so that we can check their metadata for a lastLoginTimestamp
constaccount=nk.accountGetId(ctx.userId);constlastLoginTimestamp : number=account.user.metadata.lastLoginTimestamp;letrewardCoins=0;if(!lastLoginTimestamp){// If the user has never logged in, give them 100 coins
rewardCoins=100;logger.debug("Player has never logged in before, giving them 100 coins.");}elseif(lastLoginTimestamp<previousMidnight){// If the user's last login was before midnight, we're going to give them at least 100 coins
rewardCoins=100;// Calculate how many full days have passed since midnight and the time they last logged in
consttimeBetweenMidnightAndLastLogin=previousMidnight-account.user.metadata.lastLoginTimestamp;consttotalFullDaysSinceLastLogin=Math.floor(timeBetweenMidnightAndLastLogin/msInADay);// Increase the reward by 100 for each full day since last login
rewardCoins+=totalFullDaysSinceLastLogin*100;logger.debug(`Player has logged in for the first time today and it has been a further ${totalFullDaysSinceLastLogin} days; giving them ${rewardCoins} coins.`);}else{logger.debug("Player has already logged in today, no coins are being rewarded.");}// Update the user's lastLoginTimestamp
constmetadata=account.user.metadata;metadata.lastLoginTimestamp=Date.now();nk.accountUpdateId(ctx.userId,null,null,null,null,null,null,metadata);// Give the player their coins
nk.walletUpdate(ctx.userId,{coins: rewardCoins});};