Hero.java 33 KB

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