AutoAnnounceTaskManager.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.L2DatabaseFactory;
  22. import com.l2jserver.gameserver.ThreadPoolManager;
  23. import com.l2jserver.gameserver.util.Broadcast;
  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. L2DatabaseFactory.close(conn);
  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)
  87. {
  88. Connection conn = null;
  89. try
  90. {
  91. conn = L2DatabaseFactory.getInstance().getConnection();
  92. PreparedStatement statement = conn.prepareStatement("INSERT INTO auto_announcements (id, initial, delay, cycle, memo) VALUES (?,?,?,?,?)");
  93. statement.setInt(1, _nextId);
  94. statement.setLong(2, initial);
  95. statement.setLong(3, delay);
  96. statement.setInt(4, repeat);
  97. statement.setString(5, memo);
  98. statement.execute();
  99. statement.close();
  100. String[] text = memo.split("/n");
  101. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_nextId++, delay, repeat, text), initial);
  102. }
  103. catch (Exception e)
  104. {
  105. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to add announcements data.", e);
  106. }
  107. finally
  108. {
  109. L2DatabaseFactory.close(conn);
  110. }
  111. }
  112. public void deleteAutoAnnounce(int index)
  113. {
  114. Connection conn = null;
  115. try
  116. {
  117. AutoAnnouncement a = _announces.get(index);
  118. a.stopAnnounce();
  119. conn = L2DatabaseFactory.getInstance().getConnection();
  120. PreparedStatement statement = conn.prepareStatement("DELETE FROM auto_announcements WHERE id = ?");
  121. statement.setInt(1, a._id);
  122. statement.execute();
  123. statement.close();
  124. _announces.remove(index);
  125. }
  126. catch (Exception e)
  127. {
  128. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to delete announcements data.", e);
  129. }
  130. finally
  131. {
  132. L2DatabaseFactory.close(conn);
  133. }
  134. }
  135. public class AutoAnnouncement implements Runnable
  136. {
  137. private int _id;
  138. private long _delay;
  139. private int _repeat = -1;
  140. private String[] _memo;
  141. private boolean _stopped = false;
  142. public AutoAnnouncement(int id, long delay, int repeat, String[] memo)
  143. {
  144. _id = id;
  145. _delay = delay;
  146. _repeat = repeat;
  147. _memo = memo;
  148. if (!_announces.contains(this))
  149. _announces.add(this);
  150. }
  151. public String[] getMemo()
  152. {
  153. return _memo;
  154. }
  155. public void stopAnnounce()
  156. {
  157. _stopped = true;
  158. }
  159. @Override
  160. public void run()
  161. {
  162. if (!_stopped && _repeat != 0)
  163. {
  164. for (String text : _memo)
  165. {
  166. announce(text);
  167. }
  168. if (_repeat > 0)
  169. _repeat--;
  170. ThreadPoolManager.getInstance().scheduleGeneral(this, _delay);
  171. }
  172. else
  173. {
  174. stopAnnounce();
  175. }
  176. }
  177. }
  178. public void announce(String text)
  179. {
  180. Broadcast.announceToOnlinePlayers(text);
  181. _log.info("AutoAnnounce: " + text);
  182. }
  183. @SuppressWarnings("synthetic-access")
  184. private static class SingletonHolder
  185. {
  186. protected static final AutoAnnounceTaskManager _instance = new AutoAnnounceTaskManager();
  187. }
  188. }