ClanHall.java 12 KB

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