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
|
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<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, DSALib.Models.Database.Dsa.Talent> Talents = new Dictionary<string, DSALib.Models.Database.Dsa.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)
});
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<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, string 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 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(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<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 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<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<Tuple<string, string>>> GetGroups()
{
var groups = await Firebase
.Child("Groups")
.OrderByKey()
.OnceAsync<DSALib.Models.Database.Groups.Group>();
var ret = new List<Tuple<string, string>>();
foreach (var firebaseObject in groups)
ret.Add(new Tuple<string, string>(firebaseObject.Object.Name, firebaseObject.Object.Password));
return ret;
}
public static async Task<DSALib.Models.Database.Groups.Group> GetGroup(int id)
{
var group = await Firebase
.Child("Groups")
.Child("Group" + id)
.OnceSingleAsync<DSALib.Models.Database.Groups.Group>();
return group;
}
public static async Task AddGroup(DSALib.Models.Database.Groups.Group group)
{
var lastChar = await Firebase
.Child("Groups")
.OrderByKey()
.LimitToLast(1)
.OnceAsync<DSALib.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(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();
}
}
}
|