Hero.java 34 KB

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