예제 #1
0
        public static TranslatedSearchResult <CatalogItemUnion> MergeSearchResults
        (
            TranslatedSearchResult <ICard> cardResult,
            TranslatedSearchResult <LorKeyword> keywordResult,
            TranslatedSearchResult <Deck> deckResult,
            IItemMatchSorter <CatalogItemUnion> sorter
        )
        {
            var matches = new List <ItemMatch <CatalogItemUnion> >();
            var map     = new Dictionary <CatalogItemUnion, CatalogItemUnion>();

            foreach (var m in cardResult.Matches)
            {
                matches.Add(GetMatch(m, CatalogItemUnion.AsCard(m.Item)));
                if (cardResult.TranslationMap.TryGetValue(m.Item, out ICard? tItem))
                {
                    map.Add(CatalogItemUnion.AsCard(m.Item), CatalogItemUnion.AsCard(tItem));
                }
            }

            foreach (var m in keywordResult.Matches)
            {
                matches.Add(GetMatch(m, m.Item));
                if (keywordResult.TranslationMap.TryGetValue(m.Item, out LorKeyword? tItem))
                {
                    map.Add(m.Item, tItem);
                }
            }

            foreach (var m in deckResult.Matches)
            {
                matches.Add(GetMatch(m, m.Item));
                if (deckResult.TranslationMap.TryGetValue(m.Item, out Deck? tItem))
                {
                    map.Add(m.Item, tItem);
                }
            }

            if (matches.Count == 0)
            {
                return(FromBaseResult(matches, map, cardResult));
            }

            if (sorter.ShouldSortMatches(matches))
            {
                matches.Sort(sorter.Compare);
            }

            // Use fields from whichever type provided the best match
            return(matches[0].Item.T switch
            {
                CatalogItemUnion.Type.Card => FromBaseResult(matches, map, cardResult),
                CatalogItemUnion.Type.Keyword => FromBaseResult(matches, map, keywordResult),
                CatalogItemUnion.Type.Deck => FromBaseResult(matches, map, deckResult),
                _ => FromBaseResult(matches, map, cardResult),
            });
예제 #2
0
        private static TranslatedSearchResult <CatalogItemUnion> FromBaseResult <TItem>(
            IReadOnlyList <ItemMatch <CatalogItemUnion> > matches,
            IReadOnlyDictionary <CatalogItemUnion, CatalogItemUnion> translationMap,
            TranslatedSearchResult <TItem> baseResult)
            where TItem : class
        {
            var result = new SearchResult <CatalogItemUnion>(
                baseResult.SearchTerm,
                baseResult.SearchLocale,
                baseResult.SearchVersion,
                matches);

            return(new TranslatedSearchResult <CatalogItemUnion>(
                       result,
                       baseResult.TranslationLocale,
                       translationMap));
        }