-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShieldRetriever.cs
367 lines (291 loc) · 13.9 KB
/
ShieldRetriever.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using Discord;
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace Dx2_DiscordBot
{
public class ShieldRetriever : RetrieverBase
{
#region Properties
public static List<Shield> Shields;
private const int LEV_DISTANCE = 1;
#endregion
#region Constructor
//Constructor
public ShieldRetriever(DiscordSocketClient client) : base(client)
{
MainCommand = "!dx2shield";
}
#endregion
#region Overrides
//Initialization
public async override Task ReadyAsync()
{
var ShieldsDt = await GetCSV("https://raw.githubusercontent.com/Alenael/Dx2DB/master/csv/SMT Dx2 Database - Shields.csv");
var tempShields = new List<Shield>();
foreach(DataRow row in ShieldsDt.Rows)
tempShields.Add(LoadShield(row));
Shields = tempShields;
}
//Recieve Messages here
public async override Task MessageReceivedAsync(SocketMessage message, string serverName, ulong channelId)
{
//Let Base Update
await base.MessageReceivedAsync(message, serverName, channelId);
if (message.Content.StartsWith(MainCommand))
{
var items = message.Content.Split(MainCommand);
//Save demon to be searched for
string searchedShield = items[1].Trim().Replace("*", "☆").ToLower();
if (_client.GetChannel(channelId) is IMessageChannel chnl)
{
//Try to find demon
var Shield = Shields.Find(d => d.DemonName.ToLower() == searchedShield);
//If exact demon not found
if (Shield.DemonName == null)
{
//Find anyone matching the nickname of a demon
Shield = Shields.Find(d => d.Nicknames != "" && d.NicknamesList.Any(n => n == searchedShield));
if (Shield.DemonName == null)
{
//Find all similar demons
List<String> similarShields = GetSimilarShields(searchedShield, LEV_DISTANCE);
//If no similar demons found
if (similarShields.Count == 0)
{
List<string> ShieldStartingWith = new List<string>();
ShieldStartingWith = FindShieldsStartingWith(searchedShield);
if (ShieldStartingWith.Count == 1)
{
Shield = Shields.Find(x => x.DemonName.ToLower() == ShieldStartingWith[0].ToLower());
if (Shield.DemonName != null)
await chnl.SendMessageAsync("", false, Shield.WriteToDiscord());
}
else if (ShieldStartingWith.Count > 1)
{
string answerString = "Could not find: " + searchedShield + ". Did you mean: ";
foreach (string fuzzyShield in ShieldStartingWith)
{
answerString += fuzzyShield + ", ";
}
//Remove last space and comma
answerString = answerString.Remove(answerString.Length - 2);
answerString += "?";
await chnl.SendMessageAsync(answerString, false);
}
else
{
await chnl.SendMessageAsync("Could not find: " + searchedShield, false);
}
}
//If exactly 1 demon found, return its Info
else if (similarShields.Count == 1)
{
//Find exactly this demon
Shield = Shields.Find(x => x.DemonName.ToLower() == similarShields[0].ToLower());
if (Shield.DemonName != null)
await chnl.SendMessageAsync("", false, Shield.WriteToDiscord());
}
//If similar demons found
else
{
//Build answer string
string answerString = "Could not find: " + searchedShield + ". Did you mean: ";
foreach (string fuzzyShield in similarShields)
{
answerString += fuzzyShield + ", ";
}
//Remove last space and comma
answerString = answerString.Remove(answerString.Length - 2);
answerString += "?";
await chnl.SendMessageAsync(answerString, false);
}
}
else
await chnl.SendMessageAsync("", false, Shield.WriteToDiscord());
}
else
await chnl.SendMessageAsync("", false, Shield.WriteToDiscord());
}
}
}
//Find demons starting with
private List<string> FindShieldsStartingWith(string searchedShield)
{
List<string> ShieldSW = new List<string>();
foreach(Shield Shield in Shields)
if (Shield.DemonName.ToLower().StartsWith(searchedShield.ToLower()))
ShieldSW.Add(Shield.DemonName);
return ShieldSW;
}
/// <summary>
/// Returns List of Shields whose name have a Levinshtein Distance of LEV_DISTANCE
/// </summary>
/// <param name="searchedShield">Name of the Shield that is being compared agianst</param>
/// <returns></returns>
private List<string> GetSimilarShields(string searchedShield, int levDist)
{
List<string> simShields = new List<string>();
foreach (Shield Shield in Shields)
{
int levDistance = 999;
try
{
levDistance = LevenshteinDistance.EditDistance(Shield.DemonName.ToLower(), searchedShield);
}
catch (ArgumentNullException e)
{
Logger.LogAsync("ArgumentNullException in getSimilarDemons: " + e.Message);
}
//If only off by levDist characters, add to List
if (levDistance <= levDist)
simShields.Add(Shield.DemonName);
}
return simShields;
}
//Returns the commands for this Retriever
public override string GetCommands()
{
return "\n\nShield Commands:" +
"\n* " + MainCommand + " [Demon Name] - Search's for a Shield with the demon name you provided as [Demon Name]. If nothing is found you will recieve a message back stating Shield was not found.";
}
//Returns a value based on what its passed
private static string LoadResist(string value)
{
if (value == "" || value == null)
return "";
return value.First().ToString().ToUpper() + value.Substring(1);
}
#endregion
#region Public Methods
//Creates a demon object from a data grid view row
public static Shield LoadShield(DataRow row)
{
var Shield = new Shield();
Shield.DemonName = row["demon name"] is DBNull ? "" : (string)row["demon name"];
Shield.ShieldName = row["Shield Name"] is DBNull ? "" : (string)row["Shield Name"];
Shield.Talent = row["talent name"] is DBNull ? "" : (string)row["talent name"];
Shield.Effect = row["effect"] is DBNull ? "" : (string)row["effect"];
Shield.Skill1 = row["skill 1"] is DBNull ? "" : (string)row["skill 1"];
Shield.Skill2 = row["skill 2"] is DBNull ? "" : (string)row["skill 2"];
Shield.AwakenSkill = row["Awaken Skill"] is DBNull ? "" : (string)row["Awaken Skill"];
Shield.PDef = row["Defense"] is DBNull ? "" : (string)row["Defense"];
Shield.Attribute = row["DMG reduction for atribute"] is DBNull ? "" : (string)row["DMG reduction for atribute"];
Shield.MDef = row["mdef"] is DBNull ? "" : (string)row["mdef"];
Shield.HP = row["HP%"] is DBNull ? "" : (string)row["HP%"];
Shield.Panel1 = row["panel 1"] is DBNull ? "" : (string)row["panel 1"];
Shield.Panel2 = row["panel 2"] is DBNull ? "" : (string)row["panel 2"];
Shield.Panel3 = row["panel 3"] is DBNull ? "" : (string)row["panel 3"];
Shield.Panel1Stats = row["Panel 1 Step"] is DBNull ? "" : (string)row["Panel 1 Step"];
Shield.Panel2Stats = row["panel 2 Step"] is DBNull ? "" : (string)row["panel 2 Step"];
Shield.Panel3Stats = row["panel 3 Step"] is DBNull ? "" : (string)row["panel 3 Step"];
Shield.Nicknames = row["Nickname"] is DBNull ? "" : (string)row["Nickname"];
Shield.NicknamesList = new List<string>();
if (Shield.Nicknames.Contains(","))
{
var nicknameList = Shield.Nicknames.Split(",");
foreach (var nickname in nicknameList)
Shield.NicknamesList.Add(nickname.Trim());
}
else
{
Shield.NicknamesList.Add(Shield.Nicknames.Trim());
}
Shield.Fire = LoadResist(row["fire"] is DBNull ? "" : (string)row["fire"]);
Shield.Dark = LoadResist(row["dark"] is DBNull ? "" : (string)row["dark"]);
Shield.Light = LoadResist(row["light"] is DBNull ? "" : (string)row["light"]);
Shield.Elec = LoadResist(row["elec"] is DBNull ? "" : (string)row["elec"]);
Shield.Ice = LoadResist(row["ice"] is DBNull ? "" : (string)row["ice"]);
Shield.Force = LoadResist(row["force"] is DBNull ? "" : (string)row["force"]);
Shield.Phys = LoadResist(row["phys"] is DBNull ? "" : (string)row["phys"]);
return Shield;
}
#endregion
}
#region Structs
//Object to hold Demon Data
public struct Shield
{
public string DemonName;
public string ShieldName;
public string Talent;
public string Effect;
public string Skill1;
public string Skill2;
public string AwakenSkill;
public string Panel1;
public string Panel2;
public string Panel3;
public string Panel1Stats;
public string Panel2Stats;
public string Panel3Stats;
public string PDef;
public string MDef;
public string Attribute;
public string HP;
public string Phys;
public string Fire;
public string Ice;
public string Elec;
public string Force;
public string Light;
public string Dark;
public string Nicknames;
public List<string> NicknamesList;
public Embed WriteToDiscord()
{
var url = "https://dx2wiki.com/index.php/" + Uri.EscapeDataString(ShieldName);
var thumbnail = "https://raw.githubusercontent.com/Alenael/Dx2DB/master/Images/Shields/" + Uri.EscapeDataString(ShieldName.Replace("☆", "")) + ".jpg";
var eb = new EmbedBuilder();
eb.WithTitle(ShieldName);
eb.AddField("Demon Name:", DemonName, true);
eb.AddField("Skills:",
Skill1 + "\n" +
Skill2 + "\n" +
AwakenSkill, true);
eb.AddField(Talent + ":", Effect == "" ? "-" : Effect, false);
var panelInfo1 = "";
var panelInfo2 = "";
var panelInfo3 = "";
if (Panel1 != "")
panelInfo1 = "1: " + Panel1 + " " + Panel1Stats + "\n";
if (Panel2 != "")
panelInfo2 = "2: " + Panel2 + " " + Panel2Stats + "\n";
if (Panel3 != "")
panelInfo3 = "3: " + Panel3 + " " + Panel3Stats + "\n";
if (panelInfo1 != "")
eb.AddField("Panels", panelInfo1 + panelInfo2 + panelInfo3, true);
var resist = "";
if (Phys != "")
resist += " | Phys: " + Phys + " ";
if (Fire != "")
resist += " | Fire: " + Fire + " ";
if (Ice != "")
resist += " | Ice: " + Ice + " ";
if (Elec != "")
resist += " | Elec: " + Elec + " ";
if (Force != "")
resist += " | Force: " + Force + " ";
if (Light != "")
resist += " | Light: " + Light + " ";
if (Dark != "")
resist += " | Dark: " + Dark;
if (resist.Length > 0)
resist = resist.Remove(0, 3);
eb.AddField("Resists", resist, false);
////Other Info
eb.WithFooter(Attribute +
" | HP%: " + HP +
" | PDef: " + PDef +
" | MDef: " + MDef);
eb.WithColor(Color.Red);
eb.WithUrl(url);
eb.WithThumbnailUrl(thumbnail);
return eb.Build();
}
}
#endregion
}