2017-07-28 10:28:08 +00:00
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
using NadekoBot.Modules.Games.Common.Hangman.Exceptions;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Games.Common.Hangman
|
|
|
|
|
{
|
|
|
|
|
public class TermPool
|
|
|
|
|
{
|
|
|
|
|
const string termsPath = "data/hangman3.json";
|
2017-07-31 22:11:36 +00:00
|
|
|
|
public static IReadOnlyDictionary<string, HangmanObject[]> Data { get; } = new Dictionary<string, HangmanObject[]>();
|
2017-07-28 10:28:08 +00:00
|
|
|
|
static TermPool()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-31 22:11:36 +00:00
|
|
|
|
Data = JsonConvert.DeserializeObject<Dictionary<string, HangmanObject[]>>(File.ReadAllText(termsPath));
|
2017-07-28 10:28:08 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
//ignored
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 14:37:26 +00:00
|
|
|
|
public static HangmanObject GetTerm(string type)
|
2017-07-28 10:28:08 +00:00
|
|
|
|
{
|
|
|
|
|
var rng = new NadekoRandom();
|
|
|
|
|
|
2017-09-29 14:37:26 +00:00
|
|
|
|
if (type == "random")
|
2017-07-28 10:28:08 +00:00
|
|
|
|
{
|
2017-07-31 22:11:36 +00:00
|
|
|
|
var keys = Data.Keys.ToArray();
|
2017-09-29 14:37:26 +00:00
|
|
|
|
|
|
|
|
|
type = Data.Keys.ToArray()[rng.Next(0, Data.Keys.Count())];
|
2017-07-28 10:28:08 +00:00
|
|
|
|
}
|
2017-09-29 14:37:26 +00:00
|
|
|
|
if (!Data.TryGetValue(type, out var termTypes) || termTypes.Length == 0)
|
2017-07-28 10:28:08 +00:00
|
|
|
|
throw new TermNotFoundException();
|
|
|
|
|
|
|
|
|
|
var obj = termTypes[rng.Next(0, termTypes.Length)];
|
|
|
|
|
|
|
|
|
|
obj.Word = obj.Word.Trim().ToLowerInvariant();
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|