protected ItemWithItemCount ExtractItemWithItemCount(string input) { ItemWithItemCount result = new ItemWithItemCount(); int xPosition = input.LastIndexOf(CountCharacter); if (xPosition == -1) //there is no count { result.ItemName = input.Trim(); result.ItemCount = 0; } else //the count number will be the string after the "x", turned into a number. the item name is the part before the x { string preCountSection = input.Remove(xPosition); string postCountSection = input.Substring(xPosition + 1); int itemCount = Convert.ToInt32(postCountSection); result.ItemName = preCountSection.Trim(); result.ItemCount = itemCount; } return(result); }
protected IEnumerable <ItemWithCountAndStarLevel> ExtractItemWithCountAndStarLevel(string input) { List <ItemWithCountAndStarLevel> iwcaslList = new List <ItemWithCountAndStarLevel>(); try { //see if we need to jump out to the more complex multi item, single count reward parsing if (input.Contains(RewardAndToken)) { int andIndex = input.IndexOf(RewardAndToken); string lastPhrase = input.Substring(andIndex + 5); string[] lastPhraseParts = lastPhrase.Split(Space.ToCharArray()); string firstWordOfLastPhrase = lastPhraseParts[0]; string rewardPartOfLastPhrase = lastPhrase.Replace(firstWordOfLastPhrase, String.Empty).Trim(); string inputAndReplaced = input.Replace(RewardAndToken, CommaWithSpace); string inputRewardRemoved = inputAndReplaced.Replace(rewardPartOfLastPhrase, String.Empty).Trim(); string[] rewardTypes = inputRewardRemoved.Split(Comma.ToCharArray()); string[] rewardTypesAndCounts = rewardTypes.Select(r => $"{r} {rewardPartOfLastPhrase}").ToArray(); string convertedInput = String.Join(CommaWithSpace, rewardTypesAndCounts); input = convertedInput; } //turn reward string into a list, if needed IList <string> rewardStrings = ConvertCommaSeparatedStringToList(input); //for each reward, turn it into an item name and a count IList <ItemWithItemCount> rewardItemsWithItemCounts = new List <ItemWithItemCount>(); foreach (var rewardString in rewardStrings) { ItemWithItemCount itemWithItemCount = ExtractItemWithItemCount(rewardString); rewardItemsWithItemCounts.Add(itemWithItemCount); } //now for each reward item, extract the star level if applicable foreach (ItemWithItemCount iwc in rewardItemsWithItemCounts) { ItemWithCountAndStarLevel iwcasl = new ItemWithCountAndStarLevel(); ItemWithStarLevel iwsl = ExtractItemWithStarLevel(iwc.ItemName); iwcasl.ItemName = iwsl.ItemName; iwcasl.ItemCount = iwc.ItemCount > 0 ? iwc.ItemCount : !String.IsNullOrWhiteSpace(iwsl.ItemName) ? 1 : 0; iwcasl.ItemStarLevel = iwsl.ItemStarLevel; iwcaslList.Add(iwcasl); } } catch (Exception ex) { _logger.LogWarning("ItemWithCountAndStarLevel String {iwcaslString} could not be parsed; {Message}", input, ex.Message); } return(iwcaslList); }