From b7ef3c860375baea7b5db95940519ce0746b6ecc Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Mon, 1 Oct 2018 22:20:36 +0200 Subject: added Command Response Handling --- DSACore/Commands/CommandHandler.cs | 48 +++++++++---- DSACore/FireBase/Database.cs | 87 ++++++++++++++++------- DSACore/Hubs/ChatHub.cs | 112 +++++++++++++++++++++--------- DSACore/Models/Database/DatabaseChar.cs | 16 +++++ DSACore/Models/Database/GeneralSpell.cs | 4 ++ DSACore/Models/Database/Talent.cs | 9 +++ DSACore/Models/Network/CommandResponse.cs | 31 +++++++++ DSACore/Models/Network/Group.cs | 25 ++++--- 8 files changed, 250 insertions(+), 82 deletions(-) create mode 100644 DSACore/Models/Network/CommandResponse.cs (limited to 'DSACore') diff --git a/DSACore/Commands/CommandHandler.cs b/DSACore/Commands/CommandHandler.cs index 7812ec4..f43633f 100644 --- a/DSACore/Commands/CommandHandler.cs +++ b/DSACore/Commands/CommandHandler.cs @@ -8,48 +8,69 @@ namespace DSACore.Commands { public class CommandHandler { - public static string ExecuteCommand(Command cmd) + public static CommandResponse ExecuteCommand(Command cmd) { + string res = string.Empty; + ResponseType type = ResponseType.Broadcast; switch (cmd.CmdIdentifier.ToLower()) { case "addChar": - return FileHandler.AddChar(cmd.CharId, cmd.CmdText); + res = FileHandler.AddChar(cmd.CharId, cmd.CmdText); + break; case "held": case "wert": case "werte": case "char": - return Commands.HeldList.ListAsync(cmd.CharId, cmd.CmdText); + res = Commands.HeldList.ListAsync(cmd.CharId, cmd.CmdText); + break; case "help": case "man": case "hilfe": case "h": - return Help.ShowHelp(cmd.CmdTexts.ToArray()); + res = Help.ShowHelp(cmd.CmdTexts.ToArray()); + type = ResponseType.Caller; + break; case "le": case "leben": case "lp": - return LE.LEAsync(cmd.CharId, cmd.CmdText); + res = LE.LEAsync(cmd.CharId, cmd.CmdText); + break; case "ae": case "astral": case "asp": - return AE.AEAsync(cmd.CharId, cmd.CmdText); + res = AE.AEAsync(cmd.CharId, cmd.CmdText); + break; case "list": - return List.ListAsync(cmd.CmdText); + res = List.ListAsync(cmd.CmdText); + type = ResponseType.Caller; + break; case "r": case "roll": - return RandomMisc.Roll(cmd.CmdText + " " + cmd.Cmdmodifier); + res = RandomMisc.Roll(cmd.CmdText + " " + cmd.Cmdmodifier); + break; case "solve": - return new Auxiliary.Calculator.StringSolver(cmd.CmdText + cmd.Cmdmodifier).Solve().ToString(); + res = new Auxiliary.Calculator.StringSolver(cmd.CmdText + cmd.Cmdmodifier).Solve().ToString(); + break; case "npc": - return NpcCommands.CreateNpc(cmd.CharId, cmd.CmdTexts, cmd.Cmdmodifier); + res = NpcCommands.CreateNpc(cmd.CharId, cmd.CmdTexts, cmd.Cmdmodifier); + break; } - return Proben(cmd.Name, cmd.CmdIdentifier, cmd.CmdText, cmd.Cmdmodifier); + if (res == string.Empty) + { + res= Proben(cmd.Name, cmd.CmdIdentifier, cmd.CmdText, cmd.Cmdmodifier); + } + if (res != string.Empty) + { + return new CommandResponse(res, type); + } + return new CommandResponse($"Kommando {cmd.CmdIdentifier} nicht gefunden", ResponseType.Error); } private static string Proben(string name, string command, string waffe, int erschwernis = 0) { - string res; + string res = string.Empty; switch (command.ToLower()) { case "f": @@ -87,9 +108,6 @@ namespace DSACore.Commands case "parade": res = CheckCommand(name, CommandTypes.Parade, waffe, erschwernis); break; - default: - res = $"Kommando {command} nicht gefunden"; - break; } return res; diff --git a/DSACore/FireBase/Database.cs b/DSACore/FireBase/Database.cs index 12ebb89..2c0f14d 100644 --- a/DSACore/FireBase/Database.cs +++ b/DSACore/FireBase/Database.cs @@ -1,13 +1,11 @@ -using System; +using DSACore.DSA_Game.Characters; +using DSACore.Models.Database; +using Firebase.Database; +using Firebase.Database.Query; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; -using DSACore.DSA_Game.Characters; -using DSACore.Models.Database; -using DSACore.Models; -using Firebase.Database; -using Firebase.Database.Query; namespace DSACore.FireBase @@ -18,37 +16,73 @@ namespace DSACore.FireBase static Database() { - var auth = File.ReadAllText(DSACore.DSA_Game.Dsa.rootPath+"Token"); ; // your app secret + var auth = File.ReadAllText(DSACore.DSA_Game.Dsa.rootPath + "Token"); ; // your app secret firebase = new FirebaseClient( "https://heldenonline-4d828.firebaseio.com/", new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(auth) }); + + Initialize(); + } + + private static async Task Initialize() + { + + IntializeCollection("Chars", Chars); + IntializeCollection("MeleeWeapons", MeleeList); + IntializeCollection("RangedWeapons", RangedWeapons); + IntializeCollection("Talents", Talents); + IntializeCollection("Spells", Spells); + } + + private static async Task IntializeCollection(string path, Dictionary list) + { + var temp = await firebase + .Child(path) + .OrderByKey() + .OnceAsync(); + + foreach (var firebaseObject in temp) + { + list.Add(firebaseObject.Key, firebaseObject.Object); + } } - + + public static Dictionary Chars = new Dictionary(); + + public static Dictionary MeleeList = new Dictionary(); + + public static Dictionary RangedWeapons = new Dictionary(); + + public static Dictionary Talents = new Dictionary(); + + public static Dictionary Spells = new Dictionary(); public static async Task AddChar(Character file, Models.Network.Group group) { DatabaseChar.LoadChar(file, out GroupChar groupChar, out DatabaseChar data); - + var lastChar = await firebase .Child("Chars") .OrderByKey() .LimitToLast(1) .OnceAsync(); int id = groupChar.Id = data.Id = lastChar.First().Object.Id + 1; - - await firebase + + await firebase //TODO Reomve await Operators .Child("Groups") .Child("Char" + id) - .PutAsync(data); + .PutAsync(groupChar); await firebase .Child("Chars") .Child("Char" + id) .PutAsync(data); + Chars["Char" + id] = data; + await firebase .Child("Inventories") .Child("Inventory" + id) @@ -70,27 +104,30 @@ namespace DSACore.FireBase .Child("Char" + id) .DeleteAsync(); + Chars.Remove("Char" + id); + await firebase .Child("Inventories") .Child("Inventory" + id) .DeleteAsync(); - + } public static async Task GetChar(int id) { - var chr = await firebase + /*var chr = await firebase .Child("Chars") .Child("Char" + id) .OnceSingleAsync(); - return chr; + return chr;*/ + return Chars["Char" + id]; } public static async Task GetInventory(int id) { var inv = await firebase .Child("Inventories") - .Child("Inventory"+id) + .Child("Inventory" + id) .OnceSingleAsync(); return inv; } @@ -120,11 +157,12 @@ namespace DSACore.FireBase } public static async Task GetTalent(string talent) - { + {/* return await firebase .Child("Talents") .Child(talent) - .OnceSingleAsync(); + .OnceSingleAsync();*/ + return Talents[talent]; } public static async Task AddSpell(GeneralSpell tal) @@ -145,12 +183,13 @@ namespace DSACore.FireBase public static async Task GetSpell(string spell) { - return await firebase + /*return await firebase .Child("Spells") .Child(spell) - .OnceSingleAsync(); + .OnceSingleAsync();*/ + return Spells[spell]; } - + public static async Task AddWeapon(Weapon wep) { @@ -170,7 +209,7 @@ namespace DSACore.FireBase .DeleteAsync(); } - public static async Task< Weapon> GetWeapon(string weapon, bool ranged = false) + public static async Task GetWeapon(string weapon, bool ranged = false) { string collection = ranged ? "RangedWeapons" : "MeleeWeapons"; return await firebase @@ -199,7 +238,7 @@ namespace DSACore.FireBase { var group = await firebase .Child("Groups") - .Child("Group"+id) + .Child("Group" + id) .OnceSingleAsync(); return group; } @@ -215,7 +254,7 @@ namespace DSACore.FireBase await firebase .Child("Groups") - .Child("Group"+id) + .Child("Group" + id) .PutAsync(group); } diff --git a/DSACore/Hubs/ChatHub.cs b/DSACore/Hubs/ChatHub.cs index 25f34d7..027e4df 100644 --- a/DSACore/Hubs/ChatHub.cs +++ b/DSACore/Hubs/ChatHub.cs @@ -1,14 +1,14 @@ -using System; +using DSACore.DSA_Game.Characters; +using DSACore.FireBase; +using DSACore.Models.Network; +using Microsoft.AspNetCore.SignalR; +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; -using Microsoft.AspNetCore.SignalR; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace DSACore.Hubs { @@ -34,26 +34,68 @@ namespace DSACore.Hubs public async Task SendMessage(string user, string message) { - var args = message.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); - var ident = args.First().Replace("!", ""); - if(args.Count>0){args.RemoveAt(0);} - try { string group = getGroup(Context.ConnectionId).Name; - await SendToGroup(Commands.CommandHandler.ExecuteCommand(new Command { CharId = 0, CmdIdentifier = ident, CmdTexts = args, Name = user })); } - catch(InvalidOperationException e) + catch (InvalidOperationException e) { - await Clients.Caller.SendCoreAsync("ReceiveMessage", new object[] {"Nutzer ist in keiner Gruppe. Erst joinen!"}); + //await Clients.Caller.SendCoreAsync("ReceiveMessage", + // new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); } - + + if (message[0] == '/') + { + var args = message.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); + + + var ident = args.First().Replace("/", ""); + if (args.Count > 0) + { + args.RemoveAt(0); + } + + var ret = Commands.CommandHandler.ExecuteCommand(new Command + { + CharId = 0, + CmdIdentifier = ident, + CmdTexts = args, + Name = user + }); + + switch (ret.ResponseType) + { + case ResponseType.Caller: + case ResponseType.Error: + await Clients.Caller.SendAsync("ReceiveMessage", ret.message); + break; + case ResponseType.Broadcast: + await SendToGroup(ret.message); + break; + } + + + } + else + { + await SendToGroup(message); + } + } private Task SendToGroup(string message) { - string group = getGroup(Context.ConnectionId).Name; - return Clients.Group(group).SendCoreAsync("ReceiveMessage", new object[] { getUser(Context.ConnectionId).Name, message }); + try + { + string group = getGroup(Context.ConnectionId).Name; + return Clients.Group(group).SendCoreAsync("ReceiveMessage", + new object[] {getUser(Context.ConnectionId).Name, message}); + } + catch (InvalidOperationException e) + { + return Clients.Caller.SendCoreAsync("ReceiveMessage", + new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); + } } private Models.Network.Group getGroup(string id) @@ -78,16 +120,16 @@ namespace DSACore.Hubs } } - await Clients.Caller.SendCoreAsync("ListGroups", new object[] { DSAGroups.Select(x=>x.SendGroup()) }); + await Clients.Caller.SendCoreAsync("ListGroups", new object[] { DSAGroups.Select(x => x.SendGroup()) }); //throw new NotImplementedException("add database call to get groups"); } public async Task AddGroup(string group, string password) { DSAGroups.Add(new Group(group, password)); - var Dgroup = new DSACore.Models.Database.Group{Name = group, Id = DSAGroups.Count-1}; + var Dgroup = new DSACore.Models.Database.Group { Name = group, Id = DSAGroups.Count - 1 }; //Database.AddGroup(Dgroup); - await Clients.Caller.SendCoreAsync("ReceiveMessage", new[] {$"group {@group} sucessfully added"}); + await Clients.Caller.SendCoreAsync("ReceiveMessage", new[] { $"group {@group} sucessfully added" }); //throw new NotImplementedException("add database call to add groups"); } @@ -102,14 +144,15 @@ namespace DSACore.Hubs public async Task Login(string group, string user, string hash) { //string password = System.Text.Encoding.UTF8.GetString(hash); - if (hash == DSAGroups.First(x=>x.Name == group).Password) + if (hash == DSAGroups.First(x => x.Name == group).Password) { var gGroup = DSAGroups.First(x => x.Name.Equals(group)); if (!gGroup.Users.Exists(x => x.Name.Equals(user))) { await Groups.AddToGroupAsync(Context.ConnectionId, group); + gGroup.Users.Add(new User { ConnectionId = Context.ConnectionId, Name = user }); await SendToGroup("Ein neuer Nutzer hat die Gruppe betreten"); - await Clients.Caller.SendAsync("LoginResponse", 0 ); + await Clients.Caller.SendAsync("LoginResponse", 0); } else { @@ -119,7 +162,7 @@ namespace DSACore.Hubs else { await Clients.Caller.SendAsync("LoginResponse", 2); - await Clients.Caller.SendAsync("ReceiveMessage", "Falsches Passwort!"); + //await Clients.Caller.SendAsync("ReceiveMessage", "Falsches Passwort!"); } } @@ -131,20 +174,23 @@ namespace DSACore.Hubs public async Task Disconnect() { - try + if (DSAGroups.Exists(x => x.Users.Exists(y => y.ConnectionId == Context.ConnectionId))) { - var group = getGroup(Context.ConnectionId); + try + { + var group = getGroup(Context.ConnectionId); - var user = getUser(Context.ConnectionId); - await SendToGroup(user.Name + " disconnected from the Server"); - group.Users.Remove(user); - await Groups.RemoveFromGroupAsync(Context.ConnectionId, group.Name); - } - catch (Exception e) - { - Console.WriteLine(e); - //throw; + var user = getUser(Context.ConnectionId); + await SendToGroup(user.Name + " disconnected from the Server"); + group.Users.Remove(user); + await Groups.RemoveFromGroupAsync(Context.ConnectionId, group.Name); + } + catch (Exception e) + { + Console.WriteLine(e); + //throw; + } } } diff --git a/DSACore/Models/Database/DatabaseChar.cs b/DSACore/Models/Database/DatabaseChar.cs index 03383b8..9cd865f 100644 --- a/DSACore/Models/Database/DatabaseChar.cs +++ b/DSACore/Models/Database/DatabaseChar.cs @@ -8,6 +8,22 @@ namespace DSACore.Models.Database { public class DatabaseChar { + public DatabaseChar() + { + } + + public DatabaseChar(int id, string name, string rasse, List skills, List talents, List advantages, List spells, List weaponTalents) + { + Id = id; + Name = name ?? throw new ArgumentNullException(nameof(name)); + Rasse = rasse ?? throw new ArgumentNullException(nameof(rasse)); + Skills = skills ?? throw new ArgumentNullException(nameof(skills)); + Talents = talents ?? throw new ArgumentNullException(nameof(talents)); + Advantages = advantages ?? throw new ArgumentNullException(nameof(advantages)); + Spells = spells ?? throw new ArgumentNullException(nameof(spells)); + WeaponTalents = weaponTalents ?? throw new ArgumentNullException(nameof(weaponTalents)); + } + public int Id { get; set; } public string Name { get; set; } diff --git a/DSACore/Models/Database/GeneralSpell.cs b/DSACore/Models/Database/GeneralSpell.cs index 6a6e94c..f53081e 100644 --- a/DSACore/Models/Database/GeneralSpell.cs +++ b/DSACore/Models/Database/GeneralSpell.cs @@ -17,5 +17,9 @@ namespace DSACore.Models.Database public GeneralSpell(string name, string roll) : base(name, roll) { } + + public GeneralSpell() + { + } } } diff --git a/DSACore/Models/Database/Talent.cs b/DSACore/Models/Database/Talent.cs index 117d0f7..aca65a4 100644 --- a/DSACore/Models/Database/Talent.cs +++ b/DSACore/Models/Database/Talent.cs @@ -7,6 +7,15 @@ namespace DSACore.Models.Database { public class Talent { + public Talent() + { + } + + public Talent(string name) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + } + public Talent(string name, String roll) { Name = name ?? throw new ArgumentNullException(nameof(name)); diff --git a/DSACore/Models/Network/CommandResponse.cs b/DSACore/Models/Network/CommandResponse.cs new file mode 100644 index 0000000..ed4b7d0 --- /dev/null +++ b/DSACore/Models/Network/CommandResponse.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DSACore.Models.Network +{ + public class CommandResponse + { + public CommandResponse(string message, ResponseType responseType= ResponseType.Broadcast) + { + this.message = message ?? throw new ArgumentNullException(nameof(message)); + ResponseType = responseType; + } + + public string message { get; private set; } + public ResponseType ResponseType { get; private set;} + + public override string ToString() + { + return message; + } + } + + public enum ResponseType + { + Broadcast, + Caller, + Error + } +} diff --git a/DSACore/Models/Network/Group.cs b/DSACore/Models/Network/Group.cs index 6e62dc8..76c3efb 100644 --- a/DSACore/Models/Network/Group.cs +++ b/DSACore/Models/Network/Group.cs @@ -7,18 +7,15 @@ namespace DSACore.Models.Network { public class Group { - private int _online; - public Group(string name, string password) { Name = name; Password = password; } - public Group(string name, int userCount) + public Group(string name, int userOnline) { Name = name ?? throw new ArgumentNullException(nameof(name)); - UserCount = userCount; } public string Name { get; set; } @@ -27,18 +24,26 @@ namespace DSACore.Models.Network public int UserCount { - get { return _online; RefreshOnline();} - set { _online = value; RefreshOnline();} + get { return Users.Count; } } - private void RefreshOnline() + public SendGroup SendGroup() { - _online = Users.Count; + return new SendGroup( Name, UserCount); } + } - public Group SendGroup() + public class SendGroup + { + public SendGroup(string name, int userCount) { - return new Group( Name, UserCount); + Name = name ?? throw new ArgumentNullException(nameof(name)); + UserCount = userCount; } + + public string Name { get; set; } + + public int UserCount { get; set; } + } } -- cgit v1.2.3-70-g09d2 From e7cac3a24b88a139eaf237a4342fa27c00bf2097 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Mon, 1 Oct 2018 22:56:40 +0200 Subject: created [online] and [login] groups to easyly adress specific groups of players, even though, they havent joined a group yet --- DSACore/Controllers/CommandsController.cs | 2 +- DSACore/Hubs/ChatHub.cs | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'DSACore') diff --git a/DSACore/Controllers/CommandsController.cs b/DSACore/Controllers/CommandsController.cs index 1d40a43..5f27f63 100644 --- a/DSACore/Controllers/CommandsController.cs +++ b/DSACore/Controllers/CommandsController.cs @@ -33,7 +33,7 @@ namespace DSACore.Controllers { try { - return Commands.CommandHandler.ExecuteCommand(cmd); + return Commands.CommandHandler.ExecuteCommand(cmd).message; } catch (Exception e) { diff --git a/DSACore/Hubs/ChatHub.cs b/DSACore/Hubs/ChatHub.cs index 027e4df..42e6d91 100644 --- a/DSACore/Hubs/ChatHub.cs +++ b/DSACore/Hubs/ChatHub.cs @@ -21,6 +21,8 @@ namespace DSACore.Hubs static ChatHub() { DSAGroups = Database.GetGroups().Result; + DSAGroups.Add(new Group("login", "")); + DSAGroups.Add(new Group("online", "")); //AddGroups(); } @@ -40,7 +42,7 @@ namespace DSACore.Hubs } catch (InvalidOperationException e) { - //await Clients.Caller.SendCoreAsync("ReceiveMessage", + //await Clients.Caller.SendCoreAsync("RecieveMessage", // new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); } @@ -67,7 +69,7 @@ namespace DSACore.Hubs { case ResponseType.Caller: case ResponseType.Error: - await Clients.Caller.SendAsync("ReceiveMessage", ret.message); + await Clients.Caller.SendAsync("RecieveMessage", ret.message); break; case ResponseType.Broadcast: await SendToGroup(ret.message); @@ -88,12 +90,12 @@ namespace DSACore.Hubs try { string group = getGroup(Context.ConnectionId).Name; - return Clients.Group(group).SendCoreAsync("ReceiveMessage", + return Clients.Group(group).SendCoreAsync("RecieveMessage", new object[] {getUser(Context.ConnectionId).Name, message}); } catch (InvalidOperationException e) { - return Clients.Caller.SendCoreAsync("ReceiveMessage", + return Clients.Caller.SendCoreAsync("RecieveMessage", new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); } } @@ -129,7 +131,7 @@ namespace DSACore.Hubs DSAGroups.Add(new Group(group, password)); var Dgroup = new DSACore.Models.Database.Group { Name = group, Id = DSAGroups.Count - 1 }; //Database.AddGroup(Dgroup); - await Clients.Caller.SendCoreAsync("ReceiveMessage", new[] { $"group {@group} sucessfully added" }); + await Clients.Caller.SendCoreAsync("RecieveMessage", new[] { $"group {@group} sucessfully added" }); //throw new NotImplementedException("add database call to add groups"); } @@ -149,10 +151,12 @@ namespace DSACore.Hubs var gGroup = DSAGroups.First(x => x.Name.Equals(group)); if (!gGroup.Users.Exists(x => x.Name.Equals(user))) { + await Groups.RemoveFromGroupAsync(Context.ConnectionId, "login"); await Groups.AddToGroupAsync(Context.ConnectionId, group); gGroup.Users.Add(new User { ConnectionId = Context.ConnectionId, Name = user }); await SendToGroup("Ein neuer Nutzer hat die Gruppe betreten"); await Clients.Caller.SendAsync("LoginResponse", 0); + await Clients.Caller.SendAsync("PlayerStatusChanged", new[] {user, "online"}); } else { @@ -162,7 +166,7 @@ namespace DSACore.Hubs else { await Clients.Caller.SendAsync("LoginResponse", 2); - //await Clients.Caller.SendAsync("ReceiveMessage", "Falsches Passwort!"); + //await Clients.Caller.SendAsync("RecieveMessage", "Falsches Passwort!"); } } @@ -172,8 +176,16 @@ namespace DSACore.Hubs return base.OnDisconnectedAsync(exception); } + public override Task OnConnectedAsync() + { + Groups.AddToGroupAsync(Context.ConnectionId, "login").Wait(); + Groups.AddToGroupAsync(Context.ConnectionId, "online").Wait(); + return base.OnConnectedAsync(); + } + public async Task Disconnect() { + await Groups.RemoveFromGroupAsync(Context.ConnectionId, "online"); if (DSAGroups.Exists(x => x.Users.Exists(y => y.ConnectionId == Context.ConnectionId))) { try @@ -182,7 +194,9 @@ namespace DSACore.Hubs var user = getUser(Context.ConnectionId); - await SendToGroup(user.Name + " disconnected from the Server"); + + await Clients.Caller.SendAsync("PlayerStatusChanged", new[] { user.Name, "offline" }); + //await SendToGroup(user.Name + " disconnected from the Server"); group.Users.Remove(user); await Groups.RemoveFromGroupAsync(Context.ConnectionId, group.Name); } -- cgit v1.2.3-70-g09d2