Couple.java 5.1 KB

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