Hero.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 net.sf.l2j.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.util.List;
  24. import java.util.Map;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastMap;
  27. import net.sf.l2j.Config;
  28. import net.sf.l2j.L2DatabaseFactory;
  29. import net.sf.l2j.gameserver.Olympiad;
  30. import net.sf.l2j.gameserver.datatables.ClanTable;
  31. import net.sf.l2j.gameserver.model.L2Clan;
  32. import net.sf.l2j.gameserver.model.L2ItemInstance;
  33. import net.sf.l2j.gameserver.model.L2World;
  34. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  35. import net.sf.l2j.gameserver.network.SystemMessageId;
  36. import net.sf.l2j.gameserver.serverpackets.InventoryUpdate;
  37. import net.sf.l2j.gameserver.serverpackets.PledgeShowInfoUpdate;
  38. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  39. import net.sf.l2j.gameserver.serverpackets.UserInfo;
  40. import net.sf.l2j.gameserver.templates.L2Item;
  41. import net.sf.l2j.gameserver.templates.StatsSet;
  42. public class Hero
  43. {
  44. private static Logger _log = Logger.getLogger(Hero.class.getName());
  45. private static Hero _instance;
  46. private static final String GET_HEROES = "SELECT * FROM heroes WHERE played = 1";
  47. private static final String GET_ALL_HEROES = "SELECT * FROM heroes";
  48. private static final String UPDATE_ALL = "UPDATE heroes SET played = 0";
  49. private static final String INSERT_HERO = "INSERT INTO heroes VALUES (?,?,?,?,?)";
  50. private static final String UPDATE_HERO = "UPDATE heroes SET count = ?, played = ?" +
  51. " WHERE charId = ?";
  52. 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 " +
  53. " WHERE characters.charId = ?";
  54. private static final String GET_CLAN_NAME = "SELECT clan_name FROM clan_data WHERE clan_id = (SELECT clanid FROM characters WHERE char_name = ?)";
  55. // delete hero items
  56. private static final String DELETE_ITEMS = "DELETE FROM items WHERE item_id IN " +
  57. "(6842, 6611, 6612, 6613, 6614, 6615, 6616, 6617, 6618, 6619, 6620, 6621, 9388, 9389, 9390) " +
  58. "AND owner_id NOT IN (SELECT charId FROM characters WHERE accesslevel > 0)";
  59. private static Map<Integer, StatsSet> _heroes;
  60. private static Map<Integer, StatsSet> _completeHeroes;
  61. public static final String COUNT = "count";
  62. public static final String PLAYED = "played";
  63. public static final String CLAN_NAME = "clan_name";
  64. public static final String CLAN_CREST = "clan_crest";
  65. public static final String ALLY_NAME = "ally_name";
  66. public static final String ALLY_CREST = "ally_crest";
  67. public static Hero getInstance()
  68. {
  69. if (_instance == null)
  70. _instance = new Hero();
  71. return _instance;
  72. }
  73. public Hero()
  74. {
  75. init();
  76. }
  77. private void init()
  78. {
  79. _heroes = new FastMap<Integer, StatsSet>();
  80. _completeHeroes = new FastMap<Integer, StatsSet>();
  81. PreparedStatement statement;
  82. PreparedStatement statement2;
  83. ResultSet rset;
  84. ResultSet rset2;
  85. try
  86. {
  87. Connection con = L2DatabaseFactory.getInstance().getConnection();
  88. Connection con2 = L2DatabaseFactory.getInstance().getConnection();
  89. statement = con.prepareStatement(GET_HEROES);
  90. rset = statement.executeQuery();
  91. while (rset.next())
  92. {
  93. StatsSet hero = new StatsSet();
  94. int charId = rset.getInt(Olympiad.CHAR_ID);
  95. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  96. hero.set(Olympiad.CLASS_ID, rset.getInt(Olympiad.CLASS_ID));
  97. hero.set(COUNT, rset.getInt(COUNT));
  98. hero.set(PLAYED, rset.getInt(PLAYED));
  99. statement2 = con2.prepareStatement(GET_CLAN_ALLY);
  100. statement2.setInt(1, charId);
  101. rset2 = statement2.executeQuery();
  102. if (rset2.next())
  103. {
  104. int clanId = rset2.getInt("clanid");
  105. int allyId = rset2.getInt("allyId");
  106. String clanName = "";
  107. String allyName = "";
  108. int clanCrest = 0;
  109. int allyCrest = 0;
  110. if (clanId > 0)
  111. {
  112. clanName = ClanTable.getInstance().getClan(clanId).getName();
  113. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  114. if (allyId > 0)
  115. {
  116. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  117. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  118. }
  119. }
  120. hero.set(CLAN_CREST, clanCrest);
  121. hero.set(CLAN_NAME, clanName);
  122. hero.set(ALLY_CREST, allyCrest);
  123. hero.set(ALLY_NAME, allyName);
  124. }
  125. rset2.close();
  126. statement2.close();
  127. _heroes.put(charId, hero);
  128. }
  129. rset.close();
  130. statement.close();
  131. statement = con.prepareStatement(GET_ALL_HEROES);
  132. rset = statement.executeQuery();
  133. while (rset.next())
  134. {
  135. StatsSet hero = new StatsSet();
  136. int charId = rset.getInt(Olympiad.CHAR_ID);
  137. hero.set(Olympiad.CHAR_NAME, rset.getString(Olympiad.CHAR_NAME));
  138. hero.set(Olympiad.CLASS_ID, rset.getInt(Olympiad.CLASS_ID));
  139. hero.set(COUNT, rset.getInt(COUNT));
  140. hero.set(PLAYED, rset.getInt(PLAYED));
  141. statement2 = con2.prepareStatement(GET_CLAN_ALLY);
  142. statement2.setInt(1, charId);
  143. rset2 = statement2.executeQuery();
  144. if (rset2.next())
  145. {
  146. int clanId = rset2.getInt("clanid");
  147. int allyId = rset2.getInt("allyId");
  148. String clanName = "";
  149. String allyName = "";
  150. int clanCrest = 0;
  151. int allyCrest = 0;
  152. if (clanId > 0)
  153. {
  154. clanName = ClanTable.getInstance().getClan(clanId).getName();
  155. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  156. if (allyId > 0)
  157. {
  158. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  159. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  160. }
  161. }
  162. hero.set(CLAN_CREST, clanCrest);
  163. hero.set(CLAN_NAME, clanName);
  164. hero.set(ALLY_CREST, allyCrest);
  165. hero.set(ALLY_NAME, allyName);
  166. }
  167. rset2.close();
  168. statement2.close();
  169. _completeHeroes.put(charId, hero);
  170. }
  171. rset.close();
  172. statement.close();
  173. con.close();
  174. con2.close();
  175. } catch(SQLException e)
  176. {
  177. _log.warning("Hero System: Couldnt load Heroes");
  178. if (Config.DEBUG) e.printStackTrace();
  179. }
  180. _log.info("Hero System: Loaded " + _heroes.size() + " Heroes.");
  181. _log.info("Hero System: Loaded " + _completeHeroes.size() + " all time Heroes.");
  182. }
  183. public Map<Integer, StatsSet> getHeroes()
  184. {
  185. return _heroes;
  186. }
  187. public synchronized void computeNewHeroes(List<StatsSet> newHeroes)
  188. {
  189. updateHeroes(true);
  190. L2ItemInstance[] items;
  191. InventoryUpdate iu;
  192. if (_heroes.size() != 0)
  193. {
  194. for (StatsSet hero : _heroes.values())
  195. {
  196. String name = hero.getString(Olympiad.CHAR_NAME);
  197. L2PcInstance player = L2World.getInstance().getPlayer(name);
  198. if (player == null) continue;
  199. try
  200. {
  201. player.setHero(false);
  202. items = player.getInventory().unEquipItemInBodySlotAndRecord(L2Item.SLOT_LR_HAND);
  203. iu = new InventoryUpdate();
  204. for (L2ItemInstance item : items)
  205. {
  206. iu.addModifiedItem(item);
  207. }
  208. player.sendPacket(iu);
  209. items = player.getInventory().unEquipItemInBodySlotAndRecord(L2Item.SLOT_R_HAND);
  210. iu = new InventoryUpdate();
  211. for (L2ItemInstance item : items)
  212. {
  213. iu.addModifiedItem(item);
  214. }
  215. player.sendPacket(iu);
  216. items = player.getInventory().unEquipItemInBodySlotAndRecord(L2Item.SLOT_HAIR);
  217. iu = new InventoryUpdate();
  218. for (L2ItemInstance item : items)
  219. {
  220. iu.addModifiedItem(item);
  221. }
  222. player.sendPacket(iu);
  223. items = player.getInventory().unEquipItemInBodySlotAndRecord(L2Item.SLOT_HAIR2);
  224. iu = new InventoryUpdate();
  225. for (L2ItemInstance item : items)
  226. {
  227. iu.addModifiedItem(item);
  228. }
  229. player.sendPacket(iu);
  230. items = player.getInventory().unEquipItemInBodySlotAndRecord(L2Item.SLOT_HAIRALL);
  231. iu = new InventoryUpdate();
  232. for (L2ItemInstance item : items)
  233. {
  234. iu.addModifiedItem(item);
  235. }
  236. player.sendPacket(iu);
  237. for(L2ItemInstance item : player.getInventory().getAvailableItems(false))
  238. {
  239. if (item == null) continue;
  240. if (!item.isHeroItem()) continue;
  241. player.destroyItem("Hero", item, null, true);
  242. iu = new InventoryUpdate();
  243. iu.addRemovedItem(item);
  244. player.sendPacket(iu);
  245. }
  246. player.sendPacket(new UserInfo(player));
  247. player.broadcastUserInfo();
  248. } catch (NullPointerException e) {}
  249. }
  250. }
  251. if (newHeroes.size() == 0)
  252. {
  253. _heroes.clear();
  254. return;
  255. }
  256. Map<Integer, StatsSet> heroes = new FastMap<Integer, StatsSet>();
  257. for (StatsSet hero : newHeroes)
  258. {
  259. int charId = hero.getInteger(Olympiad.CHAR_ID);
  260. if (_completeHeroes != null && _completeHeroes.containsKey(charId))
  261. {
  262. StatsSet oldHero = _completeHeroes.get(charId);
  263. int count = oldHero.getInteger(COUNT);
  264. oldHero.set(COUNT, count + 1);
  265. oldHero.set(PLAYED, 1);
  266. heroes.put(charId, oldHero);
  267. }
  268. else
  269. {
  270. StatsSet newHero = new StatsSet();
  271. newHero.set(Olympiad.CHAR_NAME, hero.getString(Olympiad.CHAR_NAME));
  272. newHero.set(Olympiad.CLASS_ID, hero.getInteger(Olympiad.CLASS_ID));
  273. newHero.set(COUNT, 1);
  274. newHero.set(PLAYED, 1);
  275. heroes.put(charId, newHero);
  276. }
  277. }
  278. deleteItemsInDb();
  279. _heroes.clear();
  280. _heroes.putAll(heroes);
  281. heroes.clear();
  282. updateHeroes(false);
  283. for (StatsSet hero : _heroes.values())
  284. {
  285. String name = hero.getString(Olympiad.CHAR_NAME);
  286. L2PcInstance player = L2World.getInstance().getPlayer(name);
  287. if (player != null)
  288. {
  289. player.setHero(true);
  290. L2Clan clan = player.getClan();
  291. if (clan != null)
  292. {
  293. clan.setReputationScore(clan.getReputationScore()+1000, true);
  294. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  295. SystemMessage sm = new SystemMessage(SystemMessageId.CLAN_MEMBER_S1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  296. sm.addString(name);
  297. sm.addNumber(1000);
  298. clan.broadcastToOnlineMembers(sm);
  299. }
  300. player.sendPacket(new UserInfo(player));
  301. player.broadcastUserInfo();
  302. }
  303. else
  304. {
  305. java.sql.Connection con = null;
  306. try
  307. {
  308. con = L2DatabaseFactory.getInstance().getConnection();
  309. PreparedStatement statement = con.prepareStatement(GET_CLAN_NAME);
  310. statement.setString(1, name);
  311. ResultSet rset = statement.executeQuery();
  312. if (rset.next())
  313. {
  314. String clanName = rset.getString("clan_name");
  315. if (clanName != null)
  316. {
  317. L2Clan clan = ClanTable.getInstance().getClanByName(clanName);
  318. if (clan != null)
  319. {
  320. clan.setReputationScore(clan.getReputationScore()+1000, true);
  321. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  322. SystemMessage sm = new SystemMessage(SystemMessageId.CLAN_MEMBER_S1_BECAME_HERO_AND_GAINED_S2_REPUTATION_POINTS);
  323. sm.addString(name);
  324. sm.addNumber(1000);
  325. clan.broadcastToOnlineMembers(sm);
  326. }
  327. }
  328. }
  329. rset.close();
  330. statement.close();
  331. }
  332. catch (Exception e)
  333. {
  334. _log.warning("could not get clan name of " + name + ": "+e);
  335. }
  336. finally
  337. {
  338. try { con.close(); } catch (Exception e) {}
  339. }
  340. }
  341. }
  342. }
  343. public void updateHeroes(boolean setDefault)
  344. {
  345. Connection con = null;
  346. try
  347. {
  348. con = L2DatabaseFactory.getInstance().getConnection();
  349. if(setDefault)
  350. {
  351. PreparedStatement statement = con.prepareStatement(UPDATE_ALL);
  352. statement.execute();
  353. statement.close();
  354. }
  355. else
  356. {
  357. PreparedStatement statement;
  358. for (Integer heroId : _heroes.keySet())
  359. {
  360. StatsSet hero = _heroes.get(heroId);
  361. if (_completeHeroes == null || !_completeHeroes.containsKey(heroId))
  362. {
  363. statement = con.prepareStatement(INSERT_HERO);
  364. statement.setInt(1, heroId);
  365. statement.setString(2, hero.getString(Olympiad.CHAR_NAME));
  366. statement.setInt(3, hero.getInteger(Olympiad.CLASS_ID));
  367. statement.setInt(4, hero.getInteger(COUNT));
  368. statement.setInt(5, hero.getInteger(PLAYED));
  369. statement.execute();
  370. Connection con2 = L2DatabaseFactory.getInstance().getConnection();
  371. PreparedStatement statement2 = con2.prepareStatement(GET_CLAN_ALLY);
  372. statement2.setInt(1, heroId);
  373. ResultSet rset2 = statement2.executeQuery();
  374. if (rset2.next())
  375. {
  376. int clanId = rset2.getInt("clanid");
  377. int allyId = rset2.getInt("allyId");
  378. String clanName = "";
  379. String allyName = "";
  380. int clanCrest = 0;
  381. int allyCrest = 0;
  382. if (clanId > 0)
  383. {
  384. clanName = ClanTable.getInstance().getClan(clanId).getName();
  385. clanCrest = ClanTable.getInstance().getClan(clanId).getCrestId();
  386. if (allyId > 0)
  387. {
  388. allyName = ClanTable.getInstance().getClan(clanId).getAllyName();
  389. allyCrest = ClanTable.getInstance().getClan(clanId).getAllyCrestId();
  390. }
  391. }
  392. hero.set(CLAN_CREST, clanCrest);
  393. hero.set(CLAN_NAME, clanName);
  394. hero.set(ALLY_CREST, allyCrest);
  395. hero.set(ALLY_NAME, allyName);
  396. }
  397. rset2.close();
  398. statement2.close();
  399. con2.close();
  400. _heroes.remove(heroId);
  401. _heroes.put(heroId, hero);
  402. _completeHeroes.put(heroId, hero);
  403. }
  404. else
  405. {
  406. statement = con.prepareStatement(UPDATE_HERO);
  407. statement.setInt(1, hero.getInteger(COUNT));
  408. statement.setInt(2, hero.getInteger(PLAYED));
  409. statement.setInt(3, heroId);
  410. statement.execute();
  411. }
  412. statement.close();
  413. }
  414. }
  415. }
  416. catch(SQLException e)
  417. {
  418. _log.warning("Hero System: Couldnt update Heroes");
  419. if (Config.DEBUG) e.printStackTrace();
  420. }
  421. finally
  422. {
  423. try{con.close();}catch(Exception e){e.printStackTrace();}
  424. }
  425. }
  426. private void deleteItemsInDb()
  427. {
  428. Connection con = null;
  429. try
  430. {
  431. con = L2DatabaseFactory.getInstance().getConnection();
  432. PreparedStatement statement = con.prepareStatement(DELETE_ITEMS);
  433. statement.execute();
  434. statement.close();
  435. }
  436. catch(SQLException e)
  437. {
  438. e.printStackTrace();
  439. }
  440. finally
  441. {
  442. try{con.close();}catch(SQLException e){e.printStackTrace();}
  443. }
  444. }
  445. }