automated updating of currency

This commit is contained in:
appelemac 2016-09-04 16:26:23 +02:00
parent 671fb47da5
commit 673d7698e0

View File

@ -12,6 +12,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml; using System.Xml;
@ -23,7 +24,8 @@ namespace NadekoBot.Modules.Searches
public class UnitConverterCommands public class UnitConverterCommands
{ {
private Logger _log; private Logger _log;
private static Timer _timer;
public static TimeSpan Span = new TimeSpan(12, 0, 0);
public UnitConverterCommands() public UnitConverterCommands()
{ {
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
@ -52,55 +54,43 @@ namespace NadekoBot.Modules.Searches
{ {
_log.Warn("Could not load units: " + e.Message); _log.Warn("Could not load units: " + e.Message);
} }
_timer = new Timer(new TimerCallback(UpdateCurrency), null, 0,(int)Span.TotalMilliseconds);
} }
public List<ConvertUnit> Units { get; set; } public void UpdateCurrency(object stateInfo)
[Command("updatecur")]
[RequireContext(ContextType.Guild)]
public async Task UpdateCurrency(IUserMessage msg)
{ {
var channel = msg.Channel as IGuildChannel; var currencyRates = UpdateCurrencyRates().Result;
var currencyRates = await UpdateCurrencyRates();
var unitTypeString = "currency"; var unitTypeString = "currency";
using (var uow = DbHandler.UnitOfWork())
{
var toRemove = Units.Where(u => u.UnitType == unitTypeString);
Units.RemoveAll(u => u.UnitType == unitTypeString);
uow.ConverterUnits.RemoveRange(toRemove.ToArray());
var baseType = new ConvertUnit() var baseType = new ConvertUnit()
{ {
Triggers = new[] { currencyRates.Base }, Triggers = new[] { currencyRates.Base },
Modifier = decimal.One, Modifier = decimal.One,
UnitType = unitTypeString UnitType = unitTypeString
}; };
var baseIndex = Units.FindIndex(x => x.UnitType == "currency" && x.Modifier == baseType.Modifier); uow.ConverterUnits.Add(baseType);
if (baseIndex == -1) uow.ConverterUnits.AddRange(currencyRates.ConversionRates.Select(u => new ConvertUnit()
Units.Add(baseType);
else
Units[baseIndex] = baseType;
using (var uow = DbHandler.UnitOfWork())
{ {
foreach (var rate in currencyRates.ConversionRates) InternalTrigger = u.Key,
{ Modifier = u.Value,
var u = new ConvertUnit() UnitType = unitTypeString
{ }).ToArray());
Triggers = new[] { rate.Key },
UnitType = unitTypeString,
Modifier = rate.Value
};
var lower = u.Triggers.First().ToLowerInvariant();
var toUpdate = Units.FindIndex(x => x.UnitType == "currency" && x.Triggers.First().ToLowerInvariant() == lower);
if (toUpdate == -1)
{
Units.Add(u);
uow.ConverterUnits.Add(u);
}
else
{
Units[toUpdate] = u;
uow.ConverterUnits.Update(u);
}
uow.Complete(); uow.Complete();
} }
_log.Info("Updated Currency");
} }
await msg.Reply("done");
} public List<ConvertUnit> Units { get; set; }
[LocalizedCommand, LocalizedDescription, LocalizedSummary] [LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task ConvertListE(IUserMessage msg) //extended and bugged list public async Task ConvertListE(IUserMessage msg) //extended and bugged list