Couple.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.Calendar;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.idfactory.IdFactory;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. /**
  26. * @author evill33t
  27. *
  28. */
  29. public class Couple
  30. {
  31. private static final Logger _log = Logger.getLogger(Couple.class.getName());
  32. // =========================================================
  33. // Data Field
  34. private int _Id = 0;
  35. private int _player1Id = 0;
  36. private int _player2Id = 0;
  37. private boolean _maried = false;
  38. private Calendar _affiancedDate;
  39. private Calendar _weddingDate;
  40. // =========================================================
  41. // Constructor
  42. public Couple(int coupleId)
  43. {
  44. _Id = coupleId;
  45. Connection con = null;
  46. try
  47. {
  48. PreparedStatement statement;
  49. ResultSet rs;
  50. con = L2DatabaseFactory.getInstance().getConnection();
  51. statement = con.prepareStatement("SELECT * FROM mods_wedding WHERE id = ?");
  52. statement.setInt(1, _Id);
  53. rs = statement.executeQuery();
  54. while (rs.next())
  55. {
  56. _player1Id = rs.getInt("player1Id");
  57. _player2Id = rs.getInt("player2Id");
  58. _maried = rs.getBoolean("married");
  59. _affiancedDate = Calendar.getInstance();
  60. _affiancedDate.setTimeInMillis(rs.getLong("affianceDate"));
  61. _weddingDate = Calendar.getInstance();
  62. _weddingDate.setTimeInMillis(rs.getLong("weddingDate"));
  63. }
  64. rs.close();
  65. statement.close();
  66. }
  67. catch (Exception e)
  68. {
  69. _log.log(Level.SEVERE, "Exception: Couple.load(): " + e.getMessage(), e);
  70. }
  71. finally
  72. {
  73. L2DatabaseFactory.close(con);
  74. }
  75. }
  76. public Couple(L2PcInstance player1, L2PcInstance player2)
  77. {
  78. int _tempPlayer1Id = player1.getObjectId();
  79. int _tempPlayer2Id = player2.getObjectId();
  80. _player1Id = _tempPlayer1Id;
  81. _player2Id = _tempPlayer2Id;
  82. _affiancedDate = Calendar.getInstance();
  83. _affiancedDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
  84. _weddingDate = Calendar.getInstance();
  85. _weddingDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
  86. Connection con = null;
  87. try
  88. {
  89. con = L2DatabaseFactory.getInstance().getConnection();
  90. PreparedStatement statement;
  91. _Id = IdFactory.getInstance().getNextId();
  92. statement = con.prepareStatement("INSERT INTO mods_wedding (id, player1Id, player2Id, married, affianceDate, weddingDate) VALUES (?, ?, ?, ?, ?, ?)");
  93. statement.setInt(1, _Id);
  94. statement.setInt(2, _player1Id);
  95. statement.setInt(3, _player2Id);
  96. statement.setBoolean(4, false);
  97. statement.setLong(5, _affiancedDate.getTimeInMillis());
  98. statement.setLong(6, _weddingDate.getTimeInMillis());
  99. statement.execute();
  100. statement.close();
  101. }
  102. catch (Exception e)
  103. {
  104. _log.log(Level.SEVERE, "Could not create couple: " + e.getMessage(), e);
  105. }
  106. finally
  107. {
  108. L2DatabaseFactory.close(con);
  109. }
  110. }
  111. public void marry()
  112. {
  113. Connection con = null;
  114. try
  115. {
  116. con = L2DatabaseFactory.getInstance().getConnection();
  117. PreparedStatement statement;
  118. statement = con.prepareStatement("UPDATE mods_wedding set married = ?, weddingDate = ? where id = ?");
  119. statement.setBoolean(1, true);
  120. _weddingDate = Calendar.getInstance();
  121. statement.setLong(2, _weddingDate.getTimeInMillis());
  122. statement.setInt(3, _Id);
  123. statement.execute();
  124. statement.close();
  125. _maried = true;
  126. }
  127. catch (Exception e)
  128. {
  129. _log.log(Level.SEVERE, "Could not marry: " + e.getMessage(), e);
  130. }
  131. finally
  132. {
  133. L2DatabaseFactory.close(con);
  134. }
  135. }
  136. public void divorce()
  137. {
  138. Connection con = null;
  139. try
  140. {
  141. con = L2DatabaseFactory.getInstance().getConnection();
  142. PreparedStatement statement;
  143. statement = con.prepareStatement("DELETE FROM mods_wedding WHERE id=?");
  144. statement.setInt(1, _Id);
  145. statement.execute();
  146. statement.close();
  147. }
  148. catch (Exception e)
  149. {
  150. _log.log(Level.SEVERE, "Exception: Couple.divorce(): " + e.getMessage(), e);
  151. }
  152. finally
  153. {
  154. L2DatabaseFactory.close(con);
  155. }
  156. }
  157. public final int getId()
  158. {
  159. return _Id;
  160. }
  161. public final int getPlayer1Id()
  162. {
  163. return _player1Id;
  164. }
  165. public final int getPlayer2Id()
  166. {
  167. return _player2Id;
  168. }
  169. public final boolean getMaried()
  170. {
  171. return _maried;
  172. }
  173. public final Calendar getAffiancedDate()
  174. {
  175. return _affiancedDate;
  176. }
  177. public final Calendar getWeddingDate()
  178. {
  179. return _weddingDate;
  180. }
  181. }