From b7ef3c860375baea7b5db95940519ce0746b6ecc Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Mon, 1 Oct 2018 22:20:36 +0200 Subject: added Command Response Handling --- DSACore/FireBase/Database.cs | 87 ++++++++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 24 deletions(-) (limited to 'DSACore/FireBase/Database.cs') 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(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 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(); public static async Task 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(); 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 GetChar(int id) { - var chr = await firebase + /*var chr = await firebase .Child("Chars") .Child("Char" + id) .OnceSingleAsync(); - return chr; + return chr;*/ + return Chars["Char" + id]; } public static async Task GetInventory(int id) { var inv = await firebase .Child("Inventories") - .Child("Inventory"+id) + .Child("Inventory" + id) .OnceSingleAsync(); return inv; } @@ -120,11 +157,12 @@ namespace DSACore.FireBase } public static async Task GetTalent(string talent) - { + {/* return await firebase .Child("Talents") .Child(talent) - .OnceSingleAsync(); + .OnceSingleAsync();*/ + return Talents[talent]; } public static async Task AddSpell(GeneralSpell tal) @@ -145,12 +183,13 @@ namespace DSACore.FireBase public static async Task GetSpell(string spell) { - return await firebase + /*return await firebase .Child("Spells") .Child(spell) - .OnceSingleAsync(); + .OnceSingleAsync();*/ + 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 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(); return group; } @@ -215,7 +254,7 @@ namespace DSACore.FireBase await firebase .Child("Groups") - .Child("Group"+id) + .Child("Group" + id) .PutAsync(group); } -- cgit v1.2.3-70-g09d2