Reformat and clean up "client" package
This commit is contained in:
@@ -34,103 +34,102 @@ import java.util.Set;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ronan - credits to Eric for showing the New Year opcodes and handler layout
|
||||
*/
|
||||
public class NewYearCardRecord {
|
||||
private int id;
|
||||
|
||||
private int senderId;
|
||||
private String senderName;
|
||||
private boolean senderDiscardCard;
|
||||
|
||||
private int receiverId;
|
||||
private String receiverName;
|
||||
private boolean receiverDiscardCard;
|
||||
|
||||
private final int senderId;
|
||||
private final String senderName;
|
||||
private boolean senderDiscardCard;
|
||||
|
||||
private final int receiverId;
|
||||
private final String receiverName;
|
||||
private boolean receiverDiscardCard;
|
||||
private boolean receiverReceivedCard;
|
||||
|
||||
private String stringContent;
|
||||
|
||||
private final String stringContent;
|
||||
private long dateSent = 0;
|
||||
private long dateReceived = 0;
|
||||
|
||||
|
||||
private ScheduledFuture<?> sendTask = null;
|
||||
|
||||
|
||||
public NewYearCardRecord(int senderid, String sender, int receiverid, String receiver, String message) {
|
||||
this.id = -1;
|
||||
|
||||
|
||||
this.senderId = senderid;
|
||||
this.senderName = sender;
|
||||
this.senderDiscardCard = false;
|
||||
|
||||
|
||||
this.receiverId = receiverid;
|
||||
this.receiverName = receiver;
|
||||
this.receiverDiscardCard = false;
|
||||
this.receiverReceivedCard = false;
|
||||
|
||||
|
||||
this.stringContent = message;
|
||||
|
||||
|
||||
this.dateSent = System.currentTimeMillis();
|
||||
this.dateReceived = 0;
|
||||
}
|
||||
|
||||
|
||||
private void setExtraNewYearCardRecord(int id, boolean senderDiscardCard, boolean receiverDiscardCard, boolean receiverReceivedCard, long dateSent, long dateReceived) {
|
||||
this.id = id;
|
||||
this.senderDiscardCard = senderDiscardCard;
|
||||
this.receiverDiscardCard = receiverDiscardCard;
|
||||
this.receiverReceivedCard = receiverReceivedCard;
|
||||
|
||||
|
||||
this.dateSent = dateSent;
|
||||
this.dateReceived = dateReceived;
|
||||
}
|
||||
|
||||
|
||||
public void setId(int cardid) {
|
||||
this.id = cardid;
|
||||
}
|
||||
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
|
||||
public int getSenderId() {
|
||||
return senderId;
|
||||
}
|
||||
|
||||
|
||||
public String getSenderName() {
|
||||
return senderName;
|
||||
}
|
||||
|
||||
|
||||
public boolean isSenderCardDiscarded() {
|
||||
return senderDiscardCard;
|
||||
}
|
||||
|
||||
|
||||
public int getReceiverId() {
|
||||
return receiverId;
|
||||
}
|
||||
|
||||
|
||||
public String getReceiverName() {
|
||||
return receiverName;
|
||||
}
|
||||
|
||||
|
||||
public boolean isReceiverCardDiscarded() {
|
||||
return receiverDiscardCard;
|
||||
}
|
||||
|
||||
|
||||
public boolean isReceiverCardReceived() {
|
||||
return receiverReceivedCard;
|
||||
}
|
||||
|
||||
|
||||
public String getMessage() {
|
||||
return stringContent;
|
||||
}
|
||||
|
||||
|
||||
public long getDateSent() {
|
||||
return dateSent;
|
||||
}
|
||||
|
||||
|
||||
public long getDateReceived() {
|
||||
return dateReceived;
|
||||
}
|
||||
|
||||
|
||||
public static void saveNewYearCard(NewYearCardRecord newyear) {
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO newyear VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
|
||||
@@ -138,47 +137,49 @@ public class NewYearCardRecord {
|
||||
ps.setString(2, newyear.senderName);
|
||||
ps.setInt(3, newyear.receiverId);
|
||||
ps.setString(4, newyear.receiverName);
|
||||
|
||||
|
||||
ps.setString(5, newyear.stringContent);
|
||||
|
||||
|
||||
ps.setBoolean(6, newyear.senderDiscardCard);
|
||||
ps.setBoolean(7, newyear.receiverDiscardCard);
|
||||
ps.setBoolean(8, newyear.receiverReceivedCard);
|
||||
|
||||
|
||||
ps.setLong(9, newyear.dateSent);
|
||||
ps.setLong(10, newyear.dateReceived);
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
try (ResultSet rs = ps.getGeneratedKeys()) {
|
||||
rs.next();
|
||||
newyear.id = rs.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch(SQLException sqle) {
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void updateNewYearCard(NewYearCardRecord newyear) {
|
||||
newyear.receiverReceivedCard = true;
|
||||
newyear.dateReceived = System.currentTimeMillis();
|
||||
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
try (PreparedStatement ps = con.prepareStatement("UPDATE newyear SET received=1, timereceived=? WHERE id=?")) {
|
||||
ps.setLong(1, newyear.dateReceived);
|
||||
ps.setInt(2, newyear.id);
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
}
|
||||
} catch(SQLException sqle) {
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static NewYearCardRecord loadNewYearCard(int cardid) {
|
||||
NewYearCardRecord nyc = Server.getInstance().getNewYearCard(cardid);
|
||||
if(nyc != null) return nyc;
|
||||
|
||||
if (nyc != null) {
|
||||
return nyc;
|
||||
}
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM newyear WHERE id = ?")) {
|
||||
ps.setInt(1, cardid);
|
||||
@@ -186,19 +187,19 @@ public class NewYearCardRecord {
|
||||
if (rs.next()) {
|
||||
NewYearCardRecord newyear = new NewYearCardRecord(rs.getInt("senderid"), rs.getString("sendername"), rs.getInt("receiverid"), rs.getString("receivername"), rs.getString("message"));
|
||||
newyear.setExtraNewYearCardRecord(rs.getInt("id"), rs.getBoolean("senderdiscard"), rs.getBoolean("receiverdiscard"), rs.getBoolean("received"), rs.getLong("timesent"), rs.getLong("timereceived"));
|
||||
|
||||
|
||||
Server.getInstance().setNewYearCard(newyear);
|
||||
return newyear;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(SQLException sqle) {
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static void loadPlayerNewYearCards(Character chr) {
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM newyear WHERE senderid = ? OR receiverid = ?")) {
|
||||
@@ -208,47 +209,49 @@ public class NewYearCardRecord {
|
||||
while (rs.next()) {
|
||||
NewYearCardRecord newyear = new NewYearCardRecord(rs.getInt("senderid"), rs.getString("sendername"), rs.getInt("receiverid"), rs.getString("receivername"), rs.getString("message"));
|
||||
newyear.setExtraNewYearCardRecord(rs.getInt("id"), rs.getBoolean("senderdiscard"), rs.getBoolean("receiverdiscard"), rs.getBoolean("received"), rs.getLong("timesent"), rs.getLong("timereceived"));
|
||||
|
||||
|
||||
chr.addNewYearRecord(newyear);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(SQLException sqle) {
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void printNewYearRecords(Character chr) {
|
||||
chr.dropMessage(5, "New Years: " + chr.getNewYearRecords().size());
|
||||
|
||||
for(NewYearCardRecord nyc : chr.getNewYearRecords()) {
|
||||
|
||||
for (NewYearCardRecord nyc : chr.getNewYearRecords()) {
|
||||
chr.dropMessage(5, "-------------------------------");
|
||||
|
||||
|
||||
chr.dropMessage(5, "Id: " + nyc.id);
|
||||
|
||||
|
||||
chr.dropMessage(5, "Sender id: " + nyc.senderId);
|
||||
chr.dropMessage(5, "Sender name: " + nyc.senderName);
|
||||
chr.dropMessage(5, "Sender discard: " + nyc.senderDiscardCard);
|
||||
|
||||
|
||||
chr.dropMessage(5, "Receiver id: " + nyc.receiverId);
|
||||
chr.dropMessage(5, "Receiver name: " + nyc.receiverName);
|
||||
chr.dropMessage(5, "Receiver discard: " + nyc.receiverDiscardCard);
|
||||
chr.dropMessage(5, "Received: " + nyc.receiverReceivedCard);
|
||||
|
||||
|
||||
chr.dropMessage(5, "Message: " + nyc.stringContent);
|
||||
chr.dropMessage(5, "Date sent: " + nyc.dateSent);
|
||||
chr.dropMessage(5, "Date recv: " + nyc.dateReceived);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void startNewYearCardTask() {
|
||||
if(sendTask != null) return;
|
||||
|
||||
if (sendTask != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
sendTask = TimerManager.getInstance().register(() -> {
|
||||
Server server = Server.getInstance();
|
||||
|
||||
int world = server.getCharacterWorld(receiverId);
|
||||
if(world == -1) {
|
||||
if (world == -1) {
|
||||
sendTask.cancel(false);
|
||||
sendTask = null;
|
||||
|
||||
@@ -256,32 +259,32 @@ public class NewYearCardRecord {
|
||||
}
|
||||
|
||||
Character target = server.getWorld(world).getPlayerStorage().getCharacterById(receiverId);
|
||||
if(target != null && target.isLoggedinWorld()) {
|
||||
if (target != null && target.isLoggedinWorld()) {
|
||||
target.sendPacket(PacketCreator.onNewYearCardRes(target, NewYearCardRecord.this, 0xC, 0));
|
||||
}
|
||||
}, 1000 * 60 * 60); //1 Hour
|
||||
}
|
||||
|
||||
|
||||
public void stopNewYearCardTask() {
|
||||
if(sendTask != null) {
|
||||
if (sendTask != null) {
|
||||
sendTask.cancel(false);
|
||||
sendTask = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void deleteNewYearCard(int id) {
|
||||
Server.getInstance().removeNewYearCard(id);
|
||||
|
||||
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
try (PreparedStatement ps = con.prepareStatement("DELETE FROM newyear WHERE id = ?")) {
|
||||
ps.setInt(1, id);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
} catch(SQLException sqle) {
|
||||
} catch (SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void removeAllNewYearCard(boolean send, Character chr) {
|
||||
int cid = chr.getId();
|
||||
|
||||
@@ -297,21 +300,21 @@ public class NewYearCardRecord {
|
||||
sqle.printStackTrace();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
Set<NewYearCardRecord> set = new HashSet<>(chr.getNewYearRecords());
|
||||
for(NewYearCardRecord nyc : set) {
|
||||
if(send) {
|
||||
if(nyc.senderId == cid) {
|
||||
for (NewYearCardRecord nyc : set) {
|
||||
if (send) {
|
||||
if (nyc.senderId == cid) {
|
||||
nyc.senderDiscardCard = true;
|
||||
nyc.receiverReceivedCard = false;
|
||||
|
||||
|
||||
chr.removeNewYearRecord(nyc);
|
||||
deleteNewYearCard(nyc.id);
|
||||
|
||||
|
||||
chr.getMap().broadcastMessage(PacketCreator.onNewYearCardRes(chr, nyc, 0xE, 0));
|
||||
|
||||
Character other = chr.getClient().getWorldServer().getPlayerStorage().getCharacterById(nyc.getReceiverId());
|
||||
if(other != null && other.isLoggedinWorld()) {
|
||||
if (other != null && other.isLoggedinWorld()) {
|
||||
other.removeNewYearRecord(nyc);
|
||||
other.getMap().broadcastMessage(PacketCreator.onNewYearCardRes(other, nyc, 0xE, 0));
|
||||
|
||||
@@ -319,17 +322,17 @@ public class NewYearCardRecord {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(nyc.receiverId == cid) {
|
||||
if (nyc.receiverId == cid) {
|
||||
nyc.receiverDiscardCard = true;
|
||||
nyc.receiverReceivedCard = false;
|
||||
|
||||
|
||||
chr.removeNewYearRecord(nyc);
|
||||
deleteNewYearCard(nyc.id);
|
||||
|
||||
|
||||
chr.getMap().broadcastMessage(PacketCreator.onNewYearCardRes(chr, nyc, 0xE, 0));
|
||||
|
||||
Character other = chr.getClient().getWorldServer().getPlayerStorage().getCharacterById(nyc.getSenderId());
|
||||
if(other != null && other.isLoggedinWorld()) {
|
||||
if (other != null && other.isLoggedinWorld()) {
|
||||
other.removeNewYearRecord(nyc);
|
||||
other.getMap().broadcastMessage(PacketCreator.onNewYearCardRes(other, nyc, 0xE, 0));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user