Sicherung
This commit is contained in:
428
Kalk/Kalk.Core/Modules/Currencies/CurrencyModule.cs
Normal file
428
Kalk/Kalk.Core/Modules/Currencies/CurrencyModule.cs
Normal file
File diff suppressed because one or more lines are too long
139
Kalk/Kalk.Core/Modules/Currencies/KalkCurrencies.cs
Normal file
139
Kalk/Kalk.Core/Modules/Currencies/KalkCurrencies.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kalk.Core.Modules;
|
||||
using Scriban;
|
||||
using Scriban.Parsing;
|
||||
using Scriban.Runtime;
|
||||
using Scriban.Syntax;
|
||||
|
||||
namespace Kalk.Core
|
||||
{
|
||||
public class KalkCurrencies : IScriptObject, IScriptCustomFunction
|
||||
{
|
||||
private KalkUnits _units;
|
||||
private readonly CurrencyModule _currencyModule;
|
||||
|
||||
public KalkCurrencies(CurrencyModule currencyModule)
|
||||
{
|
||||
_currencyModule = currencyModule;
|
||||
}
|
||||
|
||||
public void Initialize(KalkEngine engine)
|
||||
{
|
||||
if (engine == null) throw new ArgumentNullException(nameof(engine));
|
||||
_units = engine.Units;
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = 0;
|
||||
foreach (var unitPair in _units)
|
||||
{
|
||||
if (unitPair.Value is KalkCurrency) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
var keys = _units.Keys.ToList();
|
||||
foreach (var unitKey in keys)
|
||||
{
|
||||
if (_units.TryGetValue(unitKey, out var unitObject) && unitObject is KalkCurrency)
|
||||
{
|
||||
_units.Remove(unitKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetMembers()
|
||||
{
|
||||
// Show only currencies from units
|
||||
foreach (var unitPair in _units)
|
||||
{
|
||||
if (unitPair.Value is KalkCurrency) yield return unitPair.Key;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(string member)
|
||||
{
|
||||
return _units.Contains(member) && _units[member] is KalkCurrency;
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get => false;
|
||||
set => _units.IsReadOnly = value;
|
||||
}
|
||||
|
||||
public bool TryGetValue(TemplateContext context, SourceSpan span, string member, out object value)
|
||||
{
|
||||
if (_units.TryGetValue(context, span, member, out value) && value is KalkCurrency)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool CanWrite(string member) => false;
|
||||
|
||||
public bool TrySetValue(TemplateContext context, SourceSpan span, string member, object value, bool readOnly) => false;
|
||||
|
||||
public bool Remove(string member) => false;
|
||||
|
||||
public void SetReadOnly(string member, bool readOnly)
|
||||
{
|
||||
}
|
||||
|
||||
public IScriptObject Clone(bool deep)
|
||||
{
|
||||
throw new NotSupportedException("Clone is not supported");
|
||||
}
|
||||
|
||||
public int RequiredParameterCount => 0;
|
||||
|
||||
public int ParameterCount => 0;
|
||||
|
||||
public ScriptVarParamKind VarParamKind => ScriptVarParamKind.None;
|
||||
|
||||
public Type ReturnType => typeof(object);
|
||||
|
||||
public ScriptParameterInfo GetParameterInfo(int index)
|
||||
{
|
||||
throw new NotSupportedException("Currencies don't have any parameters.");
|
||||
}
|
||||
|
||||
public object Invoke(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, ScriptBlockStatement blockStatement)
|
||||
{
|
||||
if (!(callerContext.Parent is ScriptExpressionStatement))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var engine = (KalkEngine) context;
|
||||
if (_units.All(x => x.Value.GetType() != typeof(KalkCurrency)))
|
||||
{
|
||||
engine.WriteHighlightLine($"# No Currencies defined (e.g try `import {nameof(CurrencyModule)}`)");
|
||||
}
|
||||
else
|
||||
{
|
||||
_units.Display(engine, $"Builtin Currencies (Last Update: {_currencyModule.LastUpdate.ToString("dd MMM yyyy", CultureInfo.InvariantCulture)})", symbol => symbol is KalkCurrency && !symbol.IsUser, false);
|
||||
_units.Display(engine, "User Defined Currencies", symbol => symbol is KalkCurrency && symbol.IsUser, false);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public ValueTask<object> InvokeAsync(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, ScriptBlockStatement blockStatement)
|
||||
{
|
||||
return new ValueTask<object>(Invoke(context, callerContext, arguments, blockStatement));
|
||||
}
|
||||
}
|
||||
}
|
||||
80
Kalk/Kalk.Core/Modules/Currencies/KalkCurrency.cs
Normal file
80
Kalk/Kalk.Core/Modules/Currencies/KalkCurrency.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Kalk.Core.Modules;
|
||||
using Scriban;
|
||||
using Scriban.Parsing;
|
||||
using Scriban.Runtime;
|
||||
using Scriban.Syntax;
|
||||
|
||||
namespace Kalk.Core
|
||||
{
|
||||
public class KalkCurrency : KalkUnit
|
||||
{
|
||||
private readonly CurrencyModule _currencyModule;
|
||||
private const int CurrencyColumnAlign = 27;
|
||||
|
||||
public KalkCurrency(CurrencyModule currencyModule, string name) : base(name)
|
||||
{
|
||||
_currencyModule = currencyModule;
|
||||
Plural = name;
|
||||
}
|
||||
|
||||
public override string TypeName => "currency";
|
||||
|
||||
public override object GetValue() => 1.0m;
|
||||
|
||||
public override object Invoke(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, ScriptBlockStatement blockStatement)
|
||||
{
|
||||
if (!(callerContext.Parent is ScriptExpressionStatement))
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var engine = (KalkEngine)context;
|
||||
|
||||
string currencyCmd;
|
||||
string currencyDesc;
|
||||
if (Value == null)
|
||||
{
|
||||
currencyCmd = $"currency({Name});";
|
||||
currencyDesc = $"Base currency";
|
||||
}
|
||||
else
|
||||
{
|
||||
var value = (KalkBinaryExpression) Value;
|
||||
|
||||
var valueToBase = 1.0m / (decimal) value.Value;
|
||||
var format = "#0.0###";
|
||||
if (valueToBase < 0.0001m)
|
||||
{
|
||||
format = null;
|
||||
}
|
||||
|
||||
var formattedNumber = valueToBase.ToString(format, CultureInfo.InvariantCulture);
|
||||
|
||||
currencyCmd = $"currency({Name}, {formattedNumber});";
|
||||
currencyDesc = $"{formattedNumber,-8} {Name} => 1 {_currencyModule.GetSafeBaseCurrencyFromConfig().Name}";
|
||||
}
|
||||
|
||||
engine.WriteHighlightLine($"{currencyCmd, -CurrencyColumnAlign} # {currencyDesc}");
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void CheckValid(SourceSpan span, string name)
|
||||
{
|
||||
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||
if (name.Length != 3) throw new ScriptRuntimeException(span, $"Base currency `{name}` must 3 characters long instead of {name.Length}.");
|
||||
foreach (var c in name)
|
||||
{
|
||||
if (!(c >= 'A' && c <= 'Z'))
|
||||
{
|
||||
throw new ScriptRuntimeException(span, $"The character `{c}` is invalid for the base currency `{name}`. Only A-Z are allowed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user