CommunityBoardManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.Collection;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastMap;
  21. import com.l2jserver.communityserver.Config;
  22. import com.l2jserver.communityserver.L2DatabaseFactory;
  23. import com.l2jserver.communityserver.communityboard.boards.ClanBoard;
  24. import com.l2jserver.communityserver.communityboard.boards.ErrorBoard;
  25. import com.l2jserver.communityserver.communityboard.boards.FriendBoard;
  26. import com.l2jserver.communityserver.communityboard.boards.MailBoard;
  27. import com.l2jserver.communityserver.communityboard.boards.MemoBoard;
  28. import com.l2jserver.communityserver.communityboard.boards.RegionBoard;
  29. import com.l2jserver.communityserver.communityboard.boards.TopBoard;
  30. import com.l2jserver.communityserver.communityboard.boards.ClanPostBoard;
  31. import com.l2jserver.communityserver.model.Forum;
  32. import com.l2jserver.communityserver.model.L2Castle;
  33. import com.l2jserver.communityserver.model.L2Player;
  34. import com.l2jserver.communityserver.model.L2Clan;
  35. import com.l2jserver.communityserver.network.GameServerThread;
  36. import com.l2jserver.communityserver.network.netcon.BaseWritePacket;
  37. import com.l2jserver.communityserver.network.writepackets.RequestWorldInfo;
  38. public final class CommunityBoardManager
  39. {
  40. private static Logger _log = Logger.getLogger(CommunityBoardManager.class.getName());
  41. private static FastMap<Integer, CommunityBoardManager> _instances;
  42. public static CommunityBoardManager getInstance(final int sqlDPId)
  43. {
  44. if (_instances == null)
  45. _instances = new FastMap<Integer, CommunityBoardManager>();
  46. CommunityBoardManager mgr = _instances.get(sqlDPId);
  47. if (mgr == null)
  48. {
  49. mgr = new CommunityBoardManager(sqlDPId);
  50. _instances.put(sqlDPId, mgr);
  51. }
  52. return mgr;
  53. }
  54. private FastMap<Integer, Forum> _forumRoot;
  55. private FastMap<Integer, L2Player> _players;
  56. private FastMap<Integer, L2Clan> _clans;
  57. private FastMap<Integer, L2Castle> _castles;
  58. private final FastMap<String, CommunityBoard> _boards;
  59. private final int _sqlDPId;
  60. private GameServerThread _gst;
  61. private int _lastForumId = 1;
  62. private boolean _isLoaded = false;
  63. private CommunityBoardManager(final int sqlDPId)
  64. {
  65. _sqlDPId = sqlDPId;
  66. _boards = new FastMap<String, CommunityBoard>();
  67. _boards.put("_bbsloc", new RegionBoard(this));
  68. _boards.put("_bbsfriend", new FriendBoard(this));
  69. _boards.put("_bbsclan", new ClanBoard(this));
  70. _boards.put("_bbscpost", new ClanPostBoard(this));
  71. _boards.put("_bbsmail", new MailBoard(this));
  72. _boards.put("_bbsmemo", new MemoBoard(this));
  73. _boards.put("_bbshome", new TopBoard(this));
  74. _boards.put("_bbserror", new ErrorBoard(this));
  75. _forumRoot = new FastMap<Integer, Forum>();
  76. _players = new FastMap<Integer, L2Player>();
  77. _clans = new FastMap<Integer, L2Clan>();
  78. _castles = new FastMap<Integer, L2Castle>();
  79. }
  80. private void loadDataBase()
  81. {
  82. java.sql.Connection con = null;
  83. try
  84. {
  85. con = L2DatabaseFactory.getInstance().getConnection();
  86. PreparedStatement statement = con.prepareStatement("SELECT forum_id, forum_type, forum_owner_id FROM forums WHERE serverId=?");
  87. statement.setInt(1, _sqlDPId);
  88. ResultSet result = statement.executeQuery();
  89. while (result.next())
  90. {
  91. Forum f = new Forum(_sqlDPId, Integer.parseInt(result.getString("forum_id")));
  92. int type = result.getInt("forum_type");
  93. if (type == Forum.CLAN)
  94. {
  95. if (getClan(result.getInt("forum_owner_id")) == null)
  96. {
  97. // delete this forum
  98. }
  99. else
  100. {
  101. getClan(result.getInt("forum_owner_id")).setForum(f);
  102. _forumRoot.put(Integer.parseInt(result.getString("forum_id")), f);
  103. }
  104. }
  105. else if (type == Forum.PLAYER)
  106. {
  107. if (getPlayer(result.getInt("forum_owner_id")) == null)
  108. {
  109. // delete this forum
  110. }
  111. else
  112. {
  113. getPlayer(result.getInt("forum_owner_id")).setForum(f);
  114. _forumRoot.put(Integer.parseInt(result.getString("forum_id")), f);
  115. }
  116. }
  117. if (f.getID() > _lastForumId)
  118. _lastForumId = f.getID();
  119. }
  120. result.close();
  121. statement.close();
  122. }
  123. catch (Exception e)
  124. {
  125. // _log.warning("data error on Forum (root): " + e);
  126. e.printStackTrace();
  127. }
  128. finally
  129. {
  130. try
  131. {
  132. con.close();
  133. }
  134. catch (Exception e)
  135. {
  136. }
  137. }
  138. try
  139. {
  140. con = L2DatabaseFactory.getInstance().getConnection();
  141. PreparedStatement statement = con.prepareStatement("SELECT introduction,clanId FROM clan_introductions WHERE serverId=?");
  142. statement.setInt(1, _sqlDPId);
  143. ResultSet result = statement.executeQuery();
  144. while (result.next())
  145. getClan(result.getInt("clanId")).setIntroduction(result.getString("introduction"));
  146. result.close();
  147. statement.close();
  148. }
  149. catch (Exception e)
  150. {
  151. // _log.warning("data error on Forum (root): " + e);
  152. e.printStackTrace();
  153. }
  154. finally
  155. {
  156. try
  157. {
  158. con.close();
  159. }
  160. catch (Exception e)
  161. {
  162. }
  163. }
  164. int requestedClanNotices = 0;
  165. try
  166. {
  167. for(L2Clan c : _clans.values())
  168. {
  169. if (c == null)
  170. continue;
  171. if (_players.containsKey(c.getLordObjId()) && _players.get(c.getLordObjId()).isOnline())
  172. {
  173. getGST().sendPacket(new RequestWorldInfo(RequestWorldInfo.CLAN_NOTICE_DATA,c.getClanId(), "", false));
  174. requestedClanNotices++;
  175. }
  176. }
  177. _log.info("Requesting " + requestedClanNotices + " clan notices from GS.");
  178. }
  179. catch (Exception e)
  180. {
  181. _log.warning("Data error on Notice Load: " + e);
  182. // e.printStackTrace();
  183. }
  184. }
  185. private int getNewForumId()
  186. {
  187. return ++_lastForumId;
  188. }
  189. public void clean()
  190. {
  191. _forumRoot.clear();
  192. _players.clear();
  193. _clans.clear();
  194. _lastForumId = 0;
  195. }
  196. public void addPlayer(L2Player player)
  197. {
  198. if (!_players.containsKey(player.getObjId()))
  199. _players.put(player.getObjId(), player);
  200. }
  201. public void updatePlayer(int playerObjId, String name, String accountName, int playerLevel, int accessLevel, int playerClanId, boolean isOnline, int[] friendIDs)
  202. {
  203. if (_players.containsKey(playerObjId))
  204. {
  205. L2Player player = _players.get(playerObjId);
  206. if (player.getName() != name)
  207. player.setName(name);
  208. if (player.getLevel() != playerLevel)
  209. player.setLevel(playerLevel);
  210. if (player.getAccessLevel() != accessLevel)
  211. player.setAccessLevel(accessLevel);
  212. if (player.getClanId() != playerClanId)
  213. player.setClanId(playerClanId);
  214. if (player.isOnline() != isOnline)
  215. player.setIsOnline(isOnline);
  216. player.removeAllFriends();
  217. for(int i : friendIDs)
  218. {
  219. player.addFriend(i);
  220. }
  221. }
  222. else
  223. {
  224. L2Player player = new L2Player(playerObjId, name, accountName, playerLevel, accessLevel, playerClanId, isOnline);
  225. for(int i : friendIDs)
  226. {
  227. player.addFriend(i);
  228. }
  229. _players.put(playerObjId, player);
  230. _log.info("New player is successfully created with " + player.getName() + " name.");
  231. }
  232. }
  233. public L2Player getPlayer(int playerObjId)
  234. {
  235. if (!_players.containsKey(playerObjId))
  236. return null;
  237. return _players.get(playerObjId);
  238. }
  239. public L2Player getPlayerByName(String playerName)
  240. {
  241. for (L2Player p : _players.values())
  242. if (p.getName().equalsIgnoreCase(playerName.toLowerCase()))
  243. return p;
  244. return null;
  245. }
  246. public Collection<L2Player> getPlayerList()
  247. {
  248. return _players.values();
  249. }
  250. public Forum getPlayerForum(int playerObjId)
  251. {
  252. if (!_players.containsKey(playerObjId))
  253. return null;
  254. L2Player p = _players.get(playerObjId);
  255. Forum ret = p.getForum();
  256. if (ret == null && p.getLevel() >= Config.MIN_PLAYER_LVL_FOR_FORUM)
  257. {
  258. ret = new Forum(_sqlDPId, getNewForumId(), p.getName(), Forum.PLAYER, p.getObjId());
  259. p.setForum(ret);
  260. }
  261. return ret;
  262. }
  263. public void addClan(L2Clan clan)
  264. {
  265. if (!_clans.containsKey(clan.getClanId()))
  266. _clans.put(clan.getClanId(), clan);
  267. }
  268. public void updateClan(int clanId, String clanName, int level, int lordObjId, String lordName, int members, String allyName, int[] alliance, boolean isNoticeEnabled)
  269. {
  270. if (_clans.containsKey(clanId))
  271. {
  272. L2Clan clan = _clans.get(clanId);
  273. if (clan.getName() != clanName)
  274. clan.setName(clanName);
  275. if (clan.getClanLevel() != level)
  276. clan.setLevel(level);
  277. if (clan.getLordObjId() != lordObjId)
  278. {
  279. clan.setLordObjId(lordObjId);
  280. clan.setLordName(lordName);
  281. }
  282. if (clan.getLordName() != lordName)
  283. clan.setLordName(lordName);
  284. if (clan.getMembersCount() != members)
  285. clan.setMembersCount(members);
  286. if (clan.getAllianceName() != allyName)
  287. clan.setAllianceName(allyName);
  288. clan.setAllianceClanIdList(alliance);
  289. if (clan.isNoticeEnabled() != isNoticeEnabled)
  290. clan.setNoticeEnabled(isNoticeEnabled);
  291. }
  292. else
  293. {
  294. L2Clan clan = new L2Clan(clanId, clanName, level, lordObjId, lordName, members, allyName, alliance, isNoticeEnabled);
  295. _clans.put(clan.getClanId(), clan);
  296. _log.info("New clan is successfully created with " + clan.getName() + " name.");
  297. }
  298. }
  299. public L2Clan getClan(int clanId)
  300. {
  301. if (!_clans.containsKey(clanId))
  302. return null;
  303. return _clans.get(clanId);
  304. }
  305. public L2Clan getPlayersClan(int playerObjId)
  306. {
  307. if (!_players.containsKey(playerObjId))
  308. return null;
  309. int clanId = _players.get(playerObjId).getClanId();
  310. if (!_clans.containsKey(clanId))
  311. return null;
  312. return _clans.get(clanId);
  313. }
  314. public Collection<L2Clan> getClanList()
  315. {
  316. return _clans.values();
  317. }
  318. public Forum getClanForum(int clanId)
  319. {
  320. if (!_clans.containsKey(clanId))
  321. return null;
  322. L2Clan c = _clans.get(clanId);
  323. Forum ret = c.getForum();
  324. if (ret == null && c.getClanLevel() >= Config.MIN_CLAN_LVL_FOR_FORUM)
  325. {
  326. ret = new Forum(_sqlDPId, getNewForumId(), c.getName(), Forum.CLAN, c.getClanId());
  327. c.setForum(ret);
  328. }
  329. return ret;
  330. }
  331. public void addCastle(L2Castle castle)
  332. {
  333. _castles.put(castle.getId(), castle);
  334. }
  335. public L2Castle getCastle(int castleId)
  336. {
  337. return _castles.get(castleId);
  338. }
  339. public Collection<L2Castle> getCastleList()
  340. {
  341. return _castles.values();
  342. }
  343. public boolean isLoaded()
  344. {
  345. return _isLoaded;
  346. }
  347. public void setLoaded()
  348. {
  349. loadDataBase();
  350. _isLoaded = true;
  351. }
  352. public void storeClanIntro(int clanId, String intro)
  353. {
  354. java.sql.Connection con = null;
  355. try
  356. {
  357. con = L2DatabaseFactory.getInstance().getConnection();
  358. PreparedStatement statement = con.prepareStatement("INSERT INTO clan_introductions (serverId,clanId,introduction) values (?,?,?) ON DUPLICATE KEY UPDATE introduction = ?");
  359. statement.setInt(1, _sqlDPId);
  360. statement.setInt(2, clanId);
  361. statement.setString(3, intro);
  362. statement.setString(4, intro);
  363. statement.execute();
  364. statement.close();
  365. }
  366. catch (Exception e)
  367. {
  368. _log.warning("error while saving new Topic to db " + e);
  369. }
  370. finally
  371. {
  372. try
  373. {
  374. con.close();
  375. }
  376. catch (Exception e)
  377. {
  378. }
  379. }
  380. }
  381. public final void parseCmd(final int playerObjId, final String cmd)
  382. {
  383. String board = cmd.split(";")[0];
  384. try
  385. {
  386. if (_boards.containsKey(board))
  387. _boards.get(board).parseCmd(playerObjId, cmd);
  388. else
  389. _boards.get("_bbserror").parseCmd(playerObjId, "noBoard;" + cmd);
  390. }
  391. catch (Exception e)
  392. {
  393. e.printStackTrace();
  394. }
  395. }
  396. public final void parseWrite(final int playerObjId, final String url, final String arg1, final String arg2, final String arg3, final String arg4, final String arg5)
  397. {
  398. try
  399. {
  400. if (_boards.containsKey(url))
  401. _boards.get(url).parseWrite(playerObjId, arg1, arg2, arg3, arg4, arg5);
  402. else
  403. _boards.get("_bbserror").parseCmd(playerObjId, "noBoard;" + url);
  404. }
  405. catch (Exception e)
  406. {
  407. e.printStackTrace();
  408. }
  409. }
  410. public final int getSQLDPId()
  411. {
  412. return _sqlDPId;
  413. }
  414. protected final void sendPacket(final BaseWritePacket packet)
  415. {
  416. _gst.sendPacket(packet);
  417. }
  418. public final void setGST(final GameServerThread gst)
  419. {
  420. _gst = gst;
  421. }
  422. public final GameServerThread getGST()
  423. {
  424. return _gst;
  425. }
  426. }