/// <summary>
        ///     Asynchronously gets the mapping Id of the specified outcome
        /// </summary>
        /// <param name="outcomeId">The outcome identifier used to get mapped outcomeId</param>
        /// <param name="cultures">The list of <see cref="CultureInfo" /> to fetch <see cref="IOutcomeMapping" /></param>
        /// <returns>A <see cref="Task{IOutcomeMappingId}" /> representing the asynchronous operation</returns>
        public async Task <IEnumerable <IOutcomeMapping> > GetMappedOutcomeIdAsync(string outcomeId,
                                                                                   IEnumerable <CultureInfo> cultures)
        {
            if (_producer.Equals(_producerManager.Get(5)))
            {
                return(null);
            }

            var cultureInfos      = cultures.ToList();
            var marketDescription = await GetMarketDescriptorAsync(cultureInfos).ConfigureAwait(false);

            var marketMappings = GetMarketMapping(marketDescription);

            if (marketMappings == null || !marketMappings.Any())
            {
                return(null);
            }

            var allOutcomes = new List <IOutcomeMappingData>();

            foreach (var marketMapping in marketMappings)
            {
                if (marketMapping?.OutcomeMappings != null)
                {
                    allOutcomes.AddRange(marketMapping.OutcomeMappings);
                }
            }

            if (!allOutcomes.Any())
            {
                return(null);
            }

            var mappedOutcomes = allOutcomes.Where(o => Equals(o.OutcomeId.ToString(), outcomeId)).ToList();

            if (!mappedOutcomes.Any())
            {
                return(null);
            }

            var resultMappedOutcomes = new List <IOutcomeMapping>();

            foreach (var mappedOutcome in mappedOutcomes)
            {
                var producerOutcomeNames = new Dictionary <CultureInfo, string>();
                foreach (var cultureInfo in cultureInfos)
                {
                    var producerOutcomeName = mappedOutcome.GetProducerOutcomeName(cultureInfo);

                    if (!string.IsNullOrEmpty(producerOutcomeName) &&
                        _specifiers != null &&
                        _specifiers.ContainsKey("score") &&
                        marketDescription.Attributes?.FirstOrDefault(a =>
                                                                     a.Name == SdkInfo.FlexScoreMarketAttributeName) != null)
                    {
                        try
                        {
                            producerOutcomeName = FlexMarketHelper.GetName(producerOutcomeName, _specifiers);
                        }
                        catch (NameExpressionException ex)
                        {
                            ExecutionLog.Error(
                                $"The generation of name for flex score mapped outcome {outcomeId} failed", ex);
                        }
                    }

                    producerOutcomeNames[cultureInfo] = producerOutcomeName;
                }

                if (!string.IsNullOrEmpty(mappedOutcome.OutcomeId))
                {
                    resultMappedOutcomes.Add(new OutcomeMapping(mappedOutcome.ProducerOutcomeId, producerOutcomeNames,
                                                                mappedOutcome.MarketId));
                }
            }

            return(!resultMappedOutcomes.Any()
                ? null
                : resultMappedOutcomes);
        }
예제 #2
0
        /// <summary>
        /// Asynchronously gets the name of the specified outcome in the requested language
        /// </summary>
        /// <param name="outcomeId">The outcome identifier</param>
        /// <param name="culture">The language of the returned name</param>
        /// <returns>A <see cref="Task{String}"/> representing the asynchronous operation</returns>
        public async Task <string> GetOutcomeNameAsync(string outcomeId, CultureInfo culture)
        {
            if (outcomeId.StartsWith(SdkInfo.PlayerProfileMarketPrefix) || outcomeId.StartsWith(SdkInfo.CompetitorProfileMarketPrefix))
            {
                try
                {
                    return(await GetOutcomeNameFromProfileAsync(outcomeId, culture).ConfigureAwait(false));
                }
                catch (NameExpressionException ex)
                {
                    HandleErrorCondition("Failed to generate outcome name from profile", outcomeId, null, culture, ex);
                    return(null);
                }
            }

            var marketDescriptor = await GetMarketDescriptionForOutcomeAsync(outcomeId, culture, true).ConfigureAwait(false);


            var outcome = marketDescriptor?.Outcomes.FirstOrDefault(o => o.Id == outcomeId);

            if (outcome == null)
            {
                return(null);
            }

            var nameDescription = outcome.GetName(culture);

            if (string.IsNullOrEmpty(nameDescription))
            {
                HandleErrorCondition("Retrieved market descriptor does not contain name descriptor for associated outcome in the specified language", outcomeId, null, culture, null);
                return(null);
            }

            if (marketDescriptor.Attributes?.FirstOrDefault(a => a.Name == SdkInfo.FlexScoreMarketAttributeName) != null)
            {
                try
                {
                    return(FlexMarketHelper.GetName(nameDescription, _specifiers));
                }
                catch (NameExpressionException ex)
                {
                    HandleErrorCondition("The generation of name for flex score market outcome failed", outcomeId, nameDescription, culture, ex);
                }
            }

            string nameDescriptionFormat;
            IList <INameExpression> expressions;

            try
            {
                expressions = GetNameExpressions(nameDescription, out nameDescriptionFormat);
            }
            catch (Exception ex)
            {
                if (ex is ArgumentException || ex is FormatException)
                {
                    HandleErrorCondition("The name description parsing failed", outcomeId, nameDescription, culture, ex);
                    return(null);
                }
                throw;
            }

            if (expressions == null || !expressions.Any())
            {
                return(nameDescription);
            }

            IEnumerable <Task <string> > tasks;

            try
            {
                tasks = expressions.Select(e => e.BuildNameAsync(culture)).ToList();
                await Task.WhenAll(tasks).ConfigureAwait(false);
            }
            catch (NameExpressionException ex)
            {
                HandleErrorCondition("Error occurred while evaluating the name expression", outcomeId, nameDescription, culture, ex);
                return(null);
            }
            return(string.Format(nameDescriptionFormat, tasks.Select(t => (object)t.Result).ToArray()));
        }