25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

80 satır
2.1 KiB

  1. const scheduler = require('../../misc/scheduler');
  2. const rewardGenerator = require('../../misc/rewardGenerator');
  3. const mail = require('../../mail/mail');
  4. const events = require('../../misc/events');
  5. const calculateDaysSkipped = (oldTime, newTime) => {
  6. let daysSkipped = 1;
  7. if (oldTime.year === newTime.year && oldTime.month === newTime.month) {
  8. //Same year and month
  9. daysSkipped = newTime.day - oldTime.day;
  10. } else if (oldTime.year === newTime.year) {
  11. //Same month
  12. let daysInMonth = scheduler.daysInMonth(oldTime.month);
  13. daysSkipped = (daysInMonth - oldTime.day) + newTime.day;
  14. for (let i = oldTime.month + 1; i < newTime.month - 1; i++)
  15. daysSkipped += scheduler.daysInMonth(i);
  16. } else {
  17. //Different year and month
  18. const daysInMonth = scheduler.daysInMonth(oldTime.month);
  19. daysSkipped = (daysInMonth - oldTime.day) + newTime.day;
  20. for (let i = oldTime.year + 1; i < newTime.year - 1; i++)
  21. daysSkipped += 365;
  22. for (let i = oldTime.month + 1; i < 12; i++)
  23. daysSkipped += scheduler.daysInMonth(i);
  24. for (let i = 0; i < newTime.month - 1; i++)
  25. daysSkipped += scheduler.daysInMonth(i);
  26. }
  27. return daysSkipped;
  28. };
  29. module.exports = async (cpnAuth, data, character, cbDone) => {
  30. const accountInfo = cpnAuth.accountInfo;
  31. const time = scheduler.getTime();
  32. let { lastLogin, loginStreak } = accountInfo;
  33. accountInfo.lastLogin = time;
  34. if (
  35. !lastLogin ||
  36. (
  37. lastLogin.day === time.day &&
  38. lastLogin.month === time.month &&
  39. lastLogin.year === time.year
  40. )
  41. ) {
  42. cbDone();
  43. return;
  44. }
  45. const daysSkipped = calculateDaysSkipped(lastLogin, time);
  46. if (daysSkipped === 1)
  47. loginStreak++;
  48. else
  49. loginStreak = 1;
  50. loginStreak = Math.max(1, Math.min(21, loginStreak));
  51. accountInfo.loginStreak = loginStreak;
  52. const itemCount = 1 + ~~(loginStreak / 2);
  53. const rewardConfig = [];
  54. events.emit('onBeforeGenerateLoginRewards', rewardConfig);
  55. const rewards = rewardGenerator(itemCount, rewardConfig);
  56. if (!rewards.length) {
  57. cbDone();
  58. return;
  59. }
  60. rewards[0].msg = `Daily login reward for ${loginStreak} day${(loginStreak > 1) ? 's' : ''}:`;
  61. mail.sendMail(character.name, rewards, cbDone);
  62. };