Skip to content

Commit 4c2c874

Browse files
committed
i think ive fixed the massive amount of lag in the actual chat part, now it doesnt try so much or smthn (it only grabs the info from the file once when needed and stores it til otherwise)
1 parent 5b95c49 commit 4c2c874

3 files changed

Lines changed: 48 additions & 23 deletions

File tree

Additions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public class ChatListener {
327327
private readonly object lockObject = new object();
328328

329329
public string[] GetChatContent() {
330-
Thread.Sleep(50);
330+
//Thread.Sleep(50);
331331
lock (lockObject) {
332332
return ChatContent;
333333
}
@@ -336,12 +336,12 @@ public string[] GetChatContent() {
336336
public void StartListening(string ChatRoom) {
337337

338338
while (true) {
339-
Thread.Sleep(50);
340339
try {
341340
ChatContent = File.ReadLines(@"chats\"+ChatRoom+".txt").ToArray();
342341
} catch {
343342
Console.Write("");
344343
}
344+
Thread.Sleep(50);
345345
}
346346
}
347347
}

User.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,21 @@ public string Read_Messages_FileName(string TargetChat) {
195195
}
196196
}
197197

198-
public string[] Read_Messages_Chat(string Chat) {
199-
string[] DecryptedMessages = new string[File.ReadLines(@"chats\"+Chat+".txt").ToArray().Length-2];
198+
public string[] Read_Messages_Chat(string[] EncryptedMessages) {
199+
string[] DecryptedMessages = new string[EncryptedMessages.Length-2];
200200

201201

202-
Tuple<string,string> SymmetricKey = this.Read_Chat_SymmetricKey(Chat);
202+
Tuple<string,string> SymmetricKey = this.Read_Chat_SymmetricKey(EncryptedMessages);
203203

204204

205205
// for (int i = File.ReadLines(@"chats\"+Chat+".txt").ToArray().Length-1; i >= 2; i--) {
206206

207207
// }
208208

209209

210-
for (int i = 2; i < File.ReadLines(@"chats\"+Chat+".txt").ToArray().Length; i++) {
210+
for (int i = 2; i < EncryptedMessages.Length; i++) {
211211

212-
DecryptedMessages[i-2] = SymmetricEncryption.DecryptString(SymmetricKey.Item1, File.ReadLines(@"chats\"+Chat+".txt").ToArray()[i], SymmetricKey.Item2);
212+
DecryptedMessages[i-2] = SymmetricEncryption.DecryptString(SymmetricKey.Item1, EncryptedMessages[i], SymmetricKey.Item2);
213213

214214
}
215215

@@ -220,12 +220,12 @@ public string[] Read_Messages_Chat(string Chat) {
220220

221221

222222

223-
public Tuple<string, string> Read_Chat_SymmetricKey(string SelectedChat) {
223+
public Tuple<string, string> Read_Chat_SymmetricKey(string[] EncryptedMessages) {
224224
System.Threading.Thread.Sleep(500);
225225
string SymmetricKey;
226226
string iv;
227227

228-
string[] EncryptedMessages = File.ReadLines(@"chats\"+SelectedChat+".txt").ToArray();
228+
//string[] EncryptedMessages = File.ReadLines(@"chats\"+SelectedChat+".txt").ToArray();
229229

230230
// SymmetricKey = EncryptedMessages[0];
231231
// iv = EncryptedMessages[1];

program.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,14 @@ static void Main(string[] args)
182182
SelectedChat = CurrentUser.Read_Messages_FileName(SelectedChat);
183183

184184

185+
string[] ChatContent = File.ReadLines(@"chats\"+SelectedChat+".txt").ToArray();
185186

186-
string[] DecryptedChatContent = CurrentUser.Read_Messages_Chat(SelectedChat);
187+
188+
string[] DecryptedChatContent = CurrentUser.Read_Messages_Chat(ChatContent);
187189

188190
//User.Display.Content(DecryptedChatContent);
189191

190-
Tuple<string, string> SymmetricKey = CurrentUser.Read_Chat_SymmetricKey(SelectedChat);
192+
Tuple<string, string> SymmetricKey = CurrentUser.Read_Chat_SymmetricKey(ChatContent);
191193

192194

193195
// while (true) { System.Threading.Thread.Sleep(5000); }
@@ -261,40 +263,63 @@ public static void PrivateChatLoop(string SelectedChat, Tuple<int, int> BoardDim
261263
TypedText = ""; // Move this declaration outside the while loop
262264
string[] ChatMessages = {"TEMPVALUE", "TEMPVALUE"};
263265

266+
string[] DecryptedMessages = {"TEMPVALUE", "TEMPVALUE"};
267+
264268
TextListener.SetPointer(0);
265269
TextListener.SetTypedText(" ");
266270
TextListener.SetEndProgram(false);
267271

268272
TextListener.StartListening();
269273
while (true)
270274
{
271-
275+
// Console.WriteLine("start");
272276
Thread.Sleep(50);
273277

274-
if (TextListener.GetEndProgram()) {
275-
if (!string.IsNullOrEmpty(TypedText)) {
278+
if (TextListener.GetEndProgram() && !string.IsNullOrEmpty(TypedText)) {
276279

277280
if (BadWords.BadWords.list.Any(TypedText.Contains)) {
278281
Console.WriteLine("bad Word Found, Message not Sent");
279282
System.Threading.Thread.Sleep(1000);
280283
} else {
281-
CurrentUser.Write_Messages_Chat(SymmetricKey, SelectedChat, TypedText);
284+
CurrentUser.Write_Messages_Chat(SymmetricKey, SelectedChat, TypedText);
282285

283-
TextListener.StopListening();
284-
TextListener.SetTypedText("");
285-
TextListener.SetEndProgram(false);
286-
TextListener.StartListening();
286+
TextListener.StopListening();
287+
TextListener.SetTypedText("");
288+
TextListener.SetEndProgram(false);
289+
TextListener.StartListening();
287290
}
288-
}
289291
}
290292

291-
//if (TypedText != TextListener.GetTypedText() || ChatMessages.SequenceEqual(ChatListener.GetChatContent())) {
293+
if (TypedText != TextListener.GetTypedText()) {
294+
295+
// Console.WriteLine("text start");
292296

293297
TypedText = TextListener.GetTypedText();
298+
299+
User.User.Display.Content(DecryptedMessages,BoardDimensions,TypedText, true);
300+
// Console.WriteLine("text end");
301+
302+
}
303+
304+
// Console.WriteLine("line");
305+
// Console.WriteLine(ChatMessages.Length);
306+
// Console.WriteLine("line");
307+
// Console.WriteLine(ChatListener.GetChatContent());
308+
// Console.WriteLine("line");
309+
310+
if (!ChatMessages.SequenceEqual(ChatListener.GetChatContent())) {
311+
312+
// Console.WriteLine("chat start");
313+
294314
ChatMessages = ChatListener.GetChatContent();
315+
DecryptedMessages = CurrentUser.Read_Messages_Chat(ChatMessages);
295316

296-
User.User.Display.Content(CurrentUser.Read_Messages_Chat(SelectedChat),BoardDimensions,TypedText, true);
297-
//}
317+
318+
User.User.Display.Content(DecryptedMessages,BoardDimensions,TypedText, true);
319+
// Console.WriteLine("chat end");
320+
321+
}
322+
// Console.WriteLine("end");
298323
}
299324
}
300325
}

0 commit comments

Comments
 (0)