Пример #1
0
        public static string WriteDiscountRange(Item item, Shop shop)
        {
            if (item == null || shop == null)
            {
                return("");
            }

            var info = new StringBuilder();

            CatalogEntry entry = shop.Catalog?.Entries.FirstOrDefault(x => x.ItemId == item.Id);

            if (entry == null)
            {
                return("");
            }

            if (entry.DiscountChance <= 0)
            {
                return("");
            }

            info.Append($"(**{entry.MinDiscount ?? CatalogEntry.DefaultMinDiscount}**% to ");
            info.Append($"**{entry.MaxDiscount ?? CatalogEntry.DefaultMaxDiscount}**% discount range");

            info.Append($", **{RangeF.Convert(0, 1, 0, 100, entry.DiscountChance):##,0.##}**% chance of discount)");

            return(info.ToString());
        }
Пример #2
0
        /// <summary>
        /// Generates a new <see cref="ItemCatalog"/>.
        /// </summary>
        public ItemCatalog Generate(long tier = 1)
        {
            int specials        = 0;
            int discounts       = 0;
            var counters        = new Dictionary <string, int>();
            var discountEntries = new Dictionary <string, int>();
            var groupCounters   = new Dictionary <string, int>();
            int entries         = 0;

            if (Size <= 0)
            {
                throw new ArgumentException("Cannot initialize a catalog with an empty or negative size.");
            }

            while (entries < Size)
            {
                CatalogEntry entry = GetNextEntry(tier, specials, counters, groupCounters);

                if (entry == null)
                {
                    break;
                }

                if (entry.IsSpecial)
                {
                    Logger.Debug($"Special entry applied ({entry.ItemId})");
                    specials++;
                }
                else
                {
                    Logger.Debug($"Next entry loaded ({entry.ItemId})");
                }

                if (entry.DiscountChance > 0 && entry.MaxDiscount.HasValue)
                {
                    if (!discountEntries.ContainsKey(entry.ItemId) &&
                        discounts < MaxDiscountsAllowed &&
                        RandomProvider.Instance.NextDouble() <= entry.DiscountChance)
                    {
                        // NOTE: Since the upper bound is exclusive, add 1 to the max discount, so that it's possible to land on the specified max
                        int discountToApply = RandomProvider.Instance
                                              .Next(entry.MinDiscount ?? CatalogEntry.DefaultMinDiscount, (entry.MaxDiscount ?? CatalogEntry.DefaultMaxDiscount) + 1);

                        discountEntries.Add(entry.ItemId, discountToApply);

                        Logger.Debug($"{discountToApply}% discount applied ({entry.ItemId})");
                        discounts++;
                    }
                }

                if (!ItemHelper.Exists(entry.ItemId))
                {
                    throw new Exception("The specified item ID could not be found.");
                }

                if (!counters.TryAdd(entry.ItemId, 1))
                {
                    counters[entry.ItemId]++;
                }

                if (Check.NotNull(entry.GroupId))
                {
                    if (!groupCounters.TryAdd(entry.GroupId, 1))
                    {
                        groupCounters[entry.GroupId]++;
                    }
                }

                entries++;
            }

            Logger.Debug($"Compiling catalog with {entries} {Format.TryPluralize("entry", entries)}");

            return(new ItemCatalog(counters, discountEntries));
        }