Couple.java 4.6 KB

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