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

33 lines
1,023 B
C#
Raw Normal View History

2017-09-15 00:31:13 +02:00
using System;
using System.IO;
using Geekbot.net.Lib.Logger;
2017-09-15 00:31:13 +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
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.Trace(LogSource.Geekbot, $"Loaded {_totalFortunes} Fortunes");
2017-09-15 00:31:13 +02:00
}
else
{
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
}