From eeea23839887fb5f00ee34b14b50c715e0222f78 Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Thu, 23 Sep 2021 10:50:40 +0200 Subject: [PATCH] Saving and loading but still has error(Pls fix) --- PolyChat/Controller.cs | 94 ++++++++++++++++++++++++++++++++++++++- PolyChat/MainPage.xaml.cs | 1 + 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 2792c66..8349009 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -5,6 +5,9 @@ using System.Net; using PolyChat.Models; using SocketIOSharp.Server; using SocketIOSharp.Server.Client; +using System.IO; +using System.Threading; +using System; namespace PolyChat { @@ -31,6 +34,8 @@ namespace PolyChat { UIController = uiController; OwnIP = getIP(); + loadChats(); + //SaveChats("10", "{das ist ein test}"); Serve(); } @@ -93,10 +98,12 @@ namespace PolyChat Debug.WriteLine($"{type} -> {ip} content: {content}"); JObject json = new JObject( new JProperty("type", type), - new JProperty("content", content) + new JProperty("content", content), + new JProperty("timestamp", DateTime.Now.ToString()) ); Debug.WriteLine($"json: {json.ToString()}"); Connections[ip].SendMessage(json.ToString()); + SaveChats(ip, json.ToString()); } private void OnMessage(string ip, JToken[] data) @@ -106,6 +113,7 @@ namespace PolyChat { Debug.WriteLine("RAW: " + data[0]); UIController.OnIncomingMessage(ip, data[0].ToString()); + SaveChats(ip, data[0].ToString()); } else Debug.WriteLine("Undefined: " + data); } @@ -149,5 +157,89 @@ namespace PolyChat } return null; } + + /// + /// sends chatlogs as json array to uiController wit corrosponding ip + /// + /// + public void loadChats() + { + //load dir and create if non existant + if (Directory.Exists("U:\\PolyChat\\Saves")) + { + Debug.WriteLine("--Path exists.--"); + } + else + { + Directory.CreateDirectory("U:\\PolyChat\\Saves"); + Debug.WriteLine("--Path Created--."); + } + + //go through all files and send ip and json array to ui + String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); + if (filepaths.Length > 0) + { + foreach (String path in filepaths) + { + Debug.WriteLine("---Loading Saves---"); + Debug.WriteLine("--" + path + "--"); + String jsonArr = File.ReadAllText(path); + String ip = Path.GetFileName(path); + ip = ip.Substring(0, ip.Length - 4); + Debug.WriteLine("-" + ip + "-"); + Debug.WriteLine(jsonArr); + UIController.OnIncomingMessages(ip, jsonArr); + } + } + + } + + /// + /// Saves incoming chat message to + /// + /// + /// + public void SaveChats(String ip, String json) + { + //Vielleicht noch so machen dass die mit gleicher ip nacheinander gemacht + //werden damit es ncith zu überschreibungen kommt vielleicth auch ganz oben oder am ende ne + //writing flag setzen oder auch in der datei selbst ne flag setzen + //also save fils from myself + new Thread(() => + { + if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) + { + Debug.WriteLine("--File allready exists--"); + //check for integraty of file + string output = File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"); + Debug.WriteLine($"---{output}---"); + if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]")) + { + Debug.WriteLine("--adding new chatmessage--"); + //structure intact + //save new chat + String saved = output.Substring(0, output.Length - 1); + output = saved + ", " + json + " ]"; + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output); + } + else + { + Debug.WriteLine("--Structure not intact--"); + Debug.WriteLine("--redoing file--"); + //structure not intact + //redo file + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); + } + } + else + { + Debug.WriteLine("--Creating new File--"); + //setup file + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); + } + }).Start(); + } } } diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index 9f1fded..7cc022e 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -164,6 +164,7 @@ namespace PolyChat }); } + private void OnDeleteChat(object sender = null, RoutedEventArgs e = null) { Controller.CloseChat(selectedPartner.Code);