summaryrefslogtreecommitdiff
path: root/DSACore/Hubs/ChatHub.cs
diff options
context:
space:
mode:
Diffstat (limited to 'DSACore/Hubs/ChatHub.cs')
-rw-r--r--DSACore/Hubs/ChatHub.cs71
1 files changed, 58 insertions, 13 deletions
diff --git a/DSACore/Hubs/ChatHub.cs b/DSACore/Hubs/ChatHub.cs
index 6335183..46fde08 100644
--- a/DSACore/Hubs/ChatHub.cs
+++ b/DSACore/Hubs/ChatHub.cs
@@ -1,7 +1,11 @@
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;
@@ -12,7 +16,21 @@ namespace DSACore.Hubs
{
//private static Dictionary<string, User> UserGroup = new Dictionary<string, User>();
- private static List<Group> DSAGroups = new List<Group>();
+ private static List<Group> DSAGroups = new List<Group>();
+
+ static ChatHub()
+ {
+ DSAGroups = Database.GetGroups().Result;
+ //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)
{
@@ -20,39 +38,64 @@ namespace DSACore.Hubs
var ident = args.First().Replace("!", "");
if(args.Count>0){args.RemoveAt(0);}
- string group;
try
{
- group = getGroup(Context.ConnectionId).Name;
- await SendToGroup(group, user, Commands.CommandHandler.ExecuteCommand(new Command { CharId = 0, CmdIdentifier = ident, CmdTexts = args, Name = user }));
+ string group = getGroup(Context.ConnectionId).Name;
+ await SendToGroup(Commands.CommandHandler.ExecuteCommand(new Command { CharId = 0, CmdIdentifier = ident, CmdTexts = args, Name = user }));
}
catch(InvalidOperationException e)
{
- await Clients.Caller.SendCoreAsync("ReceiveMessage", new[] {"Nutzer ist in keiner Gruppe. Erst joinen!"});
+ await Clients.Caller.SendCoreAsync("ReceiveMessage", new object[] {"Nutzer ist in keiner Gruppe. Erst joinen!"});
}
}
- 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 Group getGroup(string id)
+ 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 });
//throw new NotImplementedException("add database call to get groups");
}
public async Task AddGroup(string group, string password)
{
- DSAGroups.Add(new Group{Name = group, Password = password});
- Clients.Caller.SendCoreAsync("ReceiveMessage", new[] {$"group {group} sucessfully added"});
+ 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"});
+ //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");
}
@@ -64,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");
}
}