/
SwordRetriever.cs
313 lines (251 loc) · 12 KB
/
SwordRetriever.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
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 SwordRetriever : RetrieverBase
{
#region Properties
public static List<Sword> Swords;
private const int LEV_DISTANCE = 1;
#endregion
#region Constructor
//Constructor
public SwordRetriever(DiscordSocketClient client) : base(client)
{
MainCommand = "!dx2sword";
}
#endregion
#region Overrides
//Initialization
public async override Task ReadyAsync()
{
var swordsDt = await GetCSV("https://raw.githubusercontent.com/Alenael/Dx2DB/master/csv/SMT Dx2 Database - Swords.csv");
var tempSwords = new List<Sword>();
foreach(DataRow row in swordsDt.Rows)
tempSwords.Add(LoadSword(row));
Swords = tempSwords;
}
//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 searchedSword = items[1].Trim().Replace("*", "☆").ToLower();
if (_client.GetChannel(channelId) is IMessageChannel chnl)
{
//Try to find demon
var sword = Swords.Find(d => d.DemonName.ToLower() == searchedSword);
//If exact demon not found
if (sword.DemonName == null)
{
//Find anyone matching the nickname of a demon
sword = Swords.Find(d => d.Nicknames != "" && d.NicknamesList.Any(n => n == searchedSword));
if (sword.DemonName == null)
{
//Find all similar demons
List<String> similarSwords = GetSimilarSwords(searchedSword, LEV_DISTANCE);
//If no similar demons found
if (similarSwords.Count == 0)
{
List<string> swordStartingWith = new List<string>();
swordStartingWith = FindSwordsStartingWith(searchedSword);
if (swordStartingWith.Count == 1)
{
sword = Swords.Find(x => x.DemonName.ToLower() == swordStartingWith[0].ToLower());
if (sword.DemonName != null)
await chnl.SendMessageAsync("", false, sword.WriteToDiscord());
}
else if (swordStartingWith.Count > 1)
{
string answerString = "Could not find: " + searchedSword + ". Did you mean: ";
foreach (string fuzzySword in swordStartingWith)
{
answerString += fuzzySword + ", ";
}
//Remove last space and comma
answerString = answerString.Remove(answerString.Length - 2);
answerString += "?";
await chnl.SendMessageAsync(answerString, false);
}
else
{
await chnl.SendMessageAsync("Could not find: " + searchedSword, false);
}
}
//If exactly 1 demon found, return its Info
else if (similarSwords.Count == 1)
{
//Find exactly this demon
sword = Swords.Find(x => x.DemonName.ToLower() == similarSwords[0].ToLower());
if (sword.DemonName != null)
await chnl.SendMessageAsync("", false, sword.WriteToDiscord());
}
//If similar demons found
else
{
//Build answer string
string answerString = "Could not find: " + searchedSword + ". Did you mean: ";
foreach (string fuzzySword in similarSwords)
{
answerString += fuzzySword + ", ";
}
//Remove last space and comma
answerString = answerString.Remove(answerString.Length - 2);
answerString += "?";
await chnl.SendMessageAsync(answerString, false);
}
}
else
await chnl.SendMessageAsync("", false, sword.WriteToDiscord());
}
else
await chnl.SendMessageAsync("", false, sword.WriteToDiscord());
}
}
}
//Find demons starting with
private List<string> FindSwordsStartingWith(string searchedSword)
{
List<string> swordSW = new List<string>();
foreach(Sword sword in Swords)
if (sword.DemonName.ToLower().StartsWith(searchedSword.ToLower()))
swordSW.Add(sword.DemonName);
return swordSW;
}
/// <summary>
/// Returns List of swords whose name have a Levinshtein Distance of LEV_DISTANCE
/// </summary>
/// <param name="searchedSword">Name of the Sword that is being compared agianst</param>
/// <returns></returns>
private List<string> GetSimilarSwords(string searchedSword, int levDist)
{
List<string> simSwords = new List<string>();
foreach (Sword sword in Swords)
{
int levDistance = 999;
try
{
levDistance = LevenshteinDistance.EditDistance(sword.DemonName.ToLower(), searchedSword);
}
catch (ArgumentNullException e)
{
Logger.LogAsync("ArgumentNullException in getSimilarDemons: " + e.Message);
}
//If only off by levDist characters, add to List
if (levDistance <= levDist)
simSwords.Add(sword.DemonName);
}
return simSwords;
}
//Returns the commands for this Retriever
public override string GetCommands()
{
return "\n\nSword Commands:" +
"\n* " + MainCommand + " [Demon Name] - Search's for a sword with the demon name you provided as [Demon Name]. If nothing is found you will recieve a message back stating Sword was not found.";
}
#endregion
#region Public Methods
//Creates a demon object from a data grid view row
public static Sword LoadSword(DataRow row)
{
var sword = new Sword();
sword.DemonName = row["demon name"] is DBNull ? "" : (string)row["demon name"];
sword.SwordName = row["sword Name"] is DBNull ? "" : (string)row["sword Name"];
sword.Talent = row["Talent name"] is DBNull ? "" : (string)row["Talent name"];
sword.Effect = row["Talent effect"] is DBNull ? "" : (string)row["Talent effect"];
sword.Skill1 = row["skill 1"] is DBNull ? "" : (string)row["skill 1"];
sword.Skill2 = row["skill 2"] is DBNull ? "" : (string)row["skill 2"];
sword.AwakenSkill = row["awaken skill"] is DBNull ? "" : (string)row["awaken skill"];
sword.AtkStat = row["Phys/mag atk %"] is DBNull ? "" : (string)row["Phys/mag atk %"];
sword.Attribute = row["Atribute increase"] is DBNull ? "" : (string)row["Atribute increase"];
sword.Accuracy = row["Phys ACC%"] is DBNull ? "" : (string)row["Phys ACC%"];
sword.Critical = row["Critical %"] is DBNull ? "" : (string)row["Critical %"];
sword.Panel1 = row["panel 1"] is DBNull ? "" : (string)row["panel 1"];
sword.Panel2 = row["panel 2"] is DBNull ? "" : (string)row["panel 2"];
sword.Panel3 = row["panel 3"] is DBNull ? "" : (string)row["panel 3"];
sword.Panel1Stats = row["Panel 1 Step"] is DBNull ? "" : (string)row["Panel 1 Step"];
sword.Panel2Stats = row["panel 2 Step"] is DBNull ? "" : (string)row["panel 2 Step"];
sword.Panel3Stats = row["panel 3 Step"] is DBNull ? "" : (string)row["panel 3 Step"];
sword.Nicknames = row["Nickname"] is DBNull ? "" : (string)row["Nickname"];
sword.NicknamesList = new List<string>();
if (sword.Nicknames.Contains(","))
{
var nicknameList = sword.Nicknames.Split(",");
foreach (var nickname in nicknameList)
sword.NicknamesList.Add(nickname.Trim());
}
else
{
sword.NicknamesList.Add(sword.Nicknames.Trim());
}
return sword;
}
#endregion
}
#region Structs
//Object to hold Demon Data
public struct Sword
{
public string DemonName;
public string SwordName;
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 AtkStat;
public string Attribute;
public string Accuracy;
public string Critical;
public string Nicknames;
public List<string> NicknamesList;
public Embed WriteToDiscord()
{
var url = "https://dx2wiki.com/index.php/" + Uri.EscapeDataString(SwordName);
var thumbnail = "https://raw.githubusercontent.com/Alenael/Dx2DB/master/Images/Swords/" + Uri.EscapeDataString(SwordName.Replace("☆", "")) + ".jpg";
var eb = new EmbedBuilder();
eb.WithTitle(SwordName);
eb.AddField("Demon Name:", DemonName, true);
eb.AddField("Skills:",
Skill1 + "\n" +
Skill2 + "\n" +
AwakenSkill, true);
eb.AddField(Talent + ":", 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);
////Other Info
eb.WithFooter(AtkStat +
" | Acc: " + Accuracy +
" | Crit: " + Critical);
eb.WithColor(Color.Red);
eb.WithUrl(url);
eb.WithThumbnailUrl(thumbnail);
return eb.Build();
}
}
#endregion
}