ChatSystem.java 7.74 KB
Newer Older
1
package emu.grasscutter.game.chat;
Melledy's avatar
Melledy committed
2

3
import emu.grasscutter.GameConstants;
Jaida Wu's avatar
Jaida Wu committed
4
import emu.grasscutter.command.CommandMap;
Melledy's avatar
Melledy committed
5
import emu.grasscutter.game.player.Player;
6
import emu.grasscutter.net.proto.ChatInfoOuterClass.ChatInfo;
Melledy's avatar
Melledy committed
7
8
9
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.server.packet.send.PacketPlayerChatNotify;
import emu.grasscutter.server.packet.send.PacketPrivateChatNotify;
10
11
12
import emu.grasscutter.server.packet.send.PacketPullPrivateChatRsp;
import emu.grasscutter.server.packet.send.PacketPullRecentChatRsp;
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
13

14
import java.util.regex.Pattern;
15
16
17

import static emu.grasscutter.config.Configuration.*;

18
19
20
21
22
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

23
public class ChatSystem implements ChatSystemHandler {
github-actions's avatar
github-actions committed
24
25
26
27
28
29
30
31
32
33
    static final String PREFIXES = "[/!]";
    static final Pattern RE_PREFIXES = Pattern.compile(PREFIXES);
    static final Pattern RE_COMMANDS = Pattern.compile("\n" + PREFIXES);

    // We store the chat history for ongoing sessions in the form
    //    user id -> chat partner id -> [messages]
    private final Map<Integer, Map<Integer, List<ChatInfo>>> history = new HashMap<>();

    private final GameServer server;

34
    public ChatSystem(GameServer server) {
github-actions's avatar
github-actions committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        this.server = server;
    }

    public GameServer getServer() {
        return server;
    }

    private boolean tryInvokeCommand(Player sender, Player target, String rawMessage) {
        if (!RE_PREFIXES.matcher(rawMessage.substring(0, 1)).matches())
            return false;
        for (String line : rawMessage.substring(1).split("\n[/!]"))
            CommandMap.getInstance().invoke(sender, target, line);
        return true;
    }

    /********************
     * Chat history handling
     ********************/
53
    private void putInHistory(int uid, int partnerId, ChatInfo info) {
github-actions's avatar
github-actions committed
54
55
56
        if (!this.history.containsKey(uid)) {
            this.history.put(uid, new HashMap<>());
        }
57
58
        if (!this.history.get(uid).containsKey(partnerId)) {
            this.history.get(uid).put(partnerId, new ArrayList<>());
github-actions's avatar
github-actions committed
59
60
        }

61
        this.history.get(uid).get(partnerId).add(info);
github-actions's avatar
github-actions committed
62
63
64
65
66
67
68
69
    }

    public void clearHistoryOnLogout(Player player) {
        if (this.history.containsKey(player.getUid())) {
            this.history.remove(player.getUid());
        }
    }

70
71
72
    public void handlePullPrivateChatReq(Player player, int partnerId) {
        if (this.history.getOrDefault(player.getUid(), Map.of()).containsKey(partnerId)) {
            player.sendPacket(new PacketPullPrivateChatRsp(this.history.get(player.getUid()).get(partnerId)));
github-actions's avatar
github-actions committed
73
74
75
76
77
78
79
        }
        else {
            player.sendPacket(new PacketPullPrivateChatRsp(List.of()));
        }
    }

    public void handlePullRecentChatReq(Player player) {
80
81
82
83
84
        // If this user has no chat history yet, create it by sending the server welcome messages.
        if (!this.history.getOrDefault(player.getUid(), Map.of()).containsKey(GameConstants.SERVER_CONSOLE_UID)) {
            this.sendServerWelcomeMessages(player);
        }

github-actions's avatar
github-actions committed
85
86
87
        // For now, we send the list three messages from the server for the recent chat history.
        // This matches the previous behavior, but ultimately, we should probably keep track of the last chat partner
        // for every given player and return the last messages exchanged with that partner.
88
89
90
        int historyLength = this.history.get(player.getUid()).get(GameConstants.SERVER_CONSOLE_UID).size();
        var messages = this.history.get(player.getUid()).get(GameConstants.SERVER_CONSOLE_UID).subList(Math.max(historyLength - 3, 0), historyLength);
        player.sendPacket(new PacketPullRecentChatRsp(messages));
github-actions's avatar
github-actions committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    }

