ClanHall.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. package com.l2jserver.gameserver.model.entity;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import javolution.util.FastMap;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.L2DatabaseFactory;
  27. import com.l2jserver.gameserver.ThreadPoolManager;
  28. import com.l2jserver.gameserver.datatables.ClanTable;
  29. import com.l2jserver.gameserver.datatables.DoorTable;
  30. import com.l2jserver.gameserver.instancemanager.AuctionManager;
  31. import com.l2jserver.gameserver.instancemanager.ClanHallManager;
  32. import com.l2jserver.gameserver.model.L2Clan;
  33. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  34. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  35. import com.l2jserver.gameserver.model.zone.type.L2ClanHallZone;
  36. import com.l2jserver.gameserver.network.SystemMessageId;
  37. import com.l2jserver.gameserver.network.serverpackets.PledgeShowInfoUpdate;
  38. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  39. public class ClanHall
  40. {
  41. protected static final Logger _log = Logger.getLogger(ClanHall.class.getName());
  42. private int _clanHallId;
  43. private List<L2DoorInstance> _doors;
  44. private List<String> _doorDefault;
  45. private String _name;
  46. private int _ownerId;
  47. private int _lease;
  48. private String _desc;
  49. private String _location;
  50. protected long _paidUntil;
  51. private L2ClanHallZone _zone;
  52. private int _grade;
  53. protected final int _chRate = 604800000;
  54. protected boolean _isFree = true;
  55. private Map<Integer, ClanHallFunction> _functions;
  56. protected boolean _paid;
  57. /** Clan Hall Functions */
  58. public static final int FUNC_TELEPORT = 1;
  59. public static final int FUNC_ITEM_CREATE = 2;
  60. public static final int FUNC_RESTORE_HP = 3;
  61. public static final int FUNC_RESTORE_MP = 4;
  62. public static final int FUNC_RESTORE_EXP = 5;
  63. public static final int FUNC_SUPPORT = 6;
  64. public static final int FUNC_DECO_FRONTPLATEFORM = 7;
  65. public static final int FUNC_DECO_CURTAINS = 8;
  66. public class ClanHallFunction
  67. {
  68. private int _type;
  69. private int _lvl;
  70. protected int _fee;
  71. protected int _tempFee;
  72. private long _rate;
  73. private long _endDate;
  74. protected boolean _inDebt;
  75. public boolean _cwh; // first activating clanhall function is payed from player inventory, any others from clan warehouse
  76. public ClanHallFunction(int type, int lvl, int lease, int tempLease, long rate, long time, boolean cwh)
  77. {
  78. _type = type;
  79. _lvl = lvl;
  80. _fee = lease;
  81. _tempFee = tempLease;
  82. _rate = rate;
  83. _endDate = time;
  84. initializeTask(cwh);
  85. }
  86. public int getType()
  87. {
  88. return _type;
  89. }
  90. public int getLvl()
  91. {
  92. return _lvl;
  93. }
  94. public int getLease()
  95. {
  96. return _fee;
  97. }
  98. public long getRate()
  99. {
  100. return _rate;
  101. }
  102. public long getEndTime()
  103. {
  104. return _endDate;
  105. }
  106. public void setLvl(int lvl)
  107. {
  108. _lvl = lvl;
  109. }
  110. public void setLease(int lease)
  111. {
  112. _fee = lease;
  113. }
  114. public void setEndTime(long time)
  115. {
  116. _endDate = time;
  117. }
  118. private void initializeTask(boolean cwh)
  119. {
  120. if (_isFree)
  121. return;
  122. long currentTime = System.currentTimeMillis();
  123. if (_endDate > currentTime)
  124. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), _endDate - currentTime);
  125. else
  126. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(cwh), 0);
  127. }
  128. private class FunctionTask implements Runnable
  129. {
  130. public FunctionTask(boolean cwh)
  131. {
  132. _cwh = cwh;
  133. }
  134. public void run()
  135. {
  136. try
  137. {
  138. if (_isFree)
  139. return;
  140. if (ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().getAdena() >= _fee || !_cwh)
  141. {
  142. int fee = _fee;
  143. if (getEndTime() == -1)
  144. fee = _tempFee;
  145. setEndTime(System.currentTimeMillis() + getRate());
  146. dbSave();
  147. if (_cwh)
  148. {
  149. ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().destroyItemByItemId("CH_function_fee", 57, fee, null, null);
  150. if (Config.DEBUG)
  151. _log.warning("deducted " + fee + " adena from " + getName() + " owner's cwh for function id : " + getType());
  152. }
  153. ThreadPoolManager.getInstance().scheduleGeneral(new FunctionTask(true), getRate());
  154. }
  155. else
  156. removeFunction(getType());
  157. }
  158. catch (Exception e)
  159. {
  160. _log.log(Level.SEVERE, "", e);
  161. }
  162. }
  163. }
  164. public void dbSave()
  165. {
  166. Connection con = null;
  167. try
  168. {
  169. PreparedStatement statement;
  170. con = L2DatabaseFactory.getInstance().getConnection();
  171. statement = con.prepareStatement("REPLACE INTO clanhall_functions (hall_id, type, lvl, lease, rate, endTime) VALUES (?,?,?,?,?,?)");
  172. statement.setInt(1, getId());
  173. statement.setInt(2, getType());
  174. statement.setInt(3, getLvl());
  175. statement.setInt(4, getLease());
  176. statement.setLong(5, getRate());
  177. statement.setLong(6, getEndTime());
  178. statement.execute();
  179. statement.close();
  180. }
  181. catch (Exception e)
  182. {
  183. _log.log(Level.SEVERE, "Exception: ClanHall.updateFunctions(int type, int lvl, int lease, long rate, long time, boolean addNew): "
  184. + e.getMessage(), e);
  185. }
  186. finally
  187. {
  188. L2DatabaseFactory.close(con);
  189. }
  190. }
  191. }
  192. public ClanHall(int clanHallId, String name, int ownerId, int lease, String desc, String location, long paidUntil, int Grade,
  193. boolean paid)
  194. {
  195. _clanHallId = clanHallId;
  196. _name = name;
  197. _ownerId = ownerId;
  198. if (Config.DEBUG)
  199. _log.warning("Init Owner : " + _ownerId);
  200. _lease = lease;
  201. _desc = desc;
  202. _location = location;
  203. _paidUntil = paidUntil;
  204. _grade = Grade;
  205. _paid = paid;
  206. _doorDefault = new FastList<String>();
  207. _functions = new FastMap<Integer, ClanHallFunction>();
  208. if (ownerId != 0)
  209. {
  210. _isFree = false;
  211. initialyzeTask(false);
  212. loadFunctions();
  213. }
  214. }
  215. /** Return if clanHall is paid or not */
  216. public final boolean getPaid()
  217. {
  218. return _paid;
  219. }
  220. /** Return Id Of Clan hall */
  221. public final int getId()
  222. {
  223. return _clanHallId;
  224. }
  225. /** Return name */
  226. public final String getName()
  227. {
  228. return _name;
  229. }
  230. /** Return OwnerId */
  231. public final int getOwnerId()
  232. {
  233. return _ownerId;
  234. }
  235. /** Return lease*/
  236. public final int getLease()
  237. {
  238. return _lease;
  239. }
  240. /** Return Desc */
  241. public final String getDesc()
  242. {
  243. return _desc;
  244. }
  245. /** Return Location */
  246. public final String getLocation()
  247. {
  248. return _location;
  249. }
  250. /** Return PaidUntil */
  251. public final long getPaidUntil()
  252. {
  253. return _paidUntil;
  254. }
  255. /** Return Grade */
  256. public final int getGrade()
  257. {
  258. return _grade;
  259. }
  260. /** Return all DoorInstance */
  261. public final List<L2DoorInstance> getDoors()
  262. {
  263. if (_doors == null)
  264. _doors = new FastList<L2DoorInstance>();
  265. return _doors;
  266. }
  267. /** Return Door */
  268. public final L2DoorInstance getDoor(int doorId)
  269. {
  270. if (doorId <= 0)
  271. return null;
  272. for (L2DoorInstance door : getDoors())
  273. {
  274. if (door.getDoorId() == doorId)
  275. return door;
  276. }
  277. return null;
  278. }
  279. /** Return function with id */
  280. public ClanHallFunction getFunction(int type)
  281. {
  282. if (_functions.get(type) != null)
  283. return _functions.get(type);
  284. return null;
  285. }
  286. /**
  287. * Sets this clan halls zone
  288. * @param zone
  289. */
  290. public void setZone(L2ClanHallZone zone)
  291. {
  292. _zone = zone;
  293. }
  294. /**
  295. * Return true if object is inside the zone
  296. */
  297. public boolean checkIfInZone(int x, int y, int z)
  298. {
  299. return getZone().isInsideZone(x, y, z);
  300. }
  301. /** Returns the zone of this clan hall */
  302. public L2ClanHallZone getZone()
  303. {
  304. return _zone;
  305. }
  306. /** Free this clan hall */
  307. public void free()
  308. {
  309. _ownerId = 0;
  310. _isFree = true;
  311. for (Map.Entry<Integer, ClanHallFunction> fc : _functions.entrySet())
  312. removeFunction(fc.getKey());
  313. _functions.clear();
  314. _paidUntil = 0;
  315. _paid = false;
  316. updateDb();
  317. }
  318. /** Set owner if clan hall is free */
  319. public void setOwner(L2Clan clan)
  320. {
  321. // Verify that this ClanHall is Free and Clan isn't null
  322. if (_ownerId > 0 || clan == null)
  323. return;
  324. _ownerId = clan.getClanId();
  325. _isFree = false;
  326. _paidUntil = System.currentTimeMillis();
  327. initialyzeTask(true);
  328. // Annonce to Online member new ClanHall
  329. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  330. updateDb();
  331. }
  332. /** Respawn all doors */
  333. public void spawnDoor()
  334. {
  335. spawnDoor(false);
  336. }
  337. /** Respawn all doors */
  338. public void spawnDoor(boolean isDoorWeak)
  339. {
  340. for (int i = 0; i < getDoors().size(); i++)
  341. {
  342. L2DoorInstance door = getDoors().get(i);
  343. if (door.getCurrentHp() <= 0)
  344. {
  345. door.decayMe(); // Kill current if not killed already
  346. door = DoorTable.parseList(_doorDefault.get(i), false);
  347. DoorTable.getInstance().putDoor(door); //Readd the new door to the DoorTable By Erb
  348. if (isDoorWeak)
  349. door.setCurrentHp(door.getMaxHp() / 2);
  350. door.spawnMe(door.getX(), door.getY(), door.getZ());
  351. getDoors().set(i, door);
  352. }
  353. else if (door.getOpen())
  354. door.closeMe();
  355. }
  356. }
  357. /** Open or Close Door */
  358. public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
  359. {
  360. if (activeChar != null && activeChar.getClanId() == getOwnerId())
  361. openCloseDoor(doorId, open);
  362. }
  363. public void openCloseDoor(int doorId, boolean open)
  364. {
  365. openCloseDoor(getDoor(doorId), open);
  366. }
  367. public void openCloseDoor(L2DoorInstance door, boolean open)
  368. {
  369. if (door != null)
  370. {
  371. if (open)
  372. door.openMe();
  373. else
  374. door.closeMe();
  375. }
  376. }
  377. public void openCloseDoors(L2PcInstance activeChar, boolean open)
  378. {
  379. if (activeChar != null && activeChar.getClanId() == getOwnerId())
  380. openCloseDoors(open);
  381. }
  382. public void openCloseDoors(boolean open)
  383. {
  384. for (L2DoorInstance door : getDoors())
  385. {
  386. if (door != null)
  387. {
  388. if (open)
  389. door.openMe();
  390. else
  391. door.closeMe();
  392. }
  393. }
  394. }
  395. /** Banish Foreigner */
  396. public void banishForeigners()
  397. {
  398. _zone.banishForeigners(getOwnerId());
  399. }
  400. /** Load All Functions */
  401. private void loadFunctions()
  402. {
  403. Connection con = null;
  404. try
  405. {
  406. PreparedStatement statement;
  407. ResultSet rs;
  408. con = L2DatabaseFactory.getInstance().getConnection();
  409. statement = con.prepareStatement("Select * from clanhall_functions where hall_id = ?");
  410. statement.setInt(1, getId());
  411. rs = statement.executeQuery();
  412. while (rs.next())
  413. {
  414. _functions.put(rs.getInt("type"), new ClanHallFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
  415. }
  416. rs.close();
  417. statement.close();
  418. }
  419. catch (Exception e)
  420. {
  421. _log.log(Level.SEVERE, "Exception: ClanHall.loadFunctions(): " + e.getMessage(), e);
  422. }
  423. finally
  424. {
  425. L2DatabaseFactory.close(con);
  426. }
  427. }
  428. /** Remove function In List and in DB */
  429. public void removeFunction(int functionType)
  430. {
  431. _functions.remove(functionType);
  432. Connection con = null;
  433. try
  434. {
  435. PreparedStatement statement;
  436. con = L2DatabaseFactory.getInstance().getConnection();
  437. statement = con.prepareStatement("DELETE FROM clanhall_functions WHERE hall_id=? AND type=?");
  438. statement.setInt(1, getId());
  439. statement.setInt(2, functionType);
  440. statement.execute();
  441. statement.close();
  442. }
  443. catch (Exception e)
  444. {
  445. _log.log(Level.SEVERE, "Exception: ClanHall.removeFunctions(int functionType): " + e.getMessage(), e);
  446. }
  447. finally
  448. {
  449. L2DatabaseFactory.close(con);
  450. }
  451. }
  452. public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
  453. {
  454. if (player == null)
  455. return false;
  456. if (Config.DEBUG)
  457. _log.warning("Called ClanHall.updateFunctions(int type, int lvl, int lease, long rate, boolean addNew) Owner : " + getOwnerId());
  458. if (lease > 0)
  459. {
  460. if (!player.destroyItemByItemId("Consume", 57, lease, null, true))
  461. return false;
  462. }
  463. if (addNew)
  464. _functions.put(type, new ClanHallFunction(type, lvl, lease, 0, rate, 0, false));
  465. else
  466. {
  467. if (lvl == 0 && lease == 0)
  468. removeFunction(type);
  469. else
  470. {
  471. int diffLease = lease - _functions.get(type).getLease();
  472. if (Config.DEBUG)
  473. _log.warning("Called ClanHall.updateFunctions diffLease : " + diffLease);
  474. if (diffLease > 0)
  475. {
  476. _functions.remove(type);
  477. _functions.put(type, new ClanHallFunction(type, lvl, lease, 0, rate, -1, false));
  478. }
  479. else
  480. {
  481. _functions.get(type).setLease(lease);
  482. _functions.get(type).setLvl(lvl);
  483. _functions.get(type).dbSave();
  484. }
  485. }
  486. }
  487. return true;
  488. }
  489. /** Update DB */
  490. public void updateDb()
  491. {
  492. Connection con = null;
  493. try
  494. {
  495. con = L2DatabaseFactory.getInstance().getConnection();
  496. PreparedStatement statement;
  497. statement = con.prepareStatement("UPDATE clanhall SET ownerId=?, paidUntil=?, paid=? WHERE id=?");
  498. statement.setInt(1, _ownerId);
  499. statement.setLong(2, _paidUntil);
  500. statement.setInt(3, (_paid) ? 1 : 0);
  501. statement.setInt(4, _clanHallId);
  502. statement.execute();
  503. statement.close();
  504. }
  505. catch (Exception e)
  506. {
  507. _log.log(Level.WARNING, "Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage(), e);
  508. }
  509. finally
  510. {
  511. L2DatabaseFactory.close(con);
  512. }
  513. }
  514. /** Initialize Fee Task */
  515. private void initialyzeTask(boolean forced)
  516. {
  517. long currentTime = System.currentTimeMillis();
  518. if (_paidUntil > currentTime)
  519. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - currentTime);
  520. else if (!_paid && !forced)
  521. {
  522. if (System.currentTimeMillis() + (1000 * 60 * 60 * 24) <= _paidUntil + _chRate)
  523. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), System.currentTimeMillis() + (1000 * 60 * 60 * 24));
  524. else
  525. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), (_paidUntil + _chRate) - System.currentTimeMillis());
  526. }
  527. else
  528. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), 0);
  529. }
  530. /** Fee Task */
  531. private class FeeTask implements Runnable
  532. {
  533. public FeeTask()
  534. {
  535. }
  536. public void run()
  537. {
  538. try
  539. {
  540. long _time = System.currentTimeMillis();
  541. if (_isFree)
  542. return;
  543. if(_paidUntil > _time)
  544. {
  545. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - _time);
  546. return;
  547. }
  548. L2Clan Clan = ClanTable.getInstance().getClan(getOwnerId());
  549. if (ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().getAdena() >= getLease())
  550. {
  551. if (_paidUntil != 0)
  552. {
  553. while (_paidUntil <= _time)
  554. _paidUntil += _chRate;
  555. }
  556. else
  557. _paidUntil = _time + _chRate;
  558. ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().destroyItemByItemId("CH_rental_fee", 57, getLease(), null, null);
  559. if (Config.DEBUG)
  560. _log.warning("deducted " + getLease() + " adena from " + getName() + " owner's cwh for ClanHall _paidUntil: " + _paidUntil);
  561. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - _time);
  562. _paid = true;
  563. updateDb();
  564. }
  565. else
  566. {
  567. _paid = false;
  568. if (_time > _paidUntil + _chRate)
  569. {
  570. if (ClanHallManager.getInstance().loaded())
  571. {
  572. AuctionManager.getInstance().initNPC(getId());
  573. ClanHallManager.getInstance().setFree(getId());
  574. Clan.broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.THE_CLAN_HALL_FEE_IS_ONE_WEEK_OVERDUE_THEREFORE_THE_CLAN_HALL_OWNERSHIP_HAS_BEEN_REVOKED));
  575. }
  576. else
  577. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), 3000);
  578. }
  579. else
  580. {
  581. updateDb();
  582. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PAYMENT_FOR_YOUR_CLAN_HALL_HAS_NOT_BEEN_MADE_PLEASE_MAKE_PAYMENT_TO_YOUR_CLAN_WAREHOUSE_BY_S1_TOMORROW);
  583. sm.addNumber(getLease());
  584. Clan.broadcastToOnlineMembers(sm);
  585. if (_time + (1000 * 60 * 60 * 24) <= _paidUntil + _chRate)
  586. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _time + (1000 * 60 * 60 * 24));
  587. else
  588. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), (_paidUntil + _chRate) - _time);
  589. }
  590. }
  591. }
  592. catch (Exception e)
  593. {
  594. _log.log(Level.SEVERE, "", e);
  595. }
  596. }
  597. }
  598. }