TaskBirthday.java 4.7 KB

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