1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
150
151
152
153
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#include "StarChatProcessor.hpp"
namespace Star {
char const* ChatProcessor::ServerNick = "server";
String ChatProcessor::connectClient(ConnectionId clientId, String nick) {
RecursiveMutexLocker locker(m_mutex);
if (nick.empty())
nick = strf("Player_{}", clientId);
nick = makeNickUnique(nick);
for (auto& pair : m_clients) {
pair.second.pendingMessages.append({
{MessageContext::Broadcast},
ServerConnectionId,
ServerNick,
strf("Player '{}' connected", nick)
});
}
m_clients.add(clientId, ClientInfo(clientId, nick));
m_nicks[nick] = clientId;
return nick;
}
List<ChatReceivedMessage> ChatProcessor::disconnectClient(ConnectionId clientId) {
RecursiveMutexLocker locker(m_mutex);
for (auto channel : clientChannels(clientId))
leaveChannel(clientId, channel);
auto clientInfo = m_clients.take(clientId);
m_nicks.remove(clientInfo.nick);
for (auto& pair : m_clients) {
pair.second.pendingMessages.append({
{MessageContext::Broadcast},
ServerConnectionId,
ServerNick,
strf("Player '{}' disconnected", clientInfo.nick)
});
}
return clientInfo.pendingMessages;
}
List<ConnectionId> ChatProcessor::clients() const {
RecursiveMutexLocker locker(m_mutex);
return m_clients.keys();
}
bool ChatProcessor::hasClient(ConnectionId clientId) const {
RecursiveMutexLocker locker(m_mutex);
return m_clients.contains(clientId);
}
Maybe<ConnectionId> ChatProcessor::findNick(String const& nick) const {
RecursiveMutexLocker locker(m_mutex);
if (auto m = m_nicks.maybe(nick))
return m;
if (nick == ServerNick)
return ServerConnectionId;
return {};
}
String ChatProcessor::connectionNick(ConnectionId clientId) const {
RecursiveMutexLocker locker(m_mutex);
if (clientId == ServerConnectionId)
return ServerNick;
else
return m_clients.get(clientId).nick;
}
String ChatProcessor::renick(ConnectionId clientId, String const& nick) {
RecursiveMutexLocker locker(m_mutex);
auto& clientInfo = m_clients.get(clientId);
m_nicks.remove(clientInfo.nick);
clientInfo.nick = makeNickUnique(nick);
m_clients.get(clientId).nick = nick;
m_nicks[nick] = clientId;
return nick;
}
bool ChatProcessor::joinChannel(ConnectionId clientId, String const& channelName) {
RecursiveMutexLocker locker(m_mutex);
// Right now channels are simply created on join if they don't exist.
return m_channels[channelName].add(clientId);
}
bool ChatProcessor::leaveChannel(ConnectionId clientId, String const& channelName) {
RecursiveMutexLocker locker(m_mutex);
return m_channels[channelName].remove(clientId);
}
StringList ChatProcessor::clientChannels(ConnectionId clientId) const {
RecursiveMutexLocker locker(m_mutex);
StringList channels;
for (auto const& pair : m_channels) {
if (pair.second.contains(clientId))
channels.append(pair.first);
}
return channels;
}
StringList ChatProcessor::activeChannels() const {
RecursiveMutexLocker locker(m_mutex);
StringList channels;
for (auto const& pair : m_channels) {
if (!pair.second.empty())
channels.append(pair.first);
}
return channels;
}
void ChatProcessor::broadcast(ConnectionId sourceConnectionId, String const& text, JsonObject data) {
RecursiveMutexLocker locker(m_mutex);
ChatReceivedMessage message = {
{MessageContext::Broadcast},
sourceConnectionId,
connectionNick(sourceConnectionId),
text
};
message.data = std::move(data);
if (handleCommand(message))
return;
for (auto& pair : m_clients)
pair.second.pendingMessages.append(message);
}
void ChatProcessor::message(ConnectionId sourceConnectionId, MessageContext::Mode mode, String const& channelName, String const& text, JsonObject data) {
RecursiveMutexLocker locker(m_mutex);
ChatReceivedMessage message = {
{mode, channelName},
sourceConnectionId,
connectionNick(sourceConnectionId),
text
};
message.data = std::move(data);
if (handleCommand(message))
return;
for (auto clientId : m_channels[channelName]) {
auto& clientInfo = m_clients.get(clientId);
clientInfo.pendingMessages.append(message);
}
}
void ChatProcessor::whisper(ConnectionId sourceConnectionId, ConnectionId targetClientId, String const& text, JsonObject data) {
RecursiveMutexLocker locker(m_mutex);
ChatReceivedMessage message = {
{MessageContext::Whisper},
sourceConnectionId,
connectionNick(sourceConnectionId),
text
};
message.data = std::move(data);
if (handleCommand(message))
return;
if (sourceConnectionId != ServerConnectionId)
m_clients.get(sourceConnectionId).pendingMessages.append(message);
m_clients.get(targetClientId).pendingMessages.append(message);
}
void ChatProcessor::adminBroadcast(String const& text) {
RecursiveMutexLocker locker(m_mutex);
broadcast(ServerConnectionId, text);
}
void ChatProcessor::adminMessage(MessageContext::Mode context, String const& channelName, String const& text) {
RecursiveMutexLocker locker(m_mutex);
ChatProcessor::message(ServerConnectionId, context, channelName, text);
}
void ChatProcessor::adminWhisper(ConnectionId targetClientId, String const& text) {
RecursiveMutexLocker locker(m_mutex);
whisper(ServerConnectionId, targetClientId, text);
}
List<ChatReceivedMessage> ChatProcessor::pullPendingMessages(ConnectionId clientId) {
RecursiveMutexLocker locker(m_mutex);
if (m_clients.contains(clientId))
return take(m_clients.get(clientId).pendingMessages);
return {};
}
void ChatProcessor::setCommandHandler(CommandHandler commandHandler) {
RecursiveMutexLocker locker(m_mutex);
m_commandHandler = commandHandler;
}
void ChatProcessor::clearCommandHandler() {
RecursiveMutexLocker locker(m_mutex);
m_commandHandler = {};
}
ChatProcessor::ClientInfo::ClientInfo(ConnectionId clientId, String const& nick) : clientId(clientId), nick(nick) {}
String ChatProcessor::makeNickUnique(String nick) {
while (m_nicks.contains(nick) || nick == ServerNick)
nick.append("_");
return nick;
}
bool ChatProcessor::handleCommand(ChatReceivedMessage& message) {
if (!message.text.beginsWith("/")) {
return false;
} else if (message.text.beginsWith("//")) {
message.text = message.text.substr(1);
return false;
}
String commandLine = message.text.substr(1);
String command = commandLine.extract();
String response;
if (command == "nick") {
auto newNick = renick(message.fromConnection, commandLine.trim());
response = strf("Nick changed to {}", newNick);
} else if (command == "w") {
String target = commandLine.extract();
if (m_nicks.contains(target))
whisper(message.fromConnection, m_nicks.get(target), commandLine.trim());
else
response = strf("No such nick {}", target);
} else if (m_commandHandler) {
response = m_commandHandler(message.fromConnection, command, commandLine);
} else {
response = strf("No such command {}", command);
}
if (!response.empty()) {
m_clients.get(message.fromConnection).pendingMessages.append({
MessageContext(MessageContext::CommandResult),
ServerConnectionId,
connectionNick(ServerConnectionId),
response
});
}
return true;
}
}
|