diff options
author | TrueDoctor <d-kobert@web.de> | 2018-09-29 23:59:42 +0200 |
---|---|---|
committer | TrueDoctor <d-kobert@web.de> | 2018-09-29 23:59:42 +0200 |
commit | 632781d1adf54287ecfe7cbcbc17074e14a769b2 (patch) | |
tree | ed8d27601fa4ccfa5552af9ef104d1467a05d8e8 /DSACore/FireBase | |
parent | 33dc613fffad69c1c608e21eac6fcd3f2954ead8 (diff) |
added database methods
Diffstat (limited to 'DSACore/FireBase')
-rw-r--r-- | DSACore/FireBase/Database.cs | 212 |
1 files changed, 210 insertions, 2 deletions
diff --git a/DSACore/FireBase/Database.cs b/DSACore/FireBase/Database.cs index fd8aa4c..de7d540 100644 --- a/DSACore/FireBase/Database.cs +++ b/DSACore/FireBase/Database.cs @@ -3,6 +3,9 @@ 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; @@ -11,17 +14,222 @@ namespace DSACore.FireBase { public static class Database { + public static FirebaseClient firebase; + static Database() { var auth = File.ReadAllText(DSACore.DSA_Game.Dsa.rootPath+"Token"); ; // your app secret - var firebaseClient = new FirebaseClient( + firebase = new FirebaseClient( "https://heldenonline-4d828.firebaseio.com/", new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(auth) }); } + + + 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") + .OrderBy("id") + .LimitToLast(1) + .OnceSingleAsync<DatabaseChar>(); + int id = groupChar.Id = data.Id = lastChar.Id + 1; + + await firebase + .Child("Groups") + .Child("Char" + id) + .PutAsync(data); + + await firebase + .Child("Chars") + .Child("Char" + id) + .PutAsync(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(); + + await firebase + .Child("Inventories") + .Child("Inventory" + id) + .DeleteAsync(); + + } + + public static async Task<DatabaseChar> GetChar(int id) + { + var chr = await firebase + .Child("Chars") + .Child("Char" + id) + .OnceSingleAsync<DatabaseChar>(); + return chr; + } + + public static async Task<Inventory> GetInventory(int id) + { + var inv = await firebase + .Child("Inventories") + .Child("Inventory"+id) + .OnceSingleAsync<Inventory>(); + 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(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 async Task<Talent> GetTalent(string talent) + { + return await firebase + .Child("Talents") + .Child(talent) + .OnceSingleAsync<Talent>(); + } + + public static async Task AddSpell(GeneralSpell tal) + { + await firebase + .Child("Spells") + .Child(tal.Name) + .PutAsync(tal); + } - public static void DoStuff(){} + public static async Task RemoveSpell(string spell) + { + await firebase + .Child("Spells") + .Child(spell) + .DeleteAsync(); + } + + public static async Task<GeneralSpell> GetSpell(string spell) + { + return await firebase + .Child("Spells") + .Child(spell) + .OnceSingleAsync<GeneralSpell>(); + } + + + public static async Task AddWeapon(Weapon wep) + { + await firebase + .Child("Weapons") + .Child(wep.Name) + .PutAsync(wep); + } + + public static async Task RemoveWeapon(string weapon) + { + await firebase + .Child("Weapons") + .Child(weapon) + .DeleteAsync(); + } + + public static async Task< Weapon> GetWeapon(string weapon) + { + return await firebase + .Child("Weapons") + .Child(weapon) + .OnceSingleAsync<Weapon>(); + } + + public static async Task<List<Models.Network.Group>> GetGroups() + { + var groups = await firebase + .Child("Groups") + .OrderBy("id") + .OnceAsync<Group>(); + var ret = new List<Models.Network.Group>(); + + foreach (var firebaseObject in groups) + { + ret.Add(new Models.Network.Group(firebaseObject.Object.Name, firebaseObject.Object.Password)); + } + + return ret; + } + + public static async Task<Group> GetGroup(int id) + { + var group = await firebase + .Child("Groups") + .Child("Group"+id) + .OnceSingleAsync<Group>(); + return group; + } + + public static async void AddGroup(Group group) + { + var lastChar = await firebase + .Child("Groups") + .OrderBy("id") + .LimitToLast(1) + .OnceSingleAsync<DatabaseChar>(); + int id = group.Id = lastChar.Id + 1; + + await firebase + .Child("Groups") + .Child("Group"+id) + .PutAsync(group); + } + + public static async void SetGroup(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(); + } } } |