Hero.java 34 KB

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