diff options
-rw-r--r-- | DSACore/Auxiliary/WeaponImporter.cs | 177 | ||||
-rw-r--r-- | DSACore/DSA_Game/Dsa.cs | 7 | ||||
-rw-r--r-- | DSACore/FireBase/Database.cs | 13 | ||||
-rw-r--r-- | DSACore/Hubs/ChatHub.cs | 40 | ||||
-rw-r--r-- | DSACore/Models/Database/Group.cs | 3 | ||||
-rw-r--r-- | DSACore/Models/Database/Weapon.cs | 40 | ||||
-rw-r--r-- | DSACore/Models/Network/Group.cs | 24 |
7 files changed, 285 insertions, 19 deletions
diff --git a/DSACore/Auxiliary/WeaponImporter.cs b/DSACore/Auxiliary/WeaponImporter.cs new file mode 100644 index 0000000..7a5a182 --- /dev/null +++ b/DSACore/Auxiliary/WeaponImporter.cs @@ -0,0 +1,177 @@ +using DSACore.Models.Database; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using DSACore.FireBase; +using Group = System.Text.RegularExpressions.Group; + +namespace DSACore.Auxiliary +{ + public class WeaponImporter + { + private List<MeleeWeapon> Weapons = new List<MeleeWeapon>(); + private List<RangedWeapon> Range = new List<RangedWeapon>(); + + public async Task DownloadWeapons() + { + var client = new HttpClient(); + + + + for (int i = 18; i <= 25; i++) + { + var responseString = await client.GetStringAsync("http://diarium.eu/dsa4-forge/ajax/categoryChanged/" + i); + + Regex talentRegex = new Regex(@"(?<=<option value="")([0-9]*)("">)(.*?)(?=<)"); + //Regex idsRegex = new Regex(@"(?<=<option value=\"")([0-9]*)"); + + + var talentMatch = talentRegex.Matches(responseString); + //var idMatch = idsRegex.Matches(responseString); + + var lines = new List<string>(); + var ids = new List<int>(); + + foreach (var matchGroup in talentMatch.ToList()) + { + if (matchGroup.Success) + { + lines.Add(matchGroup.Groups[3].Value); + ids.Add(int.Parse(matchGroup.Groups[1].Value)); + } + } + + + + for (int j = 0; j < lines.Count; j++) + { + var talent = lines[j]; + + var values = await client.GetStringAsync($"http://diarium.eu/dsa4-forge/ajax/calculate/" + i + "/" + ids[j] + "/0/0/0/0/0/10/0/0/0"); + + values = Regex.Unescape(values.Replace(@"\t", "")); + // ... Use named group in regular expression. + Regex expression = new Regex(@"(((?<=(<td>))|(?<=(<td style=\""padding:2px\"">))).*?(?=<\/td>))|((?<=<span style=\""font-weight:bold;text-decoration:underline;\"">).*?(?=<\/span>))"); + + // ... See if we matched. + var matches = expression.Matches(values).Select(x => x.ToString()).ToList(); + + // ... Get group by name. + await AddMelee(i, talent, matches); + Console.Write(j + ","); + //await Task.Delay(TimeSpan.FromSeconds(5)); + + } + + Console.WriteLine($"{i}: {ids.Count} => {Weapons.Count}"); + //await Task.Delay(TimeSpan.FromSeconds(5)); + } + + Console.ReadLine(); + } + + private async Task AddMelee(int i, string talent, List<string> matches) + { + string name = talent.Replace(' ', '_').Replace(".", ""); + if (!matches[1].Equals(string.Empty)) + { + var temp = new MeleeWeapon( + name, + matches[1], + int.TryParse(matches[10], out int weight) ? weight : 0, + matches[0].Split(':', StringSplitOptions.RemoveEmptyEntries).First(), + matches[11]) + { + INI = int.TryParse(matches[3], out int ini) ? ini : 0, + MW = matches[4], + TpKK = matches[2] + }; + + Weapons.Add(temp); + await Database.AddWeapon(temp); + } + /*if (i > 23) + { + var range = new RangedWeapon( + name, + matches[13], + int.TryParse(matches[10], out int weight) ? weight : 0, + matches[0].Split(':', StringSplitOptions.RemoveEmptyEntries).First(), + matches[11]) + { + AtMod = int.TryParse(matches[10], out int atMod) ? atMod : 0, + KKMod = int.TryParse(matches[11], out int kkMod) ? kkMod : 0, + AtReach = matches[3], + TpReach = matches[4], + LoadTime = int.TryParse(matches[5], out int loadTime) ? loadTime : 0 + }; + Range.Add(range); + await Database.AddWeapon(range); + return; + }*/ + if (i > 18) + { + var range = new RangedWeapon( + name, + matches[13].Replace(' ', '+'), + int.TryParse(matches[10], out int weight) ? weight : 0, + matches[0].Split(':', StringSplitOptions.RemoveEmptyEntries).First(), + matches[11]) + { + AtMod = int.TryParse(matches[18], out int atMod) ? atMod : 0, + KKMod = int.TryParse(matches[17], out int kkMod) ? kkMod : 0, + AtReach = matches[14], + TpReach = matches[15], + LoadTime = int.TryParse(matches[18], out int loadTime) ? loadTime : 0 + }; + Range.Add(range); + await Database.AddWeapon(range); + } + } + + private async Task AddRanged(int i, string talent, List<string> matches) + { + string name = talent.Replace(' ', '_').Replace(".", ""); + if (!matches[1].Equals(string.Empty)) + { + var temp = new MeleeWeapon( + name, + matches[1], + int.TryParse(matches[10], out int weight) ? weight : 0, + matches[0].Split(':', StringSplitOptions.RemoveEmptyEntries).First(), + matches[11]) + { + INI = int.TryParse(matches[3], out int ini) ? ini : 0, + MW = matches[4], + TpKK = matches[2] + }; + + Weapons.Add(temp); + await Database.AddWeapon(temp); + } + + if (i > 18) + { + var range = new RangedWeapon( + name, + matches[13].Replace(' ', '+'), + int.TryParse(matches[10], out int weight) ? weight : 0, + matches[0].Split(':', StringSplitOptions.RemoveEmptyEntries).First(), + matches[11]) + { + AtMod = int.TryParse(matches[18], out int atMod) ? atMod : 0, + KKMod = int.TryParse(matches[17], out int kkMod) ? kkMod : 0, + AtReach = matches[14], + TpReach = matches[15], + LoadTime = int.TryParse(matches[18], out int loadTime) ? loadTime : 0 + }; + Range.Add(range); + await Database.AddWeapon(range); + } + } + } +} + diff --git a/DSACore/DSA_Game/Dsa.cs b/DSACore/DSA_Game/Dsa.cs index 824872c..9602791 100644 --- a/DSACore/DSA_Game/Dsa.cs +++ b/DSACore/DSA_Game/Dsa.cs @@ -1,4 +1,5 @@ using System; +using DSACore.Auxiliary; using DSACore.FireBase; using DSALib; using DSALib.Characters; @@ -42,8 +43,6 @@ namespace DSACore.DSA_Game } } - public static void start(){} - public static void Startup() { //new .Auxiliary.Calculator.StringSolver("1d100 - (1d200 + 1) * -50000").Solve(); @@ -74,10 +73,12 @@ namespace DSACore.DSA_Game Database.AddSpell(new Models.Database.GeneralSpell(talent.Name, talent.Probe, talent.Complexity)); }*/ + //new WeaponImporter().DownloadWeapons().Wait(); + Session = new Session { - Chars = Chars.Select(x => SaveChar.FromICharacter(x)).ToList() + Chars = Chars.Select(SaveChar.FromICharacter).ToList() }; //Session.Save(); } diff --git a/DSACore/FireBase/Database.cs b/DSACore/FireBase/Database.cs index 02a4741..12ebb89 100644 --- a/DSACore/FireBase/Database.cs +++ b/DSACore/FireBase/Database.cs @@ -154,24 +154,27 @@ namespace DSACore.FireBase public static async Task AddWeapon(Weapon wep) { + string collection = wep.GetType() == typeof(MeleeWeapon) ? "MeleeWeapons" : "RangedWeapons"; await firebase - .Child("Weapons") + .Child(collection) .Child(wep.Name) .PutAsync(wep); } - public static async Task RemoveWeapon(string weapon) + public static async Task RemoveWeapon(string weapon, bool ranged = false) { + string collection = ranged ? "RangedWeapons" : "MeleeWeapons"; await firebase - .Child("Weapons") + .Child(collection) .Child(weapon) .DeleteAsync(); } - public static async Task< Weapon> GetWeapon(string weapon) + public static async Task< Weapon> GetWeapon(string weapon, bool ranged = false) { + string collection = ranged ? "RangedWeapons" : "MeleeWeapons"; return await firebase - .Child("Weapons") + .Child(collection) .Child(weapon) .OnceSingleAsync<Weapon>(); } diff --git a/DSACore/Hubs/ChatHub.cs b/DSACore/Hubs/ChatHub.cs index 46fde08..3403a17 100644 --- a/DSACore/Hubs/ChatHub.cs +++ b/DSACore/Hubs/ChatHub.cs @@ -53,7 +53,7 @@ namespace DSACore.Hubs private Task SendToGroup(string message) { string group = getGroup(Context.ConnectionId).Name; - return Clients.Group(group).SendCoreAsync("ReceiveMessage", new object[] { getUser(Context.ConnectionId), message }); + return Clients.Group(group).SendCoreAsync("ReceiveMessage", new object[] { getUser(Context.ConnectionId).Name, message }); } private Models.Network.Group getGroup(string id) @@ -78,7 +78,7 @@ namespace DSACore.Hubs } } - await Clients.Caller.SendCoreAsync("ListGroups", new object[] { DSAGroups }); + await Clients.Caller.SendCoreAsync("ListGroups", new object[] { DSAGroups.Select(x=>x.SendGroup()) }); //throw new NotImplementedException("add database call to get groups"); } @@ -99,26 +99,46 @@ namespace DSACore.Hubs //throw new NotImplementedException("add database call to add groups"); } - public async Task Login(string group, string user, string password) + public async Task Login(string group, string user, string hash) { - if (password == DSAGroups.First(x=>x.Name == group).Password) + //string password = System.Text.Encoding.UTF8.GetString(hash); + if (hash == DSAGroups.First(x=>x.Name == group).Password) { DSAGroups.First(x=>x.Name.Equals(group)).Users.Add(new User{ConnectionId = Context.ConnectionId, Name = user}); await Groups.AddToGroupAsync(Context.ConnectionId, group); + + await SendToGroup("Ein neuer Nutzer hat die Gruppe betreten"); + } + else + { + + await Clients.Caller.SendAsync("ReceiveMessage", "Falsches Passwort!"); } + } - await SendToGroup("Ein neuer Nutzer hat die Gruppe betreten"); + public override Task OnDisconnectedAsync(Exception exception) + { + Disconnect().Wait(); + return base.OnDisconnectedAsync(exception); } public async Task Disconnect() { - var group = getGroup(Context.ConnectionId); + try + { + var group = getGroup(Context.ConnectionId); - await SendToGroup("Connection lost"); - var user = getUser(Context.ConnectionId); - group.Users.Remove(user); - await Groups.RemoveFromGroupAsync(Context.ConnectionId, group.Name); + 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/Group.cs b/DSACore/Models/Database/Group.cs index 0273aed..a7bb929 100644 --- a/DSACore/Models/Database/Group.cs +++ b/DSACore/Models/Database/Group.cs @@ -12,5 +12,8 @@ namespace DSACore.Models.Database public string Password { get; set; } public int Id { get; set; } public List<GroupChar> Chars { get; set; }= new List<GroupChar>(); + } + + } diff --git a/DSACore/Models/Database/Weapon.cs b/DSACore/Models/Database/Weapon.cs index 9c75b30..b2f8a1e 100644 --- a/DSACore/Models/Database/Weapon.cs +++ b/DSACore/Models/Database/Weapon.cs @@ -7,9 +7,47 @@ namespace DSACore.Models.Database { public class Weapon { + public Weapon() + { + } + + public Weapon(string name, string damage, int weight, string weaponTalent, string price) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + Damage = damage ?? throw new ArgumentNullException(nameof(damage)); + Weight = weight; + WeaponTalent = weaponTalent ?? throw new ArgumentNullException(nameof(weaponTalent)); + Price = price; + } + public string Name { get; set; } public string Damage { get; set; } + public int Weight { get; set; } public string WeaponTalent { get; set; } - public string Modifier { get; set; } + public string Price { get; set; } + } + + public class MeleeWeapon : Weapon + { + public string TpKK { get; set; } + public int INI { get; set; } + public string MW { get; set; } + + public MeleeWeapon(string name, string damage, int weight, string weaponTalent, string price) : base(name, damage, weight, weaponTalent, price) + { + } + } + + public class RangedWeapon : Weapon + { + public int AtMod { get; set; } + public int KKMod { get; set; } + public string AtReach { get; set; } + public string TpReach { get; set; } + public int LoadTime { get; set; } + + public RangedWeapon(string name, string damage, int weight, string weaponTalent, string price) : base(name, damage, weight, weaponTalent, price) + { + } } } diff --git a/DSACore/Models/Network/Group.cs b/DSACore/Models/Network/Group.cs index 80a5a81..6e62dc8 100644 --- a/DSACore/Models/Network/Group.cs +++ b/DSACore/Models/Network/Group.cs @@ -7,14 +7,38 @@ 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) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + UserCount = userCount; + } + public string Name { get; set; } public string Password { get; set; } public List<User> Users { get; set; } = new List<User>(); + + public int UserCount + { + get { return _online; RefreshOnline();} + set { _online = value; RefreshOnline();} + } + + private void RefreshOnline() + { + _online = Users.Count; + } + + public Group SendGroup() + { + return new Group( Name, UserCount); + } } } |