-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameEngine.cs
322 lines (285 loc) · 11 KB
/
GameEngine.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 System;
using System.Collections.Generic;
using System.Linq;
using dotnetTrivia.models;
namespace dotnetTrivia
{
public class GameEngine
{
private TriviaDownloader _downloader;
private CategoryList _categoryList;
private int _correctCount;
private int _incorrectCount;
public GameEngine()
{
_downloader = new TriviaDownloader();
_correctCount = 0;
_incorrectCount = 0;
}
public void RunGame()
{
RunGame(new List<string>());
}
public void RunGame(IEnumerable<string> parameters)
{
string command = string.Empty;
bool cmdParams = (parameters != null && parameters.Count() > 0);
Console.WriteLine("Downloading categories...");
_categoryList = _downloader.LoadCategories();
Console.WriteLine($"Categories downloaded...{Environment.NewLine}");
Console.WriteLine("Type help to get started.");
do
{
Console.Write("> ");
// Command line parameters given.
if (cmdParams)
{
command = String.Join(" ", parameters);
Console.WriteLine(command);
cmdParams = false;
}
else // Normal stuff.
{
command = Console.ReadLine();
}
if (!RunCommand(command))
{
break;
}
}
while (true);
Console.WriteLine("Bye bye!");
}
///
/// Returns false, if the application should quit.
///
private bool RunCommand(string command)
{
bool retVal = true;
string[] pieces = command.Split(" ");
if (pieces.Length == 0)
{
Console.WriteLine("No command given.");
return retVal;
}
switch (pieces[0])
{
case "quit":
case "exit":
retVal = false;
break;
case "help":
PrintHelp();
break;
case "categories":
PrintCategories();
break;
case "start":
case "--start":
StartGame(pieces.TakeLast(pieces.Length - 1));
break;
case "log":
Logger.PrintLog();
break;
default:
Console.WriteLine("Unknown command.");
break;
}
return retVal;
}
private void PrintHelp()
{
Console.WriteLine("");
Console.WriteLine("To start the trivia, use the command \"start c=-1 d=all q=10\"");
Console.WriteLine("These are the default values if you just type start.");
Console.WriteLine(" c switch is the category id, -1 means all");
Console.WriteLine(" d is for difficulty, possible values: all, easy, medium, hard");
Console.WriteLine(" q is for the question count");
Console.WriteLine("");
Console.WriteLine("Use the command categories to print all the possible categories.");
}
private void PrintCategories()
{
int catLength = _categoryList.trivia_categories.Count();
foreach (Category cat in _categoryList.trivia_categories)
{
Console.WriteLine("{0}, {1}", cat.id, cat.name);
}
}
private void StartGame(IEnumerable<string> parameters)
{
QuestionList questions = GetQuestions(parameters);
if (questions != null)
{
RunTrivia(questions);
}
}
private QuestionList GetQuestions(IEnumerable<string> parameters)
{
string url = "https://opentdb.com/api.php?";
// Just start
if (parameters.Count() < 1)
{
url += "amount=10";
}
else // At least one parameter present, need to investigate more.
{
// Category parameter.
string category = parameters.Where(p => p.StartsWith("c=") || p.StartsWith("-c=")).FirstOrDefault();
if (category != null)
{
// Category number given and other than all (-1)
int catNum = -2;
if (int.TryParse(category.Substring(2, category.Length - 2), out catNum) && catNum > -1)
{
url += $"category={catNum}&";
}
else
{
Logger.Log("Invalid category: " + category.Substring(2, category.Length - 2));
}
}
// Question count.
string count = parameters.Where(p => p.StartsWith("q=") || p.StartsWith("-q=")).FirstOrDefault();
if (count != null)
{
// Question count grater than zero and less or equal to 50.
int qCount = -1;
if (int.TryParse(count.Substring(2, count.Length - 2), out qCount) && qCount > 0 && qCount <= 50)
{
url += $"amount={qCount}&";
}
else
{
Logger.Log("Invalid question count: " + count.Substring(2, count.Length - 2));
}
}
else
{
url += "amount=10";
}
// Difficulty.
string difficulty = parameters.Where(p => p.StartsWith("d=") || p.StartsWith("-d=")).FirstOrDefault();
if (difficulty != null)
{
string diff = difficulty.Substring(2, difficulty.Length - 2);
if (diff != null && (diff == "easy" || diff == "medium" || diff == "hard"))
{
url += $"difficulty={diff}";
}
else
{
Logger.Log("Invalid difficulty: " + diff);
}
}
}
// Clean url.
if (url.EndsWith("&"))
{
url = url.Substring(0, url.Length - 1);
}
// Just in case
if (url.EndsWith("?"))
{
Logger.Log($"Invalid URL: {url}");
Console.WriteLine("Invalid trivia url. See the logs for more information.");
return null;
}
Logger.Log($"Query URL: {url}");
Console.WriteLine("Loading questions...");
QuestionList questions = _downloader.LoadQuestions(url);
Console.WriteLine("Questions loaded...");
Logger.Log("");
return questions;
}
private void RunTrivia(QuestionList questions)
{
_correctCount = 0;
_incorrectCount = 0;
bool triviaAborted = false;
int questionCount = questions.results.Count();
for (int i = 0; i < questionCount; ++i)
{
Question q = questions.results.ElementAt(i);
if (q != null)
{
if (AskQuestion(q, i + 1, questionCount, ref triviaAborted))
{
++_correctCount;
}
// If trivia is aborted, then the answer is incorrect.
else
{
// Go away.
if (triviaAborted)
{
break;
}
else
{
++_incorrectCount;
}
}
}
}
Console.WriteLine("");
Console.WriteLine("####################################");
Console.WriteLine("Results:");
Console.WriteLine($"Correct: {_correctCount}");
Console.WriteLine($"Incorrect: {_incorrectCount}");
Console.WriteLine("####################################");
Console.WriteLine("");
Console.WriteLine("");
}
///
/// Returns true if user answered correctly, false otherwise.
///
private bool AskQuestion(Question question, int questionNumber, int questionCount, ref bool abortTrivia)
{
bool correctAnswer = false;
abortTrivia = false;
Console.WriteLine("");
Console.WriteLine($"Category: {question.category}");
Console.WriteLine($"Difficulty: {question.difficulty}");
Console.WriteLine($"Question {questionNumber}/{questionCount}: {System.Web.HttpUtility.HtmlDecode(question.question)}");
for (int j = 0; j < question.Answers.Count(); ++j)
{
Console.WriteLine($"{j + 1} - {System.Web.HttpUtility.HtmlDecode(question.Answers.ElementAt(j))}");
}
string answ = string.Empty;
while(true)
{
answ = string.Empty;
Console.Write("> ");
answ = Console.ReadLine();
// Number as an answer.
if (int.TryParse(answ, out int num) && num > 0 && num <= question.Answers.Count())
{
if (question.Answers.ElementAt(num - 1) == question.correct_answer)
{
Console.WriteLine("Correct! :)");
correctAnswer = true;
break;
}
else
{
Console.WriteLine("Incorrect! :(");
Console.WriteLine($"Correct answer is: {System.Web.HttpUtility.HtmlDecode(question.correct_answer)}");
correctAnswer = false;
break;
}
}
// Only "break" is allowed.
else if (answ == "break")
{
Console.WriteLine("Trivia aborted. :(");
abortTrivia = true;
correctAnswer = false;
break;
}
}
System.Threading.Thread.Sleep(1000);
return correctAnswer;
}
}
}