From 632781d1adf54287ecfe7cbcbc17074e14a769b2 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Sat, 29 Sep 2018 23:59:42 +0200 Subject: added database methods --- DSACore/Models/Database/Advantage.cs | 19 +++++++++++++ DSACore/Models/Database/Char.cs | 27 ------------------- DSACore/Models/Database/CharSpell.cs | 10 +++++-- DSACore/Models/Database/DatabaseChar.cs | 47 +++++++++++++++++++++++++++++++++ DSACore/Models/Database/Field.cs | 10 +++++-- DSACore/Models/Database/GeneralSpell.cs | 2 +- DSACore/Models/Database/Group.cs | 9 ++++--- DSACore/Models/Database/GroupChar.cs | 14 +++++----- DSACore/Models/Database/Inventory.cs | 8 +++--- DSACore/Models/Database/Roll.cs | 2 +- DSACore/Models/Database/Talent.cs | 4 +-- DSACore/Models/Database/Weapon.cs | 8 +++--- DSACore/Models/Database/WeaponTalent.cs | 19 +++++++++---- 13 files changed, 120 insertions(+), 59 deletions(-) create mode 100644 DSACore/Models/Database/Advantage.cs delete mode 100644 DSACore/Models/Database/Char.cs create mode 100644 DSACore/Models/Database/DatabaseChar.cs (limited to 'DSACore/Models/Database') diff --git a/DSACore/Models/Database/Advantage.cs b/DSACore/Models/Database/Advantage.cs new file mode 100644 index 0000000..67965fc --- /dev/null +++ b/DSACore/Models/Database/Advantage.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DSACore.Models.Database +{ + public class Advantage + { + public Advantage(string name, string value = "") + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + Value = value ?? throw new ArgumentNullException(nameof(value)); + } + + public string Name { get; set; } + public string Value { get; set; } + } +} diff --git a/DSACore/Models/Database/Char.cs b/DSACore/Models/Database/Char.cs deleted file mode 100644 index 04c16f0..0000000 --- a/DSACore/Models/Database/Char.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace DSACore.Models.Database -{ - public class Char - { - private int Id { get; set; } - - private string Name { get; set; } - - private string Rasse { get; set; } - - private List Skills { get; set; } = new List(); - - private List Talents { get; set; } = new List(); - - private List Advantages { get; set; } = new List(); - - private List Spells { get; set; } = new List(); - - private List WeaponTalents { get; set; } = new List(); - - } -} diff --git a/DSACore/Models/Database/CharSpell.cs b/DSACore/Models/Database/CharSpell.cs index cdb5447..670488c 100644 --- a/DSACore/Models/Database/CharSpell.cs +++ b/DSACore/Models/Database/CharSpell.cs @@ -7,7 +7,13 @@ namespace DSACore.Models.Database { public class CharSpell { - private string representation { get; set; } - private int value { get; set; } + public CharSpell(string representation, int value) + { + this.representation = representation ?? throw new ArgumentNullException(nameof(representation)); + this.value = value; + } + + public string representation { get; set; } + public int value { get; set; } } } diff --git a/DSACore/Models/Database/DatabaseChar.cs b/DSACore/Models/Database/DatabaseChar.cs new file mode 100644 index 0000000..03383b8 --- /dev/null +++ b/DSACore/Models/Database/DatabaseChar.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSALib; + +namespace DSACore.Models.Database +{ + public class DatabaseChar + { + public int Id { get; set; } + + public string Name { get; set; } + + public string Rasse { get; set; } + + public List Skills { get; set; } = new List(); + + public List Talents { get; set; } = new List(); + + public List Advantages { get; set; } = new List(); + + public List Spells { get; set; } = new List(); + + public List WeaponTalents { get; set; } = new List(); + + + public static void LoadChar(DSA_Game.Characters.Character file, out GroupChar group, out DatabaseChar data) + { + group = new GroupChar(); + data = new DatabaseChar(); + + group.Name = file.Name.Split(' ').First(); + group.Weapon = new Weapon(); + group.Lp = group.LpMax = file.Lebenspunkte_Basis; + group.As = group.AsMax = file.Astralpunkte_Basis; + group.Weapon = new Weapon(); + + data.Name = file.Name; + data.Advantages = file.Vorteile.Select(x => new Advantage(x.Name, x.Value)).ToList(); + data.Skills = file.Eigenschaften.Select(x => new Field(x.Key, x.Value)).ToList(); + data.Spells = file.Zauber.Select(x => new CharSpell(x.Representation, x.Value)).ToList(); + data.Talents = file.Talente.Select(x => new Field(x.Name, x.Value)).ToList(); + data.WeaponTalents = file.Kampftalente.Select(x => new WeaponTalent(x.Name, x.At, x.Pa)).ToList(); + } + } +} diff --git a/DSACore/Models/Database/Field.cs b/DSACore/Models/Database/Field.cs index fe1ea1f..b14d9da 100644 --- a/DSACore/Models/Database/Field.cs +++ b/DSACore/Models/Database/Field.cs @@ -7,7 +7,13 @@ namespace DSACore.Models.Database { public class Field { - private string Name { get; set; } - private int value { get; set; } + public Field(string name, int value = 0) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + this.Value = value; + } + + public string Name { get; set; } + public int Value { get; set; } } } diff --git a/DSACore/Models/Database/GeneralSpell.cs b/DSACore/Models/Database/GeneralSpell.cs index 4f2a8cb..807a09b 100644 --- a/DSACore/Models/Database/GeneralSpell.cs +++ b/DSACore/Models/Database/GeneralSpell.cs @@ -7,6 +7,6 @@ namespace DSACore.Models.Database { public class GeneralSpell : Talent { - private string Comlexity = "A"; + public string Comlexity = "A"; } } diff --git a/DSACore/Models/Database/Group.cs b/DSACore/Models/Database/Group.cs index fb17909..0273aed 100644 --- a/DSACore/Models/Database/Group.cs +++ b/DSACore/Models/Database/Group.cs @@ -7,9 +7,10 @@ namespace DSACore.Models.Database { public class Group { - private string Name { get; set; } - private string Discord { get; set; } - private int Id { get; set; } - private List Chars { get; set; }= new List(); + public string Name { get; set; } + public string Discord { get; set; } + public string Password { get; set; } + public int Id { get; set; } + public List Chars { get; set; }= new List(); } } diff --git a/DSACore/Models/Database/GroupChar.cs b/DSACore/Models/Database/GroupChar.cs index 66e203b..1dfc4ea 100644 --- a/DSACore/Models/Database/GroupChar.cs +++ b/DSACore/Models/Database/GroupChar.cs @@ -7,12 +7,12 @@ namespace DSACore.Models.Database { public class GroupChar { - private string Name { get; set; } - private int Id { get; set; } - private int Lp { get; set; } - private int LpMax { get; set; } - private int As { get; set; } - private int AsMax { get; set; } - private Weapon Weapon { get; set; } + public string Name { get; set; } + public int Id { get; set; } + public int Lp { get; set; } + public int LpMax { get; set; } + public int As { get; set; } + public int AsMax { get; set; } + public Weapon Weapon { get; set; } } } diff --git a/DSACore/Models/Database/Inventory.cs b/DSACore/Models/Database/Inventory.cs index 8e525c6..e6b47ec 100644 --- a/DSACore/Models/Database/Inventory.cs +++ b/DSACore/Models/Database/Inventory.cs @@ -7,9 +7,9 @@ namespace DSACore.Models.Database { public class Inventory { - private int Id { get; set; } - private List Items { get; set; } = new List(); - private List Food { get; set; } = new List(); - private List Weapons { get; set; } = new List(); + public int Id { get; set; } + public Dictionary Items { get; set; } = new Dictionary(); + public Dictionary Food { get; set; } = new Dictionary(); + public List Weapons { get; set; } = new List(); } } diff --git a/DSACore/Models/Database/Roll.cs b/DSACore/Models/Database/Roll.cs index c931e70..547fde4 100644 --- a/DSACore/Models/Database/Roll.cs +++ b/DSACore/Models/Database/Roll.cs @@ -7,6 +7,6 @@ namespace DSACore.Models.Database { public class Roll { - private string[] Skills { get; set; } = new string[3]; + public string[] Skills { get; set; } = new string[3]; } } diff --git a/DSACore/Models/Database/Talent.cs b/DSACore/Models/Database/Talent.cs index d7bfaae..6bd8cba 100644 --- a/DSACore/Models/Database/Talent.cs +++ b/DSACore/Models/Database/Talent.cs @@ -7,8 +7,8 @@ namespace DSACore.Models.Database { public class Talent { - private string Name { get; set; } + public string Name { get; set; } - private Roll Roll { get; set; } = new Roll(); + public Roll Roll { get; set; } = new Roll(); } } diff --git a/DSACore/Models/Database/Weapon.cs b/DSACore/Models/Database/Weapon.cs index b72ec20..9c75b30 100644 --- a/DSACore/Models/Database/Weapon.cs +++ b/DSACore/Models/Database/Weapon.cs @@ -7,9 +7,9 @@ namespace DSACore.Models.Database { public class Weapon { - private string Name { get; set; } - private string Damage { get; set; } - private string WeaponTalent { get; set; } - private string Modifier { get; set; } + public string Name { get; set; } + public string Damage { get; set; } + public string WeaponTalent { get; set; } + public string Modifier { get; set; } } } diff --git a/DSACore/Models/Database/WeaponTalent.cs b/DSACore/Models/Database/WeaponTalent.cs index 37d7150..4b98d24 100644 --- a/DSACore/Models/Database/WeaponTalent.cs +++ b/DSACore/Models/Database/WeaponTalent.cs @@ -1,9 +1,18 @@ -namespace DSACore.Models.Database +using System; + +namespace DSACore.Models.Database { - class WeaponTalent + public class WeaponTalent { - private string Name { get; set; } - private int At { get; set; } - private int Pa { get; set; } + public WeaponTalent(string name, int at, int pa) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + At = at; + Pa = pa; + } + + public string Name { get; set; } + public int At { get; set; } + public int Pa { get; set; } } } \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 560f454c9beb2f691730b126fc6b3e23d68d6681 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Sun, 30 Sep 2018 02:06:02 +0200 Subject: added mot of the database infrastructure --- DSACore/DSA_Game/Characters/Character.cs | 15 ++++++--- DSACore/DSA_Game/Dsa.cs | 18 ++++++++++ DSACore/FireBase/Database.cs | 16 ++++----- DSACore/Hubs/ChatHub.cs | 57 +++++++++++++++++++++++++------- DSACore/Models/Database/GeneralSpell.cs | 11 +++++- DSACore/Models/Database/Roll.cs | 12 ------- DSACore/Models/Database/Talent.cs | 8 ++++- DSACore/Models/Network/User.cs | 1 + 8 files changed, 100 insertions(+), 38 deletions(-) delete mode 100644 DSACore/Models/Database/Roll.cs (limited to 'DSACore/Models/Database') diff --git a/DSACore/DSA_Game/Characters/Character.cs b/DSACore/DSA_Game/Characters/Character.cs index 8fad2ef..247fc58 100644 --- a/DSACore/DSA_Game/Characters/Character.cs +++ b/DSACore/DSA_Game/Characters/Character.cs @@ -1,4 +1,5 @@ -using DSACore.Auxiliary; +using System.IO; +using DSACore.Auxiliary; using DSALib; using DSALib.Characters; @@ -28,7 +29,12 @@ namespace DSACore.DSA_Game.Characters public Character(string path) : this() { - this.Load(path); // load + this.Load(new MemoryStream(File.ReadAllBytes(path))); // load + this.Post_process(); // calculate derived values + } + public Character(MemoryStream stream) : this() + { + this.Load(stream); // load this.Post_process(); // calculate derived values } @@ -198,10 +204,11 @@ namespace DSACore.DSA_Game.Characters } + - private void Load(string path) + private void Load(MemoryStream stream) { - var reader = new XmlTextReader(path); + var reader = new XmlTextReader(stream); while (reader.Read()) { // read until he hits keywords diff --git a/DSACore/DSA_Game/Dsa.cs b/DSACore/DSA_Game/Dsa.cs index d47c5b6..824872c 100644 --- a/DSACore/DSA_Game/Dsa.cs +++ b/DSACore/DSA_Game/Dsa.cs @@ -1,4 +1,5 @@ using System; +using DSACore.FireBase; using DSALib; using DSALib.Characters; using Microsoft.EntityFrameworkCore.Design; @@ -21,6 +22,8 @@ namespace DSACore.DSA_Game public static List Talente { get; set; } = new List(); + public static List Zauber { get; set; } = new List(); + public static Session Session { get @@ -51,12 +54,27 @@ namespace DSACore.DSA_Game Chars.Add(new Character(filename)); (Chars.Last() as Character)?.Talente.Select(x => new Talent(x.Name, x.Probe, 0)) .Where(c => !Talente.Exists(v => v.Name.Equals(c.Name))).ToList().ForEach(v => Talente.Add(v)); + (Chars.Last() as Character)?.Zauber.Select(x => new Zauber(x.Name, x.Probe, 0, x.Complexity)) + .Where(c => !Zauber.Exists(v => v.Name.Equals(c.Name))).ToList().ForEach(v => Zauber.Add(v)); } Properties.Deserialize(rootPath+"Properties"); Properties.Serialize(rootPath + "Properties"); Talente = Talente.OrderBy(x => x.Name).ToList(); + Zauber = Zauber.OrderBy(x => x.Name).ToList(); + + /*foreach (var talent in Talente) + { + Database.AddTalent(new Models.Database.Talent(talent.Name, talent.Probe)); + } + + foreach (var talent in Zauber) + { + Database.AddSpell(new Models.Database.GeneralSpell(talent.Name, talent.Probe, talent.Complexity)); + }*/ + + Session = new Session { Chars = Chars.Select(x => SaveChar.FromICharacter(x)).ToList() diff --git a/DSACore/FireBase/Database.cs b/DSACore/FireBase/Database.cs index de7d540..02a4741 100644 --- a/DSACore/FireBase/Database.cs +++ b/DSACore/FireBase/Database.cs @@ -34,10 +34,10 @@ namespace DSACore.FireBase var lastChar = await firebase .Child("Chars") - .OrderBy("id") + .OrderByKey() .LimitToLast(1) - .OnceSingleAsync(); - int id = groupChar.Id = data.Id = lastChar.Id + 1; + .OnceAsync(); + int id = groupChar.Id = data.Id = lastChar.First().Object.Id + 1; await firebase .Child("Groups") @@ -180,7 +180,7 @@ namespace DSACore.FireBase { var groups = await firebase .Child("Groups") - .OrderBy("id") + .OrderByKey() .OnceAsync(); var ret = new List(); @@ -201,14 +201,14 @@ namespace DSACore.FireBase return group; } - public static async void AddGroup(Group group) + public static async Task AddGroup(Group group) { var lastChar = await firebase .Child("Groups") - .OrderBy("id") + .OrderByKey() .LimitToLast(1) - .OnceSingleAsync(); - int id = group.Id = lastChar.Id + 1; + .OnceAsync(); + int id = group.Id = lastChar.First().Object.Id + 1; await firebase .Child("Groups") diff --git a/DSACore/Hubs/ChatHub.cs b/DSACore/Hubs/ChatHub.cs index e489b2e..46fde08 100644 --- a/DSACore/Hubs/ChatHub.cs +++ b/DSACore/Hubs/ChatHub.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Text; using System.Threading.Tasks; +using DSACore.DSA_Game.Characters; using DSACore.FireBase; using DSACore.Models; using DSACore.Models.Network; @@ -18,11 +21,15 @@ namespace DSACore.Hubs static ChatHub() { DSAGroups = Database.GetGroups().Result; - DSAGroups.Add(new Group("TheCrew", "valid")); - DSAGroups.Add(new Group("HalloWelt", "valid")); - DSAGroups.Add(new Group("DieKrassenGamer", "valid")); - DSAGroups.Add(new Group("DSA", "valid")); - DSAGroups.Add(new Group("DieÜberhelden", "valid")); + //AddGroups(); + } + + private static async void AddGroups() + { + await Database.AddGroup(new Models.Database.Group { Name = "HalloWelt", Password = "valid" }); + await Database.AddGroup(new Models.Database.Group { Name = "Die Krassen Gamer", Password = "valid" }); + await Database.AddGroup(new Models.Database.Group { Name = "DSA", Password = "valid" }); + await Database.AddGroup(new Models.Database.Group { Name = "Die Überhelden", Password = "valid" }); } public async Task SendMessage(string user, string message) @@ -34,7 +41,7 @@ namespace DSACore.Hubs try { string group = getGroup(Context.ConnectionId).Name; - await SendToGroup(group, user, Commands.CommandHandler.ExecuteCommand(new Command { CharId = 0, CmdIdentifier = ident, CmdTexts = args, Name = user })); + await SendToGroup(Commands.CommandHandler.ExecuteCommand(new Command { CharId = 0, CmdIdentifier = ident, CmdTexts = args, Name = user })); } catch(InvalidOperationException e) { @@ -43,9 +50,10 @@ namespace DSACore.Hubs } - private Task SendToGroup(string group, string user, string message) + private Task SendToGroup(string message) { - return Clients.Group(group).SendCoreAsync("ReceiveMessage", new object[] { user, message }); + string group = getGroup(Context.ConnectionId).Name; + return Clients.Group(group).SendCoreAsync("ReceiveMessage", new object[] { getUser(Context.ConnectionId), message }); } private Models.Network.Group getGroup(string id) @@ -53,8 +61,23 @@ namespace DSACore.Hubs return DSAGroups.First(x => x.Users.Exists(y => y.ConnectionId.Equals(id))); } + private User getUser(string id) + { + return DSAGroups.First(x => x.Users.Exists(y => y.ConnectionId.Equals(id))).Users.First(z => z.ConnectionId.Equals(id)); + } + public async Task GetGroups() { + var test = Database.GetGroups(); + test.Wait(); + foreach (var group in test.Result) + { + if (!DSAGroups.Exists(x => x.Name.Equals(group.Name))) + { + DSAGroups.Add(group); + } + } + await Clients.Caller.SendCoreAsync("ListGroups", new object[] { DSAGroups }); //throw new NotImplementedException("add database call to get groups"); } @@ -63,11 +86,19 @@ namespace DSACore.Hubs { DSAGroups.Add(new Group(group, password)); var Dgroup = new DSACore.Models.Database.Group{Name = group, Id = DSAGroups.Count-1}; - Database.CreateGroup(Dgroup); + //Database.AddGroup(Dgroup); await Clients.Caller.SendCoreAsync("ReceiveMessage", new[] {$"group {@group} sucessfully added"}); //throw new NotImplementedException("add database call to add groups"); } + public async Task UploadChar(string xml) + { + var group = getGroup(Context.ConnectionId); + + Database.AddChar(new Character(new MemoryStream(Encoding.UTF8.GetBytes(xml))), group); + //throw new NotImplementedException("add database call to add groups"); + } + public async Task Login(string group, string user, string password) { if (password == DSAGroups.First(x=>x.Name == group).Password) @@ -76,17 +107,19 @@ namespace DSACore.Hubs await Groups.AddToGroupAsync(Context.ConnectionId, group); } - await SendToGroup(group, user, "Ein neuer Nutzer hat die Gruppe betreten"); + await SendToGroup("Ein neuer Nutzer hat die Gruppe betreten"); } public async Task Disconnect() { var group = getGroup(Context.ConnectionId); - var user = group.Users.First(x => x.ConnectionId.Equals(Context.ConnectionId)); + + await SendToGroup("Connection lost"); + + var user = getUser(Context.ConnectionId); group.Users.Remove(user); await Groups.RemoveFromGroupAsync(Context.ConnectionId, group.Name); - await SendToGroup(group.Name, user.Name, "Connection lost"); } } diff --git a/DSACore/Models/Database/GeneralSpell.cs b/DSACore/Models/Database/GeneralSpell.cs index 807a09b..6a6e94c 100644 --- a/DSACore/Models/Database/GeneralSpell.cs +++ b/DSACore/Models/Database/GeneralSpell.cs @@ -7,6 +7,15 @@ namespace DSACore.Models.Database { public class GeneralSpell : Talent { - public string Comlexity = "A"; + public char Comlexity = 'A'; + + public GeneralSpell(string name, string roll, char comlexity = 'A') :base(name, roll) + { + Comlexity = comlexity; + } + + public GeneralSpell(string name, string roll) : base(name, roll) + { + } } } diff --git a/DSACore/Models/Database/Roll.cs b/DSACore/Models/Database/Roll.cs deleted file mode 100644 index 547fde4..0000000 --- a/DSACore/Models/Database/Roll.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace DSACore.Models.Database -{ - public class Roll - { - public string[] Skills { get; set; } = new string[3]; - } -} diff --git a/DSACore/Models/Database/Talent.cs b/DSACore/Models/Database/Talent.cs index 6bd8cba..117d0f7 100644 --- a/DSACore/Models/Database/Talent.cs +++ b/DSACore/Models/Database/Talent.cs @@ -7,8 +7,14 @@ namespace DSACore.Models.Database { public class Talent { + public Talent(string name, String roll) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + Roll = roll.Split('/'); + } + public string Name { get; set; } - public Roll Roll { get; set; } = new Roll(); + public string[] Roll { get; set; } = new string[3]; } } diff --git a/DSACore/Models/Network/User.cs b/DSACore/Models/Network/User.cs index b207a19..04ef0a9 100644 --- a/DSACore/Models/Network/User.cs +++ b/DSACore/Models/Network/User.cs @@ -9,5 +9,6 @@ namespace DSACore.Models.Network { public string Name { get; set; } public string ConnectionId { get; set; } + public int Char { get; set; } } } -- cgit v1.2.3-54-g00ecf