2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-08-16 13:29:48 +00:00
|
|
|
|
using System.Linq;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
|
|
|
|
// THANKS @ShoMinamimoto for suggestions and coding help
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Games.Common.Trivia
|
2016-04-14 22:17:29 +00:00
|
|
|
|
{
|
|
|
|
|
public class TriviaQuestion
|
|
|
|
|
{
|
2016-02-02 11:23:58 +00:00
|
|
|
|
//represents the min size to judge levDistance with
|
2016-03-03 17:24:58 +00:00
|
|
|
|
private static readonly HashSet<Tuple<int, int>> strictness = new HashSet<Tuple<int, int>> {
|
2016-03-08 21:42:43 +00:00
|
|
|
|
new Tuple<int, int>(9, 0),
|
|
|
|
|
new Tuple<int, int>(14, 1),
|
|
|
|
|
new Tuple<int, int>(19, 2),
|
|
|
|
|
new Tuple<int, int>(22, 3),
|
2016-02-02 11:23:58 +00:00
|
|
|
|
};
|
|
|
|
|
public static int maxStringLength = 22;
|
|
|
|
|
|
2016-12-25 12:09:22 +00:00
|
|
|
|
public string Category { get; set; }
|
|
|
|
|
public string Question { get; set; }
|
2017-03-23 11:09:15 +00:00
|
|
|
|
public string ImageUrl { get; set; }
|
|
|
|
|
public string AnswerImageUrl { get; set; }
|
2016-12-25 12:09:22 +00:00
|
|
|
|
public string Answer { get; set; }
|
2017-09-11 17:12:49 +00:00
|
|
|
|
private string _cleanAnswer;
|
|
|
|
|
public string CleanAnswer => _cleanAnswer ?? (_cleanAnswer = Clean(Answer));
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2017-03-23 11:09:15 +00:00
|
|
|
|
public TriviaQuestion(string q, string a, string c, string img = null, string answerImage = null)
|
2016-04-14 22:17:29 +00:00
|
|
|
|
{
|
2016-02-02 11:23:58 +00:00
|
|
|
|
this.Question = q;
|
|
|
|
|
this.Answer = a;
|
|
|
|
|
this.Category = c;
|
2017-03-23 11:09:15 +00:00
|
|
|
|
this.ImageUrl = img;
|
|
|
|
|
this.AnswerImageUrl = answerImage ?? img;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 13:29:48 +00:00
|
|
|
|
public string GetHint() => Scramble(Answer);
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2016-04-14 22:17:29 +00:00
|
|
|
|
public bool IsAnswerCorrect(string guess)
|
|
|
|
|
{
|
|
|
|
|
if (Answer.Equals(guess))
|
|
|
|
|
{
|
2016-02-02 11:23:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-09-10 21:15:58 +00:00
|
|
|
|
var cleanGuess = Clean(guess);
|
|
|
|
|
if (CleanAnswer.Equals(cleanGuess))
|
2016-04-14 22:17:29 +00:00
|
|
|
|
{
|
2016-02-02 11:23:58 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 21:15:58 +00:00
|
|
|
|
int levDistanceClean = CleanAnswer.LevenshteinDistance(cleanGuess);
|
|
|
|
|
int levDistanceNormal = Answer.LevenshteinDistance(guess);
|
|
|
|
|
return JudgeGuess(CleanAnswer.Length, cleanGuess.Length, levDistanceClean)
|
|
|
|
|
|| JudgeGuess(Answer.Length, guess.Length, levDistanceNormal);
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 22:17:29 +00:00
|
|
|
|
private bool JudgeGuess(int guessLength, int answerLength, int levDistance)
|
|
|
|
|
{
|
|
|
|
|
foreach (Tuple<int, int> level in strictness)
|
|
|
|
|
{
|
|
|
|
|
if (guessLength <= level.Item1 || answerLength <= level.Item1)
|
|
|
|
|
{
|
2016-02-02 11:23:58 +00:00
|
|
|
|
if (levDistance <= level.Item2)
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 21:15:58 +00:00
|
|
|
|
private string Clean(string str)
|
2016-04-14 22:17:29 +00:00
|
|
|
|
{
|
2016-02-02 11:23:58 +00:00
|
|
|
|
str = " " + str.ToLower() + " ";
|
|
|
|
|
str = Regex.Replace(str, "\\s+", " ");
|
|
|
|
|
str = Regex.Replace(str, "[^\\w\\d\\s]", "");
|
|
|
|
|
//Here's where custom modification can be done
|
|
|
|
|
str = Regex.Replace(str, "\\s(a|an|the|of|in|for|to|as|at|be)\\s", " ");
|
|
|
|
|
//End custom mod and cleanup whitespace
|
|
|
|
|
str = Regex.Replace(str, "^\\s+", "");
|
|
|
|
|
str = Regex.Replace(str, "\\s+$", "");
|
|
|
|
|
//Trim the really long answers
|
|
|
|
|
str = str.Length <= maxStringLength ? str : str.Substring(0, maxStringLength);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 13:29:48 +00:00
|
|
|
|
private static string Scramble(string word)
|
|
|
|
|
{
|
2016-12-27 01:18:21 +00:00
|
|
|
|
var letters = word.ToCharArray();
|
2016-08-16 13:29:48 +00:00
|
|
|
|
var count = 0;
|
|
|
|
|
for (var i = 0; i < letters.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (letters[i] == ' ')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
if (count <= letters.Length / 5)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (count % 3 == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (letters[i] != ' ')
|
|
|
|
|
letters[i] = '_';
|
|
|
|
|
}
|
2017-02-09 19:56:27 +00:00
|
|
|
|
return string.Join(" ", new string(letters).Replace(" ", " \u2000").AsEnumerable());
|
2016-08-16 13:29:48 +00:00
|
|
|
|
}
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|