AutoAnnounceTaskManager.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package com.l2jserver.gameserver.taskmanager;
  14. import java.sql.Connection;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.util.List;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastList;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.util.Broadcast;
  25. /**
  26. * @author nBd
  27. */
  28. public class AutoAnnounceTaskManager
  29. {
  30. protected static final Logger _log = Logger.getLogger(AutoAnnounceTaskManager.class.getName());
  31. protected List<AutoAnnouncement> _announces = new FastList<AutoAnnouncement>();
  32. private int _nextId = 1;
  33. public static AutoAnnounceTaskManager getInstance()
  34. {
  35. return SingletonHolder._instance;
  36. }
  37. private AutoAnnounceTaskManager()
  38. {
  39. restore();
  40. }
  41. public List<AutoAnnouncement> getAutoAnnouncements()
  42. {
  43. return _announces;
  44. }
  45. public void restore()
  46. {
  47. if (!_announces.isEmpty())
  48. {
  49. for (AutoAnnouncement a : _announces)
  50. {
  51. a.stopAnnounce();
  52. }
  53. _announces.clear();
  54. }
  55. Connection conn = null;
  56. int count = 0;
  57. try
  58. {
  59. conn = L2DatabaseFactory.getInstance().getConnection();
  60. PreparedStatement statement = conn.prepareStatement("SELECT * FROM auto_announcements");
  61. ResultSet data = statement.executeQuery();
  62. while (data.next())
  63. {
  64. int id = data.getInt("id");
  65. long initial = data.getLong("initial");
  66. long delay = data.getLong("delay");
  67. int repeat = data.getInt("cycle");
  68. String memo = data.getString("memo");
  69. boolean isCritical = Boolean.parseBoolean(data.getString("isCritical"));
  70. String[] text = memo.split("/n");
  71. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(id, delay, repeat, text, isCritical), initial);
  72. count++;
  73. if (_nextId <= id)
  74. {
  75. _nextId = id + 1;
  76. }
  77. }
  78. data.close();
  79. statement.close();
  80. }
  81. catch (Exception e)
  82. {
  83. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to load announcements data.", e);
  84. }
  85. finally
  86. {
  87. L2DatabaseFactory.close(conn);
  88. }
  89. _log.log(Level.INFO, "AutoAnnoucements: Loaded " + count + " Auto Annoucement Data.");
  90. }
  91. public void addAutoAnnounce(long initial, long delay, int repeat, String memo, boolean isCritical)
  92. {
  93. Connection conn = null;
  94. try
  95. {
  96. conn = L2DatabaseFactory.getInstance().getConnection();
  97. PreparedStatement statement = conn.prepareStatement("INSERT INTO auto_announcements (id, initial, delay, cycle, memo, isCritical) VALUES (?,?,?,?,?,?)");
  98. statement.setInt(1, _nextId);
  99. statement.setLong(2, initial);
  100. statement.setLong(3, delay);
  101. statement.setInt(4, repeat);
  102. statement.setString(5, memo);
  103. statement.setString(6, String.valueOf(isCritical));
  104. statement.execute();
  105. statement.close();
  106. String[] text = memo.split("/n");
  107. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_nextId++, delay, repeat, text, isCritical), initial);
  108. }
  109. catch (Exception e)
  110. {
  111. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to add announcements data.", e);
  112. }
  113. finally
  114. {
  115. L2DatabaseFactory.close(conn);
  116. }
  117. }
  118. public void deleteAutoAnnounce(int index)
  119. {
  120. Connection conn = null;
  121. try
  122. {
  123. AutoAnnouncement a = _announces.get(index);
  124. a.stopAnnounce();
  125. conn = L2DatabaseFactory.getInstance().getConnection();
  126. PreparedStatement statement = conn.prepareStatement("DELETE FROM auto_announcements WHERE id = ?");
  127. statement.setInt(1, a._id);
  128. statement.execute();
  129. statement.close();
  130. _announces.remove(index);
  131. }
  132. catch (Exception e)
  133. {
  134. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to delete announcements data.", e);
  135. }
  136. finally
  137. {
  138. L2DatabaseFactory.close(conn);
  139. }
  140. }
  141. public class AutoAnnouncement implements Runnable
  142. {
  143. private final int _id;
  144. private final long _delay;
  145. private int _repeat = -1;
  146. private final String[] _memo;
  147. private boolean _stopped = false;
  148. private final boolean _isCritical;
  149. public AutoAnnouncement(int id, long delay, int repeat, String[] memo, boolean isCritical)
  150. {
  151. _id = id;
  152. _delay = delay;
  153. _repeat = repeat;
  154. _memo = memo;
  155. _isCritical = isCritical;
  156. if (!_announces.contains(this))
  157. {
  158. _announces.add(this);
  159. }
  160. }
  161. public String[] getMemo()
  162. {
  163. return _memo;
  164. }
  165. public void stopAnnounce()
  166. {
  167. _stopped = true;
  168. }
  169. public boolean isCritical()
  170. {
  171. return _isCritical;
  172. }
  173. @Override
  174. public void run()
  175. {
  176. if (!_stopped && (_repeat != 0))
  177. {
  178. for (String text : _memo)
  179. {
  180. announce(text, _isCritical);
  181. }
  182. if (_repeat > 0)
  183. {
  184. _repeat--;
  185. }
  186. ThreadPoolManager.getInstance().scheduleGeneral(this, _delay);
  187. }
  188. else
  189. {
  190. stopAnnounce();
  191. }
  192. }
  193. }
  194. public void announce(String text, boolean isCritical)
  195. {
  196. Broadcast.announceToOnlinePlayers(text, isCritical);
  197. if (Config.LOG_AUTO_ANNOUNCEMENTS)
  198. {
  199. _log.info((isCritical ? "Critical AutoAnnounce" : "AutoAnnounce") + text);
  200. }
  201. }
  202. @SuppressWarnings("synthetic-access")
  203. private static class SingletonHolder
  204. {
  205. protected static final AutoAnnounceTaskManager _instance = new AutoAnnounceTaskManager();
  206. }
  207. }