Couple.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  41. PreparedStatement ps = con.prepareStatement("SELECT * FROM mods_wedding WHERE id = ?"))
  42. {
  43. ps.setInt(1, _Id);
  44. try (ResultSet rs = ps.executeQuery())
  45. {
  46. while (rs.next())
  47. {
  48. _player1Id = rs.getInt("player1Id");
  49. _player2Id = rs.getInt("player2Id");
  50. _maried = rs.getBoolean("married");
  51. _affiancedDate = Calendar.getInstance();
  52. _affiancedDate.setTimeInMillis(rs.getLong("affianceDate"));
  53. _weddingDate = Calendar.getInstance();
  54. _weddingDate.setTimeInMillis(rs.getLong("weddingDate"));
  55. }
  56. }
  57. }
  58. catch (Exception e)
  59. {
  60. _log.log(Level.SEVERE, "Exception: Couple.load(): " + e.getMessage(), e);
  61. }
  62. }
  63. public Couple(L2PcInstance player1, L2PcInstance player2)
  64. {
  65. int _tempPlayer1Id = player1.getObjectId();
  66. int _tempPlayer2Id = player2.getObjectId();
  67. _player1Id = _tempPlayer1Id;
  68. _player2Id = _tempPlayer2Id;
  69. _affiancedDate = Calendar.getInstance();
  70. _affiancedDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
  71. _weddingDate = Calendar.getInstance();
  72. _weddingDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
  73. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  74. PreparedStatement ps = con.prepareStatement("INSERT INTO mods_wedding (id, player1Id, player2Id, married, affianceDate, weddingDate) VALUES (?, ?, ?, ?, ?, ?)"))
  75. {
  76. _Id = IdFactory.getInstance().getNextId();
  77. ps.setInt(1, _Id);
  78. ps.setInt(2, _player1Id);
  79. ps.setInt(3, _player2Id);
  80. ps.setBoolean(4, false);
  81. ps.setLong(5, _affiancedDate.getTimeInMillis());
  82. ps.setLong(6, _weddingDate.getTimeInMillis());
  83. ps.execute();
  84. }
  85. catch (Exception e)
  86. {
  87. _log.log(Level.SEVERE, "Could not create couple: " + e.getMessage(), e);
  88. }
  89. }
  90. public void marry()
  91. {
  92. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  93. PreparedStatement ps = con.prepareStatement("UPDATE mods_wedding set married = ?, weddingDate = ? where id = ?"))
  94. {
  95. ps.setBoolean(1, true);
  96. _weddingDate = Calendar.getInstance();
  97. ps.setLong(2, _weddingDate.getTimeInMillis());
  98. ps.setInt(3, _Id);
  99. ps.execute();
  100. _maried = true;
  101. }
  102. catch (Exception e)
  103. {
  104. _log.log(Level.SEVERE, "Could not marry: " + e.getMessage(), e);
  105. }
  106. }
  107. public void divorce()
  108. {
  109. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  110. PreparedStatement ps = con.prepareStatement("DELETE FROM mods_wedding WHERE id=?"))
  111. {
  112. ps.setInt(1, _Id);
  113. ps.execute();
  114. }
  115. catch (Exception e)
  116. {
  117. _log.log(Level.SEVERE, "Exception: Couple.divorce(): " + e.getMessage(), e);
  118. }
  119. }
  120. public final int getId()
  121. {
  122. return _Id;
  123. }
  124. public final int getPlayer1Id()
  125. {
  126. return _player1Id;
  127. }
  128. public final int getPlayer2Id()
  129. {
  130. return _player2Id;
  131. }
  132. public final boolean getMaried()
  133. {
  134. return _maried;
  135. }
  136. public final Calendar getAffiancedDate()
  137. {
  138. return _affiancedDate;
  139. }
  140. public final Calendar getWeddingDate()
  141. {
  142. return _weddingDate;
  143. }
  144. }