TaskBirthday.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.concurrent.TimeUnit;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  27. import com.l2jserver.gameserver.instancemanager.MailManager;
  28. import com.l2jserver.gameserver.model.entity.Message;
  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. /**
  34. * Birthday Gift task.
  35. * @author Zoey76
  36. */
  37. public class TaskBirthday extends Task
  38. {
  39. private static final String NAME = "birthday";
  40. /** Get all players that have had a birthday since last check. */
  41. private static final String SELECT_PENDING_BIRTHDAY_GIFTS = "SELECT charId, char_name, createDate, (YEAR(NOW()) - YEAR(createDate)) AS age " //
  42. + "FROM characters WHERE (YEAR(NOW()) - YEAR(createDate) > 0) AND (DATE_FORMAT(createDate, '%m-%d') > DATE_FORMAT(FROM_UNIXTIME(?), '%m-%d'))";
  43. @Override
  44. public String getName()
  45. {
  46. return NAME;
  47. }
  48. @Override
  49. public void onTimeElapsed(ExecutedTask task)
  50. {
  51. // TODO(Zoey76): Fix first run.
  52. final int birthdayGiftCount = giveBirthdayGifts(task.getLastActivation());
  53. _log.info("BirthdayManager: " + birthdayGiftCount + " gifts sent.");
  54. }
  55. private int giveBirthdayGifts(long lastActivation)
  56. {
  57. int birthdayGiftCount = 0;
  58. try (Connection con = ConnectionFactory.getInstance().getConnection();
  59. PreparedStatement ps = con.prepareStatement(SELECT_PENDING_BIRTHDAY_GIFTS))
  60. {
  61. ps.setLong(1, TimeUnit.SECONDS.convert(lastActivation, TimeUnit.MILLISECONDS));
  62. try (ResultSet rs = ps.executeQuery())
  63. {
  64. while (rs.next())
  65. {
  66. String text = Config.ALT_BIRTHDAY_MAIL_TEXT;
  67. text = text.replaceAll("$c1", rs.getString("char_name"));
  68. text = text.replaceAll("$s1", Integer.toString(rs.getInt("age")));
  69. final Message msg = new Message(rs.getInt("charId"), Config.ALT_BIRTHDAY_MAIL_SUBJECT, text, Message.SendBySystem.ALEGRIA);
  70. msg.createAttachments().addItem("Birthday", Config.ALT_BIRTHDAY_GIFT, 1, null, null);
  71. MailManager.getInstance().sendMessage(msg);
  72. birthdayGiftCount++;
  73. }
  74. }
  75. }
  76. catch (SQLException e)
  77. {
  78. _log.warning("Error checking birthdays: " + e.getMessage());
  79. }
  80. return birthdayGiftCount;
  81. }
  82. @Override
  83. public void initializate()
  84. {
  85. super.initializate();
  86. TaskManager.addUniqueTask(NAME, TaskTypes.TYPE_GLOBAL_TASK, "1", "06:30:00", "");
  87. }
  88. }