geekbot/Geekbot.net/Lib/Media/FortunesProvider.cs

37 lines
1 KiB
C#
Raw Normal View History

2017-09-15 00:31:13 +02:00
using System;
using System.IO;
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
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;
logger.Debug("Geekbot", "Loaded {totalFortunes} Fortunes");
2017-09-15 00:31:13 +02:00
}
else
{
logger.Information("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 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
}