mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-13 13:54:37 +01:00
Implement GetPrivateChatHistoryCsReq
Pretty useless on private servers but oh well
This commit is contained in:
@@ -18,6 +18,8 @@ public class GameConstants {
|
|||||||
public static final int DEFAULT_TEAMS = 6;
|
public static final int DEFAULT_TEAMS = 6;
|
||||||
public static final int MAX_MP = 5; // Client doesnt like more than 5
|
public static final int MAX_MP = 5; // Client doesnt like more than 5
|
||||||
|
|
||||||
|
public static final int MAX_CHAT_HISTORY = 100; // Max chat messages per conversation
|
||||||
|
|
||||||
// Custom
|
// Custom
|
||||||
public static final int SERVER_CONSOLE_UID = 99;
|
public static final int SERVER_CONSOLE_UID = 99;
|
||||||
public static final int EQUIPMENT_SLOT_ID = 100;
|
public static final int EQUIPMENT_SLOT_ID = 100;
|
||||||
|
|||||||
90
src/main/java/emu/lunarcore/game/chat/ChatManager.java
Normal file
90
src/main/java/emu/lunarcore/game/chat/ChatManager.java
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package emu.lunarcore.game.chat;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import emu.lunarcore.GameConstants;
|
||||||
|
import emu.lunarcore.commands.PlayerCommands;
|
||||||
|
import emu.lunarcore.game.player.BasePlayerManager;
|
||||||
|
import emu.lunarcore.game.player.Player;
|
||||||
|
import emu.lunarcore.server.packet.send.PacketRevcMsgScNotify;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||||
|
import it.unimi.dsi.fastutil.objects.ObjectList;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
public class ChatManager extends BasePlayerManager {
|
||||||
|
@Getter(AccessLevel.PRIVATE)
|
||||||
|
private final Int2ObjectMap<ObjectList<ChatMessage>> history;
|
||||||
|
|
||||||
|
public ChatManager(Player player) {
|
||||||
|
super(player);
|
||||||
|
this.history = new Int2ObjectOpenHashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<ChatMessage> getHistoryByUid(int uid) {
|
||||||
|
return getHistory().get(uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addChatMessage(ChatMessage message) {
|
||||||
|
// Get sender
|
||||||
|
int targetPlayerUid = this.getPlayer().getUid() == message.getToUid() ? message.getFromUid() : message.getToUid();
|
||||||
|
// Add to chat history
|
||||||
|
if (GameConstants.MAX_CHAT_HISTORY > 0) {
|
||||||
|
// Add to history list
|
||||||
|
var list = getHistory().computeIfAbsent(targetPlayerUid, id -> new ObjectArrayList<>());
|
||||||
|
list.add(message);
|
||||||
|
// Check max size
|
||||||
|
while (list.size() >= GameConstants.MAX_CHAT_HISTORY) {
|
||||||
|
list.remove(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Send to client
|
||||||
|
getPlayer().sendPacket(new PacketRevcMsgScNotify(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendChat(int targetUid, String text) {
|
||||||
|
// Sanity checks
|
||||||
|
if (text == null || text.length() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if command
|
||||||
|
if (text.charAt(0) == '!' || text.charAt(0) == '/') {
|
||||||
|
PlayerCommands.handle(getPlayer(), text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get target
|
||||||
|
Player target = getPlayer().getServer().getOnlinePlayerByUid(targetUid);
|
||||||
|
|
||||||
|
if (target == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create chat packet
|
||||||
|
ChatMessage message = new ChatMessage(this.getPlayer().getUid(), targetUid, text);
|
||||||
|
|
||||||
|
// Send to both players
|
||||||
|
this.addChatMessage(message);
|
||||||
|
target.getChatManager().addChatMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendChat(int targetUid, int emote) {
|
||||||
|
// Get target
|
||||||
|
Player target = getPlayer().getServer().getOnlinePlayerByUid(targetUid);
|
||||||
|
|
||||||
|
if (target == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create chat packet
|
||||||
|
ChatMessage message = new ChatMessage(this.getPlayer().getUid(), targetUid, emote);
|
||||||
|
|
||||||
|
// Send to both players
|
||||||
|
this.addChatMessage(message);
|
||||||
|
target.getChatManager().addChatMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
48
src/main/java/emu/lunarcore/game/chat/ChatMessage.java
Normal file
48
src/main/java/emu/lunarcore/game/chat/ChatMessage.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package emu.lunarcore.game.chat;
|
||||||
|
|
||||||
|
import emu.lunarcore.proto.ChatOuterClass.Chat;
|
||||||
|
import emu.lunarcore.proto.MsgTypeOuterClass.MsgType;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class ChatMessage {
|
||||||
|
private int fromUid;
|
||||||
|
private int toUid;
|
||||||
|
private String text;
|
||||||
|
private int emote;
|
||||||
|
private long time;
|
||||||
|
|
||||||
|
public ChatMessage(int fromUid, int toUid) {
|
||||||
|
this.fromUid = fromUid;
|
||||||
|
this.toUid = toUid;
|
||||||
|
this.time = System.currentTimeMillis() / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatMessage(int fromUid, int toUid, String text) {
|
||||||
|
this(fromUid, toUid);
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatMessage(int fromUid, int toUid, int emote) {
|
||||||
|
this(fromUid, toUid);
|
||||||
|
this.emote = emote;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MsgType getType() {
|
||||||
|
return this.getText() != null ? MsgType.MSG_TYPE_CUSTOM_TEXT : MsgType.MSG_TYPE_EMOJI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Chat toProto() {
|
||||||
|
var proto = Chat.newInstance()
|
||||||
|
.setSenderUid(this.getFromUid())
|
||||||
|
.setSentTime(this.getTime())
|
||||||
|
.setMsgType(this.getType())
|
||||||
|
.setEmote(this.getEmote());
|
||||||
|
|
||||||
|
if (this.getText() != null) {
|
||||||
|
proto.setText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
return proto;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,8 @@ import emu.lunarcore.game.avatar.AvatarStorage;
|
|||||||
import emu.lunarcore.game.avatar.GameAvatar;
|
import emu.lunarcore.game.avatar.GameAvatar;
|
||||||
import emu.lunarcore.game.avatar.HeroPath;
|
import emu.lunarcore.game.avatar.HeroPath;
|
||||||
import emu.lunarcore.game.battle.Battle;
|
import emu.lunarcore.game.battle.Battle;
|
||||||
|
import emu.lunarcore.game.chat.ChatManager;
|
||||||
|
import emu.lunarcore.game.chat.ChatMessage;
|
||||||
import emu.lunarcore.game.gacha.PlayerGachaInfo;
|
import emu.lunarcore.game.gacha.PlayerGachaInfo;
|
||||||
import emu.lunarcore.game.inventory.Inventory;
|
import emu.lunarcore.game.inventory.Inventory;
|
||||||
import emu.lunarcore.game.scene.Scene;
|
import emu.lunarcore.game.scene.Scene;
|
||||||
@@ -73,6 +75,7 @@ public class Player {
|
|||||||
private transient GameSession session;
|
private transient GameSession session;
|
||||||
private transient final AvatarStorage avatars;
|
private transient final AvatarStorage avatars;
|
||||||
private transient final Inventory inventory;
|
private transient final Inventory inventory;
|
||||||
|
private transient final ChatManager chatManager;
|
||||||
|
|
||||||
// Database persistent data
|
// Database persistent data
|
||||||
private LineupManager lineupManager;
|
private LineupManager lineupManager;
|
||||||
@@ -89,6 +92,7 @@ public class Player {
|
|||||||
this.gender = PlayerGender.GENDER_MAN;
|
this.gender = PlayerGender.GENDER_MAN;
|
||||||
this.avatars = new AvatarStorage(this);
|
this.avatars = new AvatarStorage(this);
|
||||||
this.inventory = new Inventory(this);
|
this.inventory = new Inventory(this);
|
||||||
|
this.chatManager = new ChatManager(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called when player is created
|
// Called when player is created
|
||||||
@@ -425,7 +429,8 @@ public class Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void dropMessage(String message) {
|
public void dropMessage(String message) {
|
||||||
this.sendPacket(new PacketRevcMsgScNotify(this, message));
|
var msg = new ChatMessage(GameConstants.SERVER_CONSOLE_UID, this.getUid(), message);
|
||||||
|
this.getChatManager().addChatMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendPacket(BasePacket packet) {
|
public void sendPacket(BasePacket packet) {
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
package emu.lunarcore.game.service;
|
|
||||||
|
|
||||||
import emu.lunarcore.commands.PlayerCommands;
|
|
||||||
import emu.lunarcore.game.player.Player;
|
|
||||||
import emu.lunarcore.server.game.BaseGameService;
|
|
||||||
import emu.lunarcore.server.game.GameServer;
|
|
||||||
|
|
||||||
public class ChatService extends BaseGameService {
|
|
||||||
|
|
||||||
public ChatService(GameServer server) {
|
|
||||||
super(server);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendPrivChat(Player player, int targetUid, String message) {
|
|
||||||
// Sanity checks
|
|
||||||
if (message == null || message.length() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if command
|
|
||||||
if (message.charAt(0) == '!') {
|
|
||||||
PlayerCommands.handle(player, message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get target
|
|
||||||
Player target = getServer().getOnlinePlayerByUid(targetUid);
|
|
||||||
|
|
||||||
if (target == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create chat packet TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendPrivChat(Player player, int targetUid, int emote) {
|
|
||||||
// Get target
|
|
||||||
Player target = getServer().getOnlinePlayerByUid(targetUid);
|
|
||||||
|
|
||||||
if (target == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create chat packet TODO
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,6 @@ import emu.lunarcore.game.battle.BattleService;
|
|||||||
import emu.lunarcore.game.challenge.ChallengeService;
|
import emu.lunarcore.game.challenge.ChallengeService;
|
||||||
import emu.lunarcore.game.gacha.GachaService;
|
import emu.lunarcore.game.gacha.GachaService;
|
||||||
import emu.lunarcore.game.player.Player;
|
import emu.lunarcore.game.player.Player;
|
||||||
import emu.lunarcore.game.service.ChatService;
|
|
||||||
import emu.lunarcore.game.service.InventoryService;
|
import emu.lunarcore.game.service.InventoryService;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
|
||||||
@@ -32,7 +31,6 @@ public class GameServer extends KcpServer {
|
|||||||
@Getter private final BattleService battleService;
|
@Getter private final BattleService battleService;
|
||||||
@Getter private final InventoryService inventoryService;
|
@Getter private final InventoryService inventoryService;
|
||||||
@Getter private final GachaService gachaService;
|
@Getter private final GachaService gachaService;
|
||||||
@Getter private final ChatService chatService;
|
|
||||||
@Getter private final ChallengeService challengeService;
|
@Getter private final ChallengeService challengeService;
|
||||||
|
|
||||||
public GameServer(GameServerConfig serverConfig) {
|
public GameServer(GameServerConfig serverConfig) {
|
||||||
@@ -48,7 +46,6 @@ public class GameServer extends KcpServer {
|
|||||||
this.battleService = new BattleService(this);
|
this.battleService = new BattleService(this);
|
||||||
this.inventoryService = new InventoryService(this);
|
this.inventoryService = new InventoryService(this);
|
||||||
this.gachaService = new GachaService(this);
|
this.gachaService = new GachaService(this);
|
||||||
this.chatService = new ChatService(this);
|
|
||||||
this.challengeService = new ChallengeService(this);
|
this.challengeService = new ChallengeService(this);
|
||||||
|
|
||||||
// Hook into shutdown event.
|
// Hook into shutdown event.
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package emu.lunarcore.server.packet.recv;
|
||||||
|
|
||||||
|
import emu.lunarcore.proto.GetPrivateChatHistoryCsReqOuterClass.GetPrivateChatHistoryCsReq;
|
||||||
|
import emu.lunarcore.server.game.GameSession;
|
||||||
|
import emu.lunarcore.server.packet.CmdId;
|
||||||
|
import emu.lunarcore.server.packet.Opcodes;
|
||||||
|
import emu.lunarcore.server.packet.PacketHandler;
|
||||||
|
import emu.lunarcore.server.packet.send.PacketGetPrivateChatHistoryScRsp;
|
||||||
|
|
||||||
|
@Opcodes(CmdId.GetPrivateChatHistoryCsReq)
|
||||||
|
public class HandlerGetPrivateChatHistoryCsReq extends PacketHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(GameSession session, byte[] header, byte[] data) throws Exception {
|
||||||
|
var req = GetPrivateChatHistoryCsReq.parseFrom(data);
|
||||||
|
|
||||||
|
var messages = session.getPlayer().getChatManager().getHistoryByUid(req.getToUid());
|
||||||
|
|
||||||
|
session.send(new PacketGetPrivateChatHistoryScRsp(req.getToUid(), messages));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,9 +17,9 @@ public class HandlerSendMsgCsReq extends PacketHandler {
|
|||||||
|
|
||||||
for (int targetUid : req.getToUid()) {
|
for (int targetUid : req.getToUid()) {
|
||||||
if (req.getMsgType() == MsgType.MSG_TYPE_CUSTOM_TEXT) {
|
if (req.getMsgType() == MsgType.MSG_TYPE_CUSTOM_TEXT) {
|
||||||
session.getServer().getChatService().sendPrivChat(session.getPlayer(), targetUid, req.getText());
|
session.getPlayer().getChatManager().sendChat(targetUid, req.getText());
|
||||||
} else if (req.getMsgType() == MsgType.MSG_TYPE_EMOJI) {
|
} else if (req.getMsgType() == MsgType.MSG_TYPE_EMOJI) {
|
||||||
session.getServer().getChatService().sendPrivChat(session.getPlayer(), targetUid, req.getEmote());
|
session.getPlayer().getChatManager().sendChat(targetUid, req.getEmote());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package emu.lunarcore.server.packet.send;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import emu.lunarcore.game.chat.ChatMessage;
|
||||||
|
import emu.lunarcore.proto.GetPrivateChatHistoryScRspOuterClass.GetPrivateChatHistoryScRsp;
|
||||||
|
import emu.lunarcore.server.packet.BasePacket;
|
||||||
|
import emu.lunarcore.server.packet.CmdId;
|
||||||
|
|
||||||
|
public class PacketGetPrivateChatHistoryScRsp extends BasePacket {
|
||||||
|
|
||||||
|
public PacketGetPrivateChatHistoryScRsp(int targetUid, Collection<ChatMessage> messages) {
|
||||||
|
super(CmdId.GetPrivateChatHistoryScRsp);
|
||||||
|
|
||||||
|
var data = GetPrivateChatHistoryScRsp.newInstance()
|
||||||
|
.setToUid(targetUid);
|
||||||
|
|
||||||
|
if (messages != null) {
|
||||||
|
for (var message : messages) {
|
||||||
|
data.addChatList(message.toProto());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setData(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package emu.lunarcore.server.packet.send;
|
package emu.lunarcore.server.packet.send;
|
||||||
|
|
||||||
import emu.lunarcore.GameConstants;
|
import emu.lunarcore.game.chat.ChatMessage;
|
||||||
import emu.lunarcore.game.player.Player;
|
|
||||||
import emu.lunarcore.proto.ChatTypeOuterClass.ChatType;
|
import emu.lunarcore.proto.ChatTypeOuterClass.ChatType;
|
||||||
import emu.lunarcore.proto.MsgTypeOuterClass.MsgType;
|
import emu.lunarcore.proto.MsgTypeOuterClass.MsgType;
|
||||||
import emu.lunarcore.proto.RevcMsgScNotifyOuterClass.RevcMsgScNotify;
|
import emu.lunarcore.proto.RevcMsgScNotifyOuterClass.RevcMsgScNotify;
|
||||||
@@ -10,15 +9,22 @@ import emu.lunarcore.server.packet.CmdId;
|
|||||||
|
|
||||||
public class PacketRevcMsgScNotify extends BasePacket {
|
public class PacketRevcMsgScNotify extends BasePacket {
|
||||||
|
|
||||||
public PacketRevcMsgScNotify(Player player, String msg) {
|
public PacketRevcMsgScNotify(ChatMessage message) {
|
||||||
super(CmdId.RevcMsgScNotify);
|
super(CmdId.RevcMsgScNotify);
|
||||||
|
|
||||||
var data = RevcMsgScNotify.newInstance()
|
var data = RevcMsgScNotify.newInstance()
|
||||||
.setText(msg)
|
|
||||||
.setChatType(ChatType.CHAT_TYPE_PRIVATE)
|
.setChatType(ChatType.CHAT_TYPE_PRIVATE)
|
||||||
.setMsgType(MsgType.MSG_TYPE_CUSTOM_TEXT)
|
.setFromUid(message.getFromUid())
|
||||||
.setFromUid(GameConstants.SERVER_CONSOLE_UID)
|
.setToUid(message.getToUid());
|
||||||
.setToUid(player.getUid());
|
|
||||||
|
MsgType msgType = message.getType();
|
||||||
|
data.setMsgType(msgType);
|
||||||
|
|
||||||
|
if (msgType == MsgType.MSG_TYPE_CUSTOM_TEXT) {
|
||||||
|
data.setText(message.getText());
|
||||||
|
} else {
|
||||||
|
data.setEmote(message.getEmote());
|
||||||
|
}
|
||||||
|
|
||||||
this.setData(data);
|
this.setData(data);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user