Hero.java 31 KB

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