2
0

AutoAnnounceTaskManager.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2004-2014 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;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.Statement;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javolution.util.FastList;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.L2DatabaseFactory;
  30. import com.l2jserver.gameserver.ThreadPoolManager;
  31. import com.l2jserver.gameserver.util.Broadcast;
  32. /**
  33. * @author nBd
  34. */
  35. public class AutoAnnounceTaskManager
  36. {
  37. private static final Logger _log = Logger.getLogger(AutoAnnounceTaskManager.class.getName());
  38. protected final List<AutoAnnouncement> _announces = new FastList<>();
  39. private int _nextId = 1;
  40. protected AutoAnnounceTaskManager()
  41. {
  42. restore();
  43. }
  44. public List<AutoAnnouncement> getAutoAnnouncements()
  45. {
  46. return _announces;
  47. }
  48. public void restore()
  49. {
  50. if (!_announces.isEmpty())
  51. {
  52. for (AutoAnnouncement a : _announces)
  53. {
  54. a.stopAnnounce();
  55. }
  56. _announces.clear();
  57. }
  58. int count = 0;
  59. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  60. Statement s = con.createStatement();
  61. ResultSet data = s.executeQuery("SELECT * FROM auto_announcements"))
  62. {
  63. while (data.next())
  64. {
  65. int id = data.getInt("id");
  66. long initial = data.getLong("initial");
  67. long delay = data.getLong("delay");
  68. int repeat = data.getInt("cycle");
  69. String memo = data.getString("memo");
  70. boolean isCritical = Boolean.parseBoolean(data.getString("isCritical"));
  71. String[] text = memo.split("/n");
  72. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(id, delay, repeat, text, isCritical), initial);
  73. count++;
  74. if (_nextId <= id)
  75. {
  76. _nextId = id + 1;
  77. }
  78. }
  79. }
  80. catch (Exception e)
  81. {
  82. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to load announcements data.", e);
  83. }
  84. _log.log(Level.INFO, "AutoAnnoucements: Loaded " + count + " Auto Annoucement Data.");
  85. }
  86. public void addAutoAnnounce(long initial, long delay, int repeat, String memo, boolean isCritical)
  87. {
  88. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  89. PreparedStatement statement = con.prepareStatement("INSERT INTO auto_announcements (id, initial, delay, cycle, memo, isCritical) VALUES (?,?,?,?,?,?)"))
  90. {
  91. statement.setInt(1, _nextId);
  92. statement.setLong(2, initial);
  93. statement.setLong(3, delay);
  94. statement.setInt(4, repeat);
  95. statement.setString(5, memo);
  96. statement.setString(6, String.valueOf(isCritical));
  97. statement.execute();
  98. String[] text = memo.split("/n");
  99. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_nextId++, delay, repeat, text, isCritical), initial);
  100. }
  101. catch (Exception e)
  102. {
  103. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to add announcements data.", e);
  104. }
  105. }
  106. public void deleteAutoAnnounce(int index)
  107. {
  108. AutoAnnouncement a = _announces.remove(index);
  109. a.stopAnnounce();
  110. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  111. PreparedStatement statement = con.prepareStatement("DELETE FROM auto_announcements WHERE id = ?"))
  112. {
  113. statement.setInt(1, a.getId());
  114. statement.execute();
  115. }
  116. catch (Exception e)
  117. {
  118. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to delete announcements data.", e);
  119. }
  120. }
  121. public class AutoAnnouncement implements Runnable
  122. {
  123. private final int _id;
  124. private final long _delay;
  125. private int _repeat = -1;
  126. private final String[] _memo;
  127. private boolean _stopped = false;
  128. private final boolean _isCritical;
  129. public AutoAnnouncement(int id, long delay, int repeat, String[] memo, boolean isCritical)
  130. {
  131. _id = id;
  132. _delay = delay;
  133. _repeat = repeat;
  134. _memo = memo;
  135. _isCritical = isCritical;
  136. if (!_announces.contains(this))
  137. {
  138. _announces.add(this);
  139. }
  140. }
  141. public int getId()
  142. {
  143. return _id;
  144. }
  145. public String[] getMemo()
  146. {
  147. return _memo;
  148. }
  149. public void stopAnnounce()
  150. {
  151. _stopped = true;
  152. }
  153. public boolean isCritical()
  154. {
  155. return _isCritical;
  156. }
  157. @Override
  158. public void run()
  159. {
  160. if (!_stopped && (_repeat != 0))
  161. {
  162. for (String text : _memo)
  163. {
  164. announce(text, _isCritical);
  165. }
  166. if (_repeat > 0)
  167. {
  168. _repeat--;
  169. }
  170. ThreadPoolManager.getInstance().scheduleGeneral(this, _delay);
  171. }
  172. else
  173. {
  174. stopAnnounce();
  175. }
  176. }
  177. }
  178. public void announce(String text, boolean isCritical)
  179. {
  180. Broadcast.announceToOnlinePlayers(text, isCritical);
  181. if (Config.LOG_AUTO_ANNOUNCEMENTS)
  182. {
  183. _log.info((isCritical ? "Critical AutoAnnounce" : "AutoAnnounce") + text);
  184. }
  185. }
  186. public static AutoAnnounceTaskManager getInstance()
  187. {
  188. return SingletonHolder._instance;
  189. }
  190. private static class SingletonHolder
  191. {
  192. protected static final AutoAnnounceTaskManager _instance = new AutoAnnounceTaskManager();
  193. }
  194. }