This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chat.cs
289 lines (243 loc) · 9.09 KB
/
Chat.cs
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
using System.Net;
using YeahGame.Messages;
namespace YeahGame;
using MessageSource = (bool IsServer, bool IsSystem, IPEndPoint? EndPoint);
public class Chat
{
const int MessagesShownWhenInactive = 5;
const int MessagesShownWhenActive = 15;
const int TimeToHideMessage = 10;
public struct Message
{
public MessageSource Source;
public float Time;
public string Content;
public readonly string TimePrefix => $"[{TimeSpan.FromSeconds(Time):hh\\:mm\\:ss}]";
public readonly string SourcePrefix
{
get
{
if (Source.IsSystem)
{ return string.Empty; }
if (Game.Connection.TryGetUserInfo(Source.EndPoint, out ConnectionUserDetails sourceInfo) &&
sourceInfo.Details is not null)
{ return $"<{sourceInfo.Details.Username}>"; }
else if (Source.EndPoint is not null)
{ return $"<{Source.EndPoint}>"; }
else if (Source.IsServer)
{ return $"<SERVER>"; }
else
{ return $"<?>"; }
}
}
public readonly string ContentText => Content.Trim();
public Message(MessageSource source, float time, string content)
{
Source = source;
Time = time;
Content = content;
}
}
public SmallRect Rect
{
get
{
Message[] messages = GetMessages().ToArray();
SmallRect result = new(
1, Game.Renderer.Height - 2 - Math.Clamp(messages.Length, 0, _isChatting ? MessagesShownWhenActive : MessagesShownWhenInactive),
0, Math.Clamp(messages.Length - 1, 0, _isChatting ? MessagesShownWhenActive : MessagesShownWhenInactive));
for (int i = 0; i < messages.Length; i++)
{
result.Width = Math.Max(result.Width, (short)(messages[i].TimePrefix.Length + messages[i].SourcePrefix.Length + messages[i].ContentText.Length + 2));
}
if (_isChatting)
{
result = result.Union(new SmallRect(1, Game.Renderer.Height - 2, InputFieldWidth, 1));
}
else if (Touch.IsTouchDevice)
{
result = result.Union(new SmallRect(1, Game.Renderer.Height - 2, 10, 1));
}
result = result.Margin(-1);
return result;
}
}
public bool IsChatting => _isChatting;
readonly List<Message> _chatMessages = new()
{
// new Message((true, null), (float)Time.NowNoCache, "a"),
// new Message((true, null), (float)Time.NowNoCache, "b"),
// new Message((true, null), (float)Time.NowNoCache, "c"),
// new Message((true, null), (float)Time.NowNoCache, "d"),
// new Message((true, null), (float)Time.NowNoCache, "e"),
// new Message((true, null), (float)Time.NowNoCache, "f"),
// new Message((true, null), (float)Time.NowNoCache, "g"),
// new Message((true, null), (float)Time.NowNoCache, "h"),
// new Message((true, null), (float)Time.NowNoCache, "i"),
// new Message((true, null), (float)Time.NowNoCache, "j"),
// new Message((true, null), (float)Time.NowNoCache, "k"),
// new Message((true, null), (float)Time.NowNoCache, "l"),
// new Message((true, null), (float)Time.NowNoCache, "m"),
// new Message((true, null), (float)Time.NowNoCache, "n"),
// new Message((true, null), (float)Time.NowNoCache, "o"),
// new Message((true, null), (float)Time.NowNoCache, "p"),
};
bool _isChatting = false;
readonly ConsoleInputField _chatInput = new(null) { NeverLoseFocus = true, Label = "Chat" };
int _scroll = 0;
bool _isSending = false;
static short InputFieldWidth
{
get
{
if (Touch.IsTouchDevice) return 10;
return 30;
}
}
void OnSent()
{
_isSending = false;
}
public void Send(string message)
{
if (string.IsNullOrEmpty(message))
{ return; }
if (!Game.IsOffline)
{
_isSending = !Game.IsServer || Game.Connection.Connections.Count > 0;
Game.Connection.Send(new ChatMessage()
{
Content = message,
Source = Game.Connection.LocalEndPoint,
SourceIsServer = Game.IsServer,
SourceIsSystem = false,
Time = Time.Now,
ShouldAck = true,
Callback = OnSent,
});
}
if (Game.IsServer ||
Game.IsOffline)
{ Add(new Message((true, false, Game.Connection.LocalEndPoint), (float)Time.NowNoCache, message)); }
}
public void SendSystem(string message)
{
if (string.IsNullOrEmpty(message))
{ return; }
if (!Game.IsOffline)
{
_isSending = !Game.IsServer || Game.Connection.Connections.Count > 0;
Game.Connection.Send(new ChatMessage()
{
Content = message,
Source = Game.Connection.LocalEndPoint,
SourceIsServer = Game.IsServer,
SourceIsSystem = true,
Time = Time.Now,
ShouldAck = true,
Callback = OnSent,
});
}
if (Game.IsServer ||
Game.IsOffline)
{ Add(new Message((true, true, Game.Connection.LocalEndPoint), (float)Time.NowNoCache, message)); }
}
public void Render()
{
if (Keyboard.IsKeyDown('\r'))
{
if (_isChatting)
{
string msgContent = _chatInput.Value.ToString().Trim();
_chatInput.Clear();
Send(msgContent);
_isChatting = false;
}
else
{
_chatInput.Clear();
_isChatting = true;
}
}
int y = Game.Renderer.Height - 4;
Message[] messages = GetMessages().ToArray();
for (int i = 0; i < messages.Length; i++)
{
ref Message message = ref messages[i];
int x = 1;
string timeText = message.TimePrefix;
Game.Renderer.Text(x, y, timeText, CharColor.Gray);
x += timeText.Length + 1;
if (!message.Source.IsSystem)
{
string sourceText = message.SourcePrefix;
Game.Renderer.Text(x, y, sourceText, CharColor.Silver);
x += sourceText.Length + 1;
}
string contentText = message.ContentText;
Game.Renderer.Text(x, y, contentText, message.Source.IsSystem ? CharColor.BrightYellow : CharColor.Silver);
// x += contentText.Length + 1;
y--;
}
if (_isChatting)
{
if (Rect.Contains(Mouse.RecordedConsolePosition))
{
_scroll += Mouse.ScrollDelta;
_scroll = Math.Clamp(_scroll, 0, messages.Length);
}
_chatInput.IsActive = true;
Game.Renderer.Text(2, Game.Renderer.Height - 2, ">");
Game.Renderer.InputField(new SmallRect(4, Game.Renderer.Height - 2, InputFieldWidth, 1), Styles.InputFieldStyle, _chatInput);
if (Game.Renderer.Button(new SmallRect(2, Game.Renderer.Height - 3, 5, 1), "Send", Styles.ButtonStyle))
{
string msgContent = _chatInput.Value.ToString().Trim();
_chatInput.Clear();
Send(msgContent);
_isChatting = false;
}
}
else
{
if (_isSending)
{ Game.Renderer.Text(2, Game.Renderer.Height - 3, "Sending ..."); }
if (Touch.IsTouchDevice)
{
if (Game.Renderer.Button(new SmallRect(1, Game.Renderer.Height - 2, 10, 1), "Open Chat", Styles.ButtonStyle))
{
_isChatting = true;
}
}
}
}
void Add(Message message)
{
_chatMessages.Add(message);
_chatMessages.Sort((a, b) => a.Time.CompareTo(b.Time));
}
public void AddSystem(string message) => Add(new Message((false, true, null), (float)Time.NowNoCache, message));
public void Feed(ChatMessage message, IPEndPoint source)
{
Add(new Message((message.SourceIsServer, message.SourceIsSystem, message.Source ?? source), message.Time, message.Content));
}
public IEnumerable<Message> GetMessages()
{
if (_isChatting)
{
int last = Math.Max(0, _chatMessages.Count - MessagesShownWhenActive);
for (int i = _chatMessages.Count - 1; i >= last; i--)
{
yield return _chatMessages[i];
}
}
else
{
int last = Math.Max(0, _chatMessages.Count - MessagesShownWhenInactive);
for (int i = _chatMessages.Count - 1; i >= last; i--)
{
if (Time.Now - _chatMessages[i].Time >= TimeToHideMessage) continue;
yield return _chatMessages[i];
}
}
}
}