Hero.java 31 KB

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