Hero.java 14 KB

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