TopicBBSManager.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.communitybbs.Manager;
  16. import java.text.DateFormat;
  17. import java.util.Calendar;
  18. import java.util.Date;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.StringTokenizer;
  22. import javolution.util.FastList;
  23. import javolution.util.FastMap;
  24. import com.l2jserver.gameserver.communitybbs.BB.Forum;
  25. import com.l2jserver.gameserver.communitybbs.BB.Post;
  26. import com.l2jserver.gameserver.communitybbs.BB.Topic;
  27. import com.l2jserver.gameserver.datatables.ClanTable;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.network.serverpackets.ShowBoard;
  30. import com.l2jserver.util.StringUtil;
  31. public class TopicBBSManager extends BaseBBSManager
  32. {
  33. private final List<Topic> _table;
  34. private final Map<Forum, Integer> _maxId;
  35. protected TopicBBSManager()
  36. {
  37. _table = new FastList<>();
  38. _maxId = new FastMap<Forum, Integer>().shared();
  39. }
  40. public void addTopic(Topic tt)
  41. {
  42. _table.add(tt);
  43. }
  44. /**
  45. * @param topic
  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. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the forum: " + ar2 + " is not implemented yet</center><br><br></body></html>", "101");
  84. activeChar.sendPacket(sb);
  85. activeChar.sendPacket(new ShowBoard(null, "102"));
  86. activeChar.sendPacket(new ShowBoard(null, "103"));
  87. }
  88. else
  89. {
  90. f.vload();
  91. 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);
  92. f.addTopic(t);
  93. TopicBBSManager.getInstance().setMaxID(t.getID(), f);
  94. Post p = new Post(activeChar.getName(), activeChar.getObjectId(), Calendar.getInstance().getTimeInMillis(), t.getID(), f.getID(), ar4);
  95. PostBBSManager.getInstance().addPostByTopic(p, t);
  96. parsecmd("_bbsmemo", activeChar);
  97. }
  98. }
  99. else if (ar1.equals("del"))
  100. {
  101. Forum f = ForumsBBSManager.getInstance().getForumByID(Integer.parseInt(ar2));
  102. if (f == null)
  103. {
  104. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the forum: " + ar2 + " does not exist !</center><br><br></body></html>", "101");
  105. activeChar.sendPacket(sb);
  106. activeChar.sendPacket(new ShowBoard(null, "102"));
  107. activeChar.sendPacket(new ShowBoard(null, "103"));
  108. }
  109. else
  110. {
  111. Topic t = f.getTopic(Integer.parseInt(ar3));
  112. if (t == null)
  113. {
  114. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the topic: " + ar3 + " does not exist !</center><br><br></body></html>", "101");
  115. activeChar.sendPacket(sb);
  116. activeChar.sendPacket(new ShowBoard(null, "102"));
  117. activeChar.sendPacket(new ShowBoard(null, "103"));
  118. }
  119. else
  120. {
  121. // CPost cp = null;
  122. Post p = PostBBSManager.getInstance().getGPosttByTopic(t);
  123. if (p != null)
  124. {
  125. p.deleteme(t);
  126. }
  127. t.deleteme(f);
  128. parsecmd("_bbsmemo", activeChar);
  129. }
  130. }
  131. }
  132. else
  133. {
  134. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the command: " + ar1 + " is not implemented yet</center><br><br></body></html>", "101");
  135. activeChar.sendPacket(sb);
  136. activeChar.sendPacket(new ShowBoard(null, "102"));
  137. activeChar.sendPacket(new ShowBoard(null, "103"));
  138. }
  139. }
  140. @Override
  141. public void parsecmd(String command, L2PcInstance activeChar)
  142. {
  143. if (command.equals("_bbsmemo"))
  144. {
  145. showTopics(activeChar.getMemo(), activeChar, 1, activeChar.getMemo().getID());
  146. }
  147. else if (command.startsWith("_bbstopics;read"))
  148. {
  149. StringTokenizer st = new StringTokenizer(command, ";");
  150. st.nextToken();
  151. st.nextToken();
  152. int idf = Integer.parseInt(st.nextToken());
  153. String index = null;
  154. if (st.hasMoreTokens())
  155. {
  156. index = st.nextToken();
  157. }
  158. int ind = 0;
  159. if (index == null)
  160. {
  161. ind = 1;
  162. }
  163. else
  164. {
  165. ind = Integer.parseInt(index);
  166. }
  167. showTopics(ForumsBBSManager.getInstance().getForumByID(idf), activeChar, ind, idf);
  168. }
  169. else if (command.startsWith("_bbstopics;crea"))
  170. {
  171. StringTokenizer st = new StringTokenizer(command, ";");
  172. st.nextToken();
  173. st.nextToken();
  174. int idf = Integer.parseInt(st.nextToken());
  175. showNewTopic(ForumsBBSManager.getInstance().getForumByID(idf), activeChar, idf);
  176. }
  177. else if (command.startsWith("_bbstopics;del"))
  178. {
  179. StringTokenizer st = new StringTokenizer(command, ";");
  180. st.nextToken();
  181. st.nextToken();
  182. int idf = Integer.parseInt(st.nextToken());
  183. int idt = Integer.parseInt(st.nextToken());
  184. Forum f = ForumsBBSManager.getInstance().getForumByID(idf);
  185. if (f == null)
  186. {
  187. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the forum: " + idf + " does not exist !</center><br><br></body></html>", "101");
  188. activeChar.sendPacket(sb);
  189. activeChar.sendPacket(new ShowBoard(null, "102"));
  190. activeChar.sendPacket(new ShowBoard(null, "103"));
  191. }
  192. else
  193. {
  194. Topic t = f.getTopic(idt);
  195. if (t == null)
  196. {
  197. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the topic: " + idt + " does not exist !</center><br><br></body></html>", "101");
  198. activeChar.sendPacket(sb);
  199. activeChar.sendPacket(new ShowBoard(null, "102"));
  200. activeChar.sendPacket(new ShowBoard(null, "103"));
  201. }
  202. else
  203. {
  204. // CPost cp = null;
  205. Post p = PostBBSManager.getInstance().getGPosttByTopic(t);
  206. if (p != null)
  207. {
  208. p.deleteme(t);
  209. }
  210. t.deleteme(f);
  211. parsecmd("_bbsmemo", activeChar);
  212. }
  213. }
  214. }
  215. else
  216. {
  217. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the command: " + command + " is not implemented yet</center><br><br></body></html>", "101");
  218. activeChar.sendPacket(sb);
  219. activeChar.sendPacket(new ShowBoard(null, "102"));
  220. activeChar.sendPacket(new ShowBoard(null, "103"));
  221. }
  222. }
  223. /**
  224. * @param forum
  225. * @param activeChar
  226. * @param idf
  227. */
  228. private void showNewTopic(Forum forum, L2PcInstance activeChar, int idf)
  229. {
  230. if (forum == null)
  231. {
  232. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the forum: " + idf + " is not implemented yet</center><br><br></body></html>", "101");
  233. activeChar.sendPacket(sb);
  234. activeChar.sendPacket(new ShowBoard(null, "102"));
  235. activeChar.sendPacket(new ShowBoard(null, "103"));
  236. }
  237. else if (forum.getType() == Forum.MEMO)
  238. {
  239. showMemoNewTopics(forum, activeChar);
  240. }
  241. else
  242. {
  243. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the forum: " + forum.getName() + " is not implemented yet</center><br><br></body></html>", "101");
  244. activeChar.sendPacket(sb);
  245. activeChar.sendPacket(new ShowBoard(null, "102"));
  246. activeChar.sendPacket(new ShowBoard(null, "103"));
  247. }
  248. }
  249. /**
  250. * @param forum
  251. * @param activeChar
  252. */
  253. private void showMemoNewTopics(Forum forum, L2PcInstance activeChar)
  254. {
  255. 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>");
  256. send1001(html, activeChar);
  257. send1002(activeChar);
  258. }
  259. /**
  260. * @param forum
  261. * @param activeChar
  262. * @param index
  263. * @param idf
  264. */
  265. private void showTopics(Forum forum, L2PcInstance activeChar, int index, int idf)
  266. {
  267. if (forum == null)
  268. {
  269. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the forum: " + idf + " is not implemented yet</center><br><br></body></html>", "101");
  270. activeChar.sendPacket(sb);
  271. activeChar.sendPacket(new ShowBoard(null, "102"));
  272. activeChar.sendPacket(new ShowBoard(null, "103"));
  273. }
  274. else if (forum.getType() == Forum.MEMO)
  275. {
  276. showMemoTopics(forum, activeChar, index);
  277. }
  278. else
  279. {
  280. ShowBoard sb = new ShowBoard("<html><body><br><br><center>the forum: " + forum.getName() + " is not implemented yet</center><br><br></body></html>", "101");
  281. activeChar.sendPacket(sb);
  282. activeChar.sendPacket(new ShowBoard(null, "102"));
  283. activeChar.sendPacket(new ShowBoard(null, "103"));
  284. }
  285. }
  286. /**
  287. * @param forum
  288. * @param activeChar
  289. * @param index
  290. */
  291. private void showMemoTopics(Forum forum, L2PcInstance activeChar, int index)
  292. {
  293. forum.vload();
  294. 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>");
  295. final DateFormat dateFormat = DateFormat.getInstance();
  296. for (int i = 0, j = getMaxID(forum) + 1; i < (12 * index); j--)
  297. {
  298. if (j < 0)
  299. {
  300. break;
  301. }
  302. Topic t = forum.getTopic(j);
  303. if (t != null)
  304. {
  305. if (i++ >= (12 * (index - 1)))
  306. {
  307. 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\">");
  308. }
  309. }
  310. }
  311. 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>");
  312. if (index == 1)
  313. {
  314. html.append("<td><button action=\"\" back=\"l2ui_ch3.prev1_down\" fore=\"l2ui_ch3.prev1\" width=16 height=16 ></td>");
  315. }
  316. else
  317. {
  318. 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>");
  319. }
  320. int nbp;
  321. nbp = forum.getTopicSize() / 8;
  322. if ((nbp * 8) != ClanTable.getInstance().getClans().length)
  323. {
  324. nbp++;
  325. }
  326. for (int i = 1; i <= nbp; i++)
  327. {
  328. if (i == index)
  329. {
  330. StringUtil.append(html, "<td> ", String.valueOf(i), " </td>");
  331. }
  332. else
  333. {
  334. StringUtil.append(html, "<td><a action=\"bypass _bbstopics;read;", String.valueOf(forum.getID()), ";", String.valueOf(i), "\"> ", String.valueOf(i), " </a></td>");
  335. }
  336. }
  337. if (index == nbp)
  338. {
  339. html.append("<td><button action=\"\" back=\"l2ui_ch3.next1_down\" fore=\"l2ui_ch3.next1\" width=16 height=16 ></td>");
  340. }
  341. else
  342. {
  343. 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>");
  344. }
  345. 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>");
  346. separateAndSend(html.toString(), activeChar);
  347. }
  348. public static TopicBBSManager getInstance()
  349. {
  350. return SingletonHolder._instance;
  351. }
  352. private static class SingletonHolder
  353. {
  354. protected static final TopicBBSManager _instance = new TopicBBSManager();
  355. }
  356. }