ClanHall.java 12 KB

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