Hero.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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 s = con.createStatement();
  107. ResultSet rset = s.executeQuery(GET_HEROES);
  108. PreparedStatement ps = con.prepareStatement(GET_CLAN_ALLY);
  109. ResultSet rset2 = s.executeQuery(GET_ALL_HEROES))
  110. {
  111. while (rset.next())
  112. {
  113. StatsSet hero = new StatsSet();
  114. int charId = rset.getInt(Olympiad.CHAR_ID);
  115. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  116. hero.set(Olympiad.CLASS_ID, rset.getInt(Olympiad.CLASS_ID));
  117. hero.set(COUNT, rset.getInt(COUNT));
  118. hero.set(PLAYED, rset.getInt(PLAYED));
  119. loadFights(charId);
  120. loadDiary(charId);
  121. loadMessage(charId);
  122. processHeros(ps, charId, hero);
  123. _heroes.put(charId, hero);
  124. }
  125. while (rset2.next())
  126. {
  127. StatsSet hero = new StatsSet();
  128. int charId = rset2.getInt(Olympiad.CHAR_ID);
  129. hero.set(Olympiad.CHAR_NAME, rset2.getString(Olympiad.CHAR_NAME));
  130. hero.set(Olympiad.CLASS_ID, rset2.getInt(Olympiad.CLASS_ID));
  131. hero.set(COUNT, rset2.getInt(COUNT));
  132. hero.set(PLAYED, rset2.getInt(PLAYED));
  133. processHeros(ps, charId, hero);
  134. _completeHeroes.put(charId, hero);
  135. }
  136. }
  137. catch (SQLException e)
  138. {
  139. _log.log(Level.WARNING, "Hero System: Couldnt load Heroes", e);
  140. }
  141. _log.info("Hero System: Loaded " + _heroes.size() + " Heroes.");
  142. _log.info("Hero System: Loaded " + _completeHeroes.size() + " all time Heroes.");
  143. }
  144. private void processHeros(PreparedStatement ps, int charId, StatsSet hero) throws SQLException
  145. {
  146. ps.setInt(1, charId);
  147. try (ResultSet rs = ps.executeQuery())
  148. {
  149. if (rs.next())
  150. {
  151. int clanId = rs.getInt("clanid");
  152. int allyId = rs.getInt("allyId");
  153. String clanName = "";
  154. String allyName = "";
  155. int clanCrest = 0;
  156. int allyCrest = 0;
  157. if (clanId > 0)
  158. {
  159. clanName = ClanTable.getInstance().getClan(clanId).getName();
  160. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  161. if (allyId > 0)
  162. {
  163. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  164. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  165. }
  166. }
  167. hero.set(CLAN_CREST, clanCrest);
  168. hero.set(CLAN_NAME, clanName);
  169. hero.set(ALLY_CREST, allyCrest);
  170. hero.set(ALLY_NAME, allyName);
  171. }
  172. ps.clearParameters();
  173. }
  174. }
  175. private String calcFightTime(long FightTime)
  176. {
  177. String format = String.format("%%0%dd", 2);
  178. FightTime = FightTime / 1000;
  179. String seconds = String.format(format, FightTime % 60);
  180. String minutes = String.format(format, (FightTime % 3600) / 60);
  181. String time = minutes + ":" + seconds;
  182. return time;
  183. }
  184. /**
  185. * Restore hero message from Db.
  186. * @param charId
  187. */
  188. public void loadMessage(int charId)
  189. {
  190. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  191. PreparedStatement statement = con.prepareStatement("SELECT message FROM heroes WHERE charId=?"))
  192. {
  193. statement.setInt(1, charId);
  194. try (ResultSet rset = statement.executeQuery())
  195. {
  196. if (rset.next())
  197. {
  198. _heroMessage.put(charId, rset.getString("message"));
  199. }
  200. }
  201. }
  202. catch (SQLException e)
  203. {
  204. _log.log(Level.WARNING, "Hero System: Couldnt load Hero Message for CharId: " + charId, e);
  205. }
  206. }
  207. public void loadDiary(int charId)
  208. {
  209. final List<StatsSet> _diary = new FastList<>();
  210. int diaryentries = 0;
  211. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  212. PreparedStatement statement = con.prepareStatement("SELECT * FROM heroes_diary WHERE charId=? ORDER BY time ASC"))
  213. {
  214. statement.setInt(1, charId);
  215. try (ResultSet rset = statement.executeQuery())
  216. {
  217. while (rset.next())
  218. {
  219. StatsSet _diaryentry = new StatsSet();
  220. long time = rset.getLong("time");
  221. int action = rset.getInt("action");
  222. int param = rset.getInt("param");
  223. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(time));
  224. _diaryentry.set("date", date);
  225. if (action == ACTION_RAID_KILLED)
  226. {
  227. L2NpcTemplate template = NpcTable.getInstance().getTemplate(param);
  228. if (template != null)
  229. {
  230. _diaryentry.set("action", template.getName() + " was defeated");
  231. }
  232. }
  233. else if (action == ACTION_HERO_GAINED)
  234. {
  235. _diaryentry.set("action", "Gained Hero status");
  236. }
  237. else if (action == ACTION_CASTLE_TAKEN)
  238. {
  239. Castle castle = CastleManager.getInstance().getCastleById(param);
  240. if (castle != null)
  241. {
  242. _diaryentry.set("action", castle.getName() + " Castle was successfuly taken");
  243. }
  244. }
  245. _diary.add(_diaryentry);
  246. diaryentries++;
  247. }
  248. }
  249. _herodiary.put(charId, _diary);
  250. _log.info("Hero System: Loaded " + diaryentries + " diary entries for Hero: " + CharNameTable.getInstance().getNameById(charId));
  251. }
  252. catch (SQLException e)
  253. {
  254. _log.log(Level.WARNING, "Hero System: Couldnt load Hero Diary for CharId: " + charId, e);
  255. }
  256. }
  257. public void loadFights(int charId)
  258. {
  259. final List<StatsSet> _fights = new FastList<>();
  260. StatsSet _herocountdata = new StatsSet();
  261. Calendar _data = Calendar.getInstance();
  262. _data.set(Calendar.DAY_OF_MONTH, 1);
  263. _data.set(Calendar.HOUR_OF_DAY, 0);
  264. _data.set(Calendar.MINUTE, 0);
  265. _data.set(Calendar.MILLISECOND, 0);
  266. long from = _data.getTimeInMillis();
  267. int numberoffights = 0;
  268. int _victorys = 0;
  269. int _losses = 0;
  270. int _draws = 0;
  271. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  272. PreparedStatement statement = con.prepareStatement("SELECT * FROM olympiad_fights WHERE (charOneId=? OR charTwoId=?) AND start<? ORDER BY start ASC"))
  273. {
  274. statement.setInt(1, charId);
  275. statement.setInt(2, charId);
  276. statement.setLong(3, from);
  277. try (ResultSet rset = statement.executeQuery())
  278. {
  279. int charOneId;
  280. int charOneClass;
  281. int charTwoId;
  282. int charTwoClass;
  283. int winner;
  284. long start;
  285. int time;
  286. int classed;
  287. while (rset.next())
  288. {
  289. charOneId = rset.getInt("charOneId");
  290. charOneClass = rset.getInt("charOneClass");
  291. charTwoId = rset.getInt("charTwoId");
  292. charTwoClass = rset.getInt("charTwoClass");
  293. winner = rset.getInt("winner");
  294. start = rset.getLong("start");
  295. time = rset.getInt("time");
  296. classed = rset.getInt("classed");
  297. if (charId == charOneId)
  298. {
  299. String name = CharNameTable.getInstance().getNameById(charTwoId);
  300. String cls = ClassListData.getInstance().getClass(charTwoClass).getClientCode();
  301. if ((name != null) && (cls != null))
  302. {
  303. StatsSet fight = new StatsSet();
  304. fight.set("oponent", name);
  305. fight.set("oponentclass", cls);
  306. fight.set("time", calcFightTime(time));
  307. String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(start));
  308. fight.set("start", date);
  309. fight.set("classed", classed);
  310. if (winner == 1)
  311. {
  312. fight.set("result", "<font color=\"00ff00\">victory</font>");
  313. _victorys++;
  314. }
  315. else if (winner == 2)
  316. {
  317. fight.set("result", "<font color=\"ff0000\">loss</font>");
  318. _losses++;
  319. }
  320. else if (winner == 0)
  321. {
  322. fight.set("result", "<font color=\"ffff00\">draw</font>");
  323. _draws++;
  324. }
  325. _fights.add(fight);
  326. numberoffights++;
  327. }
  328. }
  329. else if (charId == charTwoId)
  330. {
  331. String name = CharNameTable.getInstance().getNameById(charOneId);
  332. String cls = ClassListData.getInstance().getClass(charOneClass).getClientCode();
  333. if ((name != null) && (cls != null))
  334. {
  335. StatsSet fight = new StatsSet();
  336. fight.set("oponent", name);
  337. fight.set("oponentclass", cls);
  338. fight.set("time", calcFightTime(time));
  339. String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(start));
  340. fight.set("start", date);
  341. fight.set("classed", classed);
  342. if (winner == 1)
  343. {
  344. fight.set("result", "<font color=\"ff0000\">loss</font>");
  345. _losses++;
  346. }
  347. else if (winner == 2)
  348. {
  349. fight.set("result", "<font color=\"00ff00\">victory</font>");
  350. _victorys++;
  351. }
  352. else if (winner == 0)
  353. {
  354. fight.set("result", "<font color=\"ffff00\">draw</font>");
  355. _draws++;
  356. }
  357. _fights.add(fight);
  358. numberoffights++;
  359. }
  360. }
  361. }
  362. }
  363. _herocountdata.set("victory", _victorys);
  364. _herocountdata.set("draw", _draws);
  365. _herocountdata.set("loss", _losses);
  366. _herocounts.put(charId, _herocountdata);
  367. _herofights.put(charId, _fights);
  368. _log.info("Hero System: Loaded " + numberoffights + " fights for Hero: " + CharNameTable.getInstance().getNameById(charId));
  369. }
  370. catch (SQLException e)
  371. {
  372. _log.log(Level.WARNING, "Hero System: Couldnt load Hero fights history for CharId: " + charId, e);
  373. }
  374. }
  375. public Map<Integer, StatsSet> getHeroes()
  376. {
  377. return _heroes;
  378. }
  379. public int getHeroByClass(int classid)
  380. {
  381. for (Entry<Integer, StatsSet> e : _heroes.entrySet())
  382. {
  383. if (e.getValue().getInteger(Olympiad.CLASS_ID) == classid)
  384. {
  385. return e.getKey();
  386. }
  387. }
  388. return 0;
  389. }
  390. public void resetData()
  391. {
  392. _herodiary.clear();
  393. _herofights.clear();
  394. _herocounts.clear();
  395. _heroMessage.clear();
  396. }
  397. public void showHeroDiary(L2PcInstance activeChar, int heroclass, int charid, int page)
  398. {
  399. final int perpage = 10;
  400. if (_herodiary.containsKey(charid))
  401. {
  402. List<StatsSet> _mainlist = _herodiary.get(charid);
  403. NpcHtmlMessage DiaryReply = new NpcHtmlMessage(5);
  404. final String htmContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/olympiad/herodiary.htm");
  405. if ((htmContent != null) && _heroMessage.containsKey(charid))
  406. {
  407. DiaryReply.setHtml(htmContent);
  408. DiaryReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
  409. DiaryReply.replace("%message%", _heroMessage.get(charid));
  410. DiaryReply.disableValidation();
  411. if (!_mainlist.isEmpty())
  412. {
  413. FastList<StatsSet> _list = FastList.newInstance();
  414. _list.addAll(_mainlist);
  415. Collections.reverse(_list);
  416. boolean color = true;
  417. final StringBuilder fList = new StringBuilder(500);
  418. int counter = 0;
  419. int breakat = 0;
  420. for (int i = ((page - 1) * perpage); i < _list.size(); i++)
  421. {
  422. breakat = i;
  423. StatsSet _diaryentry = _list.get(i);
  424. StringUtil.append(fList, "<tr><td>");
  425. if (color)
  426. {
  427. StringUtil.append(fList, "<table width=270 bgcolor=\"131210\">");
  428. }
  429. else
  430. {
  431. StringUtil.append(fList, "<table width=270>");
  432. }
  433. StringUtil.append(fList, "<tr><td width=270><font color=\"LEVEL\">" + _diaryentry.getString("date") + ":xx</font></td></tr>");
  434. StringUtil.append(fList, "<tr><td width=270>" + _diaryentry.getString("action") + "</td></tr>");
  435. StringUtil.append(fList, "<tr><td>&nbsp;</td></tr></table>");
  436. StringUtil.append(fList, "</td></tr>");
  437. color = !color;
  438. counter++;
  439. if (counter >= perpage)
  440. {
  441. break;
  442. }
  443. }
  444. if (breakat < (_list.size() - 1))
  445. {
  446. 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\">");
  447. }
  448. else
  449. {
  450. DiaryReply.replace("%buttprev%", "");
  451. }
  452. if (page > 1)
  453. {
  454. 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\">");
  455. }
  456. else
  457. {
  458. DiaryReply.replace("%buttnext%", "");
  459. }
  460. DiaryReply.replace("%list%", fList.toString());
  461. FastList.recycle(_list);
  462. }
  463. else
  464. {
  465. DiaryReply.replace("%list%", "");
  466. DiaryReply.replace("%buttprev%", "");
  467. DiaryReply.replace("%buttnext%", "");
  468. }
  469. activeChar.sendPacket(DiaryReply);
  470. }
  471. }
  472. }
  473. public void showHeroFights(L2PcInstance activeChar, int heroclass, int charid, int page)
  474. {
  475. final int perpage = 20;
  476. int _win = 0;
  477. int _loss = 0;
  478. int _draw = 0;
  479. if (_herofights.containsKey(charid))
  480. {
  481. List<StatsSet> _list = _herofights.get(charid);
  482. NpcHtmlMessage FightReply = new NpcHtmlMessage(5);
  483. final String htmContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/olympiad/herohistory.htm");
  484. if (htmContent != null)
  485. {
  486. FightReply.setHtml(htmContent);
  487. FightReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
  488. FightReply.disableValidation();
  489. if (!_list.isEmpty())
  490. {
  491. if (_herocounts.containsKey(charid))
  492. {
  493. StatsSet _herocount = _herocounts.get(charid);
  494. _win = _herocount.getInteger("victory");
  495. _loss = _herocount.getInteger("loss");
  496. _draw = _herocount.getInteger("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.getInteger("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.getInteger(Olympiad.CHAR_ID);
  607. if ((_completeHeroes != null) && _completeHeroes.containsKey(charId))
  608. {
  609. StatsSet oldHero = _completeHeroes.get(charId);
  610. int count = oldHero.getInteger(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.getInteger(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.getInteger(Olympiad.CLASS_ID));
  718. insert.setInt(3, hero.getInteger(COUNT));
  719. insert.setInt(4, hero.getInteger(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.getInteger(COUNT));
  762. statement.setInt(2, hero.getInteger(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. if (_log.isLoggable(Level.SEVERE))
  835. {
  836. _log.log(Level.SEVERE, "SQL exception while saving DiaryData.", e);
  837. }
  838. }
  839. }
  840. /**
  841. * Set new hero message for hero
  842. * @param player the player instance
  843. * @param message String to set
  844. */
  845. public void setHeroMessage(L2PcInstance player, String message)
  846. {
  847. _heroMessage.put(player.getObjectId(), message);
  848. if (player.isDebug())
  849. {
  850. _log.info("Hero message for player: " + player.getName() + ":[" + player.getObjectId() + "] set to: [" + message + "]");
  851. }
  852. }
  853. /**
  854. * Update hero message in database
  855. * @param charId character objid
  856. */
  857. public void saveHeroMessage(int charId)
  858. {
  859. if (_heroMessage.get(charId) == null)
  860. {
  861. return;
  862. }
  863. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  864. PreparedStatement statement = con.prepareStatement("UPDATE heroes SET message=? WHERE charId=?;"))
  865. {
  866. statement.setString(1, _heroMessage.get(charId));
  867. statement.setInt(2, charId);
  868. statement.execute();
  869. }
  870. catch (SQLException e)
  871. {
  872. _log.log(Level.SEVERE, "SQL exception while saving HeroMessage.", e);
  873. }
  874. }
  875. private void deleteItemsInDb()
  876. {
  877. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  878. PreparedStatement statement = con.prepareStatement(DELETE_ITEMS))
  879. {
  880. statement.execute();
  881. }
  882. catch (SQLException e)
  883. {
  884. _log.log(Level.WARNING, "", e);
  885. }
  886. }
  887. /**
  888. * Saving task for {@link Hero}<BR>
  889. * Save all hero messages to DB.
  890. */
  891. public void shutdown()
  892. {
  893. for (int charId : _heroMessage.keySet())
  894. {
  895. saveHeroMessage(charId);
  896. }
  897. }
  898. /**
  899. * @param objectId the player's object Id to verify.
  900. * @return {@code true} if there are heros and the player is in the list, {@code false} otherwise.
  901. */
  902. public boolean isHero(int objectId)
  903. {
  904. return _heroes.containsKey(objectId);
  905. }
  906. private static class SingletonHolder
  907. {
  908. protected static final Hero _instance = new Hero();
  909. }
  910. }