ClanHall.java 12 KB

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