FavoriteBoard.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 handlers.communityboard;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.text.SimpleDateFormat;
  24. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  25. import com.l2jserver.gameserver.cache.HtmCache;
  26. import com.l2jserver.gameserver.handler.CommunityBoardHandler;
  27. import com.l2jserver.gameserver.handler.IParseBoardHandler;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.util.Util;
  30. /**
  31. * Favorite board.
  32. * @author Zoey76
  33. */
  34. public class FavoriteBoard implements IParseBoardHandler
  35. {
  36. // SQL Queries
  37. private static final String SELECT_FAVORITES = "SELECT * FROM `bbs_favorites` WHERE `playerId`=? ORDER BY `favAddDate` DESC";
  38. private static final String DELETE_FAVORITE = "DELETE FROM `bbs_favorites` WHERE `playerId`=? AND `favId`=?";
  39. private static final String ADD_FAVORITE = "REPLACE INTO `bbs_favorites`(`playerId`, `favTitle`, `favBypass`) VALUES(?, ?, ?)";
  40. private static final String[] COMMANDS =
  41. {
  42. "_bbsgetfav",
  43. "bbs_add_fav",
  44. "_bbsdelfav_"
  45. };
  46. @Override
  47. public String[] getCommunityBoardCommands()
  48. {
  49. return COMMANDS;
  50. }
  51. @Override
  52. public boolean parseCommunityBoardCommand(String command, L2PcInstance activeChar)
  53. {
  54. // None of this commands can be added to favorites.
  55. if (command.startsWith("_bbsgetfav"))
  56. {
  57. // Load Favorite links
  58. final String list = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/CommunityBoard/favorite_list.html");
  59. final StringBuilder sb = new StringBuilder();
  60. try (Connection con = ConnectionFactory.getInstance().getConnection();
  61. PreparedStatement ps = con.prepareStatement(SELECT_FAVORITES))
  62. {
  63. ps.setInt(1, activeChar.getObjectId());
  64. try (ResultSet rs = ps.executeQuery())
  65. {
  66. while (rs.next())
  67. {
  68. String link = list.replaceAll("%fav_bypass%", String.valueOf(rs.getString("favBypass")));
  69. link = link.replaceAll("%fav_title%", rs.getString("favTitle"));
  70. final SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  71. link = link.replaceAll("%fav_add_date%", date.format(rs.getTimestamp("favAddDate")));
  72. link = link.replaceAll("%fav_id%", String.valueOf(rs.getInt("favId")));
  73. sb.append(link);
  74. }
  75. }
  76. String html = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/CommunityBoard/favorite.html");
  77. html = html.replaceAll("%fav_list%", sb.toString());
  78. CommunityBoardHandler.separateAndSend(html, activeChar);
  79. }
  80. catch (Exception e)
  81. {
  82. LOG.warning(FavoriteBoard.class.getSimpleName() + ": Couldn't load favorite links for player " + activeChar.getName());
  83. }
  84. }
  85. else if (command.startsWith("bbs_add_fav"))
  86. {
  87. final String bypass = CommunityBoardHandler.getInstance().removeBypass(activeChar);
  88. if (bypass != null)
  89. {
  90. final String[] parts = bypass.split("&", 2);
  91. if (parts.length != 2)
  92. {
  93. LOG.warning(FavoriteBoard.class.getSimpleName() + ": Couldn't add favorite link, " + bypass + " it's not a valid bypass!");
  94. return false;
  95. }
  96. try (Connection con = ConnectionFactory.getInstance().getConnection();
  97. PreparedStatement ps = con.prepareStatement(ADD_FAVORITE))
  98. {
  99. ps.setInt(1, activeChar.getObjectId());
  100. ps.setString(2, parts[0].trim());
  101. ps.setString(3, parts[1].trim());
  102. ps.execute();
  103. // Callback
  104. parseCommunityBoardCommand("_bbsgetfav", activeChar);
  105. }
  106. catch (Exception e)
  107. {
  108. LOG.warning(FavoriteBoard.class.getSimpleName() + ": Couldn't add favorite link " + bypass + " for player " + activeChar.getName());
  109. }
  110. }
  111. }
  112. else if (command.startsWith("_bbsdelfav_"))
  113. {
  114. final String favId = command.replaceAll("_bbsdelfav_", "");
  115. if (!Util.isDigit(favId))
  116. {
  117. LOG.warning(FavoriteBoard.class.getSimpleName() + ": Couldn't delete favorite link, " + favId + " it's not a valid ID!");
  118. return false;
  119. }
  120. try (Connection con = ConnectionFactory.getInstance().getConnection();
  121. PreparedStatement ps = con.prepareStatement(DELETE_FAVORITE))
  122. {
  123. ps.setInt(1, activeChar.getObjectId());
  124. ps.setInt(2, Integer.parseInt(favId));
  125. ps.execute();
  126. // Callback
  127. parseCommunityBoardCommand("_bbsgetfav", activeChar);
  128. }
  129. catch (Exception e)
  130. {
  131. LOG.warning(FavoriteBoard.class.getSimpleName() + ": Couldn't delete favorite link ID " + favId + " for player " + activeChar.getName());
  132. }
  133. }
  134. return true;
  135. }
  136. }