Example #1
0
        private static void payPlayerSmart(TSPlayer client, string targetName, IEnumerable <string> amounts)
        {
            var combinedAmounts = new StringBuilder();

            amounts.ForEach(s => combinedAmounts.Append(s));

            //we should really think about changing the below code to using CurrencyManager.TryFindCurrencyFromString(), since its adapted from below.
            //but for better error reporting, we use this original version.
            var quadNames = CurrencyConverter.ParseQuadrantNames(combinedAmounts.ToString());

            if (quadNames.Count < 1)
            {
                client.SendErrorMessage("Invalid input. Please check your formatting, and try again.");
                return;
            }

            var mgr       = BankingPlugin.Instance.Bank.CurrencyManager;
            var firstQuad = quadNames.First();
            var currency  = mgr.GetCurrencyByQuadName(firstQuad);

            if (currency == null)
            {
                client.SendErrorMessage($"'{firstQuad}' does not belong to a known Currency.");
                return;
            }

            //ensure all quads lead to the same currency
            foreach (var quadName in quadNames)
            {
                var cur = mgr.GetCurrencyByQuadName(quadName);

                if (cur != currency)
                {
                    client.SendErrorMessage($"'{quadName}' is not valid for currency '{currency.InternalName}'.");
                    return;
                }
            }

            payPlayer(client, currency.InternalName, targetName, combinedAmounts.ToString());
        }
        /// <summary>
        /// Attempts to find the Currency represented by the value string.
        /// </summary>
        /// <param name="value">A string containing a valid currency amount.</param>
        /// <param name="currency">The Currency, if valid. Null otherwise.</param>
        /// <returns>True or false if a Currency was found.</returns>
        public bool TryFindCurrencyFromString(string value, out CurrencyDefinition currency)
        {
            currency = null;

            if (string.IsNullOrWhiteSpace(value))
            {
                return(false);
            }

            var quadNames = CurrencyConverter.ParseQuadrantNames(value);

            if (quadNames.Count < 1)
            {
                return(false);
            }

            //try to find if the first suffix/quad name is valid
            var firstQuad        = quadNames.First();
            var selectedCurrency = GetCurrencyByQuadName(firstQuad);

            if (selectedCurrency == null)
            {
                return(false);
            }

            //ensure all quads are valid, and lead to the same currency
            foreach (var quadName in quadNames)
            {
                var cur = GetCurrencyByQuadName(quadName);
                if (cur != selectedCurrency)
                {
                    return(false);
                }
            }

            currency = selectedCurrency;
            return(true);
        }