TaskBirthday.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.taskmanager.tasks;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.Calendar;
  21. import java.util.GregorianCalendar;
  22. import java.util.logging.Level;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.datatables.CharNameTable;
  26. import com.l2jserver.gameserver.instancemanager.MailManager;
  27. import com.l2jserver.gameserver.model.entity.Message;
  28. import com.l2jserver.gameserver.model.itemcontainer.Mail;
  29. import com.l2jserver.gameserver.taskmanager.Task;
  30. import com.l2jserver.gameserver.taskmanager.TaskManager;
  31. import com.l2jserver.gameserver.taskmanager.TaskManager.ExecutedTask;
  32. import com.l2jserver.gameserver.taskmanager.TaskTypes;
  33. import com.l2jserver.gameserver.util.Util;
  34. /**
  35. * @author Nyaran
  36. */
  37. public class TaskBirthday extends Task
  38. {
  39. private static final String NAME = "birthday";
  40. private static final String QUERY = "SELECT charId, createDate FROM characters WHERE createDate LIKE ?";
  41. private static final Calendar _today = Calendar.getInstance();
  42. private int _count = 0;
  43. @Override
  44. public String getName()
  45. {
  46. return NAME;
  47. }
  48. @Override
  49. public void onTimeElapsed(ExecutedTask task)
  50. {
  51. Calendar lastExecDate = Calendar.getInstance();
  52. long lastActivation = task.getLastActivation();
  53. if (lastActivation > 0)
  54. {
  55. lastExecDate.setTimeInMillis(lastActivation);
  56. }
  57. String rangeDate = "[" + Util.getDateString(lastExecDate.getTime()) + "] - [" + Util.getDateString(_today.getTime()) + "]";
  58. for (; !_today.before(lastExecDate); lastExecDate.add(Calendar.DATE, 1))
  59. {
  60. checkBirthday(lastExecDate.get(Calendar.YEAR), lastExecDate.get(Calendar.MONTH), lastExecDate.get(Calendar.DATE));
  61. }
  62. _log.info("BirthdayManager: " + _count + " gifts sent. " + rangeDate);
  63. }
  64. private void checkBirthday(int year, int month, int day)
  65. {
  66. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  67. PreparedStatement statement = con.prepareStatement(QUERY))
  68. {
  69. statement.setString(1, "%-" + getNum(month + 1) + "-" + getNum(day));
  70. try (ResultSet rset = statement.executeQuery())
  71. {
  72. while (rset.next())
  73. {
  74. int playerId = rset.getInt("charId");
  75. Calendar createDate = Calendar.getInstance();
  76. createDate.setTime(rset.getDate("createDate"));
  77. int age = year - createDate.get(Calendar.YEAR);
  78. if (age <= 0)
  79. {
  80. continue;
  81. }
  82. String text = Config.ALT_BIRTHDAY_MAIL_TEXT;
  83. if (text.contains("$c1"))
  84. {
  85. text = text.replace("$c1", CharNameTable.getInstance().getNameById(playerId));
  86. }
  87. if (text.contains("$s1"))
  88. {
  89. text = text.replace("$s1", String.valueOf(age));
  90. }
  91. Message msg = new Message(playerId, Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, Message.SendBySystem.ALEGRIA);
  92. Mail attachments = msg.createAttachments();
  93. attachments.addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
  94. MailManager.getInstance().sendMessage(msg);
  95. _count++;
  96. }
  97. }
  98. }
  99. catch (SQLException e)
  100. {
  101. _log.log(Level.WARNING, "Error checking birthdays. ", e);
  102. }
  103. // If character birthday is 29-Feb and year isn't leap, send gift on 28-feb
  104. GregorianCalendar calendar = new GregorianCalendar();
  105. if ((month == Calendar.FEBRUARY) && (day == 28) && !calendar.isLeapYear(_today.get(Calendar.YEAR)))
  106. {
  107. checkBirthday(year, Calendar.FEBRUARY, 29);
  108. }
  109. }
  110. /**
  111. * @param num the number to format.
  112. * @return the formatted number starting with a 0 if it is lower or equal than 10.
  113. */
  114. private String getNum(int num)
  115. {
  116. return (num <= 9) ? "0" + num : String.valueOf(num);
  117. }
  118. @Override
  119. public void initializate()
  120. {
  121. super.initializate();
  122. TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_GLOBAL_TASK, "1", "06:30:00", "");
  123. }
  124. }