mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-13 12:54:36 +01:00
Update mail state on ping
This commit is contained in:
@@ -27,6 +27,8 @@ public class Mailbox extends PlayerManager implements GameDatabaseObject, Iterab
|
|||||||
|
|
||||||
private List<GameMail> list;
|
private List<GameMail> list;
|
||||||
|
|
||||||
|
private transient boolean newState;
|
||||||
|
|
||||||
@Deprecated // Morphia only
|
@Deprecated // Morphia only
|
||||||
public Mailbox() {
|
public Mailbox() {
|
||||||
|
|
||||||
@@ -41,6 +43,10 @@ public class Mailbox extends PlayerManager implements GameDatabaseObject, Iterab
|
|||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearNewState() {
|
||||||
|
this.newState = false;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO optimize to an O(n) algorithm like a map
|
// TODO optimize to an O(n) algorithm like a map
|
||||||
public GameMail getMailById(int id) {
|
public GameMail getMailById(int id) {
|
||||||
return this.getList().stream()
|
return this.getList().stream()
|
||||||
@@ -56,6 +62,9 @@ public class Mailbox extends PlayerManager implements GameDatabaseObject, Iterab
|
|||||||
// Add to mail list
|
// Add to mail list
|
||||||
this.list.add(mail);
|
this.list.add(mail);
|
||||||
|
|
||||||
|
// Set state
|
||||||
|
this.newState = true;
|
||||||
|
|
||||||
// Save to database
|
// Save to database
|
||||||
Nebula.getGameDatabase().update(this, getUid(), "lastMailId", this.getLastMailId());
|
Nebula.getGameDatabase().update(this, getUid(), "lastMailId", this.getLastMailId());
|
||||||
Nebula.getGameDatabase().addToList(this, getUid(), "list", mail);
|
Nebula.getGameDatabase().addToList(this, getUid(), "list", mail);
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package emu.nebula.net;
|
|||||||
|
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import us.hebi.quickbuf.ProtoMessage;
|
import us.hebi.quickbuf.ProtoMessage;
|
||||||
import us.hebi.quickbuf.ProtoSink;
|
|
||||||
|
|
||||||
public abstract class NetHandler {
|
public abstract class NetHandler {
|
||||||
|
|
||||||
@@ -14,53 +13,22 @@ public abstract class NetHandler {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Packet encoding helper functions
|
||||||
|
|
||||||
public byte[] encodeMsg(int msgId, byte[] packet) {
|
public byte[] encodeMsg(int msgId, byte[] packet) {
|
||||||
// Create data array
|
return PacketHelper.encodeMsg(msgId, packet);
|
||||||
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
|
@SneakyThrows
|
||||||
public byte[] encodeMsg(int msgId, ProtoMessage<?> proto) {
|
public byte[] encodeMsg(int msgId, ProtoMessage<?> proto) {
|
||||||
// Create data array
|
return PacketHelper.encodeMsg(msgId, proto);
|
||||||
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 byte[] encodeMsg(int msgId) {
|
public byte[] encodeMsg(int msgId) {
|
||||||
// Create data array
|
return PacketHelper.encodeMsg(msgId);
|
||||||
byte[] data = new byte[2];
|
|
||||||
|
|
||||||
// Encode msgId
|
|
||||||
short id = (short) msgId;
|
|
||||||
data[0] = (byte) (id >> 8);
|
|
||||||
data[1] = (byte) id;
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handler
|
||||||
|
|
||||||
public abstract byte[] handle(GameSession session, byte[] message) throws Exception;
|
public abstract byte[] handle(GameSession session, byte[] message) throws Exception;
|
||||||
|
|
||||||
|
|||||||
57
src/main/java/emu/nebula/net/PacketHelper.java
Normal file
57
src/main/java/emu/nebula/net/PacketHelper.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@ package emu.nebula.server.handlers;
|
|||||||
|
|
||||||
import emu.nebula.net.NetHandler;
|
import emu.nebula.net.NetHandler;
|
||||||
import emu.nebula.net.NetMsgId;
|
import emu.nebula.net.NetMsgId;
|
||||||
|
import emu.nebula.net.PacketHelper;
|
||||||
import emu.nebula.proto.PlayerPing.Pong;
|
import emu.nebula.proto.PlayerPing.Pong;
|
||||||
|
import emu.nebula.proto.Public.MailState;
|
||||||
import emu.nebula.net.HandlerId;
|
import emu.nebula.net.HandlerId;
|
||||||
import emu.nebula.Nebula;
|
import emu.nebula.Nebula;
|
||||||
import emu.nebula.net.GameSession;
|
import emu.nebula.net.GameSession;
|
||||||
@@ -12,9 +14,24 @@ public class HandlerPlayerPingReq extends NetHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] handle(GameSession session, byte[] message) throws Exception {
|
public byte[] handle(GameSession session, byte[] message) throws Exception {
|
||||||
|
// Create response
|
||||||
var rsp = Pong.newInstance()
|
var rsp = Pong.newInstance()
|
||||||
.setServerTs(Nebula.getCurrentTime());
|
.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);
|
return this.encodeMsg(NetMsgId.player_ping_succeed_ack, rsp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user