You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

89 lines
2.2 KiB

  1. const scheduler = require('../../misc/scheduler');
  2. const rewardGenerator = require('../../misc/rewardGenerator');
  3. const maxRewardedDays = 21;
  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. let { lastLogin, loginStreak } = accountInfo;
  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. loginStreak++;
  47. else
  48. loginStreak = 1;
  49. accountInfo.loginStreak = loginStreak;
  50. const cappedLoginStreak = Math.max(1, Math.min(maxRewardedDays, loginStreak));
  51. const itemCount = 1 + ~~(cappedLoginStreak / 2);
  52. const rewards = rewardGenerator(itemCount);
  53. if (!rewards) {
  54. cbDone();
  55. return;
  56. }
  57. const msg = `Daily login reward for ${loginStreak} day${(loginStreak > 1) ? 's' : ''}`;
  58. //Hack: Mail is a mod. As such, events should be a mod that depends on mail
  59. if (global.mailManager) {
  60. await global.mailManager.sendSystemMail({
  61. to: character.name,
  62. subject: 'Login Rewards',
  63. msg,
  64. items: rewards
  65. });
  66. }
  67. cbDone();
  68. };