AutoAnnounceTaskManager.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 com.l2jserver.L2DatabaseFactory;
  21. import com.l2jserver.gameserver.ThreadPoolManager;
  22. import com.l2jserver.gameserver.util.Broadcast;
  23. import javolution.util.FastList;
  24. /**
  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. a.stopAnnounce();
  51. _announces.clear();
  52. }
  53. Connection conn = null;
  54. int count = 0;
  55. try
  56. {
  57. conn = L2DatabaseFactory.getInstance().getConnection();
  58. PreparedStatement statement = conn.prepareStatement("SELECT id, initial, delay, cycle, memo FROM auto_announcements");
  59. ResultSet data = statement.executeQuery();
  60. while (data.next())
  61. {
  62. int id = data.getInt("id");
  63. long initial = data.getLong("initial");
  64. long delay = data.getLong("delay");
  65. int repeat = data.getInt("cycle");
  66. String memo = data.getString("memo");
  67. String[] text = memo.split("/n");
  68. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(id, delay, repeat, text), initial);
  69. count++;
  70. if (_nextId < id)
  71. _nextId = id + 1;
  72. }
  73. data.close();
  74. statement.close();
  75. }
  76. catch (Exception e)
  77. {
  78. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to load announcements data.", e);
  79. }
  80. finally
  81. {
  82. try
  83. {
  84. conn.close();
  85. }
  86. catch (Exception e)
  87. {
  88. }
  89. }
  90. _log.log(Level.INFO, "AutoAnnoucements: Loaded " + count + " Auto Annoucement Data.");
  91. }
  92. public void addAutoAnnounce(long initial, long delay, int repeat, String memo)
  93. {
  94. Connection conn = null;
  95. try
  96. {
  97. conn = L2DatabaseFactory.getInstance().getConnection();
  98. PreparedStatement statement = conn.prepareStatement("INSERT INTO auto_announcements (id, initial, delay, cycle, memo) VALUES (?,?,?,?,?)");
  99. statement.setInt(1, _nextId);
  100. statement.setLong(2, initial);
  101. statement.setLong(3, delay);
  102. statement.setInt(4, repeat);
  103. statement.setString(5, memo);
  104. statement.execute();
  105. statement.close();
  106. String[] text = memo.split("/n");
  107. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_nextId++, delay, repeat, text), initial);
  108. }
  109. catch (Exception e)
  110. {
  111. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to add announcements data.", e);
  112. }
  113. finally
  114. {
  115. try
  116. {
  117. conn.close();
  118. }
  119. catch (Exception e)
  120. {
  121. }
  122. }
  123. }
  124. public void deleteAutoAnnounce(int index)
  125. {
  126. Connection conn = null;
  127. try
  128. {
  129. AutoAnnouncement a = _announces.get(index);
  130. a.stopAnnounce();
  131. conn = L2DatabaseFactory.getInstance().getConnection();
  132. PreparedStatement statement = conn.prepareStatement("DELETE FROM auto_announcements WHERE id = ?");
  133. statement.setInt(1, a._id);
  134. statement.execute();
  135. statement.close();
  136. _announces.remove(index);
  137. }
  138. catch (Exception e)
  139. {
  140. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to delete announcements data.", e);
  141. }
  142. finally
  143. {
  144. try
  145. {
  146. conn.close();
  147. }
  148. catch (Exception e)
  149. {
  150. }
  151. }
  152. }
  153. public class AutoAnnouncement implements Runnable
  154. {
  155. private int _id;
  156. private long _delay;
  157. private int _repeat = -1;
  158. private String[] _memo;
  159. private boolean _stopped = false;
  160. public AutoAnnouncement(int id, long delay, int repeat, String[] memo)
  161. {
  162. _id = id;
  163. _delay = delay;
  164. _repeat = repeat;
  165. _memo = memo;
  166. if (!_announces.contains(this))
  167. _announces.add(this);
  168. }
  169. public String[] getMemo()
  170. {
  171. return _memo;
  172. }
  173. public void stopAnnounce()
  174. {
  175. _stopped = true;
  176. }
  177. public void run()
  178. {
  179. if (!_stopped && _repeat != 0)
  180. {
  181. for (String text : _memo)
  182. {
  183. announce(text);
  184. }
  185. if (_repeat > 0)
  186. _repeat--;
  187. ThreadPoolManager.getInstance().scheduleGeneral(this, _delay);
  188. }
  189. else
  190. {
  191. stopAnnounce();
  192. }
  193. }
  194. }
  195. public void announce(String text)
  196. {
  197. Broadcast.announceToOnlinePlayers(text);
  198. _log.info("AutoAnnounce: " + text);
  199. }
  200. @SuppressWarnings("synthetic-access")
  201. private static class SingletonHolder
  202. {
  203. protected static final AutoAnnounceTaskManager _instance = new AutoAnnounceTaskManager();
  204. }
  205. }