AutoAnnounceTaskManager.java 5.9 KB

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