TopicBBSManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.communitybbs.Manager;
  20. import java.text.DateFormat;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.StringTokenizer;
  27. import java.util.concurrent.CopyOnWriteArrayList;
  28. import com.l2jserver.gameserver.communitybbs.BB.Forum;
  29. import com.l2jserver.gameserver.communitybbs.BB.Post;
  30. import com.l2jserver.gameserver.communitybbs.BB.Topic;
  31. import com.l2jserver.gameserver.data.sql.impl.ClanTable;
  32. import com.l2jserver.gameserver.handler.CommunityBoardHandler;
  33. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  34. import com.l2jserver.util.StringUtil;
  35. public class TopicBBSManager extends BaseBBSManager
  36. {
  37. private final List<Topic> _table = new CopyOnWriteArrayList<>();
  38. private final Map<Forum, Integer> _maxId = new HashMap<>();
  39. protected TopicBBSManager()
  40. {
  41. // Prevent external initialization.
  42. }
  43. public void addTopic(Topic tt)
  44. {
  45. _table.add(tt);
  46. }
  47. public void delTopic(Topic topic)
  48. {
  49. _table.remove(topic);
  50. }
  51. public void setMaxID(int id, Forum f)
  52. {
  53. _maxId.put(f, id);
  54. }
  55. public int getMaxID(Forum f)
  56. {
  57. Integer i = _maxId.get(f);
  58. if (i == null)
  59. {
  60. return 0;
  61. }
  62. return i;
  63. }
  64. public Topic getTopicByID(int idf)
  65. {
  66. for (Topic t : _table)
  67. {
  68. if (t.getID() == idf)
  69. {
  70. return t;
  71. }
  72. }
  73. return null;
  74. }
  75. @Override
  76. public void parsewrite(String ar1, String ar2, String ar3, String ar4, String ar5, L2PcInstance activeChar)
  77. {
  78. if (ar1.equals("crea"))
  79. {
  80. Forum f = ForumsBBSManager.getInstance().getForumByID(Integer.parseInt(ar2));
  81. if (f == null)
  82. {
  83. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + ar2 + " is not implemented yet</center><br><br></body></html>", activeChar);
  84. }
  85. else
  86. {
  87. f.vload();
  88. Topic t = new Topic(Topic.ConstructorType.CREATE, TopicBBSManager.getInstance().getMaxID(f) + 1, Integer.parseInt(ar2), ar5, Calendar.getInstance().getTimeInMillis(), activeChar.getName(), activeChar.getObjectId(), Topic.MEMO, 0);
  89. f.addTopic(t);
  90. TopicBBSManager.getInstance().setMaxID(t.getID(), f);
  91. Post p = new Post(activeChar.getName(), activeChar.getObjectId(), Calendar.getInstance().getTimeInMillis(), t.getID(), f.getID(), ar4);
  92. PostBBSManager.getInstance().addPostByTopic(p, t);
  93. parsecmd("_bbsmemo", activeChar);
  94. }
  95. }
  96. else if (ar1.equals("del"))
  97. {
  98. Forum f = ForumsBBSManager.getInstance().getForumByID(Integer.parseInt(ar2));
  99. if (f == null)
  100. {
  101. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + ar2 + " does not exist !</center><br><br></body></html>", activeChar);
  102. }
  103. else
  104. {
  105. Topic t = f.getTopic(Integer.parseInt(ar3));
  106. if (t == null)
  107. {
  108. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the topic: " + ar3 + " does not exist !</center><br><br></body></html>", activeChar);
  109. }
  110. else
  111. {
  112. // CPost cp = null;
  113. Post p = PostBBSManager.getInstance().getGPosttByTopic(t);
  114. if (p != null)
  115. {
  116. p.deleteme(t);
  117. }
  118. t.deleteme(f);
  119. parsecmd("_bbsmemo", activeChar);
  120. }
  121. }
  122. }
  123. else
  124. {
  125. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the command: " + ar1 + " is not implemented yet</center><br><br></body></html>", activeChar);
  126. }
  127. }
  128. @Override
  129. public void parsecmd(String command, L2PcInstance activeChar)
  130. {
  131. if (command.equals("_bbsmemo"))
  132. {
  133. showTopics(activeChar.getMemo(), activeChar, 1, activeChar.getMemo().getID());
  134. }
  135. else if (command.startsWith("_bbstopics;read"))
  136. {
  137. StringTokenizer st = new StringTokenizer(command, ";");
  138. st.nextToken();
  139. st.nextToken();
  140. int idf = Integer.parseInt(st.nextToken());
  141. String index = null;
  142. if (st.hasMoreTokens())
  143. {
  144. index = st.nextToken();
  145. }
  146. int ind = 0;
  147. if (index == null)
  148. {
  149. ind = 1;
  150. }
  151. else
  152. {
  153. ind = Integer.parseInt(index);
  154. }
  155. showTopics(ForumsBBSManager.getInstance().getForumByID(idf), activeChar, ind, idf);
  156. }
  157. else if (command.startsWith("_bbstopics;crea"))
  158. {
  159. StringTokenizer st = new StringTokenizer(command, ";");
  160. st.nextToken();
  161. st.nextToken();
  162. int idf = Integer.parseInt(st.nextToken());
  163. showNewTopic(ForumsBBSManager.getInstance().getForumByID(idf), activeChar, idf);
  164. }
  165. else if (command.startsWith("_bbstopics;del"))
  166. {
  167. StringTokenizer st = new StringTokenizer(command, ";");
  168. st.nextToken();
  169. st.nextToken();
  170. int idf = Integer.parseInt(st.nextToken());
  171. int idt = Integer.parseInt(st.nextToken());
  172. Forum f = ForumsBBSManager.getInstance().getForumByID(idf);
  173. if (f == null)
  174. {
  175. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + idf + " does not exist !</center><br><br></body></html>", activeChar);
  176. }
  177. else
  178. {
  179. Topic t = f.getTopic(idt);
  180. if (t == null)
  181. {
  182. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the topic: " + idt + " does not exist !</center><br><br></body></html>", activeChar);
  183. }
  184. else
  185. {
  186. // CPost cp = null;
  187. Post p = PostBBSManager.getInstance().getGPosttByTopic(t);
  188. if (p != null)
  189. {
  190. p.deleteme(t);
  191. }
  192. t.deleteme(f);
  193. parsecmd("_bbsmemo", activeChar);
  194. }
  195. }
  196. }
  197. else
  198. {
  199. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the command: " + command + " is not implemented yet</center><br><br></body></html>", activeChar);
  200. }
  201. }
  202. private void showNewTopic(Forum forum, L2PcInstance activeChar, int idf)
  203. {
  204. if (forum == null)
  205. {
  206. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + idf + " is not implemented yet</center><br><br></body></html>", activeChar);
  207. }
  208. else if (forum.getType() == Forum.MEMO)
  209. {
  210. showMemoNewTopics(forum, activeChar);
  211. }
  212. else
  213. {
  214. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + forum.getName() + " is not implemented yet</center><br><br></body></html>", activeChar);
  215. }
  216. }
  217. private void showMemoNewTopics(Forum forum, L2PcInstance activeChar)
  218. {
  219. 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>");
  220. send1001(html, activeChar);
  221. send1002(activeChar);
  222. }
  223. private void showTopics(Forum forum, L2PcInstance activeChar, int index, int idf)
  224. {
  225. if (forum == null)
  226. {
  227. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + idf + " is not implemented yet</center><br><br></body></html>", activeChar);
  228. }
  229. else if (forum.getType() == Forum.MEMO)
  230. {
  231. showMemoTopics(forum, activeChar, index);
  232. }
  233. else
  234. {
  235. CommunityBoardHandler.separateAndSend("<html><body><br><br><center>the forum: " + forum.getName() + " is not implemented yet</center><br><br></body></html>", activeChar);
  236. }
  237. }
  238. private void showMemoTopics(Forum forum, L2PcInstance activeChar, int index)
  239. {
  240. forum.vload();
  241. 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>");
  242. final DateFormat dateFormat = DateFormat.getInstance();
  243. for (int i = 0, j = getMaxID(forum) + 1; i < (12 * index); j--)
  244. {
  245. if (j < 0)
  246. {
  247. break;
  248. }
  249. Topic t = forum.getTopic(j);
  250. if (t != null)
  251. {
  252. if (i++ >= (12 * (index - 1)))
  253. {
  254. 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\">");
  255. }
  256. }
  257. }
  258. 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>");
  259. if (index == 1)
  260. {
  261. html.append("<td><button action=\"\" back=\"l2ui_ch3.prev1_down\" fore=\"l2ui_ch3.prev1\" width=16 height=16 ></td>");
  262. }
  263. else
  264. {
  265. 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>");
  266. }
  267. int nbp;
  268. nbp = forum.getTopicSize() / 8;
  269. if ((nbp * 8) != ClanTable.getInstance().getClanCount())
  270. {
  271. nbp++;
  272. }
  273. for (int i = 1; i <= nbp; i++)
  274. {
  275. if (i == index)
  276. {
  277. StringUtil.append(html, "<td> ", String.valueOf(i), " </td>");
  278. }
  279. else
  280. {
  281. StringUtil.append(html, "<td><a action=\"bypass _bbstopics;read;", String.valueOf(forum.getID()), ";", String.valueOf(i), "\"> ", String.valueOf(i), " </a></td>");
  282. }
  283. }
  284. if (index == nbp)
  285. {
  286. html.append("<td><button action=\"\" back=\"l2ui_ch3.next1_down\" fore=\"l2ui_ch3.next1\" width=16 height=16 ></td>");
  287. }
  288. else
  289. {
  290. 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>");
  291. }
  292. 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>");
  293. CommunityBoardHandler.separateAndSend(html.toString(), activeChar);
  294. }
  295. public static TopicBBSManager getInstance()
  296. {
  297. return SingletonHolder._instance;
  298. }
  299. private static class SingletonHolder
  300. {
  301. protected static final TopicBBSManager _instance = new TopicBBSManager();
  302. }
  303. }