Example #1
0
        private void PopulateRoomWithLikedItemsFromCharacter(Character character)
        {
            foreach (Tag likeTag in character.LikeTags)
            {
                List <Item> tagItems = itemFactory.FetchItemsByTagName(likeTag.Name);
                if (tagItems.Count == 0)
                {
                    throw new Exception("There must be items for the tag " + likeTag.Name + " please check config");
                }
                int  randomItemIndex = randomNumberGenerator.GetRandomInteger(0, tagItems.Count);
                Item selectedTagItem;
                int  loopRepeatCount = 0;
                do
                {
                    selectedTagItem = tagItems[randomItemIndex];
                    loopRepeatCount++;
                    if (loopRepeatCount > 10)
                    {
                        break;
                    }
                } while (items.Contains(selectedTagItem));

                if (!selectedTagItem.HasTagIn(character.DislikeTags))
                {
                    items.Add(selectedTagItem);
                }
            }
        }
        /// <summary>
        /// Get a unique and random list of tags up to the specified limit
        /// </summary>
        /// <returns>The random tags.</returns>
        /// <param name="maxTags">Max tags to retrieve.</param>
        public List <Tag> GetRandomTags(int maxTags, List <Tag> excludeTags, ItemFactory itemFactory, bool rejectIfSharedTag)
        {
            List <Tag> result = new List <Tag>();

            if (maxTags > 0 && tags.Count > 0)
            {
                int retryCount = 0;
                do
                {
                    int tagIndex = randomNumberGenerator.GetRandomInteger(0, tags.Count);
                    Tag tag      = tags[tagIndex];
                    if (!result.Contains(tag) && !excludeTags.Contains(tag))
                    {
                        bool rejectTag = false;
                        if (rejectIfSharedTag)
                        {
                            rejectTag = (itemFactory.FetchItemsByTagName(tag.Name).Count > 1);
                        }
                        if (!rejectTag)
                        {
                            result.Add(tag);
                        }
                    }
                    retryCount++;
                    if (retryCount > 5)
                    {
                        break;
                    }
                } while ((result.Count < maxTags) && (result.Count < (tags.Count - excludeTags.Count)));
            }
            return(result);
        }