Add NoteService to handle note operations

NoteService should be the only class with access to NoteDao;
nowhere else should NoteDao be accessed directly.

Channel dependencies are static in PacketProcessor, for now.
Ideally they would be injected in the constructor,
but since the constructor is private and I don't want to open
up that can of worms, I'll leave it like this.
At the very least, now we have a way of injecting services into
the handlers. This will make further restructuring way easier.
This commit is contained in:
P0nk
2022-12-27 10:34:55 +01:00
parent 5f1f5b7dcd
commit 389b3ad2a4
15 changed files with 199 additions and 110 deletions

View File

@@ -30,18 +30,15 @@ import client.inventory.Item;
import client.inventory.manipulator.InventoryManipulator;
import client.processor.npc.DueyProcessor;
import constants.id.ItemId;
import database.DaoException;
import database.NoteDao;
import model.Note;
import net.AbstractPacketHandler;
import net.packet.InPacket;
import net.server.Server;
import net.server.channel.Channel;
import net.server.world.World;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scripting.event.EventInstanceManager;
import server.ItemInformationProvider;
import service.NoteService;
import tools.DatabaseConnection;
import tools.PacketCreator;
import tools.Pair;
@@ -60,6 +57,12 @@ import java.sql.SQLException;
public final class RingActionHandler extends AbstractPacketHandler {
private static final Logger log = LoggerFactory.getLogger(RingActionHandler.class);
private final NoteService noteService;
public RingActionHandler(NoteService noteService) {
this.noteService = noteService;
}
private static int getEngagementBoxId(int useItemId) {
return switch (useItemId) {
case ItemId.ENGAGEMENT_BOX_MOONSTONE -> ItemId.EMPTY_ENGAGEMENT_BOX_MOONSTONE;
@@ -431,7 +434,7 @@ public final class RingActionHandler extends AbstractPacketHandler {
if (guestChr != null && guestChr.isLoggedinWorld()) {
guestChr.dropMessage(6, "[Wedding] %s".formatted(dueyMessage));
} else {
sendWeddingInvitationNote(dueyMessage, groom, name);
noteService.sendNormal(dueyMessage, groom, name);
}
Item weddingTicket = new Item(newItemId, (short) 0, (short) 1);
@@ -525,13 +528,4 @@ public final class RingActionHandler extends AbstractPacketHandler {
c.sendPacket(PacketCreator.enableActions());
}
private void sendWeddingInvitationNote(String message, String from, String to) {
Note invitationNote = Note.createNormal(message, from, to, Server.getInstance().getCurrentTime());
try {
NoteDao.save(invitationNote);
} catch (DaoException e) {
log.error("Failed to save wedding invitation note: {}", invitationNote, e);
}
}
}