TopicBBSManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright © 2004-2020 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.bbs.service;
  20. import java.text.DateFormat;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.LinkedList;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.StringTokenizer;
  28. import java.util.concurrent.CopyOnWriteArrayList;
  29. import com.l2jserver.gameserver.bbs.model.Forum;
  30. import com.l2jserver.gameserver.bbs.model.ForumType;
  31. import com.l2jserver.gameserver.bbs.model.Post;
  32. import com.l2jserver.gameserver.bbs.model.Topic;
  33. import com.l2jserver.gameserver.bbs.model.TopicType;
  34. import com.l2jserver.gameserver.dao.factory.impl.DAOFactory;
  35. import com.l2jserver.gameserver.data.sql.impl.ClanTable;
  36. import com.l2jserver.gameserver.handler.CommunityBoardHandler;
  37. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  38. import com.l2jserver.gameserver.util.StringUtil;
  39. /**
  40. * Topic BBS Manager.
  41. * @author Zoey76
  42. * @version 2.6.2.0
  43. */
  44. public class TopicBBSManager extends BaseBBSManager {
  45. private final List<Topic> table = new CopyOnWriteArrayList<>();
  46. private final Map<Forum, Integer> maxId = new HashMap<>();
  47. protected TopicBBSManager() {
  48. // Prevent external initialization.
  49. }
  50. public void addTopic(Topic topic) {
  51. table.add(topic);
  52. }
  53. public void delTopic(Topic topic) {
  54. table.remove(topic);
  55. }
  56. public void setMaxId(int id, Forum forum) {
  57. maxId.put(forum, id);
  58. }
  59. public int getMaxId(Forum f) {
  60. var i = maxId.get(f);
  61. if (i == null) {
  62. return 0;
  63. }
  64. return i;
  65. }
  66. public Topic getTopicById(int id) {
  67. for (var topic : table) {
  68. if (topic.getId() == id) {
  69. return topic;
  70. }
  71. }
  72. return null;
  73. }
  74. @Override
  75. public void parsewrite(String ar1, String ar2, String ar3, String ar4, String ar5, L2PcInstance activeChar) {
  76. if (ar1.equals("crea")) {
  77. final var forum = ForumsBBSManager.getInstance().getForumByID(Integer.parseInt(ar2));
  78. if (forum == null) {
  79. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + ar2 + " is not implemented yet</center><br><br></body></html>", activeChar);
  80. return;
  81. }
  82. DAOFactory.getInstance().getForumRepository().findById(forum);
  83. final var id = TopicBBSManager.getInstance().getMaxId(forum) + 1;
  84. final var date = Calendar.getInstance().getTimeInMillis();
  85. final var topic = new Topic(id, Integer.parseInt(ar2), ar5, date, activeChar.getName(), activeChar.getObjectId(), TopicType.MEMO, 0);
  86. TopicBBSManager.getInstance().addTopic(topic);
  87. DAOFactory.getInstance().getTopicRepository().save(topic);
  88. forum.addTopic(topic);
  89. TopicBBSManager.getInstance().setMaxId(topic.getId(), forum);
  90. final var posts = new LinkedList<Post>();
  91. posts.add(new Post(0, activeChar.getName(), activeChar.getObjectId(), Calendar.getInstance().getTimeInMillis(), topic.getId(), forum.getId(), ar4));
  92. PostBBSManager.getInstance().addPostByTopic(topic, posts);
  93. parsecmd("_bbsmemo", activeChar);
  94. } else if (ar1.equals("del")) {
  95. final var forum = ForumsBBSManager.getInstance().getForumByID(Integer.parseInt(ar2));
  96. if (forum == null) {
  97. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + ar2 + " does not exist !</center><br><br></body></html>", activeChar);
  98. return;
  99. }
  100. final var topic = forum.getTopic(Integer.parseInt(ar3));
  101. if (topic == null) {
  102. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the topic: " + ar3 + " does not exist !</center><br><br></body></html>", activeChar);
  103. return;
  104. }
  105. final var posts = PostBBSManager.getInstance().getGPosttByTopic(topic);
  106. if (!posts.isEmpty()) {
  107. PostBBSManager.getInstance().delPostByTopic(topic);
  108. DAOFactory.getInstance().getPostRepository().delete(topic);
  109. }
  110. DAOFactory.getInstance().getTopicRepository().delete(topic, forum);
  111. parsecmd("_bbsmemo", activeChar);
  112. } else {
  113. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the command: " + ar1 + " is not implemented yet</center><br><br></body></html>", activeChar);
  114. }
  115. }
  116. @Override
  117. public void parsecmd(String command, L2PcInstance activeChar) {
  118. if (command.equals("_bbsmemo")) {
  119. showTopics(activeChar.getMemo(), activeChar, 1, activeChar.getMemo().getId());
  120. } else if (command.startsWith("_bbstopics;read")) {
  121. StringTokenizer st = new StringTokenizer(command, ";");
  122. st.nextToken();
  123. st.nextToken();
  124. int idf = Integer.parseInt(st.nextToken());
  125. String index = null;
  126. if (st.hasMoreTokens()) {
  127. index = st.nextToken();
  128. }
  129. int ind = 0;
  130. if (index == null) {
  131. ind = 1;
  132. } else {
  133. ind = Integer.parseInt(index);
  134. }
  135. showTopics(ForumsBBSManager.getInstance().getForumByID(idf), activeChar, ind, idf);
  136. } else if (command.startsWith("_bbstopics;crea")) {
  137. StringTokenizer st = new StringTokenizer(command, ";");
  138. st.nextToken();
  139. st.nextToken();
  140. int idf = Integer.parseInt(st.nextToken());
  141. showNewTopic(ForumsBBSManager.getInstance().getForumByID(idf), activeChar, idf);
  142. } else if (command.startsWith("_bbstopics;del")) {
  143. StringTokenizer st = new StringTokenizer(command, ";");
  144. st.nextToken();
  145. st.nextToken();
  146. int idf = Integer.parseInt(st.nextToken());
  147. int idt = Integer.parseInt(st.nextToken());
  148. Forum f = ForumsBBSManager.getInstance().getForumByID(idf);
  149. if (f == null) {
  150. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + idf + " does not exist !</center><br><br></body></html>", activeChar);
  151. } else {
  152. Topic t = f.getTopic(idt);
  153. if (t == null) {
  154. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the topic: " + idt + " does not exist !</center><br><br></body></html>", activeChar);
  155. } else {
  156. final var p = PostBBSManager.getInstance().getGPosttByTopic(t);
  157. if (p != null) {
  158. PostBBSManager.getInstance().delPostByTopic(t);
  159. DAOFactory.getInstance().getPostRepository().delete(t);
  160. }
  161. DAOFactory.getInstance().getTopicRepository().delete(t, f);
  162. parsecmd("_bbsmemo", activeChar);
  163. }
  164. }
  165. } else {
  166. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the command: " + command + " is not implemented yet</center><br><br></body></html>", activeChar);
  167. }
  168. }
  169. private void showNewTopic(Forum forum, L2PcInstance activeChar, int idf) {
  170. if (forum == null) {
  171. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + idf + " is not implemented yet</center><br><br></body></html>", activeChar);
  172. } else if (forum.getType() == ForumType.MEMO) {
  173. showMemoNewTopics(forum, activeChar);
  174. } else {
  175. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + forum.getName() + " is not implemented yet</center><br><br></body></html>", activeChar);
  176. }
  177. }
  178. private void showMemoNewTopics(Forum forum, L2PcInstance activeChar) {
  179. final String html = StringUtil.concat("<html><body><br><br><table border=0 width=610><tr><td width=10></td><td width=600 align=left><a action=\"bypass _bbshome\">HOME</a>&nbsp;>&nbsp;<a action=\"bypass _bbsmemo\">Memo Form</a></td></tr></table><img src=\"L2UI.squareblank\" width=\"1\" height=\"10\"><center><table border=0 cellspacing=0 cellpadding=0><tr><td width=610><img src=\"sek.cbui355\" width=\"610\" height=\"1\"><br1><img src=\"sek.cbui355\" width=\"610\" height=\"1\"></td></tr></table><table fixwidth=610 border=0 cellspacing=0 cellpadding=0><tr><td><img src=\"l2ui.mini_logo\" width=5 height=20></td></tr><tr><td><img src=\"l2ui.mini_logo\" width=5 height=1></td><td align=center FIXWIDTH=60 height=29>&$413;</td><td FIXWIDTH=540><edit var = \"Title\" width=540 height=13></td><td><img src=\"l2ui.mini_logo\" width=5 height=1></td></tr></table><table fixwidth=610 border=0 cellspacing=0 cellpadding=0><tr><td><img src=\"l2ui.mini_logo\" width=5 height=10></td></tr><tr><td><img src=\"l2ui.mini_logo\" width=5 height=1></td><td align=center FIXWIDTH=60 height=29 valign=top>&$427;</td><td align=center FIXWIDTH=540><MultiEdit var =\"Content\" width=535 height=313></td><td><img src=\"l2ui.mini_logo\" width=5 height=1></td></tr><tr><td><img src=\"l2ui.mini_logo\" width=5 height=10></td></tr></table><table fixwidth=610 border=0 cellspacing=0 cellpadding=0><tr><td><img src=\"l2ui.mini_logo\" width=5 height=10></td></tr><tr><td><img src=\"l2ui.mini_logo\" width=5 height=1></td><td align=center FIXWIDTH=60 height=29>&nbsp;</td><td align=center FIXWIDTH=70><button value=\"&$140;\" action=\"Write Topic crea ", String.valueOf(forum.getId()), " Title Content Title\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\" ></td><td align=center FIXWIDTH=70><button value = \"&$141;\" action=\"bypass _bbsmemo\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\"> </td><td align=center FIXWIDTH=400>&nbsp;</td><td><img src=\"l2ui.mini_logo\" width=5 height=1></td></tr></table></center></body></html>");
  180. send1001(html, activeChar);
  181. send1002(activeChar);
  182. }
  183. private void showTopics(Forum forum, L2PcInstance activeChar, int index, int idf) {
  184. if (forum == null) {
  185. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + idf + " is not implemented yet</center><br><br></body></html>", activeChar);
  186. } else if (forum.getType() == ForumType.MEMO) {
  187. showMemoTopics(forum, activeChar, index);
  188. } else {
  189. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + forum.getName() + " is not implemented yet</center><br><br></body></html>", activeChar);
  190. }
  191. }
  192. private void showMemoTopics(Forum forum, L2PcInstance activeChar, int index) {
  193. DAOFactory.getInstance().getForumRepository().findById(forum);
  194. final StringBuilder html = StringUtil.startAppend(2000, "<html><body><br><br><table border=0 width=610><tr><td width=10></td><td width=600 align=left><a action=\"bypass _bbshome\">HOME</a>&nbsp;>&nbsp;<a action=\"bypass _bbsmemo\">Memo Form</a></td></tr></table><img src=\"L2UI.squareblank\" width=\"1\" height=\"10\"><center><table border=0 cellspacing=0 cellpadding=2 bgcolor=888888 width=610><tr><td FIXWIDTH=5></td><td FIXWIDTH=415 align=center>&$413;</td><td FIXWIDTH=120 align=center></td><td FIXWIDTH=70 align=center>&$418;</td></tr></table>");
  195. final DateFormat dateFormat = DateFormat.getInstance();
  196. for (int i = 0, j = getMaxId(forum) + 1; i < (12 * index); j--) {
  197. if (j < 0) {
  198. break;
  199. }
  200. Topic t = forum.getTopic(j);
  201. if (t != null) {
  202. if (i++ >= (12 * (index - 1))) {
  203. StringUtil.append(html, "<table border=0 cellspacing=0 cellpadding=5 WIDTH=610><tr><td FIXWIDTH=5></td><td FIXWIDTH=415><a action=\"bypass _bbsposts;read;", String.valueOf(forum.getId()), ";", String.valueOf(t.getId()), "\">", t.getName(), "</a></td><td FIXWIDTH=120 align=center></td><td FIXWIDTH=70 align=center>", dateFormat.format(new Date(t.getDate())), "</td></tr></table><img src=\"L2UI.Squaregray\" width=\"610\" height=\"1\">");
  204. }
  205. }
  206. }
  207. html.append("<br><table width=610 cellspace=0 cellpadding=0><tr><td width=50><button value=\"&$422;\" action=\"bypass _bbsmemo\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\"></td><td width=510 align=center><table border=0><tr>");
  208. if (index == 1) {
  209. html.append("<td><button action=\"\" back=\"l2ui_ch3.prev1_down\" fore=\"l2ui_ch3.prev1\" width=16 height=16 ></td>");
  210. } else {
  211. StringUtil.append(html, "<td><button action=\"bypass _bbstopics;read;", String.valueOf(forum.getId()), ";", String.valueOf(index - 1), "\" back=\"l2ui_ch3.prev1_down\" fore=\"l2ui_ch3.prev1\" width=16 height=16 ></td>");
  212. }
  213. int nbp;
  214. nbp = forum.getTopicSize() / 8;
  215. if ((nbp * 8) != ClanTable.getInstance().getClanCount()) {
  216. nbp++;
  217. }
  218. for (int i = 1; i <= nbp; i++) {
  219. if (i == index) {
  220. StringUtil.append(html, "<td> ", String.valueOf(i), " </td>");
  221. } else {
  222. StringUtil.append(html, "<td><a action=\"bypass _bbstopics;read;", String.valueOf(forum.getId()), ";", String.valueOf(i), "\"> ", String.valueOf(i), " </a></td>");
  223. }
  224. }
  225. if (index == nbp) {
  226. html.append("<td><button action=\"\" back=\"l2ui_ch3.next1_down\" fore=\"l2ui_ch3.next1\" width=16 height=16 ></td>");
  227. } else {
  228. StringUtil.append(html, "<td><button action=\"bypass _bbstopics;read;", String.valueOf(forum.getId()), ";", String.valueOf(index + 1), "\" back=\"l2ui_ch3.next1_down\" fore=\"l2ui_ch3.next1\" width=16 height=16 ></td>");
  229. }
  230. StringUtil.append(html, "</tr></table> </td> <td align=right><button value = \"&$421;\" action=\"bypass _bbstopics;crea;", String.valueOf(forum.getId()), "\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\" ></td></tr><tr><td><img src=\"l2ui.mini_logo\" width=5 height=10></td></tr><tr> <td></td><td align=center><table border=0><tr><td></td><td><edit var = \"Search\" width=130 height=11></td><td><button value=\"&$420;\" action=\"Write 5 -2 0 Search _ _\" back=\"l2ui_ch3.smallbutton2_down\" width=65 height=20 fore=\"l2ui_ch3.smallbutton2\"> </td> </tr></table> </td></tr></table><br><br><br></center></body></html>");
  231. CommunityBoardHandler.separateAndSend(html.toString(), activeChar);
  232. }
  233. public static TopicBBSManager getInstance() {
  234. return SingletonHolder._instance;
  235. }
  236. private static class SingletonHolder {
  237. protected static final TopicBBSManager _instance = new TopicBBSManager();
  238. }
  239. }