-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerChat.cs
More file actions
315 lines (282 loc) · 12.6 KB
/
PlayerChat.cs
File metadata and controls
315 lines (282 loc) · 12.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// We implemented a chat system that works directly with UNET. The chat supports
// different channels that can be used to communicate with other players:
//
// - **Local Chat:** by default, all messages that don't start with a **/** are
// addressed to the local chat. If one player writes a local message, then all
// players around him _(all observers)_ will be able to see the message.
// - **Whisper Chat:** a player can write a private message to another player by
// using the **/ name message** format.
// - **Guild Chat:** we implemented guild chat support with the **/g message**
// - **Info Chat:** the info chat can be used by the server to notify all
// players about important news. The clients won't be able to write any info
// messages.
//
// _Note: the channel names, colors and commands can be edited in the Inspector_
using System;
using UnityEngine;
using Mirror;
namespace uMMORPG
{
[Serializable]
public class ChannelInfo
{
public string command; // /w etc.
public string identifierOut; // for sending
public string identifierIn; // for receiving
public GameObject textPrefab;
public ChannelInfo(string command, string identifierOut, string identifierIn, GameObject textPrefab)
{
this.command = command;
this.identifierOut = identifierOut;
this.identifierIn = identifierIn;
this.textPrefab = textPrefab;
}
}
[Serializable]
public struct ChatMessage
{
public string sender;
public string identifier;
public string message;
public string replyPrefix; // copied to input when clicking the message
public GameObject textPrefab;
public ChatMessage(string sender, string identifier, string message, string replyPrefix, GameObject textPrefab)
{
this.sender = sender;
this.identifier = identifier;
this.message = message;
this.replyPrefix = replyPrefix;
this.textPrefab = textPrefab;
}
// construct the message
public string Construct()
{
return "<b>" + sender + identifier + ":</b> " + message;
}
}
[RequireComponent(typeof(PlayerGuild))]
[RequireComponent(typeof(PlayerParty))]
[DisallowMultipleComponent]
public class PlayerChat : NetworkBehaviour
{
[Header("Components")] // to be assigned in inspector
public PlayerGuild guild;
public PlayerParty party;
[Header("Channels")]
public ChannelInfo whisperChannel = new ChannelInfo("/w", "(TO)", "(FROM)", null);
public ChannelInfo localChannel = new ChannelInfo("", "", "", null);
public ChannelInfo partyChannel = new ChannelInfo("/p", "(Party)", "(Party)", null);
public ChannelInfo guildChannel = new ChannelInfo("/g", "(Guild)", "(Guild)", null);
public ChannelInfo infoChannel = new ChannelInfo("", "(Info)", "(Info)", null);
[Header("Other")]
public int maxLength = 70;
[Header("Events")]
public UnityEventString onSubmit;
public override void OnStartLocalPlayer()
{
// test messages
UIChat.singleton.AddMessage(new ChatMessage("", infoChannel.identifierIn, "Use /w NAME to whisper", "", infoChannel.textPrefab));
UIChat.singleton.AddMessage(new ChatMessage("", infoChannel.identifierIn, "Use /p for party chat", "", infoChannel.textPrefab));
UIChat.singleton.AddMessage(new ChatMessage("", infoChannel.identifierIn, "Use /g for guild chat", "", infoChannel.textPrefab));
UIChat.singleton.AddMessage(new ChatMessage("", infoChannel.identifierIn, "Or click on a message to reply", "", infoChannel.textPrefab));
UIChat.singleton.AddMessage(new ChatMessage("Someone", guildChannel.identifierIn, "Anyone here?", "/g ", guildChannel.textPrefab));
UIChat.singleton.AddMessage(new ChatMessage("Someone", partyChannel.identifierIn, "Let's hunt!", "/p ", partyChannel.textPrefab));
UIChat.singleton.AddMessage(new ChatMessage("Someone", whisperChannel.identifierIn, "Are you there?", "/w Someone ", whisperChannel.textPrefab));
UIChat.singleton.AddMessage(new ChatMessage("Someone", localChannel.identifierIn, "Hello!", "/w Someone ", localChannel.textPrefab));
}
// submit tries to send the string and then returns the new input text
[Client]
public string OnSubmit(string text)
{
// not empty and not only spaces?
if (!string.IsNullOrWhiteSpace(text))
{
// command in the commands list?
// note: we don't do 'break' so that one message could potentially
// be sent to multiple channels (see mmorpg local chat)
string lastCommand = "";
if (text.StartsWith(whisperChannel.command))
{
// whisper
(string user, string message) = ParsePM(whisperChannel.command, text);
if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(message))
{
if (user != name)
{
lastCommand = whisperChannel.command + " " + user + " ";
CmdMsgWhisper(user, message);
}
else Debug.Log("cant whisper to self");
}
else Debug.Log("invalid whisper format: " + user + "/" + message);
}
else if (!text.StartsWith("/"))
{
// local chat is special: it has no command
lastCommand = "";
CmdMsgLocal(text);
}
else if (text.StartsWith(partyChannel.command))
{
// party
string msg = ParseGeneral(partyChannel.command, text);
if (!string.IsNullOrWhiteSpace(msg))
{
lastCommand = partyChannel.command + " ";
CmdMsgParty(msg);
}
}
else if (text.StartsWith(guildChannel.command))
{
// guild
string msg = ParseGeneral(guildChannel.command, text);
if (!string.IsNullOrWhiteSpace(msg))
{
lastCommand = guildChannel.command + " ";
CmdMsgGuild(msg);
}
}
// addon system hooks
onSubmit.Invoke(text);
// input text should be set to lastcommand
return lastCommand;
}
// input text should be cleared
return "";
}
// parse a message of form "/command message"
internal static string ParseGeneral(string command, string msg)
{
// return message without command prefix (if any)
return msg.StartsWith(command + " ") ? msg.Substring(command.Length + 1) : "";
}
// parse a private message
internal static (string user, string message) ParsePM(string command, string pm)
{
// parse to /w content
string content = ParseGeneral(command, pm);
// now split the content in "user msg"
if (content != "")
{
// find the first space that separates the name and the message
int i = content.IndexOf(" ");
if (i >= 0)
{
string user = content.Substring(0, i);
string msg = content.Substring(i+1);
return (user, msg);
}
}
return ("", "");
}
// networking //////////////////////////////////////////////////////////////
[Command]
void CmdMsgLocal(string message)
{
if (message.Length > maxLength) return;
// it's local chat, so let's send it to all observers via ClientRpc
RpcMsgLocal(name, message);
}
[Command]
void CmdMsgParty(string message)
{
if (message.Length > maxLength) return;
// send message to all online party members
if (party.InParty())
{
foreach (string member in party.party.members)
{
if (Player.onlinePlayers.TryGetValue(member, out Player onlinePlayer))
{
// call TargetRpc on that GameObject for that connection
onlinePlayer.chat.TargetMsgParty(name, message);
}
}
}
}
[Command]
void CmdMsgGuild(string message)
{
if (message.Length > maxLength) return;
// send message to all online guild members
if (guild.InGuild())
{
foreach (GuildMember member in guild.guild.members)
{
if (Player.onlinePlayers.TryGetValue(member.name, out Player onlinePlayer))
{
// call TargetRpc on that GameObject for that connection
onlinePlayer.chat.TargetMsgGuild(name, message);
}
}
}
}
[Command]
void CmdMsgWhisper(string playerName, string message)
{
if (message.Length > maxLength) return;
// find the player with that name
if (Player.onlinePlayers.TryGetValue(playerName, out Player onlinePlayer))
{
// receiver gets a 'from' message, sender gets a 'to' message
// (call TargetRpc on that GameObject for that connection)
onlinePlayer.chat.TargetMsgWhisperFrom(name, message);
TargetMsgWhisperTo(playerName, message);
}
}
// send a global info message to everyone
[Server]
public void SendGlobalMessage(string message)
{
foreach (Player player in Player.onlinePlayers.Values)
player.chat.TargetMsgInfo(message);
}
// message handlers ////////////////////////////////////////////////////////
[TargetRpc]
public void TargetMsgWhisperFrom(string sender, string message)
{
// add message with identifierIn
string identifier = whisperChannel.identifierIn;
string reply = whisperChannel.command + " " + sender + " "; // whisper
UIChat.singleton.AddMessage(new ChatMessage(sender, identifier, message, reply, whisperChannel.textPrefab));
}
[TargetRpc]
public void TargetMsgWhisperTo(string receiver, string message)
{
// add message with identifierOut
string identifier = whisperChannel.identifierOut;
string reply = whisperChannel.command + " " + receiver + " "; // whisper
UIChat.singleton.AddMessage(new ChatMessage(receiver, identifier, message, reply, whisperChannel.textPrefab));
}
[ClientRpc]
public void RpcMsgLocal(string sender, string message)
{
// add message with identifierIn or Out depending on who sent it
string identifier = sender != name ? localChannel.identifierIn : localChannel.identifierOut;
string reply = whisperChannel.command + " " + sender + " "; // whisper
UIChat.singleton.AddMessage(new ChatMessage(sender, identifier, message, reply, localChannel.textPrefab));
}
[TargetRpc]
public void TargetMsgGuild(string sender, string message)
{
string reply = whisperChannel.command + " " + sender + " "; // whisper
UIChat.singleton.AddMessage(new ChatMessage(sender, guildChannel.identifierIn, message, reply, guildChannel.textPrefab));
}
[TargetRpc]
public void TargetMsgParty(string sender, string message)
{
string reply = whisperChannel.command + " " + sender + " "; // whisper
UIChat.singleton.AddMessage(new ChatMessage(sender, partyChannel.identifierIn, message, reply, partyChannel.textPrefab));
}
[TargetRpc]
public void TargetMsgInfo(string message)
{
AddMsgInfo(message);
}
// info message can be added from client too
public void AddMsgInfo(string message)
{
UIChat.singleton.AddMessage(new ChatMessage("", infoChannel.identifierIn, message, "", infoChannel.textPrefab));
}
}
}