Update mail state on ping

This commit is contained in:
Melledy
2025-10-30 08:28:40 -07:00
parent 7c5c0e21b7
commit 3a44ebbdc3
4 changed files with 90 additions and 39 deletions

View File

@@ -27,6 +27,8 @@ public class Mailbox extends PlayerManager implements GameDatabaseObject, Iterab
private List<GameMail> list;
private transient boolean newState;
@Deprecated // Morphia only
public Mailbox() {
@@ -41,6 +43,10 @@ public class Mailbox extends PlayerManager implements GameDatabaseObject, Iterab
this.save();
}
public void clearNewState() {
this.newState = false;
}
// TODO optimize to an O(n) algorithm like a map
public GameMail getMailById(int id) {
return this.getList().stream()
@@ -56,6 +62,9 @@ public class Mailbox extends PlayerManager implements GameDatabaseObject, Iterab
// Add to mail list
this.list.add(mail);
// Set state
this.newState = true;
// Save to database
Nebula.getGameDatabase().update(this, getUid(), "lastMailId", this.getLastMailId());
Nebula.getGameDatabase().addToList(this, getUid(), "list", mail);

View File

@@ -2,7 +2,6 @@ package emu.nebula.net;
import lombok.SneakyThrows;
import us.hebi.quickbuf.ProtoMessage;
import us.hebi.quickbuf.ProtoSink;
public abstract class NetHandler {
@@ -14,53 +13,22 @@ public abstract class NetHandler {
return true;
}
// Packet encoding helper functions
public byte[] encodeMsg(int msgId, byte[] packet) {
// Create data array
byte[] data = new byte[packet.length + 2];
// Encode msgId
short id = (short) msgId;
data[0] = (byte) (id >> 8);
data[1] = (byte) id;
// Copy packet to data array
System.arraycopy(packet, 0, data, 2, packet.length);
// Complete
return data;
return PacketHelper.encodeMsg(msgId, packet);
}
@SneakyThrows
public byte[] encodeMsg(int msgId, ProtoMessage<?> proto) {
// Create data array
byte[] data = new byte[proto.getCachedSize() + 2];
// Encode msgId
short id = (short) msgId;
data[0] = (byte) (id >> 8);
data[1] = (byte) id;
// Create proto sink
var output = ProtoSink.newInstance(data, 2, proto.getCachedSize());
// Copy packet to data array
proto.writeTo(output);
// Complete
return data;
return PacketHelper.encodeMsg(msgId, proto);
}
public byte[] encodeMsg(int msgId) {
// Create data array
byte[] data = new byte[2];
// Encode msgId
short id = (short) msgId;
data[0] = (byte) (id >> 8);
data[1] = (byte) id;
return data;
return PacketHelper.encodeMsg(msgId);
}
// Handler
public abstract byte[] handle(GameSession session, byte[] message) throws Exception;

View File

@@ -0,0 +1,57 @@
package emu.nebula.net;
import lombok.SneakyThrows;
import us.hebi.quickbuf.ProtoMessage;
import us.hebi.quickbuf.ProtoSink;
public class PacketHelper {
public static byte[] encodeMsg(int msgId, byte[] packet) {
// Create data array
byte[] data = new byte[packet.length + 2];
// Encode msgId
short id = (short) msgId;
data[0] = (byte) (id >> 8);
data[1] = (byte) id;
// Copy packet to data array
System.arraycopy(packet, 0, data, 2, packet.length);
// Complete
return data;
}
@SneakyThrows
public static byte[] encodeMsg(int msgId, ProtoMessage<?> proto) {
// Create data array
byte[] data = new byte[proto.getCachedSize() + 2];
// Encode msgId
short id = (short) msgId;
data[0] = (byte) (id >> 8);
data[1] = (byte) id;
// Create proto sink
var output = ProtoSink.newInstance(data, 2, proto.getCachedSize());
// Copy packet to data array
proto.writeTo(output);
// Complete
return data;
}
public static byte[] encodeMsg(int msgId) {
// Create data array
byte[] data = new byte[2];
// Encode msgId
short id = (short) msgId;
data[0] = (byte) (id >> 8);
data[1] = (byte) id;
return data;
}
}

View File

@@ -2,7 +2,9 @@ package emu.nebula.server.handlers;
import emu.nebula.net.NetHandler;
import emu.nebula.net.NetMsgId;
import emu.nebula.net.PacketHelper;
import emu.nebula.proto.PlayerPing.Pong;
import emu.nebula.proto.Public.MailState;
import emu.nebula.net.HandlerId;
import emu.nebula.Nebula;
import emu.nebula.net.GameSession;
@@ -12,9 +14,24 @@ public class HandlerPlayerPingReq extends NetHandler {
@Override
public byte[] handle(GameSession session, byte[] message) throws Exception {
// Create response
var rsp = Pong.newInstance()
.setServerTs(Nebula.getCurrentTime());
// Update mail state flag
if (session.getPlayer().getMailbox().isNewState()) {
// Clear
session.getPlayer().getMailbox().clearNewState();
// Send mail state notify
byte[] nextPackage = PacketHelper.encodeMsg(
NetMsgId.mail_state_notify,
MailState.newInstance().setNew(true)
);
rsp.setNextPackage(nextPackage);
}
return this.encodeMsg(NetMsgId.player_ping_succeed_ack, rsp);
}