summaryrefslogtreecommitdiff
path: root/DSACore
diff options
context:
space:
mode:
authorTrueDoctor <d-kobert@web.de>2018-10-01 22:20:36 +0200
committerTrueDoctor <d-kobert@web.de>2018-10-01 22:20:36 +0200
commitb7ef3c860375baea7b5db95940519ce0746b6ecc (patch)
tree270b9f6ec1c58b153037738f6c6b2aa83205d9fc /DSACore
parenta5a5d368cddd6e8a15298002ccb3b10c90a33980 (diff)
added Command Response Handling
Diffstat (limited to 'DSACore')
-rw-r--r--DSACore/Commands/CommandHandler.cs48
-rw-r--r--DSACore/FireBase/Database.cs87
-rw-r--r--DSACore/Hubs/ChatHub.cs112
-rw-r--r--DSACore/Models/Database/DatabaseChar.cs16
-rw-r--r--DSACore/Models/Database/GeneralSpell.cs4
-rw-r--r--DSACore/Models/Database/Talent.cs9
-rw-r--r--DSACore/Models/Network/CommandResponse.cs31
-rw-r--r--DSACore/Models/Network/Group.cs25
8 files changed, 250 insertions, 82 deletions
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<T>(string path, Dictionary<string, T> list)
+ {
+ var temp = await firebase
+ .Child(path)
+ .OrderByKey()
+ .OnceAsync<T>();
+
+ foreach (var firebaseObject in temp)
+ {
+ list.Add(firebaseObject.Key, firebaseObject.Object);
+ }
}
-
+
+ public static Dictionary<string,DatabaseChar> Chars = new Dictionary<string, DatabaseChar>();
+
+ public static Dictionary<string, MeleeWeapon> MeleeList = new Dictionary<string, MeleeWeapon>();
+
+ public static Dictionary<string, RangedWeapon> RangedWeapons = new Dictionary<string, RangedWeapon>();
+
+ public static Dictionary<string, Talent> Talents = new Dictionary<string, Talent>();
+
+ public static Dictionary<string, GeneralSpell> Spells = new Dictionary<string, GeneralSpell>();
public static async Task<int> 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<DatabaseChar>();
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<DatabaseChar> GetChar(int id)
{
- var chr = await firebase
+ /*var chr = await firebase
.Child("Chars")
.Child("Char" + id)
.OnceSingleAsync<DatabaseChar>();
- return chr;
+ return chr;*/
+ return Chars["Char" + id];
}
public static async Task<Inventory> GetInventory(int id)
{
var inv = await firebase
.Child("Inventories")
- .Child("Inventory"+id)
+ .Child("Inventory" + id)
.OnceSingleAsync<Inventory>();
return inv;
}
@@ -120,11 +157,12 @@ namespace DSACore.FireBase
}
public static async Task<Talent> GetTalent(string talent)
- {
+ {/*
return await firebase
.Child("Talents")
.Child(talent)
- .OnceSingleAsync<Talent>();
+ .OnceSingleAsync<Talent>();*/
+ return Talents[talent];
}
public static async Task AddSpell(GeneralSpell tal)
@@ -145,12 +183,13 @@ namespace DSACore.FireBase
public static async Task<GeneralSpell> GetSpell(string spell)
{
- return await firebase
+ /*return await firebase
.Child("Spells")
.Child(spell)
- .OnceSingleAsync<GeneralSpell>();
+ .OnceSingleAsync<GeneralSpell>();*/
+ 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<Weapon> 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<Group>();
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<Field> skills, List<Field> talents, List<Advantage> advantages, List<CharSpell> spells, List<WeaponTalent> 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; }
+
}
}