2017-09-15 00:31:13 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2017-09-26 13:02:38 +02:00
|
|
|
|
using Serilog;
|
2017-09-15 00:31:13 +02:00
|
|
|
|
|
2017-09-27 17:18:31 +02:00
|
|
|
|
namespace Geekbot.net.Lib.Media
|
2017-09-15 00:31:13 +02:00
|
|
|
|
{
|
2017-09-15 22:56:03 +02:00
|
|
|
|
internal class FortunesProvider : IFortunesProvider
|
2017-09-15 00:31:13 +02:00
|
|
|
|
{
|
2017-09-15 22:56:03 +02:00
|
|
|
|
private readonly string[] fortuneArray;
|
|
|
|
|
private readonly Random rnd;
|
|
|
|
|
private readonly int totalFortunes;
|
2017-09-15 00:31:13 +02:00
|
|
|
|
|
2017-09-26 13:02:38 +02:00
|
|
|
|
public FortunesProvider(Random rnd, ILogger logger)
|
2017-09-15 00:31:13 +02:00
|
|
|
|
{
|
2017-09-15 12:58:49 +02:00
|
|
|
|
var path = Path.GetFullPath("./Storage/fortunes");
|
2017-09-15 00:31:13 +02:00
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
2017-09-15 22:56:03 +02:00
|
|
|
|
var rawFortunes = File.ReadAllText(path);
|
2017-09-15 00:31:13 +02:00
|
|
|
|
fortuneArray = rawFortunes.Split("%");
|
|
|
|
|
totalFortunes = fortuneArray.Length;
|
2017-09-19 20:39:49 +02:00
|
|
|
|
this.rnd = rnd;
|
2017-09-27 22:48:09 +02:00
|
|
|
|
logger.Verbose($"[Geekbot] [Fortunes] Loaded {totalFortunes} Fortunes");
|
2017-09-15 00:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-09-26 13:02:38 +02:00
|
|
|
|
logger.Error("Fortunes File not found");
|
|
|
|
|
logger.Error($"Path should be {path}");
|
2017-09-15 00:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetRandomFortune()
|
|
|
|
|
{
|
|
|
|
|
return fortuneArray[rnd.Next(0, totalFortunes)];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 12:58:49 +02:00
|
|
|
|
public interface IFortunesProvider
|
2017-09-15 00:31:13 +02:00
|
|
|
|
{
|
|
|
|
|
string GetRandomFortune();
|
|
|
|
|
}
|
2017-09-15 22:56:03 +02:00
|
|
|
|
}
|