Advertisement
morpheus32

CommunityBosses.java

Feb 16th, 2016
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.76 KB | None | 0 0
  1. package services.community;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.LinkedHashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import java.util.StringTokenizer;
  11. import java.util.TreeMap;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. import l2f.gameserver.Config;
  15. import l2f.gameserver.cache.ImagesCache;
  16. import l2f.gameserver.data.htm.HtmCache;
  17. import l2f.gameserver.data.xml.holder.NpcHolder;
  18. import l2f.gameserver.handler.bbs.CommunityBoardManager;
  19. import l2f.gameserver.handler.bbs.ICommunityBoardHandler;
  20. import l2f.gameserver.instancemanager.RaidBossSpawnManager;
  21. import l2f.gameserver.model.GameObjectsStorage;
  22. import l2f.gameserver.model.Player;
  23. import l2f.gameserver.model.entity.olympiad.Olympiad;
  24. import l2f.gameserver.model.instances.NpcInstance;
  25. import l2f.gameserver.network.serverpackets.RadarControl;
  26. import l2f.gameserver.network.serverpackets.ShowBoard;
  27. import l2f.gameserver.scripts.ScriptFile;
  28. import l2f.gameserver.taskmanager.AutoImageSenderManager;
  29. import l2f.gameserver.templates.StatsSet;
  30. import l2f.gameserver.templates.npc.MinionData;
  31. import l2f.gameserver.templates.npc.NpcTemplate;
  32. import l2f.gameserver.utils.Util;
  33.  
  34. import org.apache.commons.lang3.StringUtils;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37.  
  38. import actions.RewardListInfo;
  39.  
  40. public class CommunityBosses implements ScriptFile, ICommunityBoardHandler
  41. {
  42. private static final Logger _log = LoggerFactory.getLogger(CommunityBosses.class);
  43.  
  44. private static final int BOSSES_PER_PAGE = 10;
  45. private static final int[] BOSSES_TO_NOT_SHOW = { 29006,//Core
  46. 29001,//Queen Ant
  47. 29014,//Orfen
  48. 25692,//Aenkinel
  49. 25423,//Fairy Queen Timiniel
  50. 25010,//Furious Thieles
  51. 25532,//Kechi
  52. 25119,//Messenger of Fairy Queen Berun
  53. 25159,//Paniel the Unicorn
  54. 25163,//Roaring Skylancer
  55. 25070 //Enchanted Forest Watcher Ruell
  56. };
  57.  
  58. @Override
  59. public void onLoad()
  60. {
  61. if(Config.COMMUNITYBOARD_ENABLED)
  62. {
  63. _log.info("CommunityBoard: Bosses loaded.");
  64. CommunityBoardManager.getInstance().registerHandler(this);
  65. }
  66. }
  67.  
  68. @Override
  69. public void onReload()
  70. {
  71. if(Config.COMMUNITYBOARD_ENABLED)
  72. CommunityBoardManager.getInstance().removeHandler(this);
  73. }
  74.  
  75. @Override
  76. public void onShutdown()
  77. {}
  78.  
  79. @Override
  80. public String[] getBypassCommands()
  81. {
  82. return new String[] { "_bbsmemo", "_bbsbosslist", "_bbsboss" };
  83. }
  84.  
  85. @Override
  86. public void onBypassCommand(Player player, String bypass)
  87. {
  88. StringTokenizer st = new StringTokenizer(bypass, "_");
  89. String cmd = st.nextToken();
  90. player.setSessionVar("add_fav", null);
  91.  
  92. if("bbsmemo".equals(cmd) || "bbsbosslist".equals(cmd))//_bbsbosslist_sort_page_search
  93. {
  94. int sort = Integer.parseInt(st.hasMoreTokens() ? st.nextToken() : "1");
  95. int page = Integer.parseInt(st.hasMoreTokens() ? st.nextToken() : "0");
  96. String search = st.hasMoreTokens() ? st.nextToken().trim() : "";
  97.  
  98. sendBossListPage(player, getSortByIndex(sort), page, search);
  99. }
  100. else if("bbsboss".equals(cmd))//_bbsboss_sort_page_search_rbId_btn
  101. {
  102. int sort = Integer.parseInt(st.hasMoreTokens() ? st.nextToken() : "3");
  103. int page = Integer.parseInt(st.hasMoreTokens() ? st.nextToken() : "0");
  104. String search = st.hasMoreTokens() ? st.nextToken().trim() : "";
  105. int bossId = Integer.parseInt(st.hasMoreTokens() ? st.nextToken() : "25044");
  106. int buttonClick = Integer.parseInt(st.hasMoreTokens() ? st.nextToken() : "0");
  107.  
  108. manageButtons(player, buttonClick, bossId);
  109.  
  110. sendBossDetails(player, getSortByIndex(sort), page, search, bossId);
  111. }
  112. }
  113.  
  114. /**
  115. * Showing list of bosses in Community Board with their Name, Level, Status and Show Details button
  116. * @param player guy that will receive list
  117. * @param sort index of the sorting type
  118. * @param page number of the page(Starting from 0)
  119. * @param search word in Name of the boss
  120. */
  121. private static void sendBossListPage(Player player, SortType sort, int page, String search)
  122. {
  123. String html = HtmCache.getInstance().getNotNull(Config.BBS_HOME_DIR + "bbs_boss_list.htm", player);
  124.  
  125. Map<Integer, StatsSet> allBosses = getSearchedBosses(sort, search);
  126. Map<Integer, StatsSet> bossesToShow = getBossesToShow(allBosses, page);
  127. boolean isThereNextPage = allBosses.size() > bossesToShow.size();
  128.  
  129. html = getBossListReplacements(html, page, bossesToShow, isThereNextPage);
  130.  
  131. html = getNormalReplacements(html, page, sort, search, -1);
  132. ShowBoard.separateAndSend(html, player);
  133. }
  134.  
  135. /**
  136. * Replacing %x% words in bbs_bbslink_list.htm file
  137. * @param html existing file
  138. * @param page number of the page(Starting from 0)
  139. * @param allBosses Map<BossId, BossStatsSet> of bosses that will be shown
  140. * @param nextPage Is the next page?
  141. * @return ready HTML
  142. */
  143. private static String getBossListReplacements(String html, int page, Map<Integer, StatsSet> allBosses, boolean nextPage)
  144. {
  145. String newHtml = html;
  146.  
  147. int i = 0;
  148.  
  149. for(Entry<Integer, StatsSet> entry : allBosses.entrySet())
  150. {
  151. StatsSet boss = entry.getValue();
  152. NpcTemplate temp = NpcHolder.getInstance().getTemplate(entry.getKey().intValue());
  153.  
  154. boolean isAlive = isBossAlive(boss);
  155.  
  156. newHtml = newHtml.replace("<?name_" + i + "?>", temp.getName());
  157. newHtml = newHtml.replace("<?level_" + i + "?>", String.valueOf(temp.level));
  158. newHtml = newHtml.replace("<?status_" + i + "?>", isAlive ? "Alive" : "Dead");
  159. newHtml = newHtml.replace("<?status_color_" + i + "?>", getTextColor(isAlive));
  160. newHtml = newHtml.replace("<?bp_" + i + "?>", "<button value=\"show\" action=\"bypass _bbsboss_<?sort?>_" + page + "_ <?search?> _" + entry.getKey() + "\" width=40 height=12 back=\"L2UI_CT1.ListCTRL_DF_Title_Down\" fore=\"L2UI_CT1.ListCTRL_DF_Title\">");
  161. i++;
  162. }
  163.  
  164. for(int j = i; j < BOSSES_PER_PAGE; j++)
  165. {
  166. newHtml = newHtml.replace("<?name_" + j + "?>", "...");
  167. newHtml = newHtml.replace("<?level_" + j + "?>", "...");
  168. newHtml = newHtml.replace("<?status_" + j + "?>", "...");
  169. newHtml = newHtml.replace("<?status_color_" + j + "?>", "FFFFFF");
  170. newHtml = newHtml.replace("<?bp_" + j + "?>", "...");
  171.  
  172. }
  173.  
  174. newHtml = newHtml.replace("<?previous?>", page > 0 ? "<button action=\"bypass _bbsbosslist_<?sort?>_" + (page - 1) + "_<?search?>\" width=16 height=16 back=\"L2UI_CH3.shortcut_prev_down\" fore=\"L2UI_CH3.shortcut_prev\">" : "<br>");
  175. newHtml = newHtml.replace("<?next?>", nextPage && i == BOSSES_PER_PAGE ? "<button action=\"bypass _bbsbosslist_<?sort?>_" + (page + 1) + "_<?search?>\" width=16 height=16 back=\"L2UI_CH3.shortcut_next_down\" fore=\"L2UI_CH3.shortcut_next\">" : "<br>");
  176. newHtml = newHtml.replace("<?pages?>", String.valueOf(page + 1));
  177.  
  178. return newHtml;
  179. }
  180.  
  181. /**
  182. * Getting all bosses to show(checking only page)
  183. * @param page number of the page(Starting from 0)
  184. * @return Bosses
  185. */
  186. private static Map<Integer, StatsSet> getBossesToShow(Map<Integer, StatsSet> allBosses, int page)
  187. {
  188. Map<Integer, StatsSet> bossesToShow = new LinkedHashMap<Integer, StatsSet>();
  189. int i = 0;
  190. for(Entry<Integer, StatsSet> entry : allBosses.entrySet())
  191. {
  192. if(i < page * BOSSES_PER_PAGE)
  193. {
  194. i++;
  195. }
  196. else
  197. {
  198. StatsSet boss = entry.getValue();
  199. NpcTemplate temp = NpcHolder.getInstance().getTemplate(entry.getKey().intValue());
  200. if(boss != null && temp != null)
  201. {
  202. i++;
  203. bossesToShow.put(entry.getKey(), entry.getValue());
  204. if(i > (page * BOSSES_PER_PAGE + BOSSES_PER_PAGE - 1)){ return bossesToShow; }
  205. }
  206. }
  207. }
  208. return bossesToShow;
  209. }
  210.  
  211. /**
  212. * Showing detailed info about Boss in Community Board. Including name, level, status, stats, image
  213. * @param player guy that will receive details
  214. * @param sort index of the sorting type
  215. * @param page number of the page(Starting from 0)
  216. * @param search word in Name of the boss
  217. * @param bossId Id of the boss to show
  218. */
  219. private static void sendBossDetails(Player player, SortType sort, int page, CharSequence search, int bossId)
  220. {
  221. String html = HtmCache.getInstance().getNotNull(Config.BBS_HOME_DIR + "bbs_boss_details.htm", player);
  222. StatsSet bossSet = RaidBossSpawnManager.getInstance().getAllBosses().get(bossId);
  223.  
  224. if(bossSet == null)
  225. {
  226. ShowBoard.separateAndSend(html, player);
  227. return;
  228. }
  229.  
  230. NpcTemplate bossTemplate = NpcHolder.getInstance().getTemplate(bossId);
  231. NpcInstance bossInstance = getAliveBoss(bossId);
  232.  
  233. html = getDetailedBossReplacements(html, bossSet, bossTemplate, bossInstance);
  234. html = getNormalReplacements(html, page, sort, search, bossId);
  235.  
  236. if(!AutoImageSenderManager.isImageAutoSendable(bossId))
  237. ImagesCache.getInstance().sendImageToPlayer(player, bossId);
  238.  
  239. ShowBoard.separateAndSend(html, player);
  240. }
  241.  
  242. /**
  243. * Managing buttons that were clicking in Boss Details page
  244. * @param player that clicked button
  245. * @param buttonIndex 1: Showing Location of the boss. 2: Showing Drops
  246. * @param bossId Id of the boss that player was looking into
  247. */
  248. private static void manageButtons(Player player, int buttonIndex, int bossId)
  249. {
  250. switch(buttonIndex)
  251. {
  252. case 1://Show Location
  253. RaidBossSpawnManager.showBossLocation(player, bossId);
  254. break;
  255. case 2://Show Drops
  256. if(Config.ALLOW_DROP_CALCULATOR)
  257. RewardListInfo.showInfo(player, NpcHolder.getInstance().getTemplate(bossId), true, false, 1.0);
  258. break;
  259. case 3://Go to Boss
  260. if(!player.isInZonePeace() || Olympiad.isRegistered(player))
  261. {
  262. player.sendMessage("You can do it only in safe zone!");
  263. return;
  264. }
  265. NpcInstance aliveInstance = getAliveBoss(bossId);
  266. if(aliveInstance != null)
  267. player.teleToLocation(aliveInstance.getLoc());
  268. else
  269. player.sendMessage("Boss isn't alive!");
  270. case 4://Show Location
  271. player.sendPacket(new RadarControl(2, 2, 0, 0, 0));
  272. }
  273. }
  274.  
  275. /**
  276. * Replacing all %a% words by real Values in Detailed Boss Page
  277. * @param html current Html
  278. * @param bossSet StatsSet of the boss
  279. * @param bossTemplate NpcTemplate of the boss
  280. * @param bossInstance any Instance of the boss(can be null)
  281. * @return filled HTML
  282. */
  283. private static String getDetailedBossReplacements(String html, StatsSet bossSet, NpcTemplate bossTemplate, NpcInstance bossInstance)
  284. {
  285. String newHtml = html;
  286.  
  287. boolean isAlive = isBossAlive(bossSet);
  288.  
  289. newHtml = newHtml.replace("<?name?>", bossTemplate.getName());
  290. newHtml = newHtml.replace("<?level?>", String.valueOf(bossTemplate.level));
  291. newHtml = newHtml.replace("<?status?>", isAlive ? "Alive" : "Dead");
  292. newHtml = newHtml.replace("<?status_color?>", getTextColor(isAlive));
  293. newHtml = newHtml.replace("<?minions?>", String.valueOf(getMinionsCount(bossTemplate)));
  294.  
  295. newHtml = newHtml.replace("<?currentHp?>", Util.formatAdena((int) (bossInstance != null ? (int) bossInstance.getCurrentHp() : 0)));
  296. newHtml = newHtml.replace("<?maxHp?>", Util.formatAdena((int) bossTemplate.baseHpMax));
  297. newHtml = newHtml.replace("<?minions?>", String.valueOf(getMinionsCount(bossTemplate)));
  298.  
  299. return newHtml;
  300. }
  301.  
  302. /**
  303. * Replacing page, sorts, bossId, search
  304. * @param html to fill
  305. * @param page number
  306. * @param sort type
  307. * @param search word
  308. * @param bossId If of the boss, set -1 if doesn't matter
  309. * @return new Html page
  310. */
  311. private static String getNormalReplacements(String html, int page, SortType sort, CharSequence search, int bossId)
  312. {
  313. String newHtml = html;
  314. newHtml = newHtml.replace("<?page?>", String.valueOf(page));
  315. newHtml = newHtml.replace("<?sort?>", String.valueOf(sort.index));
  316. newHtml = newHtml.replace("<?bossId?>", String.valueOf(bossId));
  317. newHtml = newHtml.replace("<?search?>", search);
  318.  
  319. for(int i = 1; i <= 6; i++)
  320. {
  321. if(Math.abs(sort.index) == i)
  322. newHtml = newHtml.replace("<?sort" + i + "?>", String.valueOf(-sort.index));
  323. else
  324. newHtml = newHtml.replace("<?sort" + i + "?>", String.valueOf(i));
  325. }
  326.  
  327. return newHtml;
  328. }
  329.  
  330. private static boolean isBossAlive(StatsSet set)
  331. {
  332. return (long) set.getInteger("respawn_delay", 0) < System.currentTimeMillis() / TimeUnit.SECONDS.toMillis(1L);
  333. }
  334.  
  335. /**
  336. * Getting alive and visible instance of the bossId
  337. * @param bossId Id of the boss
  338. * @return Instance of the boss
  339. */
  340. private static NpcInstance getAliveBoss(int bossId)
  341. {
  342. List<NpcInstance> instances = GameObjectsStorage.getAllByNpcId(bossId, true, true);
  343. return instances.isEmpty() ? null : instances.get(0);
  344. }
  345.  
  346. private static int getMinionsCount(NpcTemplate template)
  347. {
  348. int minionsCount = 0;
  349. for(MinionData minion : template.getMinionData())
  350. minionsCount += minion.getAmount();
  351. return minionsCount;
  352. }
  353.  
  354. private static String getTextColor(boolean alive)
  355. {
  356. if(alive)
  357. return "259a30";//"327b39";
  358. else
  359. return "b02e31";//"8f3d3f";
  360. }
  361.  
  362. /**
  363. * Getting List of Bosses that player is looking for(including sort and search)
  364. * @param sort Type of sorting he want to use
  365. * @param search word that he is looking for
  366. * @return Map of Bosses
  367. */
  368. private static Map<Integer, StatsSet> getSearchedBosses(SortType sort, String search)
  369. {
  370. Map<Integer, StatsSet> result = getBossesMapBySearch(search);
  371.  
  372. for(int id : BOSSES_TO_NOT_SHOW)
  373. result.remove(id);
  374.  
  375. result = sortResults(result, sort);
  376.  
  377. return result;
  378. }
  379.  
  380. /**
  381. * Getting List of Bosses that player is looking for(including search)
  382. * @param search String that boss Name needs to contains(can be Empty)
  383. * @return MapMap of Bosses
  384. */
  385. private static Map<Integer, StatsSet> getBossesMapBySearch(String search)
  386. {
  387. Map<Integer, StatsSet> finalResult = new HashMap<Integer, StatsSet>();
  388. if(search.isEmpty())
  389. {
  390. finalResult = RaidBossSpawnManager.getInstance().getAllBosses();
  391. }
  392. else
  393. {
  394. for(Entry<Integer, StatsSet> entry : RaidBossSpawnManager.getInstance().getAllBosses().entrySet())
  395. {
  396. NpcTemplate temp = NpcHolder.getInstance().getTemplate(entry.getKey().intValue());
  397. if(StringUtils.containsIgnoreCase(temp.getName(), search))
  398. finalResult.put(entry.getKey(), entry.getValue());
  399. }
  400. }
  401. return finalResult;
  402. }
  403.  
  404. /**
  405. * Sorting results by sort type
  406. * @param result map to sort
  407. * @param sort type
  408. * @return sorted Map
  409. */
  410. private static Map<Integer, StatsSet> sortResults(Map<Integer, StatsSet> result, SortType sort)
  411. {
  412. ValueComparator bvc = new ValueComparator(result, sort);
  413. Map<Integer, StatsSet> sortedMap = new TreeMap<Integer, StatsSet>(bvc);
  414. sortedMap.putAll(result);
  415. return sortedMap;
  416. }
  417.  
  418. /**
  419. * Comparator of Bosses
  420. */
  421. private static class ValueComparator implements Comparator<Integer>, Serializable
  422. {
  423. private static final long serialVersionUID = 4782405190873267622L;
  424. private final Map<Integer, StatsSet> base;
  425. private final SortType sortType;
  426.  
  427. private ValueComparator(Map<Integer, StatsSet> base, SortType sortType)
  428. {
  429. this.base = base;
  430. this.sortType = sortType;
  431. }
  432.  
  433. @Override
  434. public int compare(Integer o1, Integer o2)
  435. {
  436. int sortResult = sortById(o1, o2, sortType);
  437. if(sortResult == 0 && !o1.equals(o2) && Math.abs(sortType.index) != 1)
  438. sortResult = sortById(o1, o2, SortType.NAME_ASC);
  439. return sortResult;
  440. }
  441.  
  442. /**
  443. * Comparing a and b but sorting
  444. * @param a first variable
  445. * @param b second variable
  446. * @param sorting type of sorting
  447. * @return result of comparing
  448. */
  449. private int sortById(Integer a, Integer b, SortType sorting)
  450. {
  451. NpcTemplate temp1 = NpcHolder.getInstance().getTemplate(a.intValue());
  452. NpcTemplate temp2 = NpcHolder.getInstance().getTemplate(b.intValue());
  453. StatsSet set1 = base.get(a);
  454. StatsSet set2 = base.get(b);
  455. switch(sorting)
  456. {
  457. case NAME_ASC:
  458. return temp1.getName().compareTo(temp2.getName());
  459. case NAME_DESC:
  460. return temp2.getName().compareTo(temp1.getName());
  461. case LEVEL_ASC:
  462. return Integer.compare(temp1.level, temp2.level);
  463. case LEVEL_DESC:
  464. return Integer.compare(temp2.level, temp1.level);
  465. case STATUS_ASC:
  466. return Integer.compare(set1.getInteger("respawn_delay", 0), set2.getInteger("respawn_delay", 0));
  467. case STATUS_DESC:
  468. return Integer.compare(set2.getInteger("respawn_delay", 0), set1.getInteger("respawn_delay", 0));
  469. }
  470. return 0;
  471. }
  472. }
  473.  
  474. private enum SortType
  475. {
  476. NAME_ASC(1),
  477. NAME_DESC(-1),
  478. LEVEL_ASC(2),
  479. LEVEL_DESC(-2),
  480. STATUS_ASC(3),
  481. STATUS_DESC(-3);
  482.  
  483. public final int index;
  484.  
  485. SortType(int index)
  486. {
  487. this.index = index;
  488. }
  489. }
  490.  
  491. /**
  492. * Getting SortType by index
  493. * @param i index
  494. * @return SortType
  495. */
  496. private static SortType getSortByIndex(int i)
  497. {
  498. for(SortType type : SortType.values())
  499. if(type.index == i)
  500. return type;
  501. return SortType.NAME_ASC;
  502. }
  503.  
  504. @Override
  505. public void onWriteCommand(Player player, String bypass, String arg1, String arg2, String arg3, String arg4, String arg5)
  506. {}
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement