Hero.java 31 KB

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