-
Notifications
You must be signed in to change notification settings - Fork 0
/
LegendDB.cs
322 lines (301 loc) · 12.8 KB
/
LegendDB.cs
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using LegendDialogue;
using LegendItems;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Text;
namespace LegendSharp
{
class LegendDB
{
public static BaseItem DecodeBaseItem(BsonDocument itemDocument, String itemId)
{
String itemType = itemDocument["item_type"].AsString;
String itemSprite = itemDocument["sprite"].AsString;
String itemName = itemDocument["name"].AsString;
String itemDescription = itemDocument["description"].AsString;
int maxStack = 1;
if (itemDocument.Contains("max_stack"))
{
maxStack = itemDocument["max_stack"].AsInt32;
}
if (itemType == "weapon")
{
String weaponClass = itemDocument["weapon_class"].AsString;
double damage = itemDocument["damage"].AsDouble;
String damageType = itemDocument["damage_type"].AsString;
return new BaseWeapon(itemSprite, itemName, itemDescription, itemId, weaponClass, damage, damageType);
}
else
{
return new BaseItem(itemSprite, itemName, itemDescription, itemId, itemType, maxStack);
}
}
public static Item DecodeItem(BsonDocument itemDocument, Dictionary<String, BaseItem> baseItems)
{
String baseItemId = itemDocument.GetValue("base").AsString;
BaseItem baseItem;
if (baseItems.ContainsKey(baseItemId))
{
baseItem = baseItems[baseItemId];
}
else
{
baseItem = new BaseItem("unknown", "unknown", "You don't know anything about this item", "unknown");
}
String itemType = null;
String itemSprite = null;
String itemName = null;
String itemDescription = null;
int? maxStack = null;
int quantity = 1;
if (itemDocument.Contains("item_type"))
{
itemType = itemDocument["item_type"].AsString;
}
if (itemDocument.Contains("sprite"))
{
itemSprite = itemDocument["sprite"].AsString;
}
if (itemDocument.Contains("name"))
{
itemName = itemDocument["name"].AsString;
}
if (itemDocument.Contains("description"))
{
itemDescription = itemDocument["description"].AsString;
}
if (itemDocument.Contains("max_stack"))
{
maxStack = itemDocument["max_stack"].AsInt32;
}
if (itemDocument.Contains("quantity"))
{
quantity = itemDocument["quantity"].AsInt32;
}
if (itemType == "weapon" || (itemType == null && baseItem.type == "weapon"))
{
String weaponClass = null;
double damage = Double.NaN;
String damageType = null;
if (itemDocument.Contains("weapon_class"))
{
weaponClass = itemDocument["weapon_class"].AsString;
}
if (itemDocument.Contains("damage"))
{
damage = itemDocument["damage"].AsDouble;
}
if (itemDocument.Contains("damage_type"))
{
damageType = itemDocument["damageType"].AsString;
}
return new Weapon((BaseWeapon)baseItem, itemSprite, itemName, itemDescription, itemType, weaponClass, damage, damageType);
}
else
{
return new Item(baseItem, itemSprite, itemName, itemDescription, itemType, maxStack, quantity);
}
}
public static PlayerAction DecodeAction(BsonDocument actionDocument, Dictionary<String, BaseItem> baseItems)
{
string actionType = actionDocument.GetValue("type").AsString;
if (actionType == "set_flag")
{
string flagKey = actionDocument.GetValue("flag").AsString;
Flag flagValue = DecodeFlag(actionDocument.GetValue("value"));
return new SetFlagAction(flagKey, flagValue);
}
else if (actionType == "give_item")
{
BsonDocument itemDocument = actionDocument.GetValue("item").AsBsonDocument;
return new GiveItemAction(DecodeItem(itemDocument, baseItems));
}
else if (actionType == "increment_flag")
{
string flagKey = actionDocument.GetValue("flag").AsString;
NumericalFlag amount = (NumericalFlag)DecodeFlag(actionDocument.GetValue("value"));
return new IncrementFlagAction(flagKey, amount);
}
else
{
return new NullAction();
}
}
public static Flag DecodeFlag(BsonValue flagValue)
{
if (flagValue.IsInt32)
{
int flagNumericalValue = flagValue.AsInt32;
return new NumericalFlag((double)flagNumericalValue);
}
else if (flagValue.IsDouble)
{
double flagNumericalValue = flagValue.AsDouble;
return new NumericalFlag((double)flagNumericalValue);
}
else
{
return new NumericalFlag(0);
}
}
public static Option DecodeOption(BsonDocument optionDocument)
{
string text = optionDocument.GetValue("text").AsString;
string optionType = optionDocument.GetValue("type").AsString;
List<Requirement> requirements = new List<Requirement>();
if (optionDocument.Contains("requirements"))
{
BsonArray requirementArray = optionDocument.GetValue("requirements").AsBsonArray;
foreach (BsonValue requirementValue in requirementArray)
{
BsonDocument requirementDocument = requirementValue.AsBsonDocument;
requirements.Add(DecodeRequirement(requirementDocument));
}
}
if (optionType == "dialogue")
{
string dialogueKey = optionDocument.GetValue("dialogue").AsString;
return new DialogueOption(text, dialogueKey, requirements.ToArray());
}
else
{
//End
return new EndDialogueOption(text, requirements.ToArray());
}
}
public static Requirement DecodeRequirement(BsonDocument requirementDocument)
{
string requirementType = requirementDocument.GetValue("type").AsString;
if (requirementType == "set_flag")
{
string flagKey = requirementDocument.GetValue("flag").AsString;
return new SetRequirement(flagKey);
}
else if (requirementType == "null_flag")
{
string flagKey = requirementDocument.GetValue("flag").AsString;
return new NullRequirement(flagKey);
}
else if (requirementType == "equals")
{
string flagKey = requirementDocument.GetValue("flag").AsString;
Flag flagValue = DecodeFlag(requirementDocument.GetValue("value"));
return new EqualsRequirement(flagKey, flagValue);
}
else if (requirementType == "less_than")
{
string flagKey = requirementDocument.GetValue("flag").AsString;
NumericalFlag flagValue = (NumericalFlag)DecodeFlag(requirementDocument.GetValue("value"));
return new LessThanRequirement(flagKey, flagValue);
}
else if (requirementType == "more_than")
{
string flagKey = requirementDocument.GetValue("flag").AsString;
NumericalFlag flagValue = (NumericalFlag)DecodeFlag(requirementDocument.GetValue("value"));
return new MoreThanRequirement(flagKey, flagValue);
}
else if (requirementType == "not")
{
BsonDocument subRequirementDocument = requirementDocument.GetValue("requirement").AsBsonDocument;
return new NotRequirement(DecodeRequirement(subRequirementDocument));
}
else if (requirementType == "and")
{
List<Requirement> requirements = new List<Requirement>();
BsonArray requirementArray = requirementDocument.GetValue("requirements").AsBsonArray;
foreach (BsonValue requirementValue in requirementArray)
{
BsonDocument subRequirementDocument = requirementValue.AsBsonDocument;
requirements.Add(DecodeRequirement(requirementDocument));
}
return new AndRequirement(requirements.ToArray());
}
else if (requirementType == "or")
{
List<Requirement> requirements = new List<Requirement>();
BsonArray requirementArray = requirementDocument.GetValue("requirements").AsBsonArray;
foreach (BsonValue requirementValue in requirementArray)
{
BsonDocument subRequirementDocument = requirementValue.AsBsonDocument;
requirements.Add(DecodeRequirement(requirementDocument));
}
return new OrRequirement(requirements.ToArray());
}
else if (requirementType == "true")
{
return new TrueRequirement();
}
else
{
return new FalseRequirement();
}
}
public static Substitution DecodeSubstitution(BsonDocument subDocument, Config config)
{
string subType = subDocument.GetValue("type").AsString;
if (subType == "flag")
{
string flagKey = subDocument.GetValue("flag").AsString;
return new FlagSubstitution(flagKey);
}
else if (subType == "char")
{
string flagKey = subDocument.GetValue("flag").AsString;
return new FlagCharSubstitution(flagKey);
}
else if (subType == "item")
{
BsonDocument itemDoc = subDocument.GetValue("item").AsBsonDocument;
Item item = DecodeItem(itemDoc, config.baseItems);
return new ItemSubstitution(item);
}
else if (subType == "text")
{
string text = subDocument.GetValue("text").AsString;
return new StringSubstitution(text);
}
else
{
return new StringSubstitution("");
}
}
public static Dialogue DecodeDialogue(BsonDocument dialogueDocument, Config config)
{
string text = dialogueDocument.GetValue("text").AsString;
string author = dialogueDocument.GetValue("author").AsString;
string sprite = dialogueDocument.GetValue("sprite").AsString;
List<Option> options = new List<Option>();
if (dialogueDocument.Contains("options"))
{
var bsonOptions = dialogueDocument.GetElement("options").Value.AsBsonArray;
foreach (var bsonOption in bsonOptions)
{
var optionDocument = bsonOption.AsBsonDocument;
options.Add(DecodeOption(optionDocument.AsBsonDocument));
}
}
List<PlayerAction> actions = new List<PlayerAction>();
if (dialogueDocument.Contains("actions"))
{
var bsonActions = dialogueDocument.GetElement("actions").Value.AsBsonArray;
foreach (var bsonAction in bsonActions)
{
var actionDocument = bsonAction.AsBsonDocument;
actions.Add(DecodeAction(actionDocument, config.baseItems));
}
}
List<Substitution> substitutions = new List<Substitution>();
if (dialogueDocument.Contains("substitutions"))
{
var bsonSubs = dialogueDocument.GetElement("substitutions").Value.AsBsonArray;
foreach (var subValue in bsonSubs)
{
var subDocument = subValue.AsBsonDocument;
substitutions.Add(DecodeSubstitution(subDocument, config));
}
}
return new Dialogue(text, author, sprite, options.ToArray(), actions.ToArray(), substitutions.ToArray());
}
}
}