Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

66 linhas
1.8 KiB

  1. const scheduler = require('../../misc/scheduler');
  2. const loginRewards = require('../../config/loginRewards');
  3. const mail = require('../../mail/mail');
  4. const calculateDaysSkipped = (oldTime, newTime) => {
  5. let daysSkipped = 1;
  6. if (oldTime.year === newTime.year && oldTime.month === newTime.month) {
  7. //Same year and month
  8. daysSkipped = newTime.day - oldTime.day;
  9. } else if (oldTime.year === newTime.year) {
  10. //Same month
  11. let daysInMonth = scheduler.daysInMonth(oldTime.month);
  12. daysSkipped = (daysInMonth - oldTime.day) + newTime.day;
  13. for (let i = oldTime.month + 1; i < newTime.month - 1; i++)
  14. daysSkipped += scheduler.daysInMonth(i);
  15. } else {
  16. //Different year and month
  17. const daysInMonth = scheduler.daysInMonth(oldTime.month);
  18. daysSkipped = (daysInMonth - oldTime.day) + newTime.day;
  19. for (let i = oldTime.year + 1; i < newTime.year - 1; i++)
  20. daysSkipped += 365;
  21. for (let i = oldTime.month + 1; i < 12; i++)
  22. daysSkipped += scheduler.daysInMonth(i);
  23. for (let i = 0; i < newTime.month - 1; i++)
  24. daysSkipped += scheduler.daysInMonth(i);
  25. }
  26. return daysSkipped;
  27. };
  28. module.exports = async (cpnAuth, data, character, cbDone) => {
  29. const accountInfo = cpnAuth.accountInfo;
  30. const time = scheduler.getTime();
  31. const lastLogin = accountInfo.lastLogin;
  32. accountInfo.lastLogin = time;
  33. if (
  34. !lastLogin ||
  35. (
  36. lastLogin.day === time.day &&
  37. lastLogin.month === time.month &&
  38. lastLogin.year === time.year
  39. )
  40. ) {
  41. cbDone();
  42. return;
  43. }
  44. const daysSkipped = calculateDaysSkipped(lastLogin, time);
  45. if (daysSkipped === 1)
  46. accountInfo.loginStreak++;
  47. else
  48. accountInfo.loginStreak -= (daysSkipped - 1);
  49. accountInfo.loginStreak = Math.min(1, Math.max(21, accountInfo.loginStreak));
  50. const rewards = loginRewards.generate(accountInfo.loginStreak);
  51. mail.sendMail(character.name, rewards, cbDone);
  52. };