Get rid of Character#sendNote, refactor usages of it

This commit is contained in:
P0nk
2022-12-26 18:01:23 +01:00
parent 6be1fabc55
commit c82881e6f2
4 changed files with 56 additions and 41 deletions

View File

@@ -8795,18 +8795,6 @@ public class Character extends AbstractCharacterObject {
return skillMacros;
}
public void sendNote(String to, String msg, byte fame) throws SQLException {
try (Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO notes (`to`, `from`, `message`, `timestamp`, `fame`) VALUES (?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, to);
ps.setString(2, this.getName());
ps.setString(3, msg);
ps.setLong(4, Server.getInstance().getCurrentTime());
ps.setByte(5, fame);
ps.executeUpdate();
}
}
public static void setAriantRoomLeader(int room, String charname) {
ariantroomleader[room] = charname;
}

View File

@@ -32,6 +32,9 @@ import client.inventory.manipulator.InventoryManipulator;
import config.YamlConfig;
import constants.id.ItemId;
import constants.inventory.ItemConstants;
import database.DaoException;
import database.NoteDao;
import model.Note;
import net.AbstractPacketHandler;
import net.packet.InPacket;
import net.server.Server;
@@ -128,11 +131,9 @@ public final class CashOperationHandler extends AbstractPacketHandler {
cs.gift(Integer.parseInt(recipient.get("id")), chr.getName(), message, cItem.getSN());
c.sendPacket(PacketCreator.showGiftSucceed(recipient.get("name"), cItem));
c.sendPacket(PacketCreator.showCash(chr));
try {
chr.sendNote(recipient.get("name"), chr.getName() + " has sent you a gift! Go check out the Cash Shop.", (byte) 0); //fame or not
} catch (SQLException ex) {
ex.printStackTrace();
}
sendGiftNotificationNote(chr.getName(), recipient.get("name"));
Character receiver = c.getChannelServer().getPlayerStorage().getCharacterByName(recipient.get("name"));
if (receiver != null) {
receiver.showNote();
@@ -330,11 +331,7 @@ public final class CashOperationHandler extends AbstractPacketHandler {
cs.gainCash(toCharge, itemRing, chr.getWorld());
cs.gift(partner.getId(), chr.getName(), text, eqp.getSN(), rings.getRight());
chr.addCrushRing(Ring.loadFromDb(rings.getLeft()));
try {
chr.sendNote(partner.getName(), text, (byte) 1);
} catch (SQLException ex) {
ex.printStackTrace();
}
sendGiftNote(text, chr.getName(), partner.getName());
partner.showNote();
}
}
@@ -393,11 +390,7 @@ public final class CashOperationHandler extends AbstractPacketHandler {
cs.gainCash(payment, -itemRing.getPrice());
cs.gift(partner.getId(), chr.getName(), text, eqp.getSN(), rings.getRight());
chr.addFriendshipRing(Ring.loadFromDb(rings.getLeft()));
try {
chr.sendNote(partner.getName(), text, (byte) 1);
} catch (SQLException ex) {
ex.printStackTrace();
}
sendGiftNote(text, chr.getName(), partner.getName());
partner.showNote();
}
}
@@ -494,4 +487,23 @@ public final class CashOperationHandler extends AbstractPacketHandler {
return false;
}
}
private void sendGiftNote(String message, String from, String to) {
Note giftNote = Note.createGift(message, from, to, Server.getInstance().getCurrentTime());
sendGiftNote(giftNote);
}
private void sendGiftNotificationNote(String from, String to) {
String message = from + " has sent you a gift! Go check out the Cash Shop.";
Note giftNotificationNote = Note.createNormal(message, from, to, Server.getInstance().getCurrentTime());
sendGiftNote(giftNotificationNote);
}
private void sendGiftNote(Note giftNote) {
try {
NoteDao.save(giftNote);
} catch (DaoException e) {
log.error("Failed to send gift note {}", giftNote, e);
}
}
}

View File

@@ -27,6 +27,7 @@ import database.NoteDao;
import model.Note;
import net.AbstractPacketHandler;
import net.packet.InPacket;
import net.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.PacketCreator;
@@ -40,20 +41,18 @@ public final class NoteActionHandler extends AbstractPacketHandler {
@Override
public void handlePacket(InPacket p, Client c) {
int action = p.readByte();
if (action == 0 && c.getPlayer().getCashShop().getAvailableNotes() > 0) {
if (action == 0 && c.getPlayer().getCashShop().getAvailableNotes() > 0) { // Reply to gift in cash shop
String charname = p.readString();
String message = p.readString();
try {
if (c.getPlayer().getCashShop().isOpened()) {
c.sendPacket(PacketCreator.showCashInventory(c));
}
c.getPlayer().sendNote(charname, message, (byte) 1);
c.getPlayer().getCashShop().decreaseNotes();
} catch (SQLException e) {
e.printStackTrace();
if (c.getPlayer().getCashShop().isOpened()) {
c.sendPacket(PacketCreator.showCashInventory(c));
}
} else if (action == 1) {
boolean sendNoteSuccess = sendGiftReplyNote(message, c.getPlayer().getName(), charname);
if (sendNoteSuccess) {
c.getPlayer().getCashShop().decreaseNotes();
}
} else if (action == 1) { // Discard notes in game
int num = p.readByte();
p.readByte();
p.readByte();
@@ -82,4 +81,16 @@ public final class NoteActionHandler extends AbstractPacketHandler {
}
}
}
private boolean sendGiftReplyNote(String message, String from, String to) {
Note giftReplyNote = Note.createGift(message, from, to, Server.getInstance().getCurrentTime());
try {
NoteDao.save(giftReplyNote);
} catch (DaoException e) {
log.error("Failed to send gift reply note {}", giftReplyNote, e);
return false;
}
return true;
}
}

View File

@@ -36,6 +36,9 @@ import constants.game.GameConstants;
import constants.id.ItemId;
import constants.id.MapId;
import constants.inventory.ItemConstants;
import database.DaoException;
import database.NoteDao;
import model.Note;
import net.AbstractPacketHandler;
import net.packet.InPacket;
import net.server.Server;
@@ -358,10 +361,11 @@ public final class UseCashItemHandler extends AbstractPacketHandler {
} else if (itemType == 509) {
String sendTo = p.readString();
String msg = p.readString();
Note note = Note.createNormal(msg, player.getName(), sendTo, Server.getInstance().getCurrentTime());
try {
player.sendNote(sendTo, msg, (byte) 0);
} catch (SQLException e) {
e.printStackTrace();
NoteDao.save(note);
} catch (DaoException e) {
log.error("Failed to save note {}", note, e);
}
remove(c, position, itemId);
} else if (itemType == 510) {