2017-09-15 00:31:13 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Logger;
|
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
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
private readonly string[] _fortuneArray;
|
|
|
|
|
private readonly int _totalFortunes;
|
2017-09-15 00:31:13 +02:00
|
|
|
|
|
2018-02-14 23:01:28 +01:00
|
|
|
|
public FortunesProvider(IGeekbotLogger 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);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_fortuneArray = rawFortunes.Split("%");
|
|
|
|
|
_totalFortunes = _fortuneArray.Length;
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Trace(LogSource.Geekbot, $"Loaded {_totalFortunes} Fortunes");
|
2017-09-15 00:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Information(LogSource.Geekbot, $"Fortunes File not found at {path}");
|
2017-09-15 00:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetRandomFortune()
|
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
return _fortuneArray[new Random().Next(0, _totalFortunes)];
|
2017-09-15 00:31:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-15 22:56:03 +02:00
|
|
|
|
}
|