summaryrefslogtreecommitdiff
path: root/DSACore/FireBase/Database.cs
blob: 8946cf07a359aab7f90bbef007a3bdb9afc3d88a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DSACore.DSA_Game;
using DSACore.DSA_Game.Characters;
using DSACore.Models.Database.DSA;
using DSACore.Models.Network;
using Firebase.Database;
using Firebase.Database.Query;

namespace DSACore.FireBase
{
    public static class Database
    {
        public static FirebaseClient firebase;

        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>();

        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)
                });

            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 async Task<int> AddChar(Character file, Group group)
        {
            DatabaseChar.LoadChar(file, out var groupChar, out var data);

            var lastChar = await firebase
                .Child("Chars")
                .OrderByKey()
                .LimitToLast(1)
                .OnceAsync<DatabaseChar>();
            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 async Task<DatabaseChar> GetChar(int id)
        {
            /*var chr = await firebase
                .Child("Chars")
                .Child("Char" + id)
                .OnceSingleAsync<DatabaseChar>();
            return chr;*/
            return Chars["Char" + id];
        }

        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>();*/
            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 async Task<GeneralSpell> GetSpell(string spell)
        {
            /*return await firebase
                .Child("Spells")
                .Child(spell)
                .OnceSingleAsync<GeneralSpell>();*/
            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<Weapon> GetWeapon(string weapon, bool ranged = false)
        {
            var collection = ranged ? "RangedWeapons" : "MeleeWeapons";
            return await firebase
                .Child(collection)
                .Child(weapon)
                .OnceSingleAsync<Weapon>();
        }

        public static async Task<List<Group>> GetGroups()
        {
            var groups = await firebase
                .Child("Groups")
                .OrderByKey()
                .OnceAsync<Models.Database.Groups.Group>();
            var ret = new List<Group>();

            foreach (var firebaseObject in groups)
                ret.Add(new Group(firebaseObject.Object.Name, firebaseObject.Object.Password));

            return ret;
        }

        public static async Task<Models.Database.Groups.Group> GetGroup(int id)
        {
            var group = await firebase
                .Child("Groups")
                .Child("Group" + id)
                .OnceSingleAsync<Models.Database.Groups.Group>();
            return group;
        }

        public static async Task AddGroup(Models.Database.Groups.Group group)
        {
            var lastChar = await firebase
                .Child("Groups")
                .OrderByKey()
                .LimitToLast(1)
                .OnceAsync<Models.Database.Groups.Group>();
            var id = group.Id = lastChar.First().Object.Id + 1;

            await firebase
                .Child("Groups")
                .Child("Group" + id)
                .PutAsync(group);
        }

        public static async void SetGroup(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();
        }
    }
}