Hero.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. _log.log(Level.WARNING, "", e);
  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. _log.log(Level.WARNING, "", e);
  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. _log.log(Level.WARNING, "", e);
  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. _heroMessage.clear();
  458. }
  459. public void showHeroDiary(L2PcInstance activeChar, int heroclass, int charid, int page)
  460. {
  461. final int perpage = 10;
  462. if (_herodiary.containsKey(charid))
  463. {
  464. List<StatsSet> _mainlist = _herodiary.get(charid);
  465. NpcHtmlMessage DiaryReply = new NpcHtmlMessage(5);
  466. final String htmContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(),"data/html/olympiad/herodiary.htm");
  467. if (htmContent != null)
  468. {
  469. DiaryReply.setHtml(htmContent);
  470. DiaryReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
  471. DiaryReply.replace("%message%", _heroMessage.get(charid));
  472. DiaryReply.disableValidation();
  473. if (!_mainlist.isEmpty())
  474. {
  475. FastList<StatsSet> _list = FastList.newInstance();
  476. _list.addAll(_mainlist);
  477. Collections.reverse(_list);
  478. boolean color = true;
  479. final StringBuilder fList = new StringBuilder(500);
  480. int counter = 0;
  481. int breakat = 0;
  482. for (int i = ((page - 1) * perpage); i < _list.size(); i++)
  483. {
  484. breakat = i;
  485. StatsSet _diaryentry = _list.get(i);
  486. StringUtil.append(fList, "<tr><td>");
  487. if (color)
  488. {
  489. StringUtil.append(fList, "<table width=270 bgcolor=\"131210\">");
  490. }
  491. else
  492. {
  493. StringUtil.append(fList, "<table width=270>");
  494. }
  495. StringUtil.append(fList, "<tr><td width=270><font color=\"LEVEL\">" + _diaryentry.getString("date") + ":xx</font></td></tr>");
  496. StringUtil.append(fList, "<tr><td width=270>" + _diaryentry.getString("action") + "</td></tr>");
  497. StringUtil.append(fList, "<tr><td>&nbsp;</td></tr></table>");
  498. StringUtil.append(fList, "</td></tr>");
  499. color = !color;
  500. counter++;
  501. if (counter >= perpage)
  502. {
  503. break;
  504. }
  505. }
  506. if (breakat < (_list.size() - 1))
  507. {
  508. 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\">");
  509. }
  510. else
  511. {
  512. DiaryReply.replace("%buttprev%", "");
  513. }
  514. if (page > 1)
  515. {
  516. 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\">");
  517. }
  518. else
  519. {
  520. DiaryReply.replace("%buttnext%", "");
  521. }
  522. DiaryReply.replace("%list%", fList.toString());
  523. FastList.recycle(_list);
  524. }
  525. else
  526. {
  527. DiaryReply.replace("%list%", "");
  528. DiaryReply.replace("%buttprev%", "");
  529. DiaryReply.replace("%buttnext%", "");
  530. }
  531. activeChar.sendPacket(DiaryReply);
  532. }
  533. }
  534. }
  535. public void showHeroFights(L2PcInstance activeChar, int heroclass, int charid, int page)
  536. {
  537. final int perpage = 20;
  538. int _win = 0;
  539. int _loss = 0;
  540. int _draw = 0;
  541. if (_herofights.containsKey(charid))
  542. {
  543. List<StatsSet> _list = _herofights.get(charid);
  544. NpcHtmlMessage FightReply = new NpcHtmlMessage(5);
  545. final String htmContent = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(),"data/html/olympiad/herohistory.htm");
  546. if (htmContent != null)
  547. {
  548. FightReply.setHtml(htmContent);
  549. FightReply.replace("%heroname%", CharNameTable.getInstance().getNameById(charid));
  550. FightReply.disableValidation();
  551. if (!_list.isEmpty())
  552. {
  553. if (_herocounts.containsKey(charid))
  554. {
  555. StatsSet _herocount = _herocounts.get(charid);
  556. _win = _herocount.getInteger("victory");
  557. _loss = _herocount.getInteger("loss");
  558. _draw = _herocount.getInteger("draw");
  559. }
  560. boolean color = true;
  561. final StringBuilder fList = new StringBuilder(500);
  562. int counter = 0;
  563. int breakat = 0;
  564. for (int i = ((page - 1) * perpage); i < _list.size(); i++)
  565. {
  566. breakat = i;
  567. StatsSet fight = _list.get(i);
  568. StringUtil.append(fList, "<tr><td>");
  569. if (color)
  570. {
  571. StringUtil.append(fList, "<table width=270 bgcolor=\"131210\">");
  572. }
  573. else
  574. {
  575. StringUtil.append(fList, "<table width=270>");
  576. }
  577. 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>");
  578. 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>");
  579. StringUtil.append(fList, "<tr><td colspan=2>&nbsp;</td></tr></table>");
  580. StringUtil.append(fList, "</td></tr>");
  581. color = !color;
  582. counter++;
  583. if (counter >= perpage)
  584. {
  585. break;
  586. }
  587. }
  588. if (breakat < (_list.size() - 1))
  589. {
  590. 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\">");
  591. }
  592. else
  593. {
  594. FightReply.replace("%buttprev%", "");
  595. }
  596. if (page > 1)
  597. {
  598. 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\">");
  599. }
  600. else
  601. {
  602. FightReply.replace("%buttnext%", "");
  603. }
  604. FightReply.replace("%list%", fList.toString());
  605. }
  606. else
  607. {
  608. FightReply.replace("%list%", "");
  609. FightReply.replace("%buttprev%", "");
  610. FightReply.replace("%buttnext%", "");
  611. }
  612. FightReply.replace("%win%", String.valueOf(_win));
  613. FightReply.replace("%draw%", String.valueOf(_draw));
  614. FightReply.replace("%loos%", String.valueOf(_loss));
  615. activeChar.sendPacket(FightReply);
  616. }
  617. }
  618. }
  619. public synchronized void computeNewHeroes(List<StatsSet> newHeroes)
  620. {
  621. updateHeroes(true);
  622. if (!_heroes.isEmpty())
  623. {
  624. for (StatsSet hero : _heroes.values())
  625. {
  626. String name = hero.getString(Olympiad.CHAR_NAME);
  627. L2PcInstance player = L2World.getInstance().getPlayer(name);
  628. if (player == null)
  629. {
  630. continue;
  631. }
  632. try
  633. {
  634. player.setHero(false);
  635. for (int i = 0; i < Inventory.PAPERDOLL_TOTALSLOTS; i++)
  636. {
  637. L2ItemInstance equippedItem = player.getInventory().getPaperdollItem(i);
  638. if ((equippedItem != null) && equippedItem.isHeroItem())
  639. {
  640. player.getInventory().unEquipItemInSlot(i);
  641. }
  642. }
  643. for (L2ItemInstance item : player.getInventory().getAvailableItems(false, false))
  644. {
  645. if ((item != null) && item.isHeroItem())
  646. {
  647. player.destroyItem("Hero", item, null, true);
  648. InventoryUpdate iu = new InventoryUpdate();
  649. iu.addRemovedItem(item);
  650. player.sendPacket(iu);
  651. }
  652. }
  653. player.broadcastUserInfo();
  654. }
  655. catch (NullPointerException e)
  656. {
  657. }
  658. }
  659. }
  660. if (newHeroes.isEmpty())
  661. {
  662. _heroes.clear();
  663. return;
  664. }
  665. Map<Integer, StatsSet> heroes = new FastMap<Integer, StatsSet>();
  666. for (StatsSet hero : newHeroes)
  667. {
  668. int charId = hero.getInteger(Olympiad.CHAR_ID);
  669. if ((_completeHeroes != null) && _completeHeroes.containsKey(charId))
  670. {
  671. StatsSet oldHero = _completeHeroes.get(charId);
  672. int count = oldHero.getInteger(COUNT);
  673. oldHero.set(COUNT, count + 1);
  674. oldHero.set(PLAYED, 1);
  675. heroes.put(charId, oldHero);
  676. }
  677. else
  678. {
  679. StatsSet newHero = new StatsSet();
  680. newHero.set(Olympiad.CHAR_NAME, hero.getString(Olympiad.CHAR_NAME));
  681. newHero.set(Olympiad.CLASS_ID, hero.getInteger(Olympiad.CLASS_ID));
  682. newHero.set(COUNT, 1);
  683. newHero.set(PLAYED, 1);
  684. heroes.put(charId, newHero);
  685. }
  686. }
  687. deleteItemsInDb();
  688. _heroes.clear();
  689. _heroes.putAll(heroes);
  690. heroes.clear();
  691. updateHeroes(false);
  692. for(Integer charId : _heroes.keySet())
  693. {
  694. L2PcInstance player = L2World.getInstance().getPlayer(charId);
  695. if (player != null)
  696. {
  697. player.setHero(true);
  698. L2Clan clan = player.getClan();
  699. if (clan != null)
  700. {
  701. clan.addReputationScore(Config.HERO_POINTS, true);
  702. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_MEMBER_C1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  703. sm.addString(CharNameTable.getInstance().getNameById(charId));
  704. sm.addNumber(Config.HERO_POINTS);
  705. clan.broadcastToOnlineMembers(sm);
  706. }
  707. player.sendPacket(new UserInfo(player));
  708. player.sendPacket(new ExBrExtraUserInfo(player));
  709. player.broadcastUserInfo();
  710. // Set Gained hero and reload data
  711. setHeroGained(player.getObjectId());
  712. loadFights(player.getObjectId());
  713. loadDiary(player.getObjectId());
  714. _heroMessage.put(player.getObjectId(), "");
  715. }
  716. else
  717. {
  718. // Set Gained hero and reload data
  719. setHeroGained(charId);
  720. loadFights(charId);
  721. loadDiary(charId);
  722. _heroMessage.put(charId, "");
  723. Connection con = null;
  724. try
  725. {
  726. con = L2DatabaseFactory.getInstance().getConnection();
  727. PreparedStatement statement = con.prepareStatement(GET_CLAN_NAME);
  728. statement.setInt(1, charId);
  729. ResultSet rset = statement.executeQuery();
  730. if (rset.next())
  731. {
  732. String clanName = rset.getString("clan_name");
  733. if (clanName != null)
  734. {
  735. L2Clan clan = ClanTable.getInstance().getClanByName(clanName);
  736. if (clan != null)
  737. {
  738. clan.addReputationScore(Config.HERO_POINTS, true);
  739. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.CLAN_MEMBER_C1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  740. sm.addString(CharNameTable.getInstance().getNameById(charId));
  741. sm.addNumber(Config.HERO_POINTS);
  742. clan.broadcastToOnlineMembers(sm);
  743. }
  744. }
  745. }
  746. rset.close();
  747. statement.close();
  748. }
  749. catch (Exception e)
  750. {
  751. _log.warning("could not get clan name of player with objectId:" + charId + ": " + e);
  752. }
  753. finally
  754. {
  755. L2DatabaseFactory.close(con);
  756. }
  757. }
  758. }
  759. }
  760. public void updateHeroes(boolean setDefault)
  761. {
  762. //_herofights = new FastMap<Integer, List<StatsSet>>();
  763. //_herocounts = new FastMap<Integer, StatsSet>();
  764. Connection con = null;
  765. try
  766. {
  767. con = L2DatabaseFactory.getInstance().getConnection();
  768. if (setDefault)
  769. {
  770. PreparedStatement statement = con.prepareStatement(UPDATE_ALL);
  771. statement.execute();
  772. statement.close();
  773. }
  774. else
  775. {
  776. PreparedStatement statement;
  777. for (Integer heroId : _heroes.keySet())
  778. {
  779. StatsSet hero = _heroes.get(heroId);
  780. if ((_completeHeroes == null) || !_completeHeroes.containsKey(heroId))
  781. {
  782. statement = con.prepareStatement(INSERT_HERO);
  783. statement.setInt(1, heroId);
  784. statement.setInt(2, hero.getInteger(Olympiad.CLASS_ID));
  785. statement.setInt(3, hero.getInteger(COUNT));
  786. statement.setInt(4, hero.getInteger(PLAYED));
  787. statement.execute();
  788. Connection con2 = L2DatabaseFactory.getInstance().getConnection();
  789. PreparedStatement statement2 = con2.prepareStatement(GET_CLAN_ALLY);
  790. statement2.setInt(1, heroId);
  791. ResultSet rset2 = statement2.executeQuery();
  792. if (rset2.next())
  793. {
  794. int clanId = rset2.getInt("clanid");
  795. int allyId = rset2.getInt("allyId");
  796. String clanName = "";
  797. String allyName = "";
  798. int clanCrest = 0;
  799. int allyCrest = 0;
  800. if (clanId > 0)
  801. {
  802. clanName = ClanTable.getInstance().getClan(clanId).getName();
  803. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  804. if (allyId > 0)
  805. {
  806. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  807. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  808. }
  809. }
  810. hero.set(CLAN_CREST, clanCrest);
  811. hero.set(CLAN_NAME, clanName);
  812. hero.set(ALLY_CREST, allyCrest);
  813. hero.set(ALLY_NAME, allyName);
  814. }
  815. rset2.close();
  816. statement2.close();
  817. con2.close();
  818. _heroes.remove(heroId);
  819. _heroes.put(heroId, hero);
  820. _completeHeroes.put(heroId, hero);
  821. }
  822. else
  823. {
  824. statement = con.prepareStatement(UPDATE_HERO);
  825. statement.setInt(1, hero.getInteger(COUNT));
  826. statement.setInt(2, hero.getInteger(PLAYED));
  827. statement.setInt(3, heroId);
  828. statement.execute();
  829. }
  830. statement.close();
  831. }
  832. }
  833. }
  834. catch (SQLException e)
  835. {
  836. _log.warning("Hero System: Couldnt update Heroes");
  837. if (Config.DEBUG)
  838. {
  839. _log.log(Level.WARNING, "", e);
  840. }
  841. }
  842. finally
  843. {
  844. L2DatabaseFactory.close(con);
  845. }
  846. }
  847. public void setHeroGained(int charId)
  848. {
  849. setDiaryData(charId, ACTION_HERO_GAINED, 0);
  850. }
  851. public void setRBkilled(int charId, int npcId)
  852. {
  853. setDiaryData(charId, ACTION_RAID_KILLED, npcId);
  854. L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  855. if (_herodiary.containsKey(charId) && (template != null))
  856. {
  857. // Get Data
  858. List<StatsSet> _list = _herodiary.get(charId);
  859. // Clear old data
  860. _herodiary.remove(charId);
  861. // Prepare new data
  862. StatsSet _diaryentry = new StatsSet();
  863. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(System.currentTimeMillis()));
  864. _diaryentry.set("date", date);
  865. _diaryentry.set("action", template.getName() + " was defeated");
  866. // Add to old list
  867. _list.add(_diaryentry);
  868. // Put new list into diary
  869. _herodiary.put(charId, _list);
  870. }
  871. }
  872. public void setCastleTaken(int charId, int castleId)
  873. {
  874. setDiaryData(charId, ACTION_CASTLE_TAKEN, castleId);
  875. Castle castle = CastleManager.getInstance().getCastleById(castleId);
  876. if (_herodiary.containsKey(charId) && (castle != null))
  877. {
  878. // Get Data
  879. List<StatsSet> _list = _herodiary.get(charId);
  880. // Clear old data
  881. _herodiary.remove(charId);
  882. // Prepare new data
  883. StatsSet _diaryentry = new StatsSet();
  884. String date = (new SimpleDateFormat("yyyy-MM-dd HH")).format(new Date(System.currentTimeMillis()));
  885. _diaryentry.set("date", date);
  886. _diaryentry.set("action", castle.getName() + " Castle was successfuly taken");
  887. // Add to old list
  888. _list.add(_diaryentry);
  889. // Put new list into diary
  890. _herodiary.put(charId, _list);
  891. }
  892. }
  893. public void setDiaryData(int charId, int action, int param)
  894. {
  895. Connection con = null;
  896. try
  897. {
  898. con = L2DatabaseFactory.getInstance().getConnection();
  899. PreparedStatement statement = con.prepareStatement("INSERT INTO heroes_diary (charId, time, action, param) values(?,?,?,?)");
  900. statement.setInt(1, charId);
  901. statement.setLong(2, System.currentTimeMillis());
  902. statement.setInt(3, action);
  903. statement.setInt(4, param);
  904. statement.execute();
  905. statement.close();
  906. }
  907. catch (SQLException e)
  908. {
  909. if (_log.isLoggable(Level.SEVERE))
  910. {
  911. _log.log(Level.SEVERE, "SQL exception while saving DiaryData.", e);
  912. }
  913. }
  914. finally
  915. {
  916. L2DatabaseFactory.close(con);
  917. }
  918. }
  919. /**
  920. * Set new hero message for hero
  921. * @param charId character objid
  922. * @param message String to set
  923. */
  924. public void setHeroMessage(L2PcInstance player, String message)
  925. {
  926. _heroMessage.put(player.getObjectId(), message);
  927. if (player.isDebug())
  928. _log.info("Hero message for player: "+player.getName()+":["+player.getObjectId()+"] set to: ["+message+"]");
  929. }
  930. /**
  931. * Update hero message in database
  932. * @param charId character objid
  933. */
  934. public void saveHeroMessage(int charId)
  935. {
  936. if (_heroMessage.get(charId) == null)
  937. return;
  938. Connection con = null;
  939. try
  940. {
  941. con = L2DatabaseFactory.getInstance().getConnection();
  942. PreparedStatement statement = con.prepareStatement("UPDATE heroes SET message=? WHERE charId=?;");
  943. statement.setString(1, _heroMessage.get(charId));
  944. statement.setInt(2, charId);
  945. statement.execute();
  946. statement.close();
  947. }
  948. catch (SQLException e)
  949. {
  950. _log.log(Level.SEVERE, "SQL exception while saving HeroMessage.", e);
  951. }
  952. finally
  953. {
  954. L2DatabaseFactory.close(con);
  955. }
  956. }
  957. private void deleteItemsInDb()
  958. {
  959. Connection con = null;
  960. try
  961. {
  962. con = L2DatabaseFactory.getInstance().getConnection();
  963. PreparedStatement statement = con.prepareStatement(DELETE_ITEMS);
  964. statement.execute();
  965. statement.close();
  966. }
  967. catch (SQLException e)
  968. {
  969. _log.log(Level.WARNING, "", e);
  970. }
  971. finally
  972. {
  973. L2DatabaseFactory.close(con);
  974. }
  975. }
  976. /**
  977. * Saving task for {@link Hero}<BR>
  978. * Save all hero messages to DB.
  979. */
  980. public void shutdown()
  981. {
  982. for (int charId: _heroMessage.keySet())
  983. {
  984. saveHeroMessage(charId);
  985. }
  986. }
  987. @SuppressWarnings("synthetic-access")
  988. private static class SingletonHolder
  989. {
  990. protected static final Hero _instance = new Hero();
  991. }
  992. }