ClanHall.java 12 KB

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