Add JSON support ...finally!

This commit is contained in:
Marco Realacci 2024-01-31 03:24:59 +01:00
parent 391862b3fb
commit 276ae8af2e
3 changed files with 24 additions and 4 deletions

View file

@ -1,4 +1,5 @@
using System.Collections;
using Newtonsoft.Json;
using SoUnBot.AccessControl;
using SoUnBot.ModuleLoader;
using Telegram.Bot;
@ -37,6 +38,7 @@ namespace SoUnBot.Modules.OttoLinux
_playedQuestions = new Dictionary<long, List<int>>();
if (version == 2) LoadQuestionsV2();
else if (version == 3) LoadQuestionsJSON();
else LoadQuestions();
}
public BotGame(AccessManager accessManager)
@ -104,6 +106,13 @@ namespace SoUnBot.Modules.OttoLinux
SanitizeQuestions();
}
private void LoadQuestionsJSON()
{
var json = System.IO.File.ReadAllText(_questionsPath);
var quests = JsonConvert.DeserializeObject<Question[]>(json);
if (quests != null) _questions = quests.ToList();
}
private void SanitizeQuestions()
{
var invalidQuestions = new List<Question>();

View file

@ -3,13 +3,17 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace SoUnBot.Modules.OttoLinux
{
public class Question
{
[JsonProperty("quest")]
public String Quest { get; set; }
[JsonProperty("answers")]
public List<string> Answers { get; }
[JsonProperty("correct")]
public int Correct { get; private set; }
public Question(String quest)

View file

@ -25,14 +25,21 @@ try
{
foreach (string f in Directory.GetFiles(dataPath + "/Questions"))
{
if (!f.EndsWith("txt"))
if (f.EndsWith("txt"))
{
Console.WriteLine("Skipping " + Path.GetFileName(f) + " as the file extension is not supported");
continue;
}
Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f));
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false));
}
else if (f.EndsWith("json"))
{
Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f));
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false, 3));
}
else
{
Console.WriteLine("Skipping " + Path.GetFileName(f) + " as the file extension is not supported");
}
}
foreach (string d in Directory.GetDirectories(dataPath + "/Questions"))
{
Console.WriteLine("Loading module " + Path.GetFileName(d));