Hero.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model.entity;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Calendar;
  22. import java.util.Collections;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javolution.util.FastList;
  30. import javolution.util.FastMap;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.cache.HtmCache;
  34. import com.l2jserver.gameserver.datatables.CharNameTable;
  35. import com.l2jserver.gameserver.datatables.ClanTable;
  36. import com.l2jserver.gameserver.datatables.ClassListData;
  37. import com.l2jserver.gameserver.datatables.NpcTable;
  38. import com.l2jserver.gameserver.instancemanager.CastleManager;
  39. import com.l2jserver.gameserver.model.L2Clan;
  40. import com.l2jserver.gameserver.model.L2World;
  41. import com.l2jserver.gameserver.model.StatsSet;
  42. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  43. import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
  44. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  45. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  46. import com.l2jserver.gameserver.model.olympiad.Olympiad;
  47. import com.l2jserver.gameserver.network.SystemMessageId;
  48. import com.l2jserver.gameserver.network.serverpackets.ExBrExtraUserInfo;
  49. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  50. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  51. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  52. import com.l2jserver.gameserver.network.serverpackets.UserInfo;
  53. import com.l2jserver.util.StringUtil;
  54. /**
  55. * @author godson
  56. */
  57. public class Hero
  58. {
  59. private static final Logger _log = Logger.getLogger(Hero.class.getName());
  60. 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";
  61. 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";
  62. private static final String UPDATE_ALL = "UPDATE heroes SET played = 0";
  63. private static final String INSERT_HERO = "INSERT INTO heroes (charId, class_id, count, played) VALUES (?,?,?,?)";
  64. private static final String UPDATE_HERO = "UPDATE heroes SET count = ?, played = ? WHERE charId = ?";
  65. 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 = ?";
  66. private static final String GET_CLAN_NAME = "SELECT clan_name FROM clan_data WHERE clan_id = (SELECT clanid FROM characters WHERE charId = ?)";
  67. // delete hero items
  68. 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)";
  69. private static final Map<Integer, StatsSet> _heroes = new FastMap<>();
  70. private static final Map<Integer, StatsSet> _completeHeroes = new FastMap<>();
  71. private static final Map<Integer, StatsSet> _herocounts = new FastMap<>();
  72. private static final Map<Integer, List<StatsSet>> _herofights = new FastMap<>();
  73. private static final Map<Integer, List<StatsSet>> _herodiary = new FastMap<>();
  74. private static final Map<Integer, String> _heroMessage = new FastMap<>();
  75. public static final String COUNT = "count";
  76. public static final String PLAYED = "played";
  77. public static final String CLAN_NAME = "clan_name";
  78. public static final String CLAN_CREST = "clan_crest";
  79. public static final String ALLY_NAME = "ally_name";
  80. public static final String ALLY_CREST = "ally_crest";
  81. public static final int ACTION_RAID_KILLED = 1;
  82. public static final int ACTION_HERO_GAINED = 2;
  83. public static final int ACTION_CASTLE_TAKEN = 3;
  84. public static Hero getInstance()
  85. {
  86. return SingletonHolder._instance;
  87. }
  88. protected Hero()
  89. {
  90. init();
  91. }
  92. private void init()
  93. {
  94. _heroes.clear();
  95. _completeHeroes.clear();
  96. _herocounts.clear();
  97. _herofights.clear();
  98. _herodiary.clear();
  99. _heroMessage.clear();
  100. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  101. {
  102. PreparedStatement statement = con.prepareStatement(GET_HEROES);
  103. ResultSet rset = statement.executeQuery();
  104. PreparedStatement statement2 = con.prepareStatement(GET_CLAN_ALLY);
  105. while (rset.next())
  106. {
  107. StatsSet hero = new StatsSet();
  108. int charId = rset.getInt(Olympiad.CHAR_ID);
  109. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  110. hero.set(Olympiad.CLASS_ID, rset.getInt(Olympiad.CLASS_ID));
  111. hero.set(COUNT, rset.getInt(COUNT));
  112. hero.set(PLAYED, rset.getInt(PLAYED));
  113. loadFights(charId);
  114. loadDiary(charId);
  115. loadMessage(charId);
  116. processHeros(statement2, charId, hero);
  117. _heroes.put(charId, hero);
  118. }
  119. rset.close();
  120. statement.close();
  121. statement = con.prepareStatement(GET_ALL_HEROES);
  122. rset = statement.executeQuery();
  123. while (rset.next())
  124. {
  125. StatsSet hero = new StatsSet();
  126. int charId = rset.getInt(Olympiad.CHAR_ID);
  127. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  128. hero.set(Olympiad.CLASS_ID, rset.getInt(Olympiad.CLASS_ID));
  129. hero.set(COUNT, rset.getInt(COUNT));
  130. hero.set(PLAYED, rset.getInt(PLAYED));
  131. processHeros(statement2, charId, hero);
  132. _completeHeroes.put(charId, hero);
  133. }
  134. statement2.close();
  135. rset.close();
  136. statement.close();
  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. ResultSet rs = ps.executeQuery();
  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. rs.close();
  173. ps.clearParameters();
  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. {
  192. String message = null;
  193. PreparedStatement statement = con.prepareStatement("SELECT message FROM heroes WHERE charId=?");
  194. statement.setInt(1, charId);
  195. ResultSet rset = statement.executeQuery();
  196. rset.next();
  197. message = rset.getString("message");
  198. _heroMessage.put(charId, message);
  199. rset.close();
  200. statement.close();
  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. {
  213. PreparedStatement statement = con.prepareStatement("SELECT * FROM heroes_diary WHERE charId=? ORDER BY time ASC");
  214. statement.setInt(1, charId);
  215. ResultSet rset = statement.executeQuery();
  216. while (rset.next())
  217. {
  218. StatsSet _diaryentry = new StatsSet();
  219. long time = rset.getLong("time");
  220. int action = rset.getInt("action");
  221. int param = rset.getInt("param");
  222. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(time));
  223. _diaryentry.set("date", date);
  224. if (action == ACTION_RAID_KILLED)
  225. {
  226. L2NpcTemplate template = NpcTable.getInstance().getTemplate(param);
  227. if (template != null)
  228. {
  229. _diaryentry.set("action", template.getName() + " was defeated");
  230. }
  231. }
  232. else if (action == ACTION_HERO_GAINED)
  233. {
  234. _diaryentry.set("action", "Gained Hero status");
  235. }
  236. else if (action == ACTION_CASTLE_TAKEN)
  237. {
  238. Castle castle = CastleManager.getInstance().getCastleById(param);
  239. if (castle != null)
  240. {
  241. _diaryentry.set("action", castle.getName() + " Castle was successfuly taken");
  242. }
  243. }
  244. _diary.add(_diaryentry);
  245. diaryentries++;
  246. }
  247. rset.close();
  248. statement.close();
  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. {
  273. PreparedStatement statement = con.prepareStatement("SELECT * FROM olympiad_fights WHERE (charOneId=? OR charTwoId=?) AND start<? ORDER BY start ASC");
  274. statement.setInt(1, charId);
  275. statement.setInt(2, charId);
  276. statement.setLong(3, from);
  277. ResultSet rset = statement.executeQuery();
  278. int charOneId;
  279. int charOneClass;
  280. int charTwoId;
  281. int charTwoClass;
  282. int winner;
  283. long start;
  284. int time;
  285. int classed;
  286. while (rset.next())
  287. {
  288. charOneId = rset.getInt("charOneId");
  289. charOneClass = rset.getInt("charOneClass");
  290. charTwoId = rset.getInt("charTwoId");
  291. charTwoClass = rset.getInt("charTwoClass");
  292. winner = rset.getInt("winner");
  293. start = rset.getLong("start");
  294. time = rset.getInt("time");
  295. classed = rset.getInt("classed");
  296. if (charId == charOneId)
  297. {
  298. String name = CharNameTable.getInstance().getNameById(charTwoId);
  299. String cls = ClassListData.getInstance().getClass(charTwoClass).getClientCode();
  300. if ((name != null) && (cls != null))
  301. {
  302. StatsSet fight = new StatsSet();
  303. fight.set("oponent", name);
  304. fight.set("oponentclass", cls);
  305. fight.set("time", calcFightTime(time));
  306. String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(start));
  307. fight.set("start", date);
  308. fight.set("classed", classed);
  309. if (winner == 1)
  310. {
  311. fight.set("result", "<font color=\"00ff00\">victory</font>");
  312. _victorys++;
  313. }
  314. else if (winner == 2)
  315. {
  316. fight.set("result", "<font color=\"ff0000\">loss</font>");
  317. _losses++;
  318. }
  319. else if (winner == 0)
  320. {
  321. fight.set("result", "<font color=\"ffff00\">draw</font>");
  322. _draws++;
  323. }
  324. _fights.add(fight);
  325. numberoffights++;
  326. }
  327. }
  328. else if (charId == charTwoId)
  329. {
  330. String name = CharNameTable.getInstance().getNameById(charOneId);
  331. String cls = ClassListData.getInstance().getClass(charOneClass).getClientCode();
  332. if ((name != null) && (cls != null))
  333. {
  334. StatsSet fight = new StatsSet();
  335. fight.set("oponent", name);
  336. fight.set("oponentclass", cls);
  337. fight.set("time", calcFightTime(time));
  338. String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date(start));
  339. fight.set("start", date);
  340. fight.set("classed", classed);
  341. if (winner == 1)
  342. {
  343. fight.set("result", "<font color=\"ff0000\">loss</font>");
  344. _losses++;
  345. }
  346. else if (winner == 2)
  347. {
  348. fight.set("result", "<font color=\"00ff00\">victory</font>");
  349. _victorys++;
  350. }
  351. else if (winner == 0)
  352. {
  353. fight.set("result", "<font color=\"ffff00\">draw</font>");
  354. _draws++;
  355. }
  356. _fights.add(fight);
  357. numberoffights++;
  358. }
  359. }
  360. }
  361. rset.close();
  362. statement.close();
  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. {
  664. PreparedStatement statement = con.prepareStatement(GET_CLAN_NAME);
  665. statement.setInt(1, charId);
  666. ResultSet rset = statement.executeQuery();
  667. if (rset.next())
  668. {
  669. String clanName = rset.getString("clan_name");
  670. if (clanName != null)
  671. {
  672. L2Clan clan = ClanTable.getInstance().getClanByName(clanName);
  673. if (clan != null)
  674. {
  675. clan.addReputationScore(Config.HERO_POINTS, true);
  676. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_MEMBER_C1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  677. sm.addString(CharNameTable.getInstance().getNameById(charId));
  678. sm.addNumber(Config.HERO_POINTS);
  679. clan.broadcastToOnlineMembers(sm);
  680. }
  681. }
  682. }
  683. rset.close();
  684. statement.close();
  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. // _herofights = new FastMap<Integer, List<StatsSet>>();
  696. // _herocounts = new FastMap<Integer, StatsSet>();
  697. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  698. {
  699. PreparedStatement statement;
  700. if (setDefault)
  701. {
  702. statement = con.prepareStatement(UPDATE_ALL);
  703. statement.execute();
  704. statement.close();
  705. }
  706. else
  707. {
  708. StatsSet hero;
  709. int heroId;
  710. for (Entry<Integer, StatsSet> entry : _heroes.entrySet())
  711. {
  712. hero = entry.getValue();
  713. heroId = entry.getKey();
  714. if (_completeHeroes.isEmpty() || !_completeHeroes.containsKey(heroId))
  715. {
  716. statement = con.prepareStatement(INSERT_HERO);
  717. statement.setInt(1, heroId);
  718. statement.setInt(2, hero.getInteger(Olympiad.CLASS_ID));
  719. statement.setInt(3, hero.getInteger(COUNT));
  720. statement.setInt(4, hero.getInteger(PLAYED));
  721. statement.execute();
  722. statement.close();
  723. statement = con.prepareStatement(GET_CLAN_ALLY);
  724. statement.setInt(1, heroId);
  725. ResultSet rset = statement.executeQuery();
  726. if (rset.next())
  727. {
  728. int clanId = rset.getInt("clanid");
  729. int allyId = rset.getInt("allyId");
  730. String clanName = "";
  731. String allyName = "";
  732. int clanCrest = 0;
  733. int allyCrest = 0;
  734. if (clanId > 0)
  735. {
  736. clanName = ClanTable.getInstance().getClan(clanId).getName();
  737. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  738. if (allyId > 0)
  739. {
  740. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  741. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  742. }
  743. }
  744. hero.set(CLAN_CREST, clanCrest);
  745. hero.set(CLAN_NAME, clanName);
  746. hero.set(ALLY_CREST, allyCrest);
  747. hero.set(ALLY_NAME, allyName);
  748. }
  749. rset.close();
  750. statement.close();
  751. _heroes.remove(heroId);
  752. _heroes.put(heroId, hero);
  753. _completeHeroes.put(heroId, hero);
  754. }
  755. else
  756. {
  757. statement = con.prepareStatement(UPDATE_HERO);
  758. statement.setInt(1, hero.getInteger(COUNT));
  759. statement.setInt(2, hero.getInteger(PLAYED));
  760. statement.setInt(3, heroId);
  761. statement.execute();
  762. statement.close();
  763. }
  764. }
  765. }
  766. }
  767. catch (SQLException e)
  768. {
  769. _log.log(Level.WARNING, "Hero System: Couldnt update Heroes", e);
  770. }
  771. }
  772. public void setHeroGained(int charId)
  773. {
  774. setDiaryData(charId, ACTION_HERO_GAINED, 0);
  775. }
  776. public void setRBkilled(int charId, int npcId)
  777. {
  778. setDiaryData(charId, ACTION_RAID_KILLED, npcId);
  779. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  780. if (_herodiary.containsKey(charId) && (template != null))
  781. {
  782. // Get Data
  783. List<StatsSet> _list = _herodiary.get(charId);
  784. // Clear old data
  785. _herodiary.remove(charId);
  786. // Prepare new data
  787. StatsSet _diaryentry = new StatsSet();
  788. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(System.currentTimeMillis()));
  789. _diaryentry.set("date", date);
  790. _diaryentry.set("action", template.getName() + " was defeated");
  791. // Add to old list
  792. _list.add(_diaryentry);
  793. // Put new list into diary
  794. _herodiary.put(charId, _list);
  795. }
  796. }
  797. public void setCastleTaken(int charId, int castleId)
  798. {
  799. setDiaryData(charId, ACTION_CASTLE_TAKEN, castleId);
  800. Castle castle = CastleManager.getInstance().getCastleById(castleId);
  801. if ((castle != null) && _herodiary.containsKey(charId))
  802. {
  803. // Get Data
  804. List<StatsSet> _list = _herodiary.get(charId);
  805. // Clear old data
  806. _herodiary.remove(charId);
  807. // Prepare new data
  808. StatsSet _diaryentry = new StatsSet();
  809. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(System.currentTimeMillis()));
  810. _diaryentry.set("date", date);
  811. _diaryentry.set("action", castle.getName() + " Castle was successfuly taken");
  812. // Add to old list
  813. _list.add(_diaryentry);
  814. // Put new list into diary
  815. _herodiary.put(charId, _list);
  816. }
  817. }
  818. public void setDiaryData(int charId, int action, int param)
  819. {
  820. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  821. {
  822. PreparedStatement statement = con.prepareStatement("INSERT INTO heroes_diary (charId, time, action, param) values(?,?,?,?)");
  823. statement.setInt(1, charId);
  824. statement.setLong(2, System.currentTimeMillis());
  825. statement.setInt(3, action);
  826. statement.setInt(4, param);
  827. statement.execute();
  828. statement.close();
  829. }
  830. catch (SQLException e)
  831. {
  832. if (_log.isLoggable(Level.SEVERE))
  833. {
  834. _log.log(Level.SEVERE, "SQL exception while saving DiaryData.", e);
  835. }
  836. }
  837. }
  838. /**
  839. * Set new hero message for hero
  840. * @param player the player instance
  841. * @param message String to set
  842. */
  843. public void setHeroMessage(L2PcInstance player, String message)
  844. {
  845. _heroMessage.put(player.getObjectId(), message);
  846. if (player.isDebug())
  847. {
  848. _log.info("Hero message for player: " + player.getName() + ":[" + player.getObjectId() + "] set to: [" + message + "]");
  849. }
  850. }
  851. /**
  852. * Update hero message in database
  853. * @param charId character objid
  854. */
  855. public void saveHeroMessage(int charId)
  856. {
  857. if (_heroMessage.get(charId) == null)
  858. {
  859. return;
  860. }
  861. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  862. {
  863. PreparedStatement statement = con.prepareStatement("UPDATE heroes SET message=? WHERE charId=?;");
  864. statement.setString(1, _heroMessage.get(charId));
  865. statement.setInt(2, charId);
  866. statement.execute();
  867. statement.close();
  868. }
  869. catch (SQLException e)
  870. {
  871. _log.log(Level.SEVERE, "SQL exception while saving HeroMessage.", e);
  872. }
  873. }
  874. private void deleteItemsInDb()
  875. {
  876. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  877. {
  878. PreparedStatement statement = con.prepareStatement(DELETE_ITEMS);
  879. statement.execute();
  880. statement.close();
  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. }