From 91fc989eea5ee16c2f000064b5c040a6fe6f23b9 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Thu, 16 May 2019 01:10:10 +0200 Subject: Implement tokens --- DSACore/Hubs/ChatHub.cs | 216 --------------------------------------------- DSACore/Hubs/Login.cs | 228 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 216 deletions(-) delete mode 100644 DSACore/Hubs/ChatHub.cs create mode 100644 DSACore/Hubs/Login.cs (limited to 'DSACore/Hubs') diff --git a/DSACore/Hubs/ChatHub.cs b/DSACore/Hubs/ChatHub.cs deleted file mode 100644 index 1994164..0000000 --- a/DSACore/Hubs/ChatHub.cs +++ /dev/null @@ -1,216 +0,0 @@ -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 Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace DSACore.Hubs -{ - public class ChatHub : Hub - { - //private static Dictionary UserGroup = new Dictionary(); - - private const string receiveMethod = "ReceiveMessage";//receiveMethod; - - private static List DSAGroups = new List(); - - static ChatHub() - { - DSAGroups = Database.GetGroups().Result; - DSAGroups.Add(new Group("login", "")); - DSAGroups.Add(new Group("online", "")); - //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) - { - try - { - string group = getGroup(Context.ConnectionId).Name; - } - catch (InvalidOperationException e) - { - //await Clients.Caller.SendCoreAsync(receiveMethod, - // new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); - } - - if (message[0] == '/') - { - var args = message.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); - - bool Timon = args.Any(x => x == "hallo"); - - 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(receiveMethod, ret.message); - break; - case ResponseType.Broadcast: - await SendToGroup(ret.message); - break; - } - - - } - else - { - await SendToGroup(message); - } - - } - - private Task SendToGroup(string message) - { - try - { - string group = getGroup(Context.ConnectionId).Name; - return Clients.Group(group).SendCoreAsync(receiveMethod, - new object[] {getUser(Context.ConnectionId).Name, message}); - } - catch (InvalidOperationException e) - { - return Clients.Caller.SendCoreAsync(receiveMethod, - new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); - } - } - - private Models.Network.Group getGroup(string id) - { - 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.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 }; - //Database.AddGroup(Dgroup); - await Clients.Caller.SendCoreAsync(receiveMethod, 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); - - await 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 hash) - { - //string password = System.Text.Encoding.UTF8.GetString(hash); - 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.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 - { - await Clients.Caller.SendAsync("LoginResponse", 1); - } - } - else - { - await Clients.Caller.SendAsync("LoginResponse", 2); - //await Clients.Caller.SendAsync(receiveMethod, "Falsches Passwort!"); - } - } - - public override Task OnDisconnectedAsync(Exception exception) - { - Disconnect().Wait(); - 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 - { - var group = getGroup(Context.ConnectionId); - - - var user = getUser(Context.ConnectionId); - - 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); - } - catch (Exception e) - { - Console.WriteLine(e); - //throw; - } - } - - } - - } -} diff --git a/DSACore/Hubs/Login.cs b/DSACore/Hubs/Login.cs new file mode 100644 index 0000000..5f984e2 --- /dev/null +++ b/DSACore/Hubs/Login.cs @@ -0,0 +1,228 @@ +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 Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace DSACore.Hubs +{ + public class Users : Hub + { + //private static Dictionary UserGroup = new Dictionary(); + + private const string ReceiveMethod = "ReceiveMessage";//receiveMethod; + + private static List DsaGroups {get; set; } + public static List Tokens { get;} = new List(); + + static Users() + { + DsaGroups = Database.GetGroups().Result; + DsaGroups.Add(new Group("login", "")); + DsaGroups.Add(new Group("online", "")); + //AddGroups(); + } + + + [Obsolete] + private static async void AddGroups() + { + await Database.AddGroup(new Models.Database.Groups.Group { Name = "HalloWelt", Password = "valid" }); + await Database.AddGroup(new Models.Database.Groups.Group { Name = "Die Krassen Gamer", Password = "valid" }); + await Database.AddGroup(new Models.Database.Groups.Group { Name = "DSA", Password = "valid" }); + await Database.AddGroup(new Models.Database.Groups.Group { Name = "Die Überhelden", Password = "valid" }); + } + + public async Task SendMessage(string user, string message) + { + try + { + string group = getGroup(Context.ConnectionId).Name; + } + catch (InvalidOperationException e) + { + //await Clients.Caller.SendCoreAsync(receiveMethod, + // new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); + } + + if (message[0] == '/') + { + var args = message.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); + + bool Timon = args.Any(x => x == "hallo"); + + 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(ReceiveMethod, ret.message); + break; + case ResponseType.Broadcast: + await SendToGroup(ret.message); + break; + } + + + } + else + { + await SendToGroup(message); + } + + } + + private Task SendToGroup(string message) + { + try + { + string group = getGroup(Context.ConnectionId).Name; + return Clients.Group(group).SendCoreAsync(ReceiveMethod, + new object[] {getUser(Context.ConnectionId).Name, message}); + } + catch (InvalidOperationException e) + { + return Clients.Caller.SendCoreAsync(ReceiveMethod, + new object[] { "Nutzer ist in keiner Gruppe. Erst joinen!" }); + } + } + + private Models.Network.Group getGroup(string id) + { + 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 = await Database.GetGroups(); + + foreach (var group in test) + { + if (!DsaGroups.Exists(x => x.Name.Equals(group.Name))) + { + DsaGroups.Add(group); + } + } + + 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 Models.Database.Groups.Group { Name = group, Id = DsaGroups.Count - 1 }; + //Database.AddGroup(Dgroup); + await Clients.Caller.SendCoreAsync(ReceiveMethod, 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); + + await 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 hash) + { + //string password = System.Text.Encoding.UTF8.GetString(hash); + 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.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"}); + + Tokens.Add(new Token(group)); + await Clients.Caller.SendAsync("Token", Tokens.Last().GetHashCode()); + purgeTokens(); + } + else + { + await Clients.Caller.SendAsync("LoginResponse", 1); + } + } + else + { + await Clients.Caller.SendAsync("LoginResponse", 2); + //await Clients.Caller.SendAsync(receiveMethod, "Falsches Passwort!"); + } + } + + private void purgeTokens() + { + Tokens.RemoveAll(x => !x.IsValid()); + } + + public override Task OnDisconnectedAsync(Exception exception) + { + Disconnect().Wait(); + 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 + { + var group = getGroup(Context.ConnectionId); + + + var user = getUser(Context.ConnectionId); + + 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); + } + catch (Exception e) + { + Console.WriteLine(e); + //throw; + } + } + + } + + } +} -- cgit v1.2.3-70-g09d2