Added Temperature, case insensitivity

This commit is contained in:
appelemac 2016-08-30 21:59:33 +02:00
parent afa2cbee4f
commit d4ae9e712a
2 changed files with 132 additions and 28 deletions

View File

@ -55,46 +55,117 @@ 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() {
var u = new JsonUnit()
{
Triggers = new List<string>() { 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()));
}
}
@ -127,6 +198,7 @@ namespace NadekoBot.Modules.Searches
public decimal Modifier { get; set; }
}
#region GetXML
/*
public class UnitCollection

View File

@ -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
}
]