Hero.java 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * Copyright (C) 2004-2013 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.model.entity;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Calendar;
  26. import java.util.Collections;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Map.Entry;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33. import javolution.util.FastList;
  34. import javolution.util.FastMap;
  35. import com.l2jserver.Config;
  36. import com.l2jserver.L2DatabaseFactory;
  37. import com.l2jserver.gameserver.cache.HtmCache;
  38. import com.l2jserver.gameserver.datatables.CharNameTable;
  39. import com.l2jserver.gameserver.datatables.ClanTable;
  40. import com.l2jserver.gameserver.datatables.ClassListData;
  41. import com.l2jserver.gameserver.datatables.NpcTable;
  42. import com.l2jserver.gameserver.instancemanager.CastleManager;
  43. import com.l2jserver.gameserver.model.L2Clan;
  44. import com.l2jserver.gameserver.model.L2World;
  45. import com.l2jserver.gameserver.model.StatsSet;
  46. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  47. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  48. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  49. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  50. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  51. import com.l2jserver.gameserver.network.SystemMessageId;
  52. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  53. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  54. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  55. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  56. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  57. import com.l2jserver.util.StringUtil;
  58. /**
  59. * @author godson
  60. */
  61. public class Hero
  62. {
  63. private static final Logger _log = Logger.getLogger(Hero.class.getName());
  64. private static final String GET_HEROES = "SELECT heroes.charId, characters.char_name, heroes.class_id, heroes.count, heroes.played FROM heroes, characters WHERE characters.charId = heroes.charId AND heroes.played = 1";
  65. private static final String GET_ALL_HEROES = "SELECT heroes.charId, characters.char_name, heroes.class_id, heroes.count, heroes.played FROM heroes, characters WHERE characters.charId = heroes.charId";
  66. private static final String UPDATE_ALL = "UPDATE heroes SET played = 0";
  67. private static final String INSERT_HERO = "INSERT INTO heroes (charId, class_id, count, played) VALUES (?,?,?,?)";
  68. private static final String UPDATE_HERO = "UPDATE heroes SET count = ?, played = ? WHERE charId = ?";
  69. private static final String GET_CLAN_ALLY = "SELECT characters.clanid AS clanid, coalesce(clan_data.ally_Id, 0) AS allyId FROM characters LEFT JOIN clan_data ON clan_data.clan_id = characters.clanid WHERE characters.charId = ?";
  70. private static final String GET_CLAN_NAME = "SELECT clan_name FROM clan_data WHERE clan_id = (SELECT clanid FROM characters WHERE charId = ?)";
  71. // delete hero items
  72. private static final String DELETE_ITEMS = "DELETE FROM items WHERE item_id IN (6842, 6611, 6612, 6613, 6614, 6615, 6616, 6617, 6618, 6619, 6620, 6621, 9388, 9389, 9390) AND owner_id NOT IN (SELECT charId FROM characters WHERE accesslevel > 0)";
  73. private static final Map<Integer, StatsSet> _heroes = new FastMap<>();
  74. private static final Map<Integer, StatsSet> _completeHeroes = new FastMap<>();
  75. private static final Map<Integer, StatsSet> _herocounts = new FastMap<>();
  76. private static final Map<Integer, List<StatsSet>> _herofights = new FastMap<>();
  77. private static final Map<Integer, List<StatsSet>> _herodiary = new FastMap<>();
  78. private static final Map<Integer, String> _heroMessage = new FastMap<>();
  79. public static final String COUNT = "count";
  80. public static final String PLAYED = "played";
  81. public static final String CLAN_NAME = "clan_name";
  82. public static final String CLAN_CREST = "clan_crest";
  83. public static final String ALLY_NAME = "ally_name";
  84. public static final String ALLY_CREST = "ally_crest";
  85. public static final int ACTION_RAID_KILLED = 1;
  86. public static final int ACTION_HERO_GAINED = 2;
  87. public static final int ACTION_CASTLE_TAKEN = 3;
  88. public static Hero getInstance()
  89. {
  90. return SingletonHolder._instance;
  91. }
  92. protected Hero()
  93. {
  94. init();
  95. }
  96. private void init()
  97. {
  98. _heroes.clear();
  99. _completeHeroes.clear();
  100. _herocounts.clear();
  101. _herofights.clear();
  102. _herodiary.clear();
  103. _heroMessage.clear();
  104. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  105. {
  106. PreparedStatement statement = con.prepareStatement(GET_HEROES);
  107. ResultSet rset = statement.executeQuery();
  108. PreparedStatement statement2 = con.prepareStatement(GET_CLAN_ALLY);
  109. while (rset.next())
  110. {
  111. StatsSet hero = new StatsSet();
  112. int charId = rset.getInt(Olympiad.CHAR_ID);
  113. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  114. hero.set(Olympiad.CLASS_ID, rset.getInt(Olympiad.CLASS_ID));
  115. hero.set(COUNT, rset.getInt(COUNT));
  116. hero.set(PLAYED, rset.getInt(PLAYED));
  117. loadFights(charId);
  118. loadDiary(charId);
  119. loadMessage(charId);
  120. processHeros(statement2, charId, hero);
  121. _heroes.put(charId, hero);
  122. }
  123. rset.close();
  124. statement.close();
  125. statement = con.prepareStatement(GET_ALL_HEROES);
  126. rset = statement.executeQuery();
  127. while (rset.next())
  128. {
  129. StatsSet hero = new StatsSet();
  130. int charId = rset.getInt(Olympiad.CHAR_ID);
  131. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  132. hero.set(Olympiad.CLASS_ID, rset.getInt(Olympiad.CLASS_ID));
  133. hero.set(COUNT, rset.getInt(COUNT));
  134. hero.set(PLAYED, rset.getInt(PLAYED));
  135. processHeros(statement2, charId, hero);
  136. _completeHeroes.put(charId, hero);
  137. }
  138. statement2.close();
  139. rset.close();
  140. statement.close();
  141. }
  142. catch (SQLException e)
  143. {
  144. _log.log(Level.WARNING, "Hero System: Couldnt load Heroes", e);
  145. }
  146. _log.info("Hero System: Loaded " + _heroes.size() + " Heroes.");
  147. _log.info("Hero System: Loaded " + _completeHeroes.size() + " all time Heroes.");
  148. }
  149. private void processHeros(PreparedStatement ps, int charId, StatsSet hero) throws SQLException
  150. {
  151. ps.setInt(1, charId);
  152. ResultSet rs = ps.executeQuery();
  153. if (rs.next())
  154. {
  155. int clanId = rs.getInt("clanid");
  156. int allyId = rs.getInt("allyId");
  157. String clanName = "";
  158. String allyName = "";
  159. int clanCrest = 0;
  160. int allyCrest = 0;
  161. if (clanId > 0)
  162. {
  163. clanName = ClanTable.getInstance().getClan(clanId).getName();
  164. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  165. if (allyId > 0)
  166. {
  167. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  168. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  169. }
  170. }
  171. hero.set(CLAN_CREST, clanCrest);
  172. hero.set(CLAN_NAME, clanName);
  173. hero.set(ALLY_CREST, allyCrest);
  174. hero.set(ALLY_NAME, allyName);
  175. }
  176. rs.close();
  177. ps.clearParameters();
  178. }
  179. private String calcFightTime(long FightTime)
  180. {
  181. String format = String.format("%%0%dd", 2);
  182. FightTime = FightTime / 1000;
  183. String seconds = String.format(format, FightTime % 60);
  184. String minutes = String.format(format, (FightTime % 3600) / 60);
  185. String time = minutes + ":" + seconds;
  186. return time;
  187. }
  188. /**
  189. * Restore hero message from Db.
  190. * @param charId
  191. */
  192. public void loadMessage(int charId)
  193. {
  194. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  195. {
  196. String message = null;
  197. PreparedStatement statement = con.prepareStatement("SELECT message FROM heroes WHERE charId=?");
  198. statement.setInt(1, charId);
  199. ResultSet rset = statement.executeQuery();
  200. rset.next();
  201. message = rset.getString("message");
  202. _heroMessage.put(charId, message);
  203. rset.close();
  204. statement.close();
  205. }
  206. catch (SQLException e)
  207. {
  208. _log.log(Level.WARNING, "Hero System: Couldnt load Hero Message for CharId: " + charId, e);
  209. }
  210. }
  211. public void loadDiary(int charId)
  212. {
  213. final List<StatsSet> _diary = new FastList<>();
  214. int diaryentries = 0;
  215. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  216. {
  217. PreparedStatement statement = con.prepareStatement("SELECT * FROM heroes_diary WHERE charId=? ORDER BY time ASC");
  218. statement.setInt(1, charId);
  219. ResultSet rset = statement.executeQuery();
  220. while (rset.next())
  221. {
  222. StatsSet _diaryentry = new StatsSet();
  223. long time = rset.getLong("time");
  224. int action = rset.getInt("action");
  225. int param = rset.getInt("param");
  226. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(time));
  227. _diaryentry.set("date", date);
  228. if (action == ACTION_RAID_KILLED)
  229. {
  230. L2NpcTemplate template = NpcTable.getInstance().getTemplate(param);
  231. if (template != null)
  232. {
  233. _diaryentry.set("action", template.getName() + " was defeated");
  234. }
  235. }
  236. else if (action == ACTION_HERO_GAINED)
  237. {
  238. _diaryentry.set("action", "Gained Hero status");
  239. }
  240. else if (action == ACTION_CASTLE_TAKEN)
  241. {
  242. Castle castle = CastleManager.getInstance().getCastleById(param);
  243. if (castle != null)
  244. {
  245. _diaryentry.set("action", castle.getName() + " Castle was successfuly taken");
  246. }
  247. }
  248. _diary.add(_diaryentry);
  249. diaryentries++;
  250. }
  251. rset.close();
  252. statement.close();
  253. _herodiary.put(charId, _diary);
  254. _log.info("Hero System: Loaded " + diaryentries + " diary entries for Hero: " + CharNameTable.getInstance().getNameById(charId));
  255. }
  256. catch (SQLException e)
  257. {
  258. _log.log(Level.WARNING, "Hero System: Couldnt load Hero Diary for CharId: " + charId, e);
  259. }
  260. }
  261. public void loadFights(int charId)
  262. {
  263. final List<StatsSet> _fights = new FastList<>();
  264. StatsSet _herocountdata = new StatsSet();
  265. Calendar _data = Calendar.getInstance();
  266. _data.set(Calendar.DAY_OF_MONTH, 1);
  267. _data.set(Calendar.HOUR_OF_DAY, 0);
  268. _data.set(Calendar.MINUTE, 0);
  269. _data.set(Calendar.MILLISECOND, 0);
  270. long from = _data.getTimeInMillis();
  271. int numberoffights = 0;
  272. int _victorys = 0;
  273. int _losses = 0;
  274. int _draws = 0;
  275. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  276. {
  277. PreparedStatement statement = con.prepareStatement("SELECT * FROM olympiad_fights WHERE (charOneId=? OR charTwoId=?) AND start<? ORDER BY start ASC");
  278. statement.setInt(1, charId);
  279. statement.setInt(2, charId);
  280. statement.setLong(3, from);
  281. ResultSet rset = statement.executeQuery();
  282. int charOneId;
  283. int charOneClass;
  284. int charTwoId;
  285. int charTwoClass;
  286. int winner;
  287. long start;
  288. int time;
  289. int classed;
  290. while (rset.next())
  291. {
  292. charOneId = rset.getInt("charOneId");
  293. charOneClass = rset.getInt("charOneClass");
  294. charTwoId = rset.getInt("charTwoId");
  295. charTwoClass = rset.getInt("charTwoClass");
  296. winner = rset.getInt("winner");
  297. start = rset.getLong("start");
  298. time = rset.getInt("time");
  299. classed = rset.getInt("classed");
  300. if (charId == charOneId)
  301. {
  302. String name = CharNameTable.getInstance().getNameById(charTwoId);
  303. String cls = ClassListData.getInstance().getClass(charTwoClass).getClientCode();
  304. if ((name != null) && (cls != null))
  305. {
  306. StatsSet fight = new StatsSet();
  307. fight.set("oponent", name);
  308. fight.set("oponentclass", cls);
  309. fight.set("time", calcFightTime(time));
  310. String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(start));
  311. fight.set("start", date);
  312. fight.set("classed", classed);
  313. if (winner == 1)
  314. {
  315. fight.set("result", "<font color=\"00ff00\">victory</font>");
  316. _victorys++;
  317. }
  318. else if (winner == 2)
  319. {
  320. fight.set("result", "<font color=\"ff0000\">loss</font>");
  321. _losses++;
  322. }
  323. else if (winner == 0)
  324. {
  325. fight.set("result", "<font color=\"ffff00\">draw</font>");
  326. _draws++;
  327. }
  328. _fights.add(fight);
  329. numberoffights++;
  330. }
  331. }
  332. else if (charId == charTwoId)
  333. {
  334. String name = CharNameTable.getInstance().getNameById(charOneId);
  335. String cls = ClassListData.getInstance().getClass(charOneClass).getClientCode();
  336. if ((name != null) && (cls != null))
  337. {
  338. StatsSet fight = new StatsSet();
  339. fight.set("oponent", name);
  340. fight.set("oponentclass", cls);
  341. fight.set("time", calcFightTime(time));
  342. String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(start));
  343. fight.set("start", date);
  344. fight.set("classed", classed);
  345. if (winner == 1)
  346. {
  347. fight.set("result", "<font color=\"ff0000\">loss</font>");
  348. _losses++;
  349. }
  350. else if (winner == 2)
  351. {
  352. fight.set("result", "<font color=\"00ff00\">victory</font>");
  353. _victorys++;
  354. }
  355. else if (winner == 0)
  356. {
  357. fight.set("result", "<font color=\"ffff00\">draw</font>");
  358. _draws++;
  359. }
  360. _fights.add(fight);
  361. numberoffights++;
  362. }
  363. }
  364. }
  365. rset.close();
  366. statement.close();
  367. _herocountdata.set("victory", _victorys);
  368. _herocountdata.set("draw", _draws);
  369. _herocountdata.set("loss", _losses);
  370. _herocounts.put(charId, _herocountdata);
  371. _herofights.put(charId, _fights);
  372. _log.info("Hero System: Loaded " + numberoffights + " fights for Hero: " + CharNameTable.getInstance().getNameById(charId));
  373. }
  374. catch (SQLException e)
  375. {
  376. _log.log(Level.WARNING, "Hero System: Couldnt load Hero fights history for CharId: " + charId, e);
  377. }
  378. }
  379. public Map<Integer, StatsSet> getHeroes()
  380. {
  381. return _heroes;
  382. }
  383. public int getHeroByClass(int classid)
  384. {
  385. for (Entry<Integer, StatsSet> e : _heroes.entrySet())
  386. {
  387. if (e.getValue().getInteger(Olympiad.CLASS_ID) == classid)
  388. {
  389. return e.getKey();
  390. }
  391. }
  392. return 0;
  393. }
  394. public void resetData()
  395. {
  396. _herodiary.clear();
  397. _herofights.clear();
  398. _herocounts.clear();
  399. _heroMessage.clear();
  400. }
  401. public void showHeroDiary(L2PcInstance activeChar, int heroclass, int charid, int page)
  402. {
  403. final int perpage = 10;
  404. if (_herodiary.containsKey(charid))
  405. {
  406. List<StatsSet> _mainlist = _herodiary.get(charid);
  407. NpcHtmlMessage DiaryReply = new NpcHtmlMessage(5);
  408. final String htmContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/olympiad/herodiary.htm");
  409. if ((htmContent != null) && _heroMessage.containsKey(charid))
  410. {
  411. DiaryReply.setHtml(htmContent);
  412. DiaryReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
  413. DiaryReply.replace("%message%", _heroMessage.get(charid));
  414. DiaryReply.disableValidation();
  415. if (!_mainlist.isEmpty())
  416. {
  417. FastList<StatsSet> _list = FastList.newInstance();
  418. _list.addAll(_mainlist);
  419. Collections.reverse(_list);
  420. boolean color = true;
  421. final StringBuilder fList = new StringBuilder(500);
  422. int counter = 0;
  423. int breakat = 0;
  424. for (int i = ((page - 1) * perpage); i < _list.size(); i++)
  425. {
  426. breakat = i;
  427. StatsSet _diaryentry = _list.get(i);
  428. StringUtil.append(fList, "<tr><td>");
  429. if (color)
  430. {
  431. StringUtil.append(fList, "<table width=270 bgcolor=\"131210\">");
  432. }
  433. else
  434. {
  435. StringUtil.append(fList, "<table width=270>");
  436. }
  437. StringUtil.append(fList, "<tr><td width=270><font color=\"LEVEL\">" + _diaryentry.getString("date") + ":xx</font></td></tr>");
  438. StringUtil.append(fList, "<tr><td width=270>" + _diaryentry.getString("action") + "</td></tr>");
  439. StringUtil.append(fList, "<tr><td>&nbsp;</td></tr></table>");
  440. StringUtil.append(fList, "</td></tr>");
  441. color = !color;
  442. counter++;
  443. if (counter >= perpage)
  444. {
  445. break;
  446. }
  447. }
  448. if (breakat < (_list.size() - 1))
  449. {
  450. DiaryReply.replace("%buttprev%", "<button value=\"Prev\" action=\"bypass _diary?class=" + heroclass + "&page=" + (page + 1) + "\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">");
  451. }
  452. else
  453. {
  454. DiaryReply.replace("%buttprev%", "");
  455. }
  456. if (page > 1)
  457. {
  458. DiaryReply.replace("%buttnext%", "<button value=\"Next\" action=\"bypass _diary?class=" + heroclass + "&page=" + (page - 1) + "\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">");
  459. }
  460. else
  461. {
  462. DiaryReply.replace("%buttnext%", "");
  463. }
  464. DiaryReply.replace("%list%", fList.toString());
  465. FastList.recycle(_list);
  466. }
  467. else
  468. {
  469. DiaryReply.replace("%list%", "");
  470. DiaryReply.replace("%buttprev%", "");
  471. DiaryReply.replace("%buttnext%", "");
  472. }
  473. activeChar.sendPacket(DiaryReply);
  474. }
  475. }
  476. }
  477. public void showHeroFights(L2PcInstance activeChar, int heroclass, int charid, int page)
  478. {
  479. final int perpage = 20;
  480. int _win = 0;
  481. int _loss = 0;
  482. int _draw = 0;
  483. if (_herofights.containsKey(charid))
  484. {
  485. List<StatsSet> _list = _herofights.get(charid);
  486. NpcHtmlMessage FightReply = new NpcHtmlMessage(5);
  487. final String htmContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/olympiad/herohistory.htm");
  488. if (htmContent != null)
  489. {
  490. FightReply.setHtml(htmContent);
  491. FightReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
  492. FightReply.disableValidation();
  493. if (!_list.isEmpty())
  494. {
  495. if (_herocounts.containsKey(charid))
  496. {
  497. StatsSet _herocount = _herocounts.get(charid);
  498. _win = _herocount.getInteger("victory");
  499. _loss = _herocount.getInteger("loss");
  500. _draw = _herocount.getInteger("draw");
  501. }
  502. boolean color = true;
  503. final StringBuilder fList = new StringBuilder(500);
  504. int counter = 0;
  505. int breakat = 0;
  506. for (int i = ((page - 1) * perpage); i < _list.size(); i++)
  507. {
  508. breakat = i;
  509. StatsSet fight = _list.get(i);
  510. StringUtil.append(fList, "<tr><td>");
  511. if (color)
  512. {
  513. StringUtil.append(fList, "<table width=270 bgcolor=\"131210\">");
  514. }
  515. else
  516. {
  517. StringUtil.append(fList, "<table width=270>");
  518. }
  519. StringUtil.append(fList, "<tr><td width=220><font color=\"LEVEL\">" + fight.getString("start") + "</font>&nbsp;&nbsp;" + fight.getString("result") + "</td><td width=50 align=right>" + (fight.getInteger("classed") > 0 ? "<font color=\"FFFF99\">cls</font>" : "<font color=\"999999\">non-cls<font>") + "</td></tr>");
  520. StringUtil.append(fList, "<tr><td width=220>vs " + fight.getString("oponent") + " (" + fight.getString("oponentclass") + ")</td><td width=50 align=right>(" + fight.getString("time") + ")</td></tr>");
  521. StringUtil.append(fList, "<tr><td colspan=2>&nbsp;</td></tr></table>");
  522. StringUtil.append(fList, "</td></tr>");
  523. color = !color;
  524. counter++;
  525. if (counter >= perpage)
  526. {
  527. break;
  528. }
  529. }
  530. if (breakat < (_list.size() - 1))
  531. {
  532. FightReply.replace("%buttprev%", "<button value=\"Prev\" action=\"bypass _match?class=" + heroclass + "&page=" + (page + 1) + "\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">");
  533. }
  534. else
  535. {
  536. FightReply.replace("%buttprev%", "");
  537. }
  538. if (page > 1)
  539. {
  540. FightReply.replace("%buttnext%", "<button value=\"Next\" action=\"bypass _match?class=" + heroclass + "&page=" + (page - 1) + "\" width=60 height=25 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">");
  541. }
  542. else
  543. {
  544. FightReply.replace("%buttnext%", "");
  545. }
  546. FightReply.replace("%list%", fList.toString());
  547. }
  548. else
  549. {
  550. FightReply.replace("%list%", "");
  551. FightReply.replace("%buttprev%", "");
  552. FightReply.replace("%buttnext%", "");
  553. }
  554. FightReply.replace("%win%", String.valueOf(_win));
  555. FightReply.replace("%draw%", String.valueOf(_draw));
  556. FightReply.replace("%loos%", String.valueOf(_loss));
  557. activeChar.sendPacket(FightReply);
  558. }
  559. }
  560. }
  561. public synchronized void computeNewHeroes(List<StatsSet> newHeroes)
  562. {
  563. updateHeroes(true);
  564. if (!_heroes.isEmpty())
  565. {
  566. for (StatsSet hero : _heroes.values())
  567. {
  568. String name = hero.getString(Olympiad.CHAR_NAME);
  569. L2PcInstance player = L2World.getInstance().getPlayer(name);
  570. if (player == null)
  571. {
  572. continue;
  573. }
  574. try
  575. {
  576. player.setHero(false);
  577. for (int i = 0; i < Inventory.PAPERDOLL_TOTALSLOTS; i++)
  578. {
  579. L2ItemInstance equippedItem = player.getInventory().getPaperdollItem(i);
  580. if ((equippedItem != null) && equippedItem.isHeroItem())
  581. {
  582. player.getInventory().unEquipItemInSlot(i);
  583. }
  584. }
  585. for (L2ItemInstance item : player.getInventory().getAvailableItems(false, false, false))
  586. {
  587. if ((item != null) && item.isHeroItem())
  588. {
  589. player.destroyItem("Hero", item, null, true);
  590. InventoryUpdate iu = new InventoryUpdate();
  591. iu.addRemovedItem(item);
  592. player.sendPacket(iu);
  593. }
  594. }
  595. player.broadcastUserInfo();
  596. }
  597. catch (NullPointerException e)
  598. {
  599. }
  600. }
  601. }
  602. if (newHeroes.isEmpty())
  603. {
  604. _heroes.clear();
  605. return;
  606. }
  607. Map<Integer, StatsSet> heroes = new FastMap<>();
  608. for (StatsSet hero : newHeroes)
  609. {
  610. int charId = hero.getInteger(Olympiad.CHAR_ID);
  611. if ((_completeHeroes != null) && _completeHeroes.containsKey(charId))
  612. {
  613. StatsSet oldHero = _completeHeroes.get(charId);
  614. int count = oldHero.getInteger(COUNT);
  615. oldHero.set(COUNT, count + 1);
  616. oldHero.set(PLAYED, 1);
  617. heroes.put(charId, oldHero);
  618. }
  619. else
  620. {
  621. StatsSet newHero = new StatsSet();
  622. newHero.set(Olympiad.CHAR_NAME, hero.getString(Olympiad.CHAR_NAME));
  623. newHero.set(Olympiad.CLASS_ID, hero.getInteger(Olympiad.CLASS_ID));
  624. newHero.set(COUNT, 1);
  625. newHero.set(PLAYED, 1);
  626. heroes.put(charId, newHero);
  627. }
  628. }
  629. deleteItemsInDb();
  630. _heroes.clear();
  631. _heroes.putAll(heroes);
  632. heroes.clear();
  633. updateHeroes(false);
  634. L2PcInstance player;
  635. for (Integer charId : _heroes.keySet())
  636. {
  637. player = L2World.getInstance().getPlayer(charId);
  638. if (player != null)
  639. {
  640. player.setHero(true);
  641. L2Clan clan = player.getClan();
  642. if (clan != null)
  643. {
  644. clan.addReputationScore(Config.HERO_POINTS, true);
  645. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_MEMBER_C1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  646. sm.addString(CharNameTable.getInstance().getNameById(charId));
  647. sm.addNumber(Config.HERO_POINTS);
  648. clan.broadcastToOnlineMembers(sm);
  649. }
  650. player.sendPacket(new UserInfo(player));
  651. player.sendPacket(new ExBrExtraUserInfo(player));
  652. player.broadcastUserInfo();
  653. // Set Gained hero and reload data
  654. setHeroGained(player.getObjectId());
  655. loadFights(player.getObjectId());
  656. loadDiary(player.getObjectId());
  657. _heroMessage.put(player.getObjectId(), "");
  658. }
  659. else
  660. {
  661. // Set Gained hero and reload data
  662. setHeroGained(charId);
  663. loadFights(charId);
  664. loadDiary(charId);
  665. _heroMessage.put(charId, "");
  666. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  667. {
  668. PreparedStatement statement = con.prepareStatement(GET_CLAN_NAME);
  669. statement.setInt(1, charId);
  670. ResultSet rset = statement.executeQuery();
  671. if (rset.next())
  672. {
  673. String clanName = rset.getString("clan_name");
  674. if (clanName != null)
  675. {
  676. L2Clan clan = ClanTable.getInstance().getClanByName(clanName);
  677. if (clan != null)
  678. {
  679. clan.addReputationScore(Config.HERO_POINTS, true);
  680. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_MEMBER_C1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  681. sm.addString(CharNameTable.getInstance().getNameById(charId));
  682. sm.addNumber(Config.HERO_POINTS);
  683. clan.broadcastToOnlineMembers(sm);
  684. }
  685. }
  686. }
  687. rset.close();
  688. statement.close();
  689. }
  690. catch (Exception e)
  691. {
  692. _log.warning("could not get clan name of player with objectId:" + charId + ": " + e);
  693. }
  694. }
  695. }
  696. }
  697. public void updateHeroes(boolean setDefault)
  698. {
  699. // _herofights = new FastMap<Integer, List<StatsSet>>();
  700. // _herocounts = new FastMap<Integer, StatsSet>();
  701. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  702. {
  703. PreparedStatement statement;
  704. if (setDefault)
  705. {
  706. statement = con.prepareStatement(UPDATE_ALL);
  707. statement.execute();
  708. statement.close();
  709. }
  710. else
  711. {
  712. StatsSet hero;
  713. int heroId;
  714. for (Entry<Integer, StatsSet> entry : _heroes.entrySet())
  715. {
  716. hero = entry.getValue();
  717. heroId = entry.getKey();
  718. if (_completeHeroes.isEmpty() || !_completeHeroes.containsKey(heroId))
  719. {
  720. statement = con.prepareStatement(INSERT_HERO);
  721. statement.setInt(1, heroId);
  722. statement.setInt(2, hero.getInteger(Olympiad.CLASS_ID));
  723. statement.setInt(3, hero.getInteger(COUNT));
  724. statement.setInt(4, hero.getInteger(PLAYED));
  725. statement.execute();
  726. statement.close();
  727. statement = con.prepareStatement(GET_CLAN_ALLY);
  728. statement.setInt(1, heroId);
  729. ResultSet rset = statement.executeQuery();
  730. if (rset.next())
  731. {
  732. int clanId = rset.getInt("clanid");
  733. int allyId = rset.getInt("allyId");
  734. String clanName = "";
  735. String allyName = "";
  736. int clanCrest = 0;
  737. int allyCrest = 0;
  738. if (clanId > 0)
  739. {
  740. clanName = ClanTable.getInstance().getClan(clanId).getName();
  741. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  742. if (allyId > 0)
  743. {
  744. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  745. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  746. }
  747. }
  748. hero.set(CLAN_CREST, clanCrest);
  749. hero.set(CLAN_NAME, clanName);
  750. hero.set(ALLY_CREST, allyCrest);
  751. hero.set(ALLY_NAME, allyName);
  752. }
  753. rset.close();
  754. statement.close();
  755. _heroes.remove(heroId);
  756. _heroes.put(heroId, hero);
  757. _completeHeroes.put(heroId, hero);
  758. }
  759. else
  760. {
  761. statement = con.prepareStatement(UPDATE_HERO);
  762. statement.setInt(1, hero.getInteger(COUNT));
  763. statement.setInt(2, hero.getInteger(PLAYED));
  764. statement.setInt(3, heroId);
  765. statement.execute();
  766. statement.close();
  767. }
  768. }
  769. }
  770. }
  771. catch (SQLException e)
  772. {
  773. _log.log(Level.WARNING, "Hero System: Couldnt update Heroes", e);
  774. }
  775. }
  776. public void setHeroGained(int charId)
  777. {
  778. setDiaryData(charId, ACTION_HERO_GAINED, 0);
  779. }
  780. public void setRBkilled(int charId, int npcId)
  781. {
  782. setDiaryData(charId, ACTION_RAID_KILLED, npcId);
  783. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  784. if (_herodiary.containsKey(charId) && (template != null))
  785. {
  786. // Get Data
  787. List<StatsSet> _list = _herodiary.get(charId);
  788. // Clear old data
  789. _herodiary.remove(charId);
  790. // Prepare new data
  791. StatsSet _diaryentry = new StatsSet();
  792. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(System.currentTimeMillis()));
  793. _diaryentry.set("date", date);
  794. _diaryentry.set("action", template.getName() + " was defeated");
  795. // Add to old list
  796. _list.add(_diaryentry);
  797. // Put new list into diary
  798. _herodiary.put(charId, _list);
  799. }
  800. }
  801. public void setCastleTaken(int charId, int castleId)
  802. {
  803. setDiaryData(charId, ACTION_CASTLE_TAKEN, castleId);
  804. Castle castle = CastleManager.getInstance().getCastleById(castleId);
  805. if ((castle != null) && _herodiary.containsKey(charId))
  806. {
  807. // Get Data
  808. List<StatsSet> _list = _herodiary.get(charId);
  809. // Clear old data
  810. _herodiary.remove(charId);
  811. // Prepare new data
  812. StatsSet _diaryentry = new StatsSet();
  813. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(System.currentTimeMillis()));
  814. _diaryentry.set("date", date);
  815. _diaryentry.set("action", castle.getName() + " Castle was successfuly taken");
  816. // Add to old list
  817. _list.add(_diaryentry);
  818. // Put new list into diary
  819. _herodiary.put(charId, _list);
  820. }
  821. }
  822. public void setDiaryData(int charId, int action, int param)
  823. {
  824. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  825. {
  826. PreparedStatement statement = con.prepareStatement("INSERT INTO heroes_diary (charId, time, action, param) values(?,?,?,?)");
  827. statement.setInt(1, charId);
  828. statement.setLong(2, System.currentTimeMillis());
  829. statement.setInt(3, action);
  830. statement.setInt(4, param);
  831. statement.execute();
  832. statement.close();
  833. }
  834. catch (SQLException e)
  835. {
  836. if (_log.isLoggable(Level.SEVERE))
  837. {
  838. _log.log(Level.SEVERE, "SQL exception while saving DiaryData.", e);
  839. }
  840. }
  841. }
  842. /**
  843. * Set new hero message for hero
  844. * @param player the player instance
  845. * @param message String to set
  846. */
  847. public void setHeroMessage(L2PcInstance player, String message)
  848. {
  849. _heroMessage.put(player.getObjectId(), message);
  850. if (player.isDebug())
  851. {
  852. _log.info("Hero message for player: " + player.getName() + ":[" + player.getObjectId() + "] set to: [" + message + "]");
  853. }
  854. }
  855. /**
  856. * Update hero message in database
  857. * @param charId character objid
  858. */
  859. public void saveHeroMessage(int charId)
  860. {
  861. if (_heroMessage.get(charId) == null)
  862. {
  863. return;
  864. }
  865. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  866. {
  867. PreparedStatement statement = con.prepareStatement("UPDATE heroes SET message=? WHERE charId=?;");
  868. statement.setString(1, _heroMessage.get(charId));
  869. statement.setInt(2, charId);
  870. statement.execute();
  871. statement.close();
  872. }
  873. catch (SQLException e)
  874. {
  875. _log.log(Level.SEVERE, "SQL exception while saving HeroMessage.", e);
  876. }
  877. }
  878. private void deleteItemsInDb()
  879. {
  880. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  881. {
  882. PreparedStatement statement = con.prepareStatement(DELETE_ITEMS);
  883. statement.execute();
  884. statement.close();
  885. }
  886. catch (SQLException e)
  887. {
  888. _log.log(Level.WARNING, "", e);
  889. }
  890. }
  891. /**
  892. * Saving task for {@link Hero}<BR>
  893. * Save all hero messages to DB.
  894. */
  895. public void shutdown()
  896. {
  897. for (int charId : _heroMessage.keySet())
  898. {
  899. saveHeroMessage(charId);
  900. }
  901. }
  902. /**
  903. * @param objectId the player's object Id to verify.
  904. * @return {@code true} if there are heros and the player is in the list, {@code false} otherwise.
  905. */
  906. public boolean isHero(int objectId)
  907. {
  908. return _heroes.containsKey(objectId);
  909. }
  910. private static class SingletonHolder
  911. {
  912. protected static final Hero _instance = new Hero();
  913. }
  914. }