AutoAnnouncement.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.announce;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.concurrent.ScheduledFuture;
  26. import java.util.logging.Level;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  29. import com.l2jserver.gameserver.ThreadPoolManager;
  30. import com.l2jserver.gameserver.util.Broadcast;
  31. /**
  32. * @author UnAfraid
  33. */
  34. public final class AutoAnnouncement extends Announcement implements Runnable
  35. {
  36. private static final String INSERT_QUERY = "INSERT INTO announcements (`type`, `content`, `author`, `initial`, `delay`, `repeat`) VALUES (?, ?, ?, ?, ?, ?)";
  37. private static final String UPDATE_QUERY = "UPDATE announcements SET `type` = ?, `content` = ?, `author` = ?, `initial` = ?, `delay` = ?, `repeat` = ? WHERE id = ?";
  38. private long _initial;
  39. private long _delay;
  40. private int _repeat = -1;
  41. private int _currentState;
  42. private ScheduledFuture<?> _task;
  43. public AutoAnnouncement(AnnouncementType type, String content, String author, long initial, long delay, int repeat)
  44. {
  45. super(type, content, author);
  46. _initial = initial;
  47. _delay = delay;
  48. _repeat = repeat;
  49. restartMe();
  50. }
  51. public AutoAnnouncement(ResultSet rset) throws SQLException
  52. {
  53. super(rset);
  54. _initial = rset.getLong("initial");
  55. _delay = rset.getLong("delay");
  56. _repeat = rset.getInt("repeat");
  57. restartMe();
  58. }
  59. public long getInitial()
  60. {
  61. return _initial;
  62. }
  63. public void setInitial(long initial)
  64. {
  65. _initial = initial;
  66. }
  67. public long getDelay()
  68. {
  69. return _delay;
  70. }
  71. public void setDelay(long delay)
  72. {
  73. _delay = delay;
  74. }
  75. public int getRepeat()
  76. {
  77. return _repeat;
  78. }
  79. public void setRepeat(int repeat)
  80. {
  81. _repeat = repeat;
  82. }
  83. @Override
  84. public boolean storeMe()
  85. {
  86. try (Connection con = ConnectionFactory.getInstance().getConnection();
  87. PreparedStatement ps = con.prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS))
  88. {
  89. ps.setInt(1, getType().ordinal());
  90. ps.setString(2, getContent());
  91. ps.setString(3, getAuthor());
  92. ps.setLong(4, getInitial());
  93. ps.setLong(5, getDelay());
  94. ps.setInt(6, getRepeat());
  95. ps.execute();
  96. try (ResultSet rset = ps.getGeneratedKeys())
  97. {
  98. if (rset.next())
  99. {
  100. _id = rset.getInt(1);
  101. }
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store announcement: ", e);
  107. return false;
  108. }
  109. return true;
  110. }
  111. @Override
  112. public boolean updateMe()
  113. {
  114. try (Connection con = ConnectionFactory.getInstance().getConnection();
  115. PreparedStatement ps = con.prepareStatement(UPDATE_QUERY))
  116. {
  117. ps.setInt(1, getType().ordinal());
  118. ps.setString(2, getContent());
  119. ps.setString(3, getAuthor());
  120. ps.setLong(4, getInitial());
  121. ps.setLong(5, getDelay());
  122. ps.setLong(6, getRepeat());
  123. ps.setLong(7, getId());
  124. ps.execute();
  125. }
  126. catch (Exception e)
  127. {
  128. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update announcement: ", e);
  129. return false;
  130. }
  131. return true;
  132. }
  133. @Override
  134. public boolean deleteMe()
  135. {
  136. if ((_task != null) && !_task.isCancelled())
  137. {
  138. _task.cancel(false);
  139. }
  140. return super.deleteMe();
  141. }
  142. public void restartMe()
  143. {
  144. if ((_task != null) && !_task.isCancelled())
  145. {
  146. _task.cancel(false);
  147. }
  148. _currentState = _repeat;
  149. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, _initial);
  150. }
  151. @Override
  152. public void run()
  153. {
  154. if ((_currentState == -1) || (_currentState > 0))
  155. {
  156. for (String content : getContent().split(Config.EOL))
  157. {
  158. Broadcast.toAllOnlinePlayers(content, (getType() == AnnouncementType.AUTO_CRITICAL));
  159. }
  160. if (_currentState != -1)
  161. {
  162. _currentState--;
  163. }
  164. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, _delay);
  165. }
  166. }
  167. }