AuctionableHall.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.clanhall;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  25. import com.l2jserver.gameserver.ThreadPoolManager;
  26. import com.l2jserver.gameserver.data.sql.impl.ClanTable;
  27. import com.l2jserver.gameserver.instancemanager.AuctionManager;
  28. import com.l2jserver.gameserver.instancemanager.ClanHallManager;
  29. import com.l2jserver.gameserver.model.L2Clan;
  30. import com.l2jserver.gameserver.model.StatsSet;
  31. import com.l2jserver.gameserver.model.entity.ClanHall;
  32. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  33. import com.l2jserver.gameserver.network.SystemMessageId;
  34. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  35. public final class AuctionableHall extends ClanHall
  36. {
  37. protected long _paidUntil;
  38. private final int _grade;
  39. protected boolean _paid;
  40. private final int _lease;
  41. private final int _chRate = 604800000;
  42. public AuctionableHall(StatsSet set)
  43. {
  44. super(set);
  45. _paidUntil = set.getLong("paidUntil");
  46. _grade = set.getInt("grade");
  47. _paid = set.getBoolean("paid");
  48. _lease = set.getInt("lease");
  49. if (getOwnerId() != 0)
  50. {
  51. _isFree = false;
  52. initialyzeTask(false);
  53. loadFunctions();
  54. }
  55. }
  56. /**
  57. * @return if clanHall is paid or not
  58. */
  59. public final boolean getPaid()
  60. {
  61. return _paid;
  62. }
  63. /** Return lease */
  64. @Override
  65. public final int getLease()
  66. {
  67. return _lease;
  68. }
  69. /** Return PaidUntil */
  70. @Override
  71. public final long getPaidUntil()
  72. {
  73. return _paidUntil;
  74. }
  75. /** Return Grade */
  76. @Override
  77. public final int getGrade()
  78. {
  79. return _grade;
  80. }
  81. @Override
  82. public final void free()
  83. {
  84. super.free();
  85. _paidUntil = 0;
  86. _paid = false;
  87. }
  88. @Override
  89. public final void setOwner(L2Clan clan)
  90. {
  91. super.setOwner(clan);
  92. _paidUntil = System.currentTimeMillis();
  93. initialyzeTask(true);
  94. }
  95. /**
  96. * Initialize Fee Task
  97. * @param forced
  98. */
  99. private final void initialyzeTask(boolean forced)
  100. {
  101. long currentTime = System.currentTimeMillis();
  102. if (_paidUntil > currentTime)
  103. {
  104. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - currentTime);
  105. }
  106. else if (!_paid && !forced)
  107. {
  108. if ((System.currentTimeMillis() + (3600000 * 24)) <= (_paidUntil + _chRate))
  109. {
  110. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), System.currentTimeMillis() + (3600000 * 24));
  111. }
  112. else
  113. {
  114. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), (_paidUntil + _chRate) - System.currentTimeMillis());
  115. }
  116. }
  117. else
  118. {
  119. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), 0);
  120. }
  121. }
  122. /** Fee Task */
  123. protected class FeeTask implements Runnable
  124. {
  125. private final Logger _log = Logger.getLogger(FeeTask.class.getName());
  126. @Override
  127. public void run()
  128. {
  129. try
  130. {
  131. long _time = System.currentTimeMillis();
  132. if (isFree())
  133. {
  134. return;
  135. }
  136. if (_paidUntil > _time)
  137. {
  138. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - _time);
  139. return;
  140. }
  141. L2Clan Clan = ClanTable.getInstance().getClan(getOwnerId());
  142. if (ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().getAdena() >= getLease())
  143. {
  144. if (_paidUntil != 0)
  145. {
  146. while (_paidUntil <= _time)
  147. {
  148. _paidUntil += _chRate;
  149. }
  150. }
  151. else
  152. {
  153. _paidUntil = _time + _chRate;
  154. }
  155. ClanTable.getInstance().getClan(getOwnerId()).getWarehouse().destroyItemByItemId("CH_rental_fee", Inventory.ADENA_ID, getLease(), null, null);
  156. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _paidUntil - _time);
  157. _paid = true;
  158. updateDb();
  159. }
  160. else
  161. {
  162. _paid = false;
  163. if (_time > (_paidUntil + _chRate))
  164. {
  165. if (ClanHallManager.getInstance().loaded())
  166. {
  167. AuctionManager.getInstance().initNPC(getId());
  168. ClanHallManager.getInstance().setFree(getId());
  169. Clan.broadcastToOnlineMembers(SystemMessage.getSystemMessage(SystemMessageId.THE_CLAN_HALL_FEE_IS_ONE_WEEK_OVERDUE_THEREFORE_THE_CLAN_HALL_OWNERSHIP_HAS_BEEN_REVOKED));
  170. }
  171. else
  172. {
  173. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), 3000);
  174. }
  175. }
  176. else
  177. {
  178. updateDb();
  179. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PAYMENT_FOR_YOUR_CLAN_HALL_HAS_NOT_BEEN_MADE_PLEASE_MAKE_PAYMENT_TO_YOUR_CLAN_WAREHOUSE_BY_S1_TOMORROW);
  180. sm.addInt(getLease());
  181. Clan.broadcastToOnlineMembers(sm);
  182. if ((_time + (3600000 * 24)) <= (_paidUntil + _chRate))
  183. {
  184. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), _time + (3600000 * 24));
  185. }
  186. else
  187. {
  188. ThreadPoolManager.getInstance().scheduleGeneral(new FeeTask(), (_paidUntil + _chRate) - _time);
  189. }
  190. }
  191. }
  192. }
  193. catch (Exception e)
  194. {
  195. _log.log(Level.SEVERE, "", e);
  196. }
  197. }
  198. }
  199. @Override
  200. public final void updateDb()
  201. {
  202. try (Connection con = ConnectionFactory.getInstance().getConnection();
  203. PreparedStatement ps = con.prepareStatement("UPDATE clanhall SET ownerId=?, paidUntil=?, paid=? WHERE id=?"))
  204. {
  205. ps.setInt(1, getOwnerId());
  206. ps.setLong(2, getPaidUntil());
  207. ps.setInt(3, (getPaid()) ? 1 : 0);
  208. ps.setInt(4, getId());
  209. ps.execute();
  210. }
  211. catch (Exception e)
  212. {
  213. _log.log(Level.WARNING, "Exception: updateOwnerInDB(L2Clan clan): " + e.getMessage(), e);
  214. }
  215. }
  216. }