Simplify the mail handler

This commit is contained in:
KingRainbow44 2023-04-02 21:23:02 -04:00
parent ddafeb9ed3
commit 90fb606f68
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
3 changed files with 45 additions and 53 deletions

View File

@ -7,15 +7,19 @@ import dev.morphia.annotations.Transient;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.proto.*; import emu.grasscutter.net.proto.*;
import emu.grasscutter.utils.Utils; import emu.grasscutter.net.proto.EquipParamOuterClass.EquipParam;
import emu.grasscutter.net.proto.MailCollectStateOuterClass.MailCollectState;
import emu.grasscutter.net.proto.MailTextContentOuterClass.MailTextContent;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static emu.grasscutter.net.proto.MailItemOuterClass.MailItem.*;
@Entity(value = "mail", useDiscriminator = false) @Entity(value = "mail", useDiscriminator = false)
public class Mail { public final class Mail {
@Id private ObjectId id; @Id private ObjectId id;
@Indexed private int ownerUid; @Indexed private int ownerUid;
public MailContent mailContent; public MailContent mailContent;
@ -73,7 +77,7 @@ public class Mail {
.setImportance(this.importance) .setImportance(this.importance)
.setIsRead(this.isRead) .setIsRead(this.isRead)
.setIsAttachmentGot(this.isAttachmentGot) .setIsAttachmentGot(this.isAttachmentGot)
.setCollectState(MailCollectStateOuterClass.MailCollectState.MAIL_COLLECT_STATE_NOT_COLLECTIBLE) .setCollectState(MailCollectState.MAIL_COLLECT_STATE_NOT_COLLECTIBLE)
.build(); .build();
} }
@ -103,8 +107,8 @@ public class Mail {
this.sender = sender; this.sender = sender;
} }
public MailTextContentOuterClass.MailTextContent toProto() { public MailTextContent toProto() {
return MailTextContentOuterClass.MailTextContent.newBuilder() return MailTextContent.newBuilder()
.setTitle(this.title) .setTitle(this.title)
.setContent(this.content) .setContent(this.content)
.setSender(this.sender) .setSender(this.sender)
@ -139,13 +143,13 @@ public class Mail {
} }
public MailItemOuterClass.MailItem toProto() { public MailItemOuterClass.MailItem toProto() {
return MailItemOuterClass.MailItem.newBuilder() return newBuilder().setEquipParam(EquipParam.newBuilder()
.setEquipParam(Utils.make(() -> EquipParamOuterClass.EquipParam.newBuilder()
.setItemId(this.itemId) .setItemId(this.itemId)
.setItemNum(this.itemCount) .setItemNum(this.itemCount)
.setItemLevel(this.itemLevel) .setItemLevel(this.itemLevel)
.setPromoteLevel(0)//mock .setPromoteLevel(0)//mock
.build())).build(); .build())
.build();
} }
} }

View File

@ -1,47 +1,17 @@
package emu.grasscutter.server.packet.recv; package emu.grasscutter.server.packet.recv;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.GetAllMailNotifyOuterClass; import emu.grasscutter.net.proto.GetAllMailNotifyOuterClass.GetAllMailNotify;
import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketGetAllMailResultNotify; import emu.grasscutter.server.packet.send.PacketGetAllMailResultNotify;
import emu.grasscutter.utils.Utils;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Opcodes(PacketOpcodes.GetAllMailNotify) @Opcodes(PacketOpcodes.GetAllMailNotify)
public class HandlerGetAllMailNotify extends PacketHandler { public final class HandlerGetAllMailNotify extends PacketHandler {
private static final int MAX_MAIL_DATA_NUM_PER_PACKET = 40;
private static void subdivide(Player player) {
var notGiftedMails = player.getAllMail().stream().filter(mail -> mail.stateValue == 1).toList();
var packetsBeSent = notGiftedMails.size() / MAX_MAIL_DATA_NUM_PER_PACKET + 1;
var curPacketNum = new AtomicInteger(1);
for (int i = 0; i < packetsBeSent; i++) {
player.sendPacket(new PacketGetAllMailResultNotify(packetsBeSent, curPacketNum.get(), Utils.make(() -> {
var index = (curPacketNum.get() - 1) * MAX_MAIL_DATA_NUM_PER_PACKET;
return notGiftedMails.subList(index, curPacketNum.get() == packetsBeSent ? notGiftedMails.size() - 1 : index + MAX_MAIL_DATA_NUM_PER_PACKET - 1);
}).stream().map(mail -> mail.toProto(player)).toList(), createTransaction(player), false));
curPacketNum.incrementAndGet();
}
}
private static String createTransaction(Player player) {
return player.getUid() + "-" + Utils.getCurrentSeconds() + "-" + 0;//mock
}
@Override @Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
var req = GetAllMailNotifyOuterClass.GetAllMailNotify.parseFrom(payload); var req = GetAllMailNotify.parseFrom(payload);
var gift = req.getIsCollected(); session.send(new PacketGetAllMailResultNotify(session.getPlayer(), req.getIsCollected()));
if (gift) {
session.send(new PacketGetAllMailResultNotify(1, 1, List.of(), "", true));
return;
}
subdivide(session.getPlayer());
} }
} }

View File

@ -1,22 +1,40 @@
package emu.grasscutter.server.packet.send; package emu.grasscutter.server.packet.send;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.GetAllMailResultNotifyOuterClass; import emu.grasscutter.net.proto.GetAllMailResultNotifyOuterClass.GetAllMailResultNotify;
import emu.grasscutter.net.proto.MailDataOuterClass; import emu.grasscutter.utils.Utils;
import java.time.Instant;
import java.util.List; import java.util.List;
public class PacketGetAllMailResultNotify extends BasePacket { public final class PacketGetAllMailResultNotify extends BasePacket {
public PacketGetAllMailResultNotify(int packetBeSentNum, int packetNum, List<MailDataOuterClass.MailData> mailData, String transaction, boolean isGift) { /**
* @param player The player to fetch the mail for.
* @param gifts Is the mail for gifts?
*/
public PacketGetAllMailResultNotify(Player player, boolean gifts) {
super(PacketOpcodes.GetAllMailResultNotify); super(PacketOpcodes.GetAllMailResultNotify);
this.setData(GetAllMailResultNotifyOuterClass.GetAllMailResultNotify.newBuilder() var packet = GetAllMailResultNotify.newBuilder()
.setPacketBeSentNum(packetBeSentNum) .setTransaction(player.getUid() + "-" + Utils.getCurrentSeconds() + "-" + 0)
.addAllMailList(mailData) .setIsCollected(gifts)
.setTransaction(transaction) .setPacketBeSentNum(1)
.setIsCollected(isGift) .setPacketNum(1);
.setPacketNum(packetNum)
.build()); var inbox = player.getAllMail();
if (!gifts && inbox.size() > 0) {
packet.addAllMailList(inbox.stream()
.filter(mail -> mail.stateValue == 1)
.filter(mail -> mail.expireTime > Instant.now().getEpochSecond())
.map(mail -> mail.toProto(player)).toList());
} else {
// Empty mailbox.
// TODO: Implement the gift mailbox.
packet.addAllMailList(List.of());
}
this.setData(packet.build());
} }
} }