-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionWindow.xaml.cs
256 lines (217 loc) · 8 KB
/
SessionWindow.xaml.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
using System.IO;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
namespace JKLM
{
/// <summary>
/// Interaction logic for SessionWindow.xaml
/// </summary>
public partial class SessionWindow : Window
{
private Dictionary<int, List<string>> words = new Dictionary<int, List<string>>();
private Dictionary<int, List<string>> resultWords = null;
private List<string> usedWords = new List<string>();
public SessionWindow()
{
InitializeComponent();
FillWords();
this.Deactivated += SessionWindow_Deactivated;
}
/// <summary>
/// Event handler for window deactivation.
/// </summary>
private void SessionWindow_Deactivated(object? sender, EventArgs e)
{
if ((bool)autoTypeCheckbox.IsChecked && chosenTextbox.Text.Length != 0)
{
try
{
float delay = float.Parse(delayTextbox.Text);
Keyboard.Type(chosenTextbox.Text, delay);
if ((bool)autoEnterCheckbox.IsChecked)
{
Keyboard.Type(Key.Enter);
}
RemoveWord(chosenTextbox.Text);
inputTextbox.Clear();
outputBox.Clear();
chosenTextbox.Clear();
resultWords = null;
}
catch { }
}
}
/// <summary>
/// Fills the words dictionary from JSON files.
/// </summary>
private void FillWords()
{
words.Clear();
// string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
// string wordsDirectory = Path.Combine(appDataPath, "JKLM", "words");
string[] files = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\words", "*.json");
foreach (string file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
string lengthStr = fileName.Split('_')[1];
int length = int.Parse(lengthStr);
string jsonContent = File.ReadAllText(file);
List<string> wordList = JsonSerializer.Deserialize<List<string>>(jsonContent);
words[length] = wordList;
}
}
/// <summary>
/// Gets words containing the specified input string.
/// </summary>
private Dictionary<int, List<string>> GetWordsContainingString(Dictionary<int, List<string>> words, string inputString)
{
Dictionary<int, List<string>> result = new Dictionary<int, List<string>>();
foreach (var entry in words)
{
List<string> matchingWords = entry.Value.FindAll(word => word.Contains(inputString));
if (matchingWords.Count > 0)
{
result[entry.Key] = matchingWords;
}
}
return result;
}
/// <summary>
/// Gets a random word from the specified word dictionary within the given length range.
/// </summary>
private string GetRandomWord(Dictionary<int, List<string>> words, int minLength, int maxLength)
{
List<string> filteredWords = new List<string>();
foreach (var entry in words)
{
if (entry.Key >= minLength && entry.Key <= maxLength)
{
filteredWords.AddRange(entry.Value);
}
}
if (filteredWords.Count == 0)
{
return null;
}
Random random = new Random();
int randomIndex = random.Next(filteredWords.Count);
return filteredWords[randomIndex];
}
/// <summary>
/// Chooses a random word from the result words.
/// </summary>
private void ChooseRandomWord()
{
if (resultWords == null) { return; }
try
{
int min = int.Parse(minWordLengthTextbox.Text);
int max = int.Parse(maxWordLengthTextbox.Text);
string rand = GetRandomWord(resultWords, min, max);
chosenTextbox.Text = rand;
}
catch { }
}
/// <summary>
/// Copies the specified word to the clipboard.
/// </summary>
private void CopyToClipboard(string word)
{
Clipboard.SetText(word);
}
/// <summary>
/// Removes the specified word from the word list and adds it to the used words list.
/// </summary>
private void RemoveWord(string word)
{
words[word.Length].Remove(word);
usedWords.Add(word);
usedWordsTextbox.AppendText(word + "\n");
}
/// <summary>
/// Validates that the text input contains only alphabetic characters.
/// </summary>
private void TextValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^a-zA-Z]+");
e.Handled = regex.IsMatch(e.Text);
}
/// <summary>
/// Processes the input word by removing spaces and trimming whitespace.
/// </summary>
private string ProcessInput(string word)
{
return word.Replace(" ", "").Trim().ToLower();
}
/// <summary>
/// Event handler for the solve button click event.
/// </summary>
private void solveButton_Click(object sender, RoutedEventArgs e)
{
if (inputTextbox.Text.Length == 0) { return; }
string inp = ProcessInput(inputTextbox.Text);
Dictionary<int, List<string>> res = GetWordsContainingString(words, inp);
clearButton_Click(sender, e);
if (res.Count == 0)
{
outputBox.AppendText($"There are no words found for: {inp}");
return;
}
resultWords = res;
outputBox.AppendText($"Results for: {inp}\n");
foreach (var entry in res)
{
outputBox.AppendText($"Length: {entry.Key}\n");
outputBox.AppendText($"{string.Join(", ", entry.Value)}\n\n");
}
ChooseRandomWord();
}
/// <summary>
/// Event handler for the clear button click event.
/// </summary>
private void clearButton_Click(object sender, RoutedEventArgs e)
{
inputTextbox.Clear();
outputBox.Clear();
chosenTextbox.Clear();
resultWords = null;
}
/// <summary>
/// Event handler for the shuffle button click event.
/// </summary>
private void shuffleButton_Click(object sender, RoutedEventArgs e)
{
ChooseRandomWord();
}
/// <summary>
/// Event handler for the copy button click event.
/// </summary>
private void copyButton_Click(object sender, RoutedEventArgs e)
{
if (chosenTextbox.Text.Length == 0) { return; }
CopyToClipboard(chosenTextbox.Text);
RemoveWord(chosenTextbox.Text);
clearButton_Click(sender, e);
}
/// <summary>
/// Event handler for the input textbox key down event.
/// </summary>
private void inputTextbox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
solveButton_Click(sender, e);
}
}
/// <summary>
/// Validates that the text input contains only numeric characters.
/// </summary>
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
}
}