From f89f308c525e9deebc6d2cf6416e27dfe1a299dc Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sun, 19 May 2019 16:03:38 +0200 Subject: Cleanup DiscoBot Project --- DiscoBot/DSA_Game/Save/Properties.cs | 88 ----------------------------------- DiscoBot/DSA_Game/Save/SaveCommand.cs | 82 -------------------------------- DiscoBot/DSA_Game/Save/Session.cs | 60 ------------------------ 3 files changed, 230 deletions(-) delete mode 100644 DiscoBot/DSA_Game/Save/Properties.cs delete mode 100644 DiscoBot/DSA_Game/Save/SaveCommand.cs delete mode 100644 DiscoBot/DSA_Game/Save/Session.cs (limited to 'DiscoBot/DSA_Game/Save') diff --git a/DiscoBot/DSA_Game/Save/Properties.cs b/DiscoBot/DSA_Game/Save/Properties.cs deleted file mode 100644 index 67d30b0..0000000 --- a/DiscoBot/DSA_Game/Save/Properties.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DiscoBot.DSA_Game.Save -{ - using System.Collections; - using System.IO; - using System.Reflection; - using System.Runtime.CompilerServices; - - using DiscoBot.Audio; - using DiscoBot.Auxiliary; - using DiscoBot.Commands; - - using Discord; - - using Newtonsoft.Json; - - public static class Properties - { - private static Dictionary objects; - - static Properties() - { - objects = new Dictionary(); - /*this.objects.Add("Sounds", new List()); - this.objects.Add("CommandInfos", new List());*/ - } - - public static List CommandInfos { get => objects["CommandInfo"] as List; set => objects["CommandInfo"] = value; } // use Properties.Commandinfos to access the abstract Object array - - public static List Sounds { get => objects["Sound"] as List; set => objects["Sound"] = value; } - - public static void Deserialize(string path = @"..\..\Properties") - { - - var files = Directory.GetFiles(path, "*.json"); - - foreach (string file in files) - { - try - { - string name = file.Split('\\').Last().Split('.')[0].Replace('-', '.'); - string data = File.ReadAllText(file); - Type type = Type.GetType(name); - if (data.StartsWith("[")) - { - type = typeof(List<>).MakeGenericType(type); - } - - var o = JsonConvert.DeserializeObject(data, type); - objects.Add(name.Split('.').Last(), o); - } - catch (Exception e) - { - // ignored - var log = new LogMessage(LogSeverity.Warning, "Properties", $"Laden von Save-File {file} fehlgeschlagen.", e); - Console.WriteLine(log); - } - - } - - } - - public static void Serialize(string path = @"..\..\Properties\") - { - try - { - foreach (var o in objects) - { - string assembly = o.Value is IList list ? ((IList)list)[0]?.GetType().FullName : o.Value.GetType().FullName; - - var name = path + assembly.Replace('.', '-') + ".json"; - File.WriteAllText(name, JsonConvert.SerializeObject(o.Value, Formatting.Indented)); // Deserialize Data and create CommandInfo Struct - } - } - catch (Exception e) - { - // ignored - var log = new LogMessage(LogSeverity.Warning, "Properties", $"Speichern von Save-File fehlgeschlagen.", e); - Console.WriteLine(log); - } - } - } -} diff --git a/DiscoBot/DSA_Game/Save/SaveCommand.cs b/DiscoBot/DSA_Game/Save/SaveCommand.cs deleted file mode 100644 index 1f160ec..0000000 --- a/DiscoBot/DSA_Game/Save/SaveCommand.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DiscoBot.DSA_Game.Save -{ - using System.Diagnostics; - using System.IO; - using System.Net; - using System.Net.Http; - - using DiscoBot.Auxiliary; - - using Discord.Commands; - - public class SaveCommand : ModuleBase - { - [Command("load"), Summary("Load Session")] - [Alias("session")] - public async Task LoadSessionAsync([Remainder, Summary("Session Name")] string name = "") - { - if (name.Equals("?") || name.Equals(string.Empty)) - { - await this.ReplyAsync($"Gespeicherte Sessions:"); - await this.ReplyAsync(this.ListSessions()); - return; - } - - var path = DSA_Game.Save.Session.DirectoryPath + @"\" + name; - - var files = Directory.GetFiles(path); - var session = files.OrderByDescending(x => Convert.ToInt32(x.Split('-').Last().Split('.').First())).First(); - Dsa.Session = Session.Load(session); - - await this.ReplyAsync($"{name} wurde geladen"); - } - - [Command("save", RunMode = RunMode.Async), Summary("Save Session")] - public async Task SessionSaveAsync([Remainder, Summary("Session Name")] string name = "") - { - //var sendFile = this.Context.Channel.SendWebFile("https://cdn.discordapp.com/attachments/377123019673567232/465615882048110603/giphy.gif"); - - if (name.Equals("?") || name.Equals(string.Empty)) - { - await this.ReplyAsync($"Gespeicherte Sessions:"); - await this.ReplyAsync(this.ListSessions()); - return; - } - - var path = DSA_Game.Save.Session.DirectoryPath + @"\" + name; - if (Directory.Exists(path)) - { - var files = Directory.GetFiles(path); - int current = files.Max(x => Convert.ToInt32(x.Split('-').Last().Split('.').First())); - Dsa.Session.SessionName = name; - Dsa.Session.Save(path + "\\" + name + $"-{++current}.json"); - } - else - { - Directory.CreateDirectory(path); - Dsa.Session.SessionName = name; - Dsa.Session.Save(path + "\\" + name + $"-0.json"); - } - - await this.ReplyAsync($"{name} wurde gespeichert"); - //await sendFile; - } - - private string[] ListSessions() - { - string[] dirs = Directory.GetDirectories(Session.DirectoryPath).OrderByDescending(x => new DirectoryInfo(x).LastAccessTime.Ticks).ToArray(); - for (int i = 0; i < dirs.Length; i++) - { - dirs[i] += "; " + new DirectoryInfo(dirs[i]).LastAccessTime; - } - - return dirs; - } - } -} diff --git a/DiscoBot/DSA_Game/Save/Session.cs b/DiscoBot/DSA_Game/Save/Session.cs deleted file mode 100644 index 578fa50..0000000 --- a/DiscoBot/DSA_Game/Save/Session.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DiscoBot.DSA_Game.Save -{ - using System.IO; - using System.Runtime.CompilerServices; - - using DiscoBot.DSA_Game.Characters; - - using Discord; - using Discord.Commands; - - using Newtonsoft.Json; - - public class Session - { - public static string DirectoryPath { get; set; } = @"..\..\sessions"; - - public ICommandContext GeneralContext { get; set; } - - public Dictionary Relation { get; set; } = new Dictionary(); // dictionary to match the char - - public List Chars { get; set; } = new List(); // list of all characters - - public string SessionName { get; set; } - - public static Session Load(string path = @"..\..\session.json") - { - try - { - return JsonConvert.DeserializeObject(File.ReadAllText(path)); // Deserialize Data and create Session Object - } - catch (Exception e) - { - // ignored - var log = new LogMessage(LogSeverity.Warning, "Properties", $"Laden von Save-File {path} fehlgeschlagen.", e); - Console.WriteLine(log); - return null; - } - } - - public void Save(string path = @"..\..\session.json") - { - try - { - File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented)); // Deserialize Data and create CommandInfo Struct - } - catch (Exception e) - { - var log = new LogMessage(LogSeverity.Warning, "Properties", $"Speichern von Save-File {path} fehlgeschlagen.", e); - Console.WriteLine(log); - // ignored - } - } - } -} -- cgit v1.2.3-54-g00ecf