ClanHall.java 12 KB

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