From 88a2bb83f8f4380f2d8f1aab6f5260df57c5a673 Mon Sep 17 00:00:00 2001 From: appelemac Date: Tue, 30 Aug 2016 18:51:27 +0200 Subject: [PATCH 1/6] conversion --- .../Searches/Commands/UnitConversion.cs | 206 ++++ src/NadekoBot/units.json | 949 ++++++++++++++++++ 2 files changed, 1155 insertions(+) create mode 100644 src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs create mode 100644 src/NadekoBot/units.json diff --git a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs new file mode 100644 index 00000000..e3a0ed20 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs @@ -0,0 +1,206 @@ +using Discord; +using Discord.Commands; +using NadekoBot.Attributes; +using NadekoBot.Extensions; +using Newtonsoft.Json; +using NLog; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace NadekoBot.Modules.Searches +{ + public partial class Searches + { + [Group] + public class UnitConverterCommands + { + private Logger _log; + + public UnitConverterCommands() + { + _log = LogManager.GetCurrentClassLogger(); + + try + { + Units = JsonConvert.DeserializeObject>(File.ReadAllText("units.json")); + } + catch (Exception e) + { + _log.Warn("Could not load units: " + e.Message); + + } + + } + + public List Units { get; set; } + + + + [Command("updatecur")] + [RequireContext(ContextType.Guild)] + public async Task UpdateCurrency(IUserMessage msg) + { + var channel = msg.Channel as IGuildChannel; + var currencyRates = await UpdateCurrencyRates(); + var unitTypeString = "currency"; + var baseType = new JsonUnit() + { + Triggers = new List() { currencyRates.Base }, + Modifier = decimal.One, + UnitType = unitTypeString + }; + foreach (var rate in currencyRates.ConversionRates) + { + Units.Add(new JsonUnit() { + Triggers = new List() { rate.Key}, + UnitType = unitTypeString, + Modifier = rate.Value + }); + } + File.WriteAllText("units.json", JsonConvert.SerializeObject(Units, Formatting.Indented)); + await msg.Reply("done"); + } + + [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [RequireContext(ContextType.Guild)] + public async Task ConvertList(IUserMessage msg) + { + var sb = new StringBuilder("Units that can be used by the converter: "); + var res = Units.GroupBy(x => x.UnitType); + foreach (var group in res) + { + sb.AppendLine($"{group.Key}: ```xl"); + foreach (var el in group) + { + sb.AppendLine(string.Join(",", el.Triggers)); + } + sb.AppendLine("```"); + } + await msg.ReplyLong(sb.ToString()); + } + [LocalizedCommand, LocalizedDescription, LocalizedSummary] + public async Task Convert(IUserMessage msg, string origin, string target, decimal value) + { + var originUnit = Units.Find(x => x.Triggers.Contains(origin)); + var targetUnit = Units.Find(x => x.Triggers.Contains(target)); + if (originUnit.UnitType != targetUnit.UnitType) + { + await msg.Reply(string.Format("Cannot convert {0} to {1}: types of unit are not equal", originUnit.Triggers.First(), targetUnit.Triggers.First())); + return; + } + decimal res = (value * originUnit.Modifier) / targetUnit.Modifier; + await msg.Reply(string.Format("{0} {1} is equal to {2} {3}", value, originUnit.Triggers.First(), res, targetUnit.Triggers.First())); + } + } + + + public static async Task UpdateCurrencyRates() + { + using (var http = new HttpClient()) + { + var res = await http.GetStringAsync("http://api.fixer.io/latest").ConfigureAwait(false); + return JsonConvert.DeserializeObject(res); + } + } + + public class Rates + { + [JsonProperty("base")] + public string Base { get; set; } + [JsonProperty("date")] + public DateTime Date { get; set; } + [JsonProperty("rates")] + public Dictionary ConversionRates { get; set; } + } + + + public class JsonUnit + { + public List Triggers { get; set; } + public string UnitType { get; set; } + public decimal Modifier { get; set; } + } + + #region GetXML + /* + public class UnitCollection + { + public List UnitTypes; + + public UnitCollection(string content) + { + using (var xmlReader = XmlReader.Create(File.OpenRead("units.xml"), new XmlReaderSettings() { IgnoreComments = true, IgnoreWhitespace = true })) + { + XmlDocument doc = new XmlDocument(); + doc.Load(xmlReader); + + UnitTypes = new List(); + foreach (XmlNode node in doc.LastChild.ChildNodes) + { //units/ + UnitType type = new UnitType() + { + Name = node.Name + }; + var units = new List(); + foreach (XmlNode unitNode in node.ChildNodes) + { + var curNode = unitNode.FirstChild; + Unit u = new Unit() + { + Key = curNode.InnerText, + Singular = (curNode = curNode.NextSibling).InnerText, + Plural = (curNode = curNode.NextSibling).InnerText, + Symbol = (curNode = curNode.NextSibling).InnerText, + Source = curNode.NextSibling.NextSibling.InnerText + }; + List factors = new List(); + foreach (XmlNode factorNode in curNode.NextSibling.ChildNodes) + { + Factor f = new Factor() + { + Modifier = factorNode.FirstChild.InnerText.Replace(" ", "") + }; + f.From = factorNode.Attributes.GetNamedItem("from").InnerText; + factors.Add(f); + } + u.Factors = factors; + units.Add(u); + } + type.Units = units; + UnitTypes.Add(type); + } + } + } + + public class UnitType + { + public string Name { get; set; } + public List Units { get; set; } + } + + public class Unit + { + public string Key { get; set; } + public string Plural { get; set; } + public string Singular { get; set; } + public string Symbol { get; set; } + public List Factors { get; set; } + public string Source { get; set; } + } + + public class Factor + { + public string From { get; set; } + public string Modifier { get; set; } + } + } + */ + #endregion + } +} \ No newline at end of file diff --git a/src/NadekoBot/units.json b/src/NadekoBot/units.json new file mode 100644 index 00000000..13121a80 --- /dev/null +++ b/src/NadekoBot/units.json @@ -0,0 +1,949 @@ +[ + { + "Triggers": [ + "millimeter", + "millimeters", + "millimeter", + "mm" + ], + "UnitType": "length", + "Modifier": 0.001 + }, + { + "Triggers": [ + "centimeter", + "centimeters", + "centimeter", + "cm" + ], + "UnitType": "length", + "Modifier": 0.01 + }, + { + "Triggers": [ + "decimeter", + "decimeters", + "decimeter", + "dm" + ], + "UnitType": "length", + "Modifier": 0.1 + }, + { + "Triggers": [ + "meter", + "meters", + "meter", + "m" + ], + "UnitType": "length", + "Modifier": 1.0 + }, + { + "Triggers": [ + "kilometer", + "kilometers", + "kilometer", + "km" + ], + "UnitType": "length", + "Modifier": 1000.0 + }, + { + "Triggers": [ + "foot", + "feet", + "foot", + "ft" + ], + "UnitType": "length", + "Modifier": 0.3048 + }, + { + "Triggers": [ + "inch", + "inches", + "inch", + "in" + ], + "UnitType": "length", + "Modifier": 0.0254 + }, + { + "Triggers": [ + "mile", + "miles", + "mile", + "mi" + ], + "UnitType": "length", + "Modifier": 1609.344 + }, + { + "Triggers": [ + "yard", + "yards", + "yard", + "yd" + ], + "UnitType": "length", + "Modifier": 0.9144 + }, + { + "Triggers": [ + "cubic foot", + "cubic feet", + "cubic foot", + "ft3" + ], + "UnitType": "volume", + "Modifier": 0.02831685 + }, + { + "Triggers": [ + "cubic inch", + "cubic inches", + "cubic inch", + "in3" + ], + "UnitType": "volume", + "Modifier": 0.00001638706 + }, + { + "Triggers": [ + "cubic mile", + "cubic miles", + "cubic mile", + "mi3" + ], + "UnitType": "volume", + "Modifier": 4168182000.0 + }, + { + "Triggers": [ + "cubic yard", + "cubic yards", + "cubic yard", + "yd3" + ], + "UnitType": "volume", + "Modifier": 0.7645549 + }, + { + "Triggers": [ + "cup", + "cups", + "cup", + "cup" + ], + "UnitType": "volume", + "Modifier": 0.0002365882 + }, + { + "Triggers": [ + "imperial gallon", + "Imperial gallons", + "Imperial gallon", + "gal" + ], + "UnitType": "volume", + "Modifier": 0.00454609 + }, + { + "Triggers": [ + "us gallon", + "US gallons", + "US gallon", + "gal" + ], + "UnitType": "volume", + "Modifier": 0.003785412 + }, + { + "Triggers": [ + "milliliter", + "milliliters", + "milliliter", + "mL" + ], + "UnitType": "volume", + "Modifier": 0.000001 + }, + { + "Triggers": [ + "liter", + "liters", + "liter", + "L" + ], + "UnitType": "volume", + "Modifier": 0.001 + }, + { + "Triggers": [ + "imperial fluid ounce", + "Imperial fluid ounces", + "Imperial fluid ounce", + "fl oz" + ], + "UnitType": "volume", + "Modifier": 0.00002841306 + }, + { + "Triggers": [ + "us fluid ounce", + "US fluid ounces", + "US fluid ounce", + "fl oz" + ], + "UnitType": "volume", + "Modifier": 0.00002957353 + }, + { + "Triggers": [ + "imperial pint", + "Imperial pints", + "Imperial pint", + "pt" + ], + "UnitType": "volume", + "Modifier": 0.00056826125 + }, + { + "Triggers": [ + "us liquid pint", + "US pints (liquid)", + "US pint (liquid)", + "pt" + ], + "UnitType": "volume", + "Modifier": 0.0004731765 + }, + { + "Triggers": [ + "us dry pint", + "US pints (dry)", + "US pint (dry)", + "pt" + ], + "UnitType": "volume", + "Modifier": 0.0005506105 + }, + { + "Triggers": [ + "imperial quart", + "Imperial quarts", + "Imperial quart", + "qt" + ], + "UnitType": "volume", + "Modifier": 0.00113652297 + }, + { + "Triggers": [ + "us liquid quart", + "US quarts (liquid)", + "US quart (liquid)", + "qt" + ], + "UnitType": "volume", + "Modifier": 0.0009463529 + }, + { + "Triggers": [ + "us dry quart", + "US quarts (dry)", + "US quart (dry)", + "qt" + ], + "UnitType": "volume", + "Modifier": 0.001101221 + }, + { + "Triggers": [ + "tablespoon", + "tablespoons", + "tablespoon", + "tbsp" + ], + "UnitType": "volume", + "Modifier": 0.00001478676 + }, + { + "Triggers": [ + "teaspoon", + "teaspoons", + "teaspoon", + "tspn" + ], + "UnitType": "volume", + "Modifier": 0.000004928922 + }, + { + "Triggers": [ + "milligram", + "milligrams", + "milligram", + "mg" + ], + "UnitType": "weight", + "Modifier": 0.000001 + }, + { + "Triggers": [ + "gram", + "grams", + "gram", + "g" + ], + "UnitType": "weight", + "Modifier": 0.001 + }, + { + "Triggers": [ + "kilogram", + "kilograms", + "kilogram", + "kg" + ], + "UnitType": "weight", + "Modifier": 1.0 + }, + { + "Triggers": [ + "carat", + "carats", + "carat", + "CD" + ], + "UnitType": "weight", + "Modifier": 0.00020 + }, + { + "Triggers": [ + "grain", + "grains", + "grain", + "gr" + ], + "UnitType": "weight", + "Modifier": 0.00006479891 + }, + { + "Triggers": [ + "ounce", + "ounces", + "ounce", + "oz" + ], + "UnitType": "weight", + "Modifier": 0.02834952 + }, + { + "Triggers": [ + "pennyweight", + "pennyweights", + "pennyweight", + "dwt" + ], + "UnitType": "weight", + "Modifier": 0.001555174 + }, + { + "Triggers": [ + "pound", + "pounds", + "pound", + "lb" + ], + "UnitType": "weight", + "Modifier": 0.4535924 + }, + { + "Triggers": [ + "stone", + "stones", + "stone", + "st" + ], + "UnitType": "weight", + "Modifier": 6.35029318 + }, + { + "Triggers": [ + "slug", + "slugs", + "slug", + "slug" + ], + "UnitType": "weight", + "Modifier": 14.59390 + }, + { + "Triggers": [ + "metric ton", + "metric tons", + "metric ton", + "t" + ], + "UnitType": "weight", + "Modifier": 1000.0 + }, + { + "Triggers": [ + "long ton", + "long tons", + "long ton", + "t" + ], + "UnitType": "weight", + "Modifier": 1016.047 + }, + { + "Triggers": [ + "short ton", + "short tons", + "short ton", + "t" + ], + "UnitType": "weight", + "Modifier": 907.1847 + }, + { + "Triggers": [ + "acre", + "acres", + "acre", + "acre" + ], + "UnitType": "area", + "Modifier": 4046.873 + }, + { + "Triggers": [ + "are", + "ares", + "are", + "a" + ], + "UnitType": "area", + "Modifier": 100.0 + }, + { + "Triggers": [ + "hectare", + "hectares", + "hectare", + "ha" + ], + "UnitType": "area", + "Modifier": 10000.0 + }, + { + "Triggers": [ + "square foot", + "square feet", + "square foot", + "ft2" + ], + "UnitType": "area", + "Modifier": 0.09290304 + }, + { + "Triggers": [ + "square meter", + "square meters", + "square meter", + "m2" + ], + "UnitType": "area", + "Modifier": 1.0 + }, + { + "Triggers": [ + "square kilometer", + "square kilometers", + "square kilometer", + "km2" + ], + "UnitType": "area", + "Modifier": 1000000.0 + }, + { + "Triggers": [ + "square inch", + "square inches", + "square inch", + "in2" + ], + "UnitType": "area", + "Modifier": 0.00064516 + }, + { + "Triggers": [ + "square yard", + "square yards", + "square yard", + "yd2" + ], + "UnitType": "area", + "Modifier": 0.8361274 + }, + { + "Triggers": [ + "square mile", + "square miles", + "square mile", + "mi2" + ], + "UnitType": "area", + "Modifier": 2589988.0 + }, + { + "Triggers": [ + "aankadam", + "aankadam", + "aankadam", + "aankadam" + ], + "UnitType": "area", + "Modifier": 6.69 + }, + { + "Triggers": [ + "perch", + "perches", + "perch", + "perch" + ], + "UnitType": "area", + "Modifier": 25.29 + }, + { + "Triggers": [ + "cent", + "cents", + "cent", + "cent" + ], + "UnitType": "area", + "Modifier": 40.47 + }, + { + "Triggers": [ + "chatak", + "chataks", + "chatak", + "chatak" + ], + "UnitType": "area", + "Modifier": 41.81 + }, + { + "Triggers": [ + "kottah", + "kottah (B)", + "kottah (B)", + "kottah (B)" + ], + "UnitType": "area", + "Modifier": 66.89 + }, + { + "Triggers": [ + "guntha", + "guntha", + "guntha", + "guntha" + ], + "UnitType": "area", + "Modifier": 101.17 + }, + { + "Triggers": [ + "ground", + "grounds", + "ground", + "ground" + ], + "UnitType": "area", + "Modifier": 222.97 + }, + { + "Triggers": [ + "marla", + "marla", + "marla", + "marla" + ], + "UnitType": "area", + "Modifier": 501.68 + }, + { + "Triggers": [ + "rood", + "roods", + "rood", + "rood" + ], + "UnitType": "area", + "Modifier": 1011.71 + }, + { + "Triggers": [ + "bigha I", + "bigha I", + "bigha I", + "bigha I" + ], + "UnitType": "area", + "Modifier": 1618.74 + }, + { + "Triggers": [ + "bigha II", + "bigha II", + "bigha II", + "bigha II" + ], + "UnitType": "area", + "Modifier": 2529.29 + }, + { + "Triggers": [ + "kanal", + "kanal", + "kanal", + "kanal" + ], + "UnitType": "area", + "Modifier": 10033.53 + }, + { + "Triggers": [ + "biswa I", + "biswa I", + "biswa I", + "biswa I" + ], + "UnitType": "area", + "Modifier": 32374.85 + }, + { + "Triggers": [ + "biswa II", + "biswa II", + "biswa II", + "biswa II" + ], + "UnitType": "area", + "Modifier": 50585.71 + }, + { + "Triggers": [ + "pascal", + "pascal", + "pascal", + "Pa" + ], + "UnitType": "pressure", + "Modifier": 1.0 + }, + { + "Triggers": [ + "torr", + "torr", + "torr", + "Torr" + ], + "UnitType": "pressure", + "Modifier": 133.3224 + }, + { + "Triggers": [ + "bar", + "bars", + "bar", + "bar" + ], + "UnitType": "pressure", + "Modifier": 100000.0 + }, + { + "Triggers": [ + "millibar", + "millibars", + "millibar", + "mb" + ], + "UnitType": "pressure", + "Modifier": 100.0 + }, + { + "Triggers": [ + "psi", + "psi", + "psi", + "lbf/in2" + ], + "UnitType": "pressure", + "Modifier": 6894.757 + }, + { + "Triggers": [ + "day", + "days", + "day", + "d" + ], + "UnitType": "time", + "Modifier": 86400.0 + }, + { + "Triggers": [ + "hour", + "hours", + "hour", + "h" + ], + "UnitType": "time", + "Modifier": 3600.0 + }, + { + "Triggers": [ + "minute", + "minutes", + "minute", + "min" + ], + "UnitType": "time", + "Modifier": 60.0 + }, + { + "Triggers": [ + "year", + "years", + "year", + "yr" + ], + "UnitType": "time", + "Modifier": 31536000.0 + }, + { + "Triggers": [ + "AUD" + ], + "UnitType": "currency", + "Modifier": 1.4787 + }, + { + "Triggers": [ + "BGN" + ], + "UnitType": "currency", + "Modifier": 1.9558 + }, + { + "Triggers": [ + "BRL" + ], + "UnitType": "currency", + "Modifier": 3.5991 + }, + { + "Triggers": [ + "CAD" + ], + "UnitType": "currency", + "Modifier": 1.4562 + }, + { + "Triggers": [ + "CHF" + ], + "UnitType": "currency", + "Modifier": 1.0951 + }, + { + "Triggers": [ + "CNY" + ], + "UnitType": "currency", + "Modifier": 7.4565 + }, + { + "Triggers": [ + "CZK" + ], + "UnitType": "currency", + "Modifier": 27.025 + }, + { + "Triggers": [ + "DKK" + ], + "UnitType": "currency", + "Modifier": 7.4448 + }, + { + "Triggers": [ + "GBP" + ], + "UnitType": "currency", + "Modifier": 0.8517 + }, + { + "Triggers": [ + "HKD" + ], + "UnitType": "currency", + "Modifier": 8.6631 + }, + { + "Triggers": [ + "HRK" + ], + "UnitType": "currency", + "Modifier": 7.4846 + }, + { + "Triggers": [ + "HUF" + ], + "UnitType": "currency", + "Modifier": 308.97 + }, + { + "Triggers": [ + "IDR" + ], + "UnitType": "currency", + "Modifier": 14814.35 + }, + { + "Triggers": [ + "ILS" + ], + "UnitType": "currency", + "Modifier": 4.2241 + }, + { + "Triggers": [ + "INR" + ], + "UnitType": "currency", + "Modifier": 74.8703 + }, + { + "Triggers": [ + "JPY" + ], + "UnitType": "currency", + "Modifier": 114.27 + }, + { + "Triggers": [ + "KRW" + ], + "UnitType": "currency", + "Modifier": 1244.47 + }, + { + "Triggers": [ + "MXN" + ], + "UnitType": "currency", + "Modifier": 20.7542 + }, + { + "Triggers": [ + "MYR" + ], + "UnitType": "currency", + "Modifier": 4.5205 + }, + { + "Triggers": [ + "NOK" + ], + "UnitType": "currency", + "Modifier": 9.2873 + }, + { + "Triggers": [ + "NZD" + ], + "UnitType": "currency", + "Modifier": 1.5427 + }, + { + "Triggers": [ + "PHP" + ], + "UnitType": "currency", + "Modifier": 51.797 + }, + { + "Triggers": [ + "PLN" + ], + "UnitType": "currency", + "Modifier": 4.3436 + }, + { + "Triggers": [ + "RON" + ], + "UnitType": "currency", + "Modifier": 4.4505 + }, + { + "Triggers": [ + "RUB" + ], + "UnitType": "currency", + "Modifier": 72.4564 + }, + { + "Triggers": [ + "SEK" + ], + "UnitType": "currency", + "Modifier": 9.5008 + }, + { + "Triggers": [ + "SGD" + ], + "UnitType": "currency", + "Modifier": 1.5196 + }, + { + "Triggers": [ + "THB" + ], + "UnitType": "currency", + "Modifier": 38.608 + }, + { + "Triggers": [ + "TRY" + ], + "UnitType": "currency", + "Modifier": 3.2977 + }, + { + "Triggers": [ + "USD" + ], + "UnitType": "currency", + "Modifier": 1.1168 + }, + { + "Triggers": [ + "ZAR" + ], + "UnitType": "currency", + "Modifier": 16.0537 + } +] \ No newline at end of file From d4ae9e712ac1fee731b564aff49600a8aab7d989 Mon Sep 17 00:00:00 2001 From: appelemac Date: Tue, 30 Aug 2016 21:59:33 +0200 Subject: [PATCH 2/6] Added Temperature, case insensitivity --- .../Searches/Commands/UnitConversion.cs | 128 ++++++++++++++---- src/NadekoBot/units.json | 32 +++++ 2 files changed, 132 insertions(+), 28 deletions(-) diff --git a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs index e3a0ed20..2a859061 100644 --- a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs +++ b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs @@ -40,7 +40,7 @@ namespace NadekoBot.Modules.Searches public List Units { get; set; } - + [Command("updatecur")] [RequireContext(ContextType.Guild)] @@ -55,70 +55,141 @@ namespace NadekoBot.Modules.Searches Modifier = decimal.One, UnitType = unitTypeString }; + var baseIndex = Units.FindIndex(x => x.UnitType == "currency" && x.Modifier == baseType.Modifier); + if (baseIndex == -1) + Units.Add(baseType); + else + Units[baseIndex] = baseType; foreach (var rate in currencyRates.ConversionRates) { - Units.Add(new JsonUnit() { - Triggers = new List() { rate.Key}, + var u = new JsonUnit() + { + Triggers = new List() { 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); + else + Units[toUpdate] = u; } File.WriteAllText("units.json", JsonConvert.SerializeObject(Units, Formatting.Indented)); await msg.Reply("done"); } - [LocalizedCommand, LocalizedDescription, LocalizedSummary] [RequireContext(ContextType.Guild)] - public async Task ConvertList(IUserMessage msg) + public async Task ConvertListE(IUserMessage msg) //extended and bugged list { - var sb = new StringBuilder("Units that can be used by the converter: "); + var channel = msg.Channel as IGuildChannel; + + var sb = new StringBuilder("Units that can be used by the converter: \n"); var res = Units.GroupBy(x => x.UnitType); foreach (var group in res) { sb.AppendLine($"{group.Key}: ```xl"); foreach (var el in group) { - sb.AppendLine(string.Join(",", el.Triggers)); + sb.Append($" [{string.Join(",", el.Triggers)}] "); } sb.AppendLine("```"); } - await msg.ReplyLong(sb.ToString()); + await msg.ReplyLong(sb.ToString(), "```xl", "```", "```xl"); + } + [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [RequireContext(ContextType.Guild)] + public async Task ConvertList(IUserMessage msg) + { + var sb = new StringBuilder("Units that can be used by the converter: \n"); + var res = Units.GroupBy(x => x.UnitType); + foreach (var group in res) + { + sb.AppendLine($"{group.Key}: ```xl"); + sb.AppendLine(string.Join(",", group.Select(x => x.Triggers.FirstOrDefault()).OrderBy(x => x))); + sb.AppendLine("```"); + } + await msg.ReplyLong(sb.ToString(), "```xl", "```", "```xl"); } [LocalizedCommand, LocalizedDescription, LocalizedSummary] public async Task Convert(IUserMessage msg, string origin, string target, decimal value) { - var originUnit = Units.Find(x => x.Triggers.Contains(origin)); - var targetUnit = Units.Find(x => x.Triggers.Contains(target)); + var originUnit = Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(origin.ToLowerInvariant())); + var targetUnit = Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(target.ToLowerInvariant())); + if (originUnit == null || targetUnit == null) + { + await msg.Reply(string.Format("Cannot convert {0} to {1}: units not found", originUnit.Triggers.First(), targetUnit.Triggers.First())); + return; + } if (originUnit.UnitType != targetUnit.UnitType) { await msg.Reply(string.Format("Cannot convert {0} to {1}: types of unit are not equal", originUnit.Triggers.First(), targetUnit.Triggers.First())); return; } - decimal res = (value * originUnit.Modifier) / targetUnit.Modifier; + decimal res; + if (originUnit.Triggers == targetUnit.Triggers) res = value; + else if (originUnit.UnitType == "temperature") + { + //don't really care too much about efficiency, so just convert to Kelvin, then to target + switch (originUnit.Triggers.First().ToUpperInvariant()) + { + case "C": + res = value + (decimal)273.15; //celcius! + break; + case "F": + res = (value + (decimal)459.67) * ((decimal)5 / 9); + break; + default: + res = value; + break; + } + //from Kelvin to target + switch (targetUnit.Triggers.First()) + { + case "C": + res = value - (decimal)273.15; //celcius! + break; + case "F": + res = res * ((decimal)9 / 5) - (decimal)458.67; + break; + default: + break; + } + } + else + { + //I just love currency + if (originUnit.UnitType == "currency") + { + res = (value * targetUnit.Modifier) / originUnit.Modifier; + } else + res = (value * originUnit.Modifier) / targetUnit.Modifier; + } + res = Math.Round(res, 2); await msg.Reply(string.Format("{0} {1} is equal to {2} {3}", value, originUnit.Triggers.First(), res, targetUnit.Triggers.First())); } } - public static async Task UpdateCurrencyRates() + public static async Task UpdateCurrencyRates() + { + using (var http = new HttpClient()) { - using (var http = new HttpClient()) - { - var res = await http.GetStringAsync("http://api.fixer.io/latest").ConfigureAwait(false); - return JsonConvert.DeserializeObject(res); - } + var res = await http.GetStringAsync("http://api.fixer.io/latest").ConfigureAwait(false); + return JsonConvert.DeserializeObject(res); } + } + + public class Rates + { + [JsonProperty("base")] + public string Base { get; set; } + [JsonProperty("date")] + public DateTime Date { get; set; } + [JsonProperty("rates")] + public Dictionary ConversionRates { get; set; } + } - public class Rates - { - [JsonProperty("base")] - public string Base { get; set; } - [JsonProperty("date")] - public DateTime Date { get; set; } - [JsonProperty("rates")] - public Dictionary ConversionRates { get; set; } - } - public class JsonUnit { @@ -127,6 +198,7 @@ namespace NadekoBot.Modules.Searches public decimal Modifier { get; set; } } + #region GetXML /* public class UnitCollection diff --git a/src/NadekoBot/units.json b/src/NadekoBot/units.json index 13121a80..f7fe8cbe 100644 --- a/src/NadekoBot/units.json +++ b/src/NadekoBot/units.json @@ -945,5 +945,37 @@ ], "UnitType": "currency", "Modifier": 16.0537 + }, + { + "Triggers": [ + "K", + "kelvin" + ], + "UnitType": "temperature", + "Modifier": 0.00 + }, + { + "Triggers": [ + "F", + "fahrenheit" + ], + "UnitType": "temperature", + "Modifier": 0.00 + }, + { + "Triggers": [ + "C", + "Celcius", + "Centigrade" + ], + "UnitType": "temperature", + "Modifier": 0.00 + }, + { + "Triggers": [ + "EUR" + ], + "UnitType": "currency", + "Modifier": 1.0 } ] \ No newline at end of file From 0bf79cb888c037d085a5af3a6b9a5d54371f1461 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 4 Sep 2016 15:11:02 +0200 Subject: [PATCH 3/6] finalization --- .../Searches/Commands/UnitConversion.cs | 150 ++++++------------ .../Services/Database/IUnitOfWork.cs | 1 + .../Services/Database/Models/ConvertUnit.cs | 38 +++++ .../Services/Database/NadekoContext.cs | 2 + .../Repositories/IUnitConverterRepository.cs | 14 ++ .../Impl/UnitCOnverterRepository.cs | 28 ++++ src/NadekoBot/Services/Database/UnitOfWork.cs | 2 + 7 files changed, 133 insertions(+), 102 deletions(-) create mode 100644 src/NadekoBot/Services/Database/Models/ConvertUnit.cs create mode 100644 src/NadekoBot/Services/Database/Repositories/IUnitConverterRepository.cs create mode 100644 src/NadekoBot/Services/Database/Repositories/Impl/UnitCOnverterRepository.cs diff --git a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs index 2a859061..8f503ff8 100644 --- a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs +++ b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs @@ -2,6 +2,8 @@ using Discord.Commands; using NadekoBot.Attributes; using NadekoBot.Extensions; +using NadekoBot.Services; +using NadekoBot.Services.Database.Models; using Newtonsoft.Json; using NLog; using System; @@ -28,19 +30,31 @@ namespace NadekoBot.Modules.Searches try { - Units = JsonConvert.DeserializeObject>(File.ReadAllText("units.json")); + using (var uow = DbHandler.UnitOfWork()) + { + //need to do this the first time + if (uow.ConverterUnits.Empty()) + { + var content = JsonConvert.DeserializeObject>(File.ReadAllText("units.json")).Select(u => new ConvertUnit() + { + Modifier = u.Modifier, + UnitType = u.UnitType, + InternalTrigger = string.Join("|", u.Triggers) + }); + + uow.ConverterUnits.AddRange(content.ToArray()); + uow.Complete(); + } + Units = uow.ConverterUnits.GetAll().ToList(); + } } catch (Exception e) { _log.Warn("Could not load units: " + e.Message); - } - } - public List Units { get; set; } - - + public List Units { get; set; } [Command("updatecur")] [RequireContext(ContextType.Guild)] @@ -49,9 +63,9 @@ namespace NadekoBot.Modules.Searches var channel = msg.Channel as IGuildChannel; var currencyRates = await UpdateCurrencyRates(); var unitTypeString = "currency"; - var baseType = new JsonUnit() + var baseType = new ConvertUnit() { - Triggers = new List() { currencyRates.Base }, + Triggers = new[] { currencyRates.Base }, Modifier = decimal.One, UnitType = unitTypeString }; @@ -60,22 +74,31 @@ namespace NadekoBot.Modules.Searches Units.Add(baseType); else Units[baseIndex] = baseType; - foreach (var rate in currencyRates.ConversionRates) + using (var uow = DbHandler.UnitOfWork()) { - var u = new JsonUnit() + foreach (var rate in currencyRates.ConversionRates) { - Triggers = new List() { 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); - else - Units[toUpdate] = u; + var u = new ConvertUnit() + { + 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(); + } } - File.WriteAllText("units.json", JsonConvert.SerializeObject(Units, Formatting.Indented)); await msg.Reply("done"); } [LocalizedCommand, LocalizedDescription, LocalizedSummary] @@ -162,8 +185,9 @@ namespace NadekoBot.Modules.Searches if (originUnit.UnitType == "currency") { res = (value * targetUnit.Modifier) / originUnit.Modifier; - } else - res = (value * originUnit.Modifier) / targetUnit.Modifier; + } + else + res = (value * originUnit.Modifier) / targetUnit.Modifier; } res = Math.Round(res, 2); await msg.Reply(string.Format("{0} {1} is equal to {2} {3}", value, originUnit.Triggers.First(), res, targetUnit.Triggers.First())); @@ -190,89 +214,11 @@ namespace NadekoBot.Modules.Searches public Dictionary ConversionRates { get; set; } } - - public class JsonUnit + public class MeasurementUnit { public List Triggers { get; set; } public string UnitType { get; set; } public decimal Modifier { get; set; } } - - - #region GetXML - /* - public class UnitCollection - { - public List UnitTypes; - - public UnitCollection(string content) - { - using (var xmlReader = XmlReader.Create(File.OpenRead("units.xml"), new XmlReaderSettings() { IgnoreComments = true, IgnoreWhitespace = true })) - { - XmlDocument doc = new XmlDocument(); - doc.Load(xmlReader); - - UnitTypes = new List(); - foreach (XmlNode node in doc.LastChild.ChildNodes) - { //units/ - UnitType type = new UnitType() - { - Name = node.Name - }; - var units = new List(); - foreach (XmlNode unitNode in node.ChildNodes) - { - var curNode = unitNode.FirstChild; - Unit u = new Unit() - { - Key = curNode.InnerText, - Singular = (curNode = curNode.NextSibling).InnerText, - Plural = (curNode = curNode.NextSibling).InnerText, - Symbol = (curNode = curNode.NextSibling).InnerText, - Source = curNode.NextSibling.NextSibling.InnerText - }; - List factors = new List(); - foreach (XmlNode factorNode in curNode.NextSibling.ChildNodes) - { - Factor f = new Factor() - { - Modifier = factorNode.FirstChild.InnerText.Replace(" ", "") - }; - f.From = factorNode.Attributes.GetNamedItem("from").InnerText; - factors.Add(f); - } - u.Factors = factors; - units.Add(u); - } - type.Units = units; - UnitTypes.Add(type); - } - } - } - - public class UnitType - { - public string Name { get; set; } - public List Units { get; set; } - } - - public class Unit - { - public string Key { get; set; } - public string Plural { get; set; } - public string Singular { get; set; } - public string Symbol { get; set; } - public List Factors { get; set; } - public string Source { get; set; } - } - - public class Factor - { - public string From { get; set; } - public string Modifier { get; set; } - } - } - */ - #endregion } } \ No newline at end of file diff --git a/src/NadekoBot/Services/Database/IUnitOfWork.cs b/src/NadekoBot/Services/Database/IUnitOfWork.cs index 905af9af..14719461 100644 --- a/src/NadekoBot/Services/Database/IUnitOfWork.cs +++ b/src/NadekoBot/Services/Database/IUnitOfWork.cs @@ -17,6 +17,7 @@ namespace NadekoBot.Services.Database ISelfAssignedRolesRepository SelfAssignedRoles { get; } IBotConfigRepository BotConfig { get; } IRepeaterRepository Repeaters { get; } + IUnitConverterRepository ConverterUnits { get; } int Complete(); Task CompleteAsync(); diff --git a/src/NadekoBot/Services/Database/Models/ConvertUnit.cs b/src/NadekoBot/Services/Database/Models/ConvertUnit.cs new file mode 100644 index 00000000..3a487218 --- /dev/null +++ b/src/NadekoBot/Services/Database/Models/ConvertUnit.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Threading.Tasks; + +namespace NadekoBot.Services.Database.Models +{ + public class ConvertUnit : DbEntity + { + public ConvertUnit() { } + [NotMapped] + private string[] _triggersValue; + [NotMapped] + public string[] Triggers + { + get + { + return _triggersValue ?? (_triggersValue = InternalTrigger.Split('|')); + } + set + { + _triggersValue = value; + InternalTrigger = string.Join("|", _triggersValue); + } + } + //protected or private? + /// + /// DO NOT CALL THIS + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public string InternalTrigger { get; set; } + public string UnitType { get; set; } + public decimal Modifier { get; set; } + } + +} diff --git a/src/NadekoBot/Services/Database/NadekoContext.cs b/src/NadekoBot/Services/Database/NadekoContext.cs index ae32be4a..da725a30 100644 --- a/src/NadekoBot/Services/Database/NadekoContext.cs +++ b/src/NadekoBot/Services/Database/NadekoContext.cs @@ -20,6 +20,7 @@ namespace NadekoBot.Services.Database public DbSet BotConfig { get; set; } public DbSet Repeaters { get; set; } public DbSet Currency { get; set; } + public DbSet ConversionUnits { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -83,6 +84,7 @@ namespace NadekoBot.Services.Database .HasIndex(c => c.UserId) .IsUnique(); #endregion + } protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder); } diff --git a/src/NadekoBot/Services/Database/Repositories/IUnitConverterRepository.cs b/src/NadekoBot/Services/Database/Repositories/IUnitConverterRepository.cs new file mode 100644 index 00000000..147f38d8 --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/IUnitConverterRepository.cs @@ -0,0 +1,14 @@ +using NadekoBot.Services.Database.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace NadekoBot.Services.Database.Repositories +{ + public interface IUnitConverterRepository : IRepository + { + void AddOrUpdate(Func check, ConvertUnit toAdd, Func toUpdate); + bool Empty(); + } +} diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/UnitCOnverterRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/UnitCOnverterRepository.cs new file mode 100644 index 00000000..846fe524 --- /dev/null +++ b/src/NadekoBot/Services/Database/Repositories/Impl/UnitCOnverterRepository.cs @@ -0,0 +1,28 @@ +using NadekoBot.Services.Database.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace NadekoBot.Services.Database.Repositories.Impl +{ + public class UnitConverterRepository : Repository, IUnitConverterRepository + { + public UnitConverterRepository(DbContext context) : base(context) + { + } + + public void AddOrUpdate(Func check, ConvertUnit toAdd, Func toUpdate) + { + var existing = _set.FirstOrDefault(check); + if (existing != null) + { + existing = toUpdate.Invoke(existing); + } + else _set.Add(toAdd); + } + + public bool Empty() => !_set.Any(); + } +} diff --git a/src/NadekoBot/Services/Database/UnitOfWork.cs b/src/NadekoBot/Services/Database/UnitOfWork.cs index f8e42e64..58def7dd 100644 --- a/src/NadekoBot/Services/Database/UnitOfWork.cs +++ b/src/NadekoBot/Services/Database/UnitOfWork.cs @@ -38,6 +38,8 @@ namespace NadekoBot.Services.Database private ICurrencyRepository _currency; public ICurrencyRepository Currency => _currency ?? (_currency = new CurrencyRepository(_context)); + private IUnitConverterRepository _conUnits; + public IUnitConverterRepository ConverterUnits => _conUnits ?? (_conUnits = new UnitConverterRepository(_context)); public UnitOfWork(NadekoContext context) { From 673d7698e02b9106d75a994bd1b5d42a475e2c83 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 4 Sep 2016 16:26:23 +0200 Subject: [PATCH 4/6] automated updating of currency --- .../Searches/Commands/UnitConversion.cs | 82 ++++++++----------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs index 8f503ff8..94fe0c77 100644 --- a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs +++ b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs @@ -12,6 +12,7 @@ using System.IO; using System.Linq; using System.Net.Http; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Xml; @@ -23,7 +24,8 @@ namespace NadekoBot.Modules.Searches public class UnitConverterCommands { private Logger _log; - + private static Timer _timer; + public static TimeSpan Span = new TimeSpan(12, 0, 0); public UnitConverterCommands() { _log = LogManager.GetCurrentClassLogger(); @@ -52,55 +54,43 @@ namespace NadekoBot.Modules.Searches { _log.Warn("Could not load units: " + e.Message); } + + + + _timer = new Timer(new TimerCallback(UpdateCurrency), null, 0,(int)Span.TotalMilliseconds); + + } + + public void UpdateCurrency(object stateInfo) + { + var currencyRates = UpdateCurrencyRates().Result; + 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() + { + Triggers = new[] { currencyRates.Base }, + Modifier = decimal.One, + UnitType = unitTypeString + }; + uow.ConverterUnits.Add(baseType); + uow.ConverterUnits.AddRange(currencyRates.ConversionRates.Select(u => new ConvertUnit() + { + InternalTrigger = u.Key, + Modifier = u.Value, + UnitType = unitTypeString + }).ToArray()); + uow.Complete(); + } + _log.Info("Updated Currency"); } public List Units { get; set; } - [Command("updatecur")] - [RequireContext(ContextType.Guild)] - public async Task UpdateCurrency(IUserMessage msg) - { - var channel = msg.Channel as IGuildChannel; - var currencyRates = await UpdateCurrencyRates(); - var unitTypeString = "currency"; - var baseType = new ConvertUnit() - { - Triggers = new[] { currencyRates.Base }, - Modifier = decimal.One, - UnitType = unitTypeString - }; - var baseIndex = Units.FindIndex(x => x.UnitType == "currency" && x.Modifier == baseType.Modifier); - if (baseIndex == -1) - Units.Add(baseType); - else - Units[baseIndex] = baseType; - using (var uow = DbHandler.UnitOfWork()) - { - foreach (var rate in currencyRates.ConversionRates) - { - var u = new ConvertUnit() - { - 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(); - } - } - await msg.Reply("done"); - } + [LocalizedCommand, LocalizedDescription, LocalizedSummary] [RequireContext(ContextType.Guild)] public async Task ConvertListE(IUserMessage msg) //extended and bugged list From b4037f2c33dc44ab7e7b8107dc817a76a6192612 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 4 Sep 2016 16:57:02 +0200 Subject: [PATCH 5/6] seperated models, added alias attribute --- .../Commands/Models/MeasurementUnit.cs | 11 ++++++++ .../Modules/Searches/Commands/Models/Rates.cs | 14 +++++++++++ .../Searches/Commands/UnitConversion.cs | 25 +++---------------- 3 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 src/NadekoBot/Modules/Searches/Commands/Models/MeasurementUnit.cs create mode 100644 src/NadekoBot/Modules/Searches/Commands/Models/Rates.cs diff --git a/src/NadekoBot/Modules/Searches/Commands/Models/MeasurementUnit.cs b/src/NadekoBot/Modules/Searches/Commands/Models/MeasurementUnit.cs new file mode 100644 index 00000000..ba4d1bb7 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Commands/Models/MeasurementUnit.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace NadekoBot.Modules.Searches.Commands.Models +{ + public class MeasurementUnit + { + public List Triggers { get; set; } + public string UnitType { get; set; } + public decimal Modifier { get; set; } + } +} diff --git a/src/NadekoBot/Modules/Searches/Commands/Models/Rates.cs b/src/NadekoBot/Modules/Searches/Commands/Models/Rates.cs new file mode 100644 index 00000000..5aed22fa --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Commands/Models/Rates.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; + +namespace NadekoBot.Modules.Searches.Commands.Models +{ + public class Rates + { + public string Base { get; set; } + public DateTime Date { get; set; } + [JsonProperty("rates")] + public Dictionary ConversionRates { get; set; } + } +} diff --git a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs index 94fe0c77..856f541e 100644 --- a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs +++ b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs @@ -2,6 +2,7 @@ using Discord.Commands; using NadekoBot.Attributes; using NadekoBot.Extensions; +using NadekoBot.Modules.Searches.Commands.Models; using NadekoBot.Services; using NadekoBot.Services.Database.Models; using Newtonsoft.Json; @@ -91,7 +92,7 @@ namespace NadekoBot.Modules.Searches public List Units { get; set; } - [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [RequireContext(ContextType.Guild)] public async Task ConvertListE(IUserMessage msg) //extended and bugged list { @@ -110,7 +111,7 @@ namespace NadekoBot.Modules.Searches } await msg.ReplyLong(sb.ToString(), "```xl", "```", "```xl"); } - [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] [RequireContext(ContextType.Guild)] public async Task ConvertList(IUserMessage msg) { @@ -124,7 +125,7 @@ namespace NadekoBot.Modules.Searches } await msg.ReplyLong(sb.ToString(), "```xl", "```", "```xl"); } - [LocalizedCommand, LocalizedDescription, LocalizedSummary] + [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] public async Task Convert(IUserMessage msg, string origin, string target, decimal value) { var originUnit = Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(origin.ToLowerInvariant())); @@ -184,7 +185,6 @@ namespace NadekoBot.Modules.Searches } } - public static async Task UpdateCurrencyRates() { using (var http = new HttpClient()) @@ -193,22 +193,5 @@ namespace NadekoBot.Modules.Searches return JsonConvert.DeserializeObject(res); } } - - public class Rates - { - [JsonProperty("base")] - public string Base { get; set; } - [JsonProperty("date")] - public DateTime Date { get; set; } - [JsonProperty("rates")] - public Dictionary ConversionRates { get; set; } - } - - public class MeasurementUnit - { - public List Triggers { get; set; } - public string UnitType { get; set; } - public decimal Modifier { get; set; } - } } } \ No newline at end of file From 141d7aecf4e1ee9bd5f83b0f17738be3dcaa36cd Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 4 Sep 2016 18:21:22 +0200 Subject: [PATCH 6/6] readd currency bug --- .../Modules/Searches/Commands/UnitConversion.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs index 856f541e..a4096ab8 100644 --- a/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs +++ b/src/NadekoBot/Modules/Searches/Commands/UnitConversion.cs @@ -78,12 +78,16 @@ namespace NadekoBot.Modules.Searches UnitType = unitTypeString }; uow.ConverterUnits.Add(baseType); - uow.ConverterUnits.AddRange(currencyRates.ConversionRates.Select(u => new ConvertUnit() + Units.Add(baseType); + var range = currencyRates.ConversionRates.Select(u => new ConvertUnit() { InternalTrigger = u.Key, Modifier = u.Value, UnitType = unitTypeString - }).ToArray()); + }).ToArray(); + uow.ConverterUnits.AddRange(range); + Units.AddRange(range); + uow.Complete(); } _log.Info("Updated Currency"); @@ -132,7 +136,7 @@ namespace NadekoBot.Modules.Searches var targetUnit = Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(target.ToLowerInvariant())); if (originUnit == null || targetUnit == null) { - await msg.Reply(string.Format("Cannot convert {0} to {1}: units not found", originUnit.Triggers.First(), targetUnit.Triggers.First())); + await msg.Reply(string.Format("Cannot convert {0} to {1}: units not found", origin, target)); return; } if (originUnit.UnitType != targetUnit.UnitType)