ClanPostBoard.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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.communityserver.communityboard.boards;
  16. import java.text.DateFormat;
  17. import java.util.Collection;
  18. import java.util.Date;
  19. import java.util.logging.Logger;
  20. import javolution.text.TextBuilder;
  21. import javolution.util.FastList;
  22. import com.l2jserver.communityserver.cache.HtmCache;
  23. import com.l2jserver.communityserver.communityboard.CommunityBoard;
  24. import com.l2jserver.communityserver.communityboard.CommunityBoardManager;
  25. import com.l2jserver.communityserver.model.Comment;
  26. import com.l2jserver.communityserver.model.Forum;
  27. import com.l2jserver.communityserver.model.Topic;
  28. import com.l2jserver.communityserver.model.Post;
  29. import com.l2jserver.communityserver.model.Topic.ConstructorType;
  30. import com.l2jserver.communityserver.network.writepackets.PlayerSendMessage;
  31. public final class ClanPostBoard extends CommunityBoard
  32. {
  33. private static Logger _log = Logger.getLogger(ClanPostBoard.class.getName());
  34. public ClanPostBoard(final CommunityBoardManager mgr)
  35. {
  36. super(mgr);
  37. }
  38. @Override
  39. public void parseCmd(final int playerObjId, final String cmd)
  40. {
  41. int clanId = Integer.valueOf(cmd.split(";")[3]);
  42. Forum clanForum = getCommunityBoardManager().getClanForum(clanId);
  43. int type;
  44. if (cmd.split(";")[2].equalsIgnoreCase("announce"))
  45. type = Topic.ANNOUNCE;
  46. else if (cmd.split(";")[2].equalsIgnoreCase("cbb"))
  47. type = Topic.BULLETIN;
  48. else
  49. {
  50. _log.info("Clan Post Board command error: " + cmd);
  51. return;
  52. }
  53. int perNon = clanForum.gettopic(type).getPermissions();
  54. int perMem = perNon % 10;
  55. perNon = (perNon - perMem) / 10;
  56. boolean isPlayerMember = getCommunityBoardManager().getPlayer(playerObjId).getClanId() == clanId;
  57. boolean isLeader = getCommunityBoardManager().getClan(clanId).getLordObjId() == playerObjId;
  58. if (!isLeader && ((isPlayerMember && perMem == 0) || (!isPlayerMember && perNon == 0)))
  59. {
  60. // TODO: this way Clan Post Board command missing part could be missed
  61. super.getCommunityBoardManager().getGST().sendPacket(new PlayerSendMessage(playerObjId,PlayerSendMessage.NO_READ_PERMISSION,""));
  62. return;
  63. }
  64. if (cmd.split(";")[1].equalsIgnoreCase("list"))
  65. {
  66. int index = 1;
  67. if (cmd.split(";").length == 5)
  68. index = Integer.valueOf(cmd.split(";")[4]);
  69. showPage(playerObjId, clanForum, type, index);
  70. }
  71. else if (cmd.split(";")[1].equalsIgnoreCase("read"))
  72. {
  73. Topic t = clanForum.gettopic(type);
  74. Post p = t.getPost(Integer.valueOf(cmd.split(";")[4]));
  75. if (p == null)
  76. _log.info("Missing post: " + cmd);
  77. else
  78. {
  79. if (cmd.split(";").length > 5)
  80. {
  81. _log.info("Index: " + cmd.split(";")[5] + ";" + cmd.split(";")[6]);
  82. showPost(playerObjId, t, p, clanId, type, Integer.valueOf(cmd.split(";")[5]), Integer.valueOf(cmd.split(";")[6]));
  83. }
  84. else
  85. showPost(playerObjId, t, p, clanId, type, 1, 1);
  86. }
  87. }
  88. else if (!isLeader && ((isPlayerMember && perMem == 1) || (!isPlayerMember && perNon == 1)))
  89. {
  90. // TODO: this way Clan Post Board command missing part could be missed
  91. super.getCommunityBoardManager().getGST().sendPacket(new PlayerSendMessage(playerObjId,PlayerSendMessage.NO_WRITE_PERMISSION,""));
  92. return;
  93. }
  94. else if (cmd.split(";")[1].equalsIgnoreCase("crea"))
  95. {
  96. showWrite(playerObjId, null, clanId, type);
  97. }
  98. else if (cmd.split(";")[1].equalsIgnoreCase("del"))
  99. {
  100. clanForum.gettopic(type).rmPostByID(Integer.valueOf(cmd.split(";")[4]));
  101. showPage(playerObjId, clanForum, type, 1);
  102. }
  103. else if (cmd.split(";")[1].equalsIgnoreCase("delcom"))
  104. {
  105. Topic t = clanForum.gettopic(type);
  106. Post p = t.getPost(Integer.valueOf(cmd.split(";")[4]));
  107. p.rmCommentByID(Integer.valueOf(cmd.split(";")[5]));
  108. showPost(playerObjId, t, p, clanId, type, 1, 1);
  109. }
  110. else if (cmd.split(";")[1].equalsIgnoreCase("edit"))
  111. {
  112. Post p = clanForum.gettopic(type).getPost(Integer.valueOf(cmd.split(";")[4]));
  113. showWrite(playerObjId, p, clanId, type);
  114. }
  115. else if (cmd.split(";")[1].equalsIgnoreCase("reply"))
  116. {
  117. Post p = clanForum.gettopic(type).getPost(Integer.valueOf(cmd.split(";")[4]));
  118. showReply(playerObjId, p, clanId, type);
  119. }
  120. else
  121. _log.info("Clan Post Board command missing: " + cmd.split(";")[1]);
  122. }
  123. private String replace(String txt, int type)
  124. {
  125. String content = txt;
  126. switch (type)
  127. {
  128. case Topic.ANNOUNCE:
  129. content = content.replaceAll("%link%", "<a action=\"bypass _bbscpost;list;announce;%clanid%\">Announcement</a>");
  130. content = content.replaceAll("%type%", "announce");
  131. content = content.replaceAll("%topicId%", String.valueOf(Topic.ANNOUNCE));
  132. content = content.replaceAll("%combobox%", "Advertise;Miscellaneous");
  133. break;
  134. case Topic.BULLETIN:
  135. content = content.replaceAll("%link%", "<a action=\"bypass _bbscpost;list;cbb;%clanid%\">Free Community</a>");
  136. content = content.replaceAll("%type%", "cbb");
  137. content = content.replaceAll("%topicId%", String.valueOf(Topic.BULLETIN));
  138. content = content.replaceAll("%combobox%", "Information;Miscellaneous");
  139. break;
  140. }
  141. return content;
  142. }
  143. public final void showPage(final int playerObjId, Forum f, int type, int index)
  144. {
  145. String content = HtmCache.getInstance().getHtm("data/staticfiles/html/clanpost.htm");
  146. if (f == null)
  147. {
  148. _log.info("Forum is NULL!!!");
  149. super.send(playerObjId, content);
  150. return;
  151. }
  152. Topic t = f.gettopic(type);
  153. TextBuilder mList = new TextBuilder();
  154. int i = 0;
  155. for (Post p : t.getAllPosts())
  156. {
  157. if (i > ((index - 1) * 10 + 9))
  158. {
  159. break;
  160. }
  161. if (i++ >= ((index - 1) * 10))
  162. {
  163. mList.append("<img src=\"L2UI.SquareBlank\" width=\"750\" height=\"3\">");
  164. mList.append("<table border=0 cellspacing=0 cellpadding=0 width=750>");
  165. mList.append("<tr> ");
  166. mList.append("<td FIXWIDTH=5></td>");
  167. mList.append("<td FIXWIDTH=80 align=center>" + p.getID() + "</td>");
  168. mList.append("<td FIXWIDTH=340><a action=\"bypass _bbscpost;read;%type%;" + f.getOwner() + ";" + p.getID() + "\">" + p.getTypeName() + p.getTitle() + "</a></td>");
  169. mList.append("<td FIXWIDTH=120 align=center>" + getCommunityBoardManager().getPlayer(p.getOwnerId()).getName() + "</td>");
  170. mList.append("<td FIXWIDTH=120 align=center>" + DateFormat.getInstance().format(new Date(p.getDate())) + "</td>");
  171. mList.append("<td FIXWIDTH=80 align=center>" + p.getReadCount() + "</td>");
  172. mList.append("<td FIXWIDTH=5></td>");
  173. mList.append("</tr>");
  174. mList.append("<tr><td height=5></td></tr>");
  175. mList.append("</table>");
  176. mList.append("<img src=\"L2UI.SquareBlank\" width=\"750\" height=\"3\">");
  177. mList.append("<img src=\"L2UI.SquareGray\" width=\"750\" height=\"1\">");
  178. }
  179. }
  180. content = content.replaceAll("%postList%", mList.toString());
  181. mList.clear();
  182. if (index == 1)
  183. {
  184. mList.append("<td><button action=\"\" back=\"l2ui_ch3.prev1_down\" fore=\"l2ui_ch3.prev1\" width=16 height=16 ></td>");
  185. }
  186. else
  187. {
  188. mList.append("<td><button action=\"bypass _bbscpost;list;%type%;%clanid%;" + (index - 1) + "\" back=\"l2ui_ch3.prev1_down\" fore=\"l2ui_ch3.prev1\" width=16 height=16 ></td>");
  189. }
  190. int nbp;
  191. nbp = t.getAllPosts().size() / 10;
  192. if (nbp * 10 != t.getAllPosts().size())
  193. {
  194. nbp++;
  195. }
  196. for (i = 1; i <= nbp; i++)
  197. {
  198. if (i == index)
  199. {
  200. mList.append("<td> " + i + " </td>");
  201. }
  202. else
  203. {
  204. mList.append("<td><a action=\"bypass _bbscpost;list;%type%;%clanid%;" + i + "\"> " + i + " </a></td>");
  205. }
  206. }
  207. if (index == nbp)
  208. {
  209. mList.append("<td><button action=\"\" back=\"l2ui_ch3.next1_down\" fore=\"l2ui_ch3.next1\" width=16 height=16 ></td>");
  210. }
  211. else
  212. {
  213. mList.append("<td><button action=\"bypass _bbscpost;list;%type%;%clanid%;" + (index + 1) + "\" back=\"l2ui_ch3.next1_down\" fore=\"l2ui_ch3.next1\" width=16 height=16 ></td>");
  214. }
  215. content = content.replaceAll("%postListLength%", mList.toString());
  216. content = replace(content, type);
  217. content = content.replaceAll("%clanid%", String.valueOf(f.getOwner()));
  218. super.send(playerObjId, content);
  219. }
  220. public final void showWrite(final int playerObjId, Post p, int clanId, int type)
  221. {
  222. String title = " ";
  223. String message = " ";
  224. String content = HtmCache.getInstance().getHtm("data/staticfiles/html/clanpost-write.htm");
  225. content = replace(content, type);
  226. content = content.replaceAll("%clanid%", String.valueOf(clanId));
  227. if (p == null)
  228. {
  229. content = content.replaceAll("%job%", "new");
  230. content = content.replaceAll("%postId%", "-1");
  231. }
  232. else
  233. {
  234. content = content.replaceAll("%job%", "edit");
  235. content = content.replaceAll("%postId%", String.valueOf(p.getID()));
  236. title = p.getTitle();
  237. message = p.getText();
  238. }
  239. super.sendWrite(playerObjId, content, message, title, title);
  240. }
  241. public final void showReply(final int playerObjId, Post p, int clanId, int type)
  242. {
  243. if (p == null)
  244. {
  245. showWrite(playerObjId, p, clanId, type);
  246. return;
  247. }
  248. String title = " ";
  249. String message = " ";
  250. String content = HtmCache.getInstance().getHtm("data/staticfiles/html/clanpost-write.htm");
  251. content = replace(content, type);
  252. content = content.replaceAll("%clanid%", String.valueOf(clanId));
  253. content = content.replaceAll("%job%", "reply");
  254. content = content.replaceAll("%postId%", String.valueOf(p.getID()));
  255. super.sendWrite(playerObjId, content, message, title, " ");
  256. }
  257. public final void showPost(final int playerObjId, Topic t, Post p, int clanId, int type, int indexR, int indexC)
  258. {
  259. p.increaseReadCount();
  260. String content = HtmCache.getInstance().getHtm("data/staticfiles/html/clanpost-show.htm");
  261. content = content.replaceAll("%postTitle%", p.getTitle());
  262. content = content.replaceAll("%postId%", String.valueOf(p.getID()));
  263. Post parent = p;
  264. if (p.getParentId() != -1)
  265. {
  266. content = content.replaceAll("%postParentId%", String.valueOf(p.getParentId()));
  267. parent = t.getPost(p.getParentId());
  268. }
  269. else
  270. content = content.replaceAll("%postParentId%", String.valueOf(p.getID()));
  271. content = content.replaceAll("%postOwnerName%", getCommunityBoardManager().getPlayer(p.getOwnerId()).getName());
  272. content = content.replaceAll("%postReadCount%", String.valueOf(p.getReadCount()));
  273. content = content.replaceAll("%postDate%", DateFormat.getInstance().format(new Date(p.getDate())));
  274. content = content.replaceAll("%mes%", p.getText());
  275. // reply list
  276. TextBuilder mList = new TextBuilder();
  277. int i = 1;
  278. FastList<Post> childrenList = t.getChildrenPosts(parent);
  279. for (Post child : childrenList)
  280. {
  281. if (i++ == indexR)
  282. {
  283. mList.append("<table border=0 cellspacing=5 cellpadding=0 WIDTH=750>");
  284. mList.append("<tr>");
  285. mList.append("<td WIDTH=60 align=center>" + child.getID() + "</td>");
  286. if (child != p)
  287. mList.append("<td width=415><a action=\"bypass _bbscpost;read;%type%;" + clanId + ";" + child.getID() + "\">" + child.getTypeName() + child.getTitle() + "</a></td>");
  288. else
  289. mList.append("<td width=415>" + child.getTypeName() + child.getTitle() + "</td>");
  290. mList.append("<td WIDTH=130 align=center>" + getCommunityBoardManager().getPlayer(child.getOwnerId()).getName() + "</td>");
  291. mList.append("<td WIDTH=80 align=center>" + DateFormat.getInstance().format(new Date(child.getDate())) + "</td>");
  292. mList.append("<td WIDTH=65 align=center>" + child.getReadCount() + "</td>");
  293. mList.append("</tr>");
  294. mList.append("</table>");
  295. }
  296. }
  297. content = content.replaceAll("%replyList%", mList.toString());
  298. if (indexR == 1)
  299. content = content.replaceAll("%prevReply%", "[Previous Reply]");
  300. else
  301. content = content.replaceAll("%prevReply%", "<a action=\"bypass _bbscpost;read;%type%;" + clanId + ";" + p.getID() + ";" + (indexR - 1) + ";" + indexC + "\">[Previous Reply]</a>");
  302. content = content.replaceAll("%replyCount%", indexR + "/" + childrenList.size());
  303. if (indexR == childrenList.size())
  304. content = content.replaceAll("%nextReply%", "[Next Reply]");
  305. else
  306. content = content.replaceAll("%nextReply%", "<a action=\"bypass _bbscpost;read;%type%;" + clanId + ";" + p.getID() + ";" + (indexR + 1) + ";" + indexC + "\">[Next Reply]</a>");
  307. // comment list
  308. mList.clear();
  309. i = 1;
  310. Collection<Comment> commentsList = p.getAllComments();
  311. int csize = commentsList.size();
  312. if (csize == 0)
  313. csize = 1;
  314. else
  315. for (Comment c : commentsList)
  316. {
  317. if (i++ == indexC)
  318. {
  319. mList.append("<tr><td><img src=\"L2UI.squaregray\" width=\"750\" height=\"1\"></td></tr>");
  320. mList.append("<tr><td>");
  321. mList.append("<table>");
  322. mList.append("<tr>");
  323. mList.append("<td WIDTH=100 valign=top>" + getCommunityBoardManager().getPlayer(c.getOwnerId()).getName() + "</td>");
  324. mList.append("<td width=10 valign=top><img src=\"L2UI.squaregray\" width=\"5\" height=\"28\"></td>");
  325. mList.append("<td FIXWIDTH=560 valign=top><font color=\"AAAAAA\">" + c.getText() + "</font></td>");
  326. mList.append("<td WIDTH=20 valign=top><a action=\"bypass _bbscpost;delcom;%type%;" + clanId + ";" + p.getID() + ";" + c.getID() + "\">&\\$425;</a></td>");
  327. mList.append("<td WIDTH=60 valign=top>" + DateFormat.getInstance().format(new Date(c.getDate())) + "</td>");
  328. mList.append("</tr>");
  329. mList.append("</table>");
  330. mList.append("</td></tr>");
  331. }
  332. }
  333. content = content.replaceAll("%commentList%", mList.toString());
  334. if (indexC == 1)
  335. content = content.replaceAll("%prevCom%", "[Previous Comment]");
  336. else
  337. content = content.replaceAll("%prevCom%", "<a action=\"bypass _bbscpost;read;%type%;" + clanId + ";" + p.getID() + ";" + indexR + ";" + (indexC - 1) + "\">[Previous Comment]</a>");
  338. content = content.replaceAll("%comCount%", indexC + "/" + csize);
  339. if (indexC == csize)
  340. content = content.replaceAll("%nextCom%", "[Next Comment]");
  341. else
  342. content = content.replaceAll("%nextCom%", "<a action=\"bypass _bbscpost;read;%type%;" + clanId + ";" + p.getID() + ";" + indexR + ";" + (indexC + 1) + "\">[Next Comment]</a>");
  343. content = replace(content, type);
  344. content = content.replaceAll("%clanid%", String.valueOf(clanId));
  345. super.send(playerObjId, content);
  346. }
  347. @Override
  348. public final void parseWrite(final int playerObjId, final String ar1, final String ar2, final String ar3, final String ar4, final String ar5)
  349. {
  350. int clanId = Integer.valueOf(ar2.split(";")[0]);
  351. int topicId = Integer.valueOf(ar2.split(";")[1]);
  352. int postId = Integer.valueOf(ar2.split(";")[2]);
  353. Forum clanForum = getCommunityBoardManager().getClanForum(clanId);
  354. int perNon = clanForum.gettopic(topicId).getPermissions();
  355. int perMem = perNon % 10;
  356. perNon = (perNon - perMem) / 10;
  357. boolean isPlayerMember = getCommunityBoardManager().getPlayer(playerObjId).getClanId() == clanId;
  358. boolean isLeader = getCommunityBoardManager().getClan(clanId).getLordObjId() == playerObjId;
  359. if (!isLeader && ((isPlayerMember && perMem != 2) || (!isPlayerMember && perNon != 2)))
  360. {
  361. super.getCommunityBoardManager().getGST().sendPacket(new PlayerSendMessage(playerObjId,PlayerSendMessage.NO_WRITE_PERMISSION,""));
  362. return;
  363. }
  364. int type = Post.ADVERTISE;
  365. if (ar5.equalsIgnoreCase("Information"))
  366. type = Post.INFORMATION;
  367. else if (ar5.equalsIgnoreCase("Miscellaneous"))
  368. type = Post.MISCELLANEOUS;
  369. if (ar1.equalsIgnoreCase("new"))
  370. {
  371. postId = clanForum.gettopic(topicId).getNewPostId();
  372. Post p = new Post(ConstructorType.CREATE, clanForum.getSqlDPId(), postId, playerObjId, "", System.currentTimeMillis(), topicId, clanForum.getID(), super.edtiPlayerTxT(ar3), super.edtiPlayerTxT(ar4), type, 0);
  373. clanForum.gettopic(topicId).addPost(p);
  374. }
  375. else if (ar1.equalsIgnoreCase("reply"))
  376. {
  377. int parentId = postId;
  378. postId = clanForum.gettopic(topicId).getNewPostId();
  379. Post p = new Post(ConstructorType.CREATE, clanForum.getSqlDPId(), postId, playerObjId, "", parentId, System.currentTimeMillis(), topicId, clanForum.getID(), super.edtiPlayerTxT(ar3), super.edtiPlayerTxT(ar4), type, 0);
  380. clanForum.gettopic(topicId).addPost(p);
  381. }
  382. else if (ar1.equalsIgnoreCase("edit"))
  383. {
  384. clanForum.gettopic(topicId).getPost(postId).updatePost(super.edtiPlayerTxT(ar3), super.edtiPlayerTxT(ar4), type);
  385. }
  386. else if (ar1.equalsIgnoreCase("com"))
  387. {
  388. Post p = clanForum.gettopic(topicId).getPost(postId);
  389. int comId = p.getNewCommentId();
  390. Comment c = new Comment(ConstructorType.CREATE, clanForum.getSqlDPId(), comId, playerObjId, System.currentTimeMillis(), postId, topicId, clanForum.getID(), super.edtiPlayerTxT(ar3));
  391. p.addComment(c);
  392. showPost(playerObjId, clanForum.gettopic(topicId), p, clanId, topicId, 1, 1);
  393. return;
  394. }
  395. else
  396. _log.info("Clan Post Write command missing: " + ar1);
  397. showPage(playerObjId, clanForum, topicId, 1);
  398. }
  399. }