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/Hubs/ChatHub.cs | 112 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 33 deletions(-) (limited to 'DSACore/Hubs/ChatHub.cs') 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; + } } } -- 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/Hubs/ChatHub.cs') 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