ClanHall.java 12 KB

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