From e6181c24124d97f2fbc932b8a68311e625463156 Mon Sep 17 00:00:00 2001 From: uzvkl Date: Tue, 11 Jun 2019 23:05:52 +0200 Subject: Move dsa related stuff to subfolder --- dsa/DSALib/FireBase/Database.cs | 270 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 dsa/DSALib/FireBase/Database.cs (limited to 'dsa/DSALib/FireBase/Database.cs') diff --git a/dsa/DSALib/FireBase/Database.cs b/dsa/DSALib/FireBase/Database.cs new file mode 100644 index 0000000..1edd699 --- /dev/null +++ b/dsa/DSALib/FireBase/Database.cs @@ -0,0 +1,270 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using DSALib.DSA_Game; +using DSALib.DSA_Game.Characters; +using DSALib.Models.Database.Dsa; +using Firebase.Database; +using Firebase.Database.Query; + +namespace DSALib.FireBase +{ + public static class Database + { + public static FirebaseClient Firebase; + + public static Dictionary Chars = new Dictionary(); + + public static Dictionary MeleeList = new Dictionary(); + + public static Dictionary RangedWeapons = new Dictionary(); + + public static Dictionary Talents = new Dictionary(); + + public static Dictionary Spells = new Dictionary(); + + static Database() + { + var auth = File.ReadAllText(Dsa.rootPath + "Token"); // your app secret + Firebase = new FirebaseClient( + "https://heldenonline-4d828.firebaseio.com/", + new FirebaseOptions + { + AuthTokenAsyncFactory = () => Task.FromResult(auth) + }); + + Task.Run(Initialize); + } + + private static void Initialize() { + var waiting = new[] { + // ToDo IntializeCollection("Chars", Chars), + IntializeCollection("MeleeWeapons", MeleeList), + IntializeCollection("RangedWeapons", RangedWeapons), + IntializeCollection("Talents", Talents), + IntializeCollection("Spells", Spells), + }; + Task.WaitAll(waiting); + } + + private static async Task IntializeCollection(string path, Dictionary list) + { + var temp = await Firebase + .Child(path) + .OrderByKey() + .OnceAsync(); + + foreach (var firebaseObject in temp) list.Add(firebaseObject.Key, firebaseObject.Object); + } + + public static async Task AddChar(Character file, string group) + { + DatabaseChar.LoadChar(file, out var groupChar, out var data); + + var lastChar = await Firebase + .Child("Chars") + .OrderByKey() + .LimitToLast(1) + .OnceAsync(); + var id = groupChar.Id = data.Id = lastChar.First().Object.Id + 1; + + await Firebase //TODO Reomve await Operators + .Child("Groups") + .Child("Char" + id) + .PutAsync(groupChar); + + await Firebase + .Child("Chars") + .Child("Char" + id) + .PutAsync(data); + + Chars["Char" + id] = data; + + await Firebase + .Child("Inventories") + .Child("Inventory" + id) + .PutAsync(new Inventory()); + + return id + 1; + } + + public static async Task RemoveChar(int id) + { + await Firebase + .Child("Groups") + .Child("Char" + id) + .DeleteAsync(); + + await Firebase + .Child("Chars") + .Child("Char" + id) + .DeleteAsync(); + + Chars.Remove("Char" + id); + + await Firebase + .Child("Inventories") + .Child("Inventory" + id) + .DeleteAsync(); + } + + public static DatabaseChar GetChar(int id) + { + /*var chr = await firebase + .Child("Chars") + .Child("Char" + id) + .OnceSingleAsync(); + return chr;*/ + return Chars["Char" + id]; + } + + public static async Task GetInventory(int id) + { + var inv = await Firebase + .Child("Inventories") + .Child("Inventory" + id) + .OnceSingleAsync(); + return inv; + } + + public static async Task SetInventory(int id, Inventory inv) + { + await Firebase + .Child("Inventories") + .Child("Inventory" + id) + .PutAsync(inv); + } + + public static async Task AddTalent(DSALib.Models.Database.Dsa.Talent tal) + { + await Firebase + .Child("Talents") + .Child(tal.Name) + .PutAsync(tal); + } + + public static async Task RemoveTalent(string talent) + { + await Firebase + .Child("Talents") + .Child(talent) + .DeleteAsync(); + } + + public static DSALib.Models.Database.Dsa.Talent GetTalent(string talent) + { + /* + return await firebase + .Child("Talents") + .Child(talent) + .OnceSingleAsync();*/ + return Talents[talent]; + } + + public static async Task AddSpell(GeneralSpell tal) + { + await Firebase + .Child("Spells") + .Child(tal.Name) + .PutAsync(tal); + } + + public static async Task RemoveSpell(string spell) + { + await Firebase + .Child("Spells") + .Child(spell) + .DeleteAsync(); + } + + public static GeneralSpell GetSpell(string spell) + { + return Spells[spell]; + } + + + public static async Task AddWeapon(Weapon wep) + { + var collection = wep.GetType() == typeof(MeleeWeapon) ? "MeleeWeapons" : "RangedWeapons"; + await Firebase + .Child(collection) + .Child(wep.Name) + .PutAsync(wep); + } + + public static async Task RemoveWeapon(string weapon, bool ranged = false) + { + var collection = ranged ? "RangedWeapons" : "MeleeWeapons"; + await Firebase + .Child(collection) + .Child(weapon) + .DeleteAsync(); + } + + public static async Task GetWeapon(string weapon, bool ranged = false) + { + var collection = ranged ? "RangedWeapons" : "MeleeWeapons"; + return await Firebase + .Child(collection) + .Child(weapon) + .OnceSingleAsync(); + } + + public static async Task>> GetGroups() + { + var groups = await Firebase + .Child("Groups") + .OrderByKey() + .OnceAsync(); + var ret = new List>(); + + foreach (var firebaseObject in groups) + ret.Add(new Tuple(firebaseObject.Object.Name, firebaseObject.Object.Password)); + + return ret; + } + + public static async Task GetGroup(int id) + { + var group = await Firebase + .Child("Groups") + .Child("Group" + id) + .OnceSingleAsync(); + return group; + } + + public static async Task AddGroup(DSALib.Models.Database.Groups.Group group) + { + var lastChar = await Firebase + .Child("Groups") + .OrderByKey() + .LimitToLast(1) + .OnceAsync(); + var id = group.Id = lastChar.First().Object.Id + 1; + + await Firebase + .Child("Groups") + .Child("Group" + id) + .PutAsync(group); + } + + public static async void SetGroup(DSALib.Models.Database.Groups.Group group) + { + await Firebase + .Child("Groups") + .Child("Group" + group.Id) + .PutAsync(group); + } + + public static async void DeleteGroup(int id) + { + await Firebase + .Child("Groups") + .Child("Group" + id) + .DeleteAsync(); + } + } +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf