mirror of
https://github.com/appinfosapienza/so-un-bot.git
synced 2025-03-13 16:05:22 +01:00
Add JSON support ...finally!
This commit is contained in:
parent
391862b3fb
commit
276ae8af2e
3 changed files with 24 additions and 4 deletions
|
@ -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>();
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -25,13 +25,20 @@ try
|
|||
{
|
||||
foreach (string f in Directory.GetFiles(dataPath + "/Questions"))
|
||||
{
|
||||
if (!f.EndsWith("txt"))
|
||||
if (f.EndsWith("txt"))
|
||||
{
|
||||
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");
|
||||
continue;
|
||||
}
|
||||
Console.WriteLine("Loading module " + Path.GetFileNameWithoutExtension(f));
|
||||
moduleLoader.LoadModule(new BotGame(acl, Path.GetFileNameWithoutExtension(f), f, false));
|
||||
}
|
||||
foreach (string d in Directory.GetDirectories(dataPath + "/Questions"))
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue