Example #1
0
        public KalkCurrency GetOrSetCurrency(string name = null, decimal?value = null)
        {
            if (name == null)
            {
                return(GetSafeBaseCurrencyFromConfig());
            }

            if (value == null)
            {
                return(RegisterBaseCurrency(name));
            }

            // Verify that the currency name is valid
            KalkCurrency.CheckValid(Engine.CurrentSpan, name);

            if (Engine.Units.TryGetValue(name, out var symbol))
            {
                if (symbol is KalkCurrency currency)
                {
                    var baseCurrency = GetSafeBaseCurrencyFromConfig();
                    if (baseCurrency == currency)
                    {
                        throw new ScriptRuntimeException(Engine.CurrentSpan, $"Cannot change the rate of the base currency `{name}`.");
                    }
                    currency.Value = new KalkBinaryExpression(1.0m / value, ScriptBinaryOperator.Multiply, baseCurrency);
                    return(currency);
                }
                throw new ScriptRuntimeException(Engine.CurrentSpan, $"Unable to define currency `{name}` as it is already attached to a different unit: {symbol}.");
            }

            // New currency
            return(RegisterCurrency(name, value.Value, true));
        }
Example #2
0
        public KalkCurrency RegisterBaseCurrency(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            KalkCurrency.CheckValid(Engine.CurrentSpan, name);

            KalkCurrency baseCurrency;

            if (Engine.Units.TryGetValue(name, out var unit))
            {
                baseCurrency = unit as KalkCurrency;
                if (baseCurrency != null)
                {
                    // It is already the default unit
                    if (unit.Value == null)
                    {
                        return(baseCurrency);
                    }

                    var existingBase    = GetSafeBaseCurrencyFromConfig();
                    var existingConvert = (KalkBinaryExpression)unit.Value;
                    unit.Value = null;

                    // ratio USD = 1.1 EUR
                    var ratio = Engine.ToObject <decimal>(Engine.CurrentSpan, existingConvert.Value);

                    existingBase.Value = new KalkBinaryExpression(1.0m, ScriptBinaryOperator.Multiply, baseCurrency);

                    foreach (var currency in Engine.Units.Values.OfType <KalkCurrency>())
                    {
                        if (currency == baseCurrency)
                        {
                            continue;
                        }

                        var existingRatio = Engine.ToObject <decimal>(Engine.CurrentSpan, ((KalkBinaryExpression)currency.Value).Value);
                        currency.Value = new KalkBinaryExpression(existingRatio / ratio, ScriptBinaryOperator.Multiply, baseCurrency);
                    }
                }
                else
                {
                    throw new ArgumentException($"Cannot create a new base currency for the existing unity `{unit}`.", nameof(name));
                }
            }
            else
            {
                if (Currencies.Count > 0)
                {
                    throw new ArgumentException($"Cannot create a new base currency when there are already currencies. You need to first create this currency relative to the existing base `{GetSafeBaseCurrencyFromConfig().Name}` currency", nameof(name));
                }

                baseCurrency = new KalkCurrency(this, name)
                {
                    Description = "Default Currency"
                };
                Engine.Units.Add(name, baseCurrency);
            }

            BaseCurrency = name;
            return(baseCurrency);
        }