Hero.java 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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().getInteger(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. NpcHtmlMessage DiaryReply = new NpcHtmlMessage(5);
  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. NpcHtmlMessage FightReply = new NpcHtmlMessage(5);
  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. FightReply.disableValidation();
  490. if (!_list.isEmpty())
  491. {
  492. if (_herocounts.containsKey(charid))
  493. {
  494. StatsSet _herocount = _herocounts.get(charid);
  495. _win = _herocount.getInteger("victory");
  496. _loss = _herocount.getInteger("loss");
  497. _draw = _herocount.getInteger("draw");
  498. }
  499. boolean color = true;
  500. final StringBuilder fList = new StringBuilder(500);
  501. int counter = 0;
  502. int breakat = 0;
  503. for (int i = ((page - 1) * perpage); i < _list.size(); i++)
  504. {
  505. breakat = i;
  506. StatsSet fight = _list.get(i);
  507. StringUtil.append(fList, "<tr><td>");
  508. if (color)
  509. {
  510. StringUtil.append(fList, "<table width=270 bgcolor=\"131210\">");
  511. }
  512. else
  513. {
  514. StringUtil.append(fList, "<table width=270>");
  515. }
  516. 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>");
  517. 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>");
  518. StringUtil.append(fList, "<tr><td colspan=2>&nbsp;</td></tr></table>");
  519. StringUtil.append(fList, "</td></tr>");
  520. color = !color;
  521. counter++;
  522. if (counter >= perpage)
  523. {
  524. break;
  525. }
  526. }
  527. if (breakat < (_list.size() - 1))
  528. {
  529. 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\">");
  530. }
  531. else
  532. {
  533. FightReply.replace("%buttprev%", "");
  534. }
  535. if (page > 1)
  536. {
  537. 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\">");
  538. }
  539. else
  540. {
  541. FightReply.replace("%buttnext%", "");
  542. }
  543. FightReply.replace("%list%", fList.toString());
  544. }
  545. else
  546. {
  547. FightReply.replace("%list%", "");
  548. FightReply.replace("%buttprev%", "");
  549. FightReply.replace("%buttnext%", "");
  550. }
  551. FightReply.replace("%win%", String.valueOf(_win));
  552. FightReply.replace("%draw%", String.valueOf(_draw));
  553. FightReply.replace("%loos%", String.valueOf(_loss));
  554. activeChar.sendPacket(FightReply);
  555. }
  556. }
  557. }
  558. public synchronized void computeNewHeroes(List<StatsSet> newHeroes)
  559. {
  560. updateHeroes(true);
  561. if (!_heroes.isEmpty())
  562. {
  563. for (StatsSet hero : _heroes.values())
  564. {
  565. String name = hero.getString(Olympiad.CHAR_NAME);
  566. L2PcInstance player = L2World.getInstance().getPlayer(name);
  567. if (player == null)
  568. {
  569. continue;
  570. }
  571. try
  572. {
  573. player.setHero(false);
  574. for (int i = 0; i < Inventory.PAPERDOLL_TOTALSLOTS; i++)
  575. {
  576. L2ItemInstance equippedItem = player.getInventory().getPaperdollItem(i);
  577. if ((equippedItem != null) && equippedItem.isHeroItem())
  578. {
  579. player.getInventory().unEquipItemInSlot(i);
  580. }
  581. }
  582. for (L2ItemInstance item : player.getInventory().getAvailableItems(false, false, false))
  583. {
  584. if ((item != null) && item.isHeroItem())
  585. {
  586. player.destroyItem("Hero", item, null, true);
  587. InventoryUpdate iu = new InventoryUpdate();
  588. iu.addRemovedItem(item);
  589. player.sendPacket(iu);
  590. }
  591. }
  592. player.broadcastUserInfo();
  593. }
  594. catch (NullPointerException e)
  595. {
  596. }
  597. }
  598. }
  599. if (newHeroes.isEmpty())
  600. {
  601. _heroes.clear();
  602. return;
  603. }
  604. Map<Integer, StatsSet> heroes = new FastMap<>();
  605. for (StatsSet hero : newHeroes)
  606. {
  607. int charId = hero.getInteger(Olympiad.CHAR_ID);
  608. if ((_completeHeroes != null) && _completeHeroes.containsKey(charId))
  609. {
  610. StatsSet oldHero = _completeHeroes.get(charId);
  611. int count = oldHero.getInteger(COUNT);
  612. oldHero.set(COUNT, count + 1);
  613. oldHero.set(PLAYED, 1);
  614. heroes.put(charId, oldHero);
  615. }
  616. else
  617. {
  618. StatsSet newHero = new StatsSet();
  619. newHero.set(Olympiad.CHAR_NAME, hero.getString(Olympiad.CHAR_NAME));
  620. newHero.set(Olympiad.CLASS_ID, hero.getInteger(Olympiad.CLASS_ID));
  621. newHero.set(COUNT, 1);
  622. newHero.set(PLAYED, 1);
  623. heroes.put(charId, newHero);
  624. }
  625. }
  626. deleteItemsInDb();
  627. _heroes.clear();
  628. _heroes.putAll(heroes);
  629. heroes.clear();
  630. updateHeroes(false);
  631. L2PcInstance player;
  632. for (Integer charId : _heroes.keySet())
  633. {
  634. player = L2World.getInstance().getPlayer(charId);
  635. if (player != null)
  636. {
  637. player.setHero(true);
  638. L2Clan clan = player.getClan();
  639. if (clan != null)
  640. {
  641. clan.addReputationScore(Config.HERO_POINTS, true);
  642. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_MEMBER_C1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  643. sm.addString(CharNameTable.getInstance().getNameById(charId));
  644. sm.addNumber(Config.HERO_POINTS);
  645. clan.broadcastToOnlineMembers(sm);
  646. }
  647. player.sendPacket(new UserInfo(player));
  648. player.sendPacket(new ExBrExtraUserInfo(player));
  649. player.broadcastUserInfo();
  650. // Set Gained hero and reload data
  651. setHeroGained(player.getObjectId());
  652. loadFights(player.getObjectId());
  653. loadDiary(player.getObjectId());
  654. _heroMessage.put(player.getObjectId(), "");
  655. }
  656. else
  657. {
  658. // Set Gained hero and reload data
  659. setHeroGained(charId);
  660. loadFights(charId);
  661. loadDiary(charId);
  662. _heroMessage.put(charId, "");
  663. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  664. PreparedStatement statement = con.prepareStatement(GET_CLAN_NAME))
  665. {
  666. statement.setInt(1, charId);
  667. try (ResultSet rset = statement.executeQuery())
  668. {
  669. if (rset.next())
  670. {
  671. String clanName = rset.getString("clan_name");
  672. if (clanName != null)
  673. {
  674. L2Clan clan = ClanTable.getInstance().getClanByName(clanName);
  675. if (clan != null)
  676. {
  677. clan.addReputationScore(Config.HERO_POINTS, true);
  678. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_MEMBER_C1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  679. sm.addString(CharNameTable.getInstance().getNameById(charId));
  680. sm.addNumber(Config.HERO_POINTS);
  681. clan.broadcastToOnlineMembers(sm);
  682. }
  683. }
  684. }
  685. }
  686. }
  687. catch (Exception e)
  688. {
  689. _log.warning("could not get clan name of player with objectId:" + charId + ": " + e);
  690. }
  691. }
  692. }
  693. }
  694. public void updateHeroes(boolean setDefault)
  695. {
  696. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  697. {
  698. if (setDefault)
  699. {
  700. try (PreparedStatement update_all = con.prepareStatement(UPDATE_ALL))
  701. {
  702. update_all.execute();
  703. }
  704. }
  705. else
  706. {
  707. StatsSet hero;
  708. int heroId;
  709. for (Entry<Integer, StatsSet> entry : _heroes.entrySet())
  710. {
  711. hero = entry.getValue();
  712. heroId = entry.getKey();
  713. if (_completeHeroes.isEmpty() || !_completeHeroes.containsKey(heroId))
  714. {
  715. try (PreparedStatement insert = con.prepareStatement(INSERT_HERO))
  716. {
  717. insert.setInt(1, heroId);
  718. insert.setInt(2, hero.getInteger(Olympiad.CLASS_ID));
  719. insert.setInt(3, hero.getInteger(COUNT));
  720. insert.setInt(4, hero.getInteger(PLAYED));
  721. insert.execute();
  722. insert.close();
  723. }
  724. try (PreparedStatement statement = con.prepareStatement(GET_CLAN_ALLY))
  725. {
  726. statement.setInt(1, heroId);
  727. try (ResultSet rset = statement.executeQuery())
  728. {
  729. if (rset.next())
  730. {
  731. int clanId = rset.getInt("clanid");
  732. int allyId = rset.getInt("allyId");
  733. String clanName = "";
  734. String allyName = "";
  735. int clanCrest = 0;
  736. int allyCrest = 0;
  737. if (clanId > 0)
  738. {
  739. clanName = ClanTable.getInstance().getClan(clanId).getName();
  740. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  741. if (allyId > 0)
  742. {
  743. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  744. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  745. }
  746. }
  747. hero.set(CLAN_CREST, clanCrest);
  748. hero.set(CLAN_NAME, clanName);
  749. hero.set(ALLY_CREST, allyCrest);
  750. hero.set(ALLY_NAME, allyName);
  751. }
  752. }
  753. }
  754. _heroes.remove(heroId);
  755. _heroes.put(heroId, hero);
  756. _completeHeroes.put(heroId, hero);
  757. }
  758. else
  759. {
  760. try (PreparedStatement statement = con.prepareStatement(UPDATE_HERO))
  761. {
  762. statement.setInt(1, hero.getInteger(COUNT));
  763. statement.setInt(2, hero.getInteger(PLAYED));
  764. statement.setInt(3, heroId);
  765. statement.execute();
  766. }
  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. PreparedStatement statement = con.prepareStatement("INSERT INTO heroes_diary (charId, time, action, param) values(?,?,?,?)"))
  826. {
  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. }
  833. catch (SQLException e)
  834. {
  835. if (_log.isLoggable(Level.SEVERE))
  836. {
  837. _log.log(Level.SEVERE, "SQL exception while saving DiaryData.", e);
  838. }
  839. }
  840. }
  841. /**
  842. * Set new hero message for hero
  843. * @param player the player instance
  844. * @param message String to set
  845. */
  846. public void setHeroMessage(L2PcInstance player, String message)
  847. {
  848. _heroMessage.put(player.getObjectId(), message);
  849. if (player.isDebug())
  850. {
  851. _log.info("Hero message for player: " + player.getName() + ":[" + player.getObjectId() + "] set to: [" + message + "]");
  852. }
  853. }
  854. /**
  855. * Update hero message in database
  856. * @param charId character objid
  857. */
  858. public void saveHeroMessage(int charId)
  859. {
  860. if (_heroMessage.get(charId) == null)
  861. {
  862. return;
  863. }
  864. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  865. PreparedStatement statement = con.prepareStatement("UPDATE heroes SET message=? WHERE charId=?;"))
  866. {
  867. statement.setString(1, _heroMessage.get(charId));
  868. statement.setInt(2, charId);
  869. statement.execute();
  870. }
  871. catch (SQLException e)
  872. {
  873. _log.log(Level.SEVERE, "SQL exception while saving HeroMessage.", e);
  874. }
  875. }
  876. private void deleteItemsInDb()
  877. {
  878. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  879. PreparedStatement statement = con.prepareStatement(DELETE_ITEMS))
  880. {
  881. statement.execute();
  882. }
  883. catch (SQLException e)
  884. {
  885. _log.log(Level.WARNING, "", e);
  886. }
  887. }
  888. /**
  889. * Saving task for {@link Hero}<BR>
  890. * Save all hero messages to DB.
  891. */
  892. public void shutdown()
  893. {
  894. for (int charId : _heroMessage.keySet())
  895. {
  896. saveHeroMessage(charId);
  897. }
  898. }
  899. /**
  900. * @param objectId the player's object Id to verify.
  901. * @return {@code true} if there are heros and the player is in the list, {@code false} otherwise.
  902. */
  903. public boolean isHero(int objectId)
  904. {
  905. return _heroes.containsKey(objectId);
  906. }
  907. private static class SingletonHolder
  908. {
  909. protected static final Hero _instance = new Hero();
  910. }
  911. }