ClanHall.java 12 KB

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