summaryrefslogtreecommitdiff
path: root/DiscoBot/DSA_Game
diff options
context:
space:
mode:
authorTrueDoctor <d-kobert@web.de>2018-06-18 11:39:41 +0200
committerTrueDoctor <d-kobert@web.de>2018-06-18 11:39:41 +0200
commit68e49d3b0936101deba1251e95b9788716c0722a (patch)
tree431d8b26148505ad8b5584fd9e7c2db644bcba15 /DiscoBot/DSA_Game
parentba1f440d0762ab77cd3f08734043df7db5b4329e (diff)
reworked Properties, to split each fiel into a different File
Diffstat (limited to 'DiscoBot/DSA_Game')
-rw-r--r--DiscoBot/DSA_Game/Dsa.cs36
-rw-r--r--DiscoBot/DSA_Game/Save/Properties.cs70
-rw-r--r--DiscoBot/DSA_Game/Save/SaveCommand.cs43
-rw-r--r--DiscoBot/DSA_Game/Save/Session.cs2
4 files changed, 105 insertions, 46 deletions
diff --git a/DiscoBot/DSA_Game/Dsa.cs b/DiscoBot/DSA_Game/Dsa.cs
index 44175b6..d640df9 100644
--- a/DiscoBot/DSA_Game/Dsa.cs
+++ b/DiscoBot/DSA_Game/Dsa.cs
@@ -18,38 +18,14 @@
public static AudioService Service { get; set; }
- public static Dictionary<string, string> Relation { get; set; } = new Dictionary<string, string>(); // dictionary to match the char
-
public static List<ICharacter> Chars { get; set; } = new List<ICharacter>(); // list of all characters
public static List<Talent> Talente { get; set; } = new List<Talent>();
- public static Properties Properties { get; set; }
-
public static Session Session { get; set; }
public static void Startup()
{
- Relation.Add("The Doctor", "Numeri Illuminus"); // Relation
- Relation.Add("Tardis", "Helga von Drachenei, Tausendsasserin"); // "Numeri Illuminus");
- Relation.Add("DSA Bot", "Morla"); // "Felis Exodus Schattenwald");
- Relation.Add("Morla", "Morla");
- Relation.Add("Rhoktar", "Rhoktar4");
- Relation.Add("MagicBro5", "Krenko");
- Relation.Add("Nicolas", "Hartmut Reiher");
- Relation.Add("TrueKuehli", "Ledur Torfinson");
-
- // Relation.Add("Papo","Gwendelson");
- // Relation.Add("Papo", "Pump aus der Gosse");
-
- // Nachteile für LE, AE, MR
- // Relation.Add("Papo", "Angilbert Arres");
-
- // Vorteile für LE, AE, MR
- Relation.Add("Papo", "Beef");
-
- // Relation.Add("Papo", "Astrallos");
- Relation.Add("Potus", "Potus");
// relation.Add("Papo", "Pump aus der Gosse");
foreach (var filename in Directory.GetFiles("helden", "*.xml"))
@@ -59,16 +35,16 @@
.Where(c => !Talente.Exists(v => v.Name.Equals(c.Name))).ToList().ForEach(v => Talente.Add(v));
}
- Properties = Properties.Deserialize();
+ Properties.Deserialize();
Properties.Serialize();
Talente = Talente.OrderBy(x => x.Name).ToList();
- Session = new Session();
- List<SaveChar> save = Chars.Select(SaveChar.FromICharacter).ToList();
- Session.Chars = save.Select(x=>x as ICharacter).ToList();
- Session.GeneralContext = GeneralContext;
- Session.Relation = Relation;
+ Session = new Session
+ {
+ Chars = Chars.Select(x => SaveChar.FromICharacter(x) as ICharacter).ToList(),
+ GeneralContext = GeneralContext
+ };
Session.Save();
}
}
diff --git a/DiscoBot/DSA_Game/Save/Properties.cs b/DiscoBot/DSA_Game/Save/Properties.cs
index fe2f798..45bad4a 100644
--- a/DiscoBot/DSA_Game/Save/Properties.cs
+++ b/DiscoBot/DSA_Game/Save/Properties.cs
@@ -6,38 +6,76 @@ 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 class Properties
+ public static class Properties
{
- public List<CommandInfo> CommandInfos { get; set; }
+ private static Dictionary<string, object> objects;
- public List<Sound> Sounds { get; set; }
+ static Properties()
+ {
+ objects = new Dictionary<string, object>();
+ /*this.objects.Add("Sounds", new List<Sound>());
+ this.objects.Add("CommandInfos", new List<CommandInfo>());*/
+ }
- public static Properties Deserialize(string path = @"..\..\Properties.json")
- {
- try
- {
- return JsonConvert.DeserializeObject<Properties>(File.ReadAllText(path)); // Deserialize Data and create CommandInfo Struct
- }
- catch (Exception e)
- {
- // ignored
- return null;
- }
+ public static List<CommandInfo> CommandInfos { get => objects["CommandInfo"] as List<CommandInfo>; set => objects["CommandInfo"] = value; } // use Properties.Commandinfos to access the abstract Object array
+
+ public static List<Sound> Sounds { get => objects["Sound"] as List<Sound>; set => objects["Sound"] = value; }
+
+ public static void Deserialize(string path = @"..\..\sessions")
+ {
+
+ 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 void Serialize(string path = @"..\..\Properties.json")
+ public static void Serialize(string path = @"..\..\sessions\")
{
try
{
- File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented)); // Deserialize Data and create CommandInfo Struct
+ 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)
{
diff --git a/DiscoBot/DSA_Game/Save/SaveCommand.cs b/DiscoBot/DSA_Game/Save/SaveCommand.cs
new file mode 100644
index 0000000..310d38a
--- /dev/null
+++ b/DiscoBot/DSA_Game/Save/SaveCommand.cs
@@ -0,0 +1,43 @@
+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.Net.Http;
+
+ using DiscoBot.Auxiliary;
+
+ using Discord.Commands;
+
+ public class SaveCommand : ModuleBase
+ {
+ [Command("save"), Summary("Save Session")]
+ public async Task ReportAsync()
+ {
+
+
+ await this.ReplyAsync($"Dein report wurde hinzugefügt");
+ }
+
+ [Command("save"), Summary("Save Session")]
+ public async Task ReportAsync([Remainder, Summary("Session Name")] string name)
+ {
+ if (name.Equals("?"))
+ {
+ await this.ReplyAsync($"Gespeicherte Sessions:");
+ await this.ReplyAsync(this.ListSessions());
+ }
+ }
+
+ private string[] ListSessions()
+ {
+ return Directory.GetDirectories(@"..\..\sessions");
+ }
+
+
+ }
+}
diff --git a/DiscoBot/DSA_Game/Save/Session.cs b/DiscoBot/DSA_Game/Save/Session.cs
index b0a34a5..15ccc18 100644
--- a/DiscoBot/DSA_Game/Save/Session.cs
+++ b/DiscoBot/DSA_Game/Save/Session.cs
@@ -22,6 +22,8 @@ namespace DiscoBot.DSA_Game.Save
public List<ICharacter> Chars { get; set; } = new List<ICharacter>(); // list of all characters
+ public string SessionName { get; set; }
+
public static Session Load(string path = @"..\..\session.json")
{
try