AutoAnnounceTaskManager.java 5.6 KB

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