geekbot/Geekbot.net/Lib/FortunesProvider.cs

40 lines
1.1 KiB
C#
Raw Normal View History

2017-09-15 00:31:13 +02:00
using System;
using System.IO;
namespace Geekbot.net.Lib
{
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-19 20:39:49 +02:00
public FortunesProvider(Random rnd)
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;
Console.WriteLine($"-- Loaded {totalFortunes} Fortunes");
2017-09-15 00:31:13 +02:00
}
else
{
Console.WriteLine("Fortunes File not found");
Console.WriteLine($"Path should be {path}");
}
}
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
}