ClanHall.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. /** Returns the zone of this clan hall */
  295. public L2ClanHallZone getZone()
  296. {
  297. return _zone;
  298. }
  299. /** Free this clan hall */
  300. public void free()
  301. {
  302. _ownerId = 0;
  303. _isFree = true;
  304. for (Map.Entry<Integer, ClanHallFunction> fc : _functions.entrySet())
  305. removeFunction(fc.getKey());
  306. _functions.clear();
  307. _paidUntil = 0;
  308. _paid = false;
  309. updateDb();
  310. }
  311. /** Set owner if clan hall is free */
  312. public void setOwner(L2Clan clan)
  313. {
  314. // Verify that this ClanHall is Free and Clan isn't null
  315. if (_ownerId > 0 || clan == null)
  316. return;
  317. _ownerId = clan.getClanId();
  318. _isFree = false;
  319. _paidUntil = System.currentTimeMillis();
  320. initialyzeTask(true);
  321. // Annonce to Online member new ClanHall
  322. clan.broadcastToOnlineMembers(new PledgeShowInfoUpdate(clan));
  323. updateDb();
  324. }
  325. /** Respawn all doors */
  326. public void spawnDoor()
  327. {
  328. spawnDoor(false);
  329. }
  330. /** Respawn all doors */
  331. public void spawnDoor(boolean isDoorWeak)
  332. {
  333. for (int i = 0; i < getDoors().size(); i++)
  334. {
  335. L2DoorInstance door = getDoors().get(i);
  336. if (door.getCurrentHp() <= 0)
  337. {
  338. door.decayMe(); // Kill current if not killed already
  339. door = DoorTable.parseList(_doorDefault.get(i), false);
  340. DoorTable.getInstance().putDoor(door); //Readd the new door to the DoorTable By Erb
  341. if (isDoorWeak)
  342. door.setCurrentHp(door.getMaxHp() / 2);
  343. door.spawnMe(door.getX(), door.getY(), door.getZ());
  344. getDoors().set(i, door);
  345. }
  346. else if (door.getOpen())
  347. door.closeMe();
  348. }
  349. }
  350. /** Open or Close Door */
  351. public void openCloseDoor(L2PcInstance activeChar, int doorId, boolean open)
  352. {
  353. if (activeChar != null && activeChar.getClanId() == getOwnerId())
  354. openCloseDoor(doorId, open);
  355. }
  356. public void openCloseDoor(int doorId, boolean open)
  357. {
  358. openCloseDoor(getDoor(doorId), open);
  359. }
  360. public void openCloseDoor(L2DoorInstance door, boolean open)
  361. {
  362. if (door != null)
  363. {
  364. if (open)
  365. door.openMe();
  366. else
  367. door.closeMe();
  368. }
  369. }
  370. public void openCloseDoors(L2PcInstance activeChar, boolean open)
  371. {
  372. if (activeChar != null && activeChar.getClanId() == getOwnerId())
  373. openCloseDoors(open);
  374. }
  375. public void openCloseDoors(boolean open)
  376. {
  377. for (L2DoorInstance door : getDoors())
  378. {
  379. if (door != null)
  380. {
  381. if (open)
  382. door.openMe();
  383. else
  384. door.closeMe();
  385. }
  386. }
  387. }
  388. /** Banish Foreigner */
  389. public void banishForeigners()
  390. {
  391. _zone.banishForeigners(getOwnerId());
  392. }
  393. /** Load All Functions */
  394. private void loadFunctions()
  395. {
  396. Connection con = null;
  397. try
  398. {
  399. PreparedStatement statement;
  400. ResultSet rs;
  401. con = L2DatabaseFactory.getInstance().getConnection();
  402. statement = con.prepareStatement("Select * from clanhall_functions where hall_id = ?");
  403. statement.setInt(1, getId());
  404. rs = statement.executeQuery();
  405. while (rs.next())
  406. {
  407. _functions.put(rs.getInt("type"), new ClanHallFunction(rs.getInt("type"), rs.getInt("lvl"), rs.getInt("lease"), 0, rs.getLong("rate"), rs.getLong("endTime"), true));
  408. }
  409. rs.close();
  410. statement.close();
  411. }
  412. catch (Exception e)
  413. {
  414. _log.log(Level.SEVERE, "Exception: ClanHall.loadFunctions(): " + e.getMessage(), e);
  415. }
  416. finally
  417. {
  418. L2DatabaseFactory.close(con);
  419. }
  420. }
  421. /** Remove function In List and in DB */
  422. public void removeFunction(int functionType)
  423. {
  424. _functions.remove(functionType);
  425. Connection con = null;
  426. try
  427. {
  428. PreparedStatement statement;
  429. con = L2DatabaseFactory.getInstance().getConnection();
  430. statement = con.prepareStatement("DELETE FROM clanhall_functions WHERE hall_id=? AND type=?");
  431. statement.setInt(1, getId());
  432. statement.setInt(2, functionType);
  433. statement.execute();
  434. statement.close();
  435. }
  436. catch (Exception e)
  437. {
  438. _log.log(Level.SEVERE, "Exception: ClanHall.removeFunctions(int functionType): " + e.getMessage(), e);
  439. }
  440. finally
  441. {
  442. L2DatabaseFactory.close(con);
  443. }
  444. }
  445. public boolean updateFunctions(L2PcInstance player, int type, int lvl, int lease, long rate, boolean addNew)
  446. {
  447. if (player == null)
  448. return false;
  449. if (Config.DEBUG)
  450. _log.warning("Called ClanHall.updateFunctions(int type, int lvl, int lease, long rate, boolean addNew) Owner : " + getOwnerId());
  451. if (lease > 0)
  452. {
  453. if (!player.destroyItemByItemId("Consume", 57, lease, null, true))
  454. return false;
  455. }
  456. if (addNew)
  457. _functions.put(type, new ClanHallFunction(type, lvl, lease, 0, rate, 0, false));
  458. else
  459. {
  460. if (lvl == 0 && lease == 0)
  461. removeFunction(type);
  462. else
  463. {
  464. int diffLease = lease - _functions.get(type).getLease();
  465. if (Config.DEBUG)
  466. _log.warning("Called ClanHall.updateFunctions diffLease : " + diffLease);
  467. if (diffLease > 0)
  468. {
  469. _functions.remove(type);
  470. _functions.put(type, new ClanHallFunction(type, lvl, lease, 0, rate, -1, false));
  471. }
  472. else
  473. {
  474. _functions.get(type).setLease(lease);
  475. _functions.get(type).setLvl(lvl);
  476. _functions.get(type).dbSave();
  477. }
  478. }
  479. }
  480. return true;
  481. }
  482. /** Update DB */
  483. public void updateDb()
  484. {
  485. Connection con = null;
  486. try
  487. {
  488. con = L2DatabaseFactory.getInstance().getConnection();
  489. PreparedStatement statement;
  490. statement = con.prepareStatement("UPDATE clanhall SET ownerId=?, paidUntil=?, paid=? WHERE id=?");
  491. statement.setInt(1, _ownerId);
  492. statement.setLong(2, _paidUntil);
  493. statement.setInt(3, (_paid) ? 1 : 0);
  494. statement.setInt(4, _clanHallId);
  495. statement.execute();
  496. statement.close();
  497. }
  498. catch (Exception e)
  499. {
  500. _log.log(Level.WARNING, "Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage(), e);
  501. }
  502. finally
  503. {
  504. L2DatabaseFactory.close(con);
  505. }
  506. }
  507. /** Initialize Fee Task */
  508. private void initialyzeTask(boolean forced)
  509. {
  510. long currentTime = System.currentTimeMillis();
  511. if (_paidUntil > currentTime)
  512. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - currentTime);
  513. else if (!_paid && !forced)
  514. {
  515. if (System.currentTimeMillis() + (1000 * 60 * 60 * 24) <= _paidUntil + _chRate)
  516. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), System.currentTimeMillis() + (1000 * 60 * 60 * 24));
  517. else
  518. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), (_paidUntil + _chRate) - System.currentTimeMillis());
  519. }
  520. else
  521. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), 0);
  522. }
  523. /** Fee Task */
  524. private class FeeTask implements Runnable
  525. {
  526. public FeeTask()
  527. {
  528. }
  529. public void run()
  530. {
  531. try
  532. {
  533. long _time = System.currentTimeMillis();
  534. if (_isFree)
  535. return;
  536. if(_paidUntil > _time)
  537. {
  538. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - _time);
  539. return;
  540. }
  541. L2Clan Clan = ClanTable.getInstance().getClan(getOwnerId());
  542. if (ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().getAdena() >= getLease())
  543. {
  544. if (_paidUntil != 0)
  545. {
  546. while (_paidUntil <= _time)
  547. _paidUntil += _chRate;
  548. }
  549. else
  550. _paidUntil = _time + _chRate;
  551. ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().destroyItemByItemId("CH_rental_fee", 57, getLease(), null, null);
  552. if (Config.DEBUG)
  553. _log.warning("deducted " + getLease() + " adena from " + getName() + " owner's cwh for ClanHall _paidUntil: " + _paidUntil);
  554. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - _time);
  555. _paid = true;
  556. updateDb();
  557. }
  558. else
  559. {
  560. _paid = false;
  561. if (_time > _paidUntil + _chRate)
  562. {
  563. if (ClanHallManager.getInstance().loaded())
  564. {
  565. AuctionManager.getInstance().initNPC(getId());
  566. ClanHallManager.getInstance().setFree(getId());
  567. Clan.broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.THE_CLAN_HALL_FEE_IS_ONE_WEEK_OVERDUE_THEREFORE_THE_CLAN_HALL_OWNERSHIP_HAS_BEEN_REVOKED));
  568. }
  569. else
  570. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), 3000);
  571. }
  572. else
  573. {
  574. updateDb();
  575. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PAYMENT_FOR_YOUR_CLAN_HALL_HAS_NOT_BEEN_MADE_PLEASE_MAKE_PAYMENT_TO_YOUR_CLAN_WAREHOUSE_BY_S1_TOMORROW);
  576. sm.addNumber(getLease());
  577. Clan.broadcastToOnlineMembers(sm);
  578. if (_time + (1000 * 60 * 60 * 24) <= _paidUntil + _chRate)
  579. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _time + (1000 * 60 * 60 * 24));
  580. else
  581. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), (_paidUntil + _chRate) - _time);
  582. }
  583. }
  584. }
  585. catch (Exception e)
  586. {
  587. _log.log(Level.SEVERE, "", e);
  588. }
  589. }
  590. }
  591. }