    /********************
     * Sending messages
     ********************/
    public void sendPrivateMessageFromServer(int targetUid, String message) {
        // Sanity checks.
        if (message == null || message.length() == 0) {
            return;
        }

        // Get target.
        Player target = getServer().getPlayerByUid(targetUid);
        if (target == null) {
            return;
        }

        // Create chat packet and put in history.
        var packet = new PacketPrivateChatNotify(GameConstants.SERVER_CONSOLE_UID, targetUid, message);
        putInHistory(targetUid, GameConstants.SERVER_CONSOLE_UID, packet.getChatInfo());

        // Send.
        target.sendPacket(packet);
    }
    public void sendPrivateMessageFromServer(int targetUid, int emote) {
        // Get target.
        Player target = getServer().getPlayerByUid(targetUid);
        if (target == null) {
            return;
        }

        // Create chat packet and put in history.
        var packet = new PacketPrivateChatNotify(GameConstants.SERVER_CONSOLE_UID, targetUid, emote);
        putInHistory(targetUid, GameConstants.SERVER_CONSOLE_UID, packet.getChatInfo());

        // Send.
        target.sendPacket(packet);
    }

    public void sendPrivateMessage(Player player, int targetUid, String message) {
        // Sanity checks.
        if (message == null || message.length() == 0) {
            return;
        }

        // Get target.
        Player target = getServer().getPlayerByUid(targetUid);

        if (target == null && targetUid != GameConstants.SERVER_CONSOLE_UID) {
            return;
        }

        // Create chat packet.
        var packet = new PacketPrivateChatNotify(player.getUid(), targetUid, message);

        // Send and put in history.
        player.sendPacket(packet);
        putInHistory(player.getUid(), targetUid, packet.getChatInfo());

150
151
152
153
        // Check if command
        boolean isCommand = tryInvokeCommand(player, target, message);

        if ((target != null) && (!isCommand)) {
github-actions's avatar
github-actions committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
            target.sendPacket(packet);
            putInHistory(targetUid, player.getUid(), packet.getChatInfo());
        }
    }

    public void sendPrivateMessage(Player player, int targetUid, int emote) {
        // Get target.
        Player target = getServer().getPlayerByUid(targetUid);

        if (target == null && targetUid != GameConstants.SERVER_CONSOLE_UID) {
            return;
        }

        // Create chat packet.
        var packet = new PacketPrivateChatNotify(player.getUid(), target.getUid(), emote);

        // Send and put is history.
        player.sendPacket(packet);
        putInHistory(player.getUid(), targetUid, packet.getChatInfo());

        if (target != null) {
            target.sendPacket(packet);
            putInHistory(targetUid, player.getUid(), packet.getChatInfo());
        }
    }

    public void sendTeamMessage(Player player, int channel, String message) {
        // Sanity checks
        if (message == null || message.length() == 0) {
            return;
        }

        // Check if command
        if (tryInvokeCommand(player, null, message)) {
            return;
        }

        // Create and send chat packet
        player.getWorld().broadcastPacket(new PacketPlayerChatNotify(player, channel, message));
    }
    public void sendTeamMessage(Player player, int channel, int icon) {
        // Create and send chat packet
        player.getWorld().broadcastPacket(new PacketPlayerChatNotify(player, channel, icon));
    }

    /********************
     * Welcome messages
     ********************/
202
    private void sendServerWelcomeMessages(Player player) {
github-actions's avatar
github-actions committed
203
204
205
206
207
208
209
210
211
212
        var joinOptions = GAME_INFO.joinOptions;

        if (joinOptions.welcomeEmotes != null && joinOptions.welcomeEmotes.length > 0) {
            this.sendPrivateMessageFromServer(player.getUid(), joinOptions.welcomeEmotes[Utils.randomRange(0, joinOptions.welcomeEmotes.length - 1)]);
        }

        if (joinOptions.welcomeMessage != null && joinOptions.welcomeMessage.length() > 0) {
            this.sendPrivateMessageFromServer(player.getUid(), joinOptions.welcomeMessage);
        }
    }
Melledy's avatar
Melledy committed
213
}