TaskBirthday.java 3.2 KB

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