Hero.java 34 KB

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