Skip to content

Commit

Permalink
closes #98
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Jun 26, 2024
1 parent 3c5a31f commit 9fbe72d
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 13 deletions.
3 changes: 3 additions & 0 deletions interfaces/carddata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ export interface ICard {

imageClass?: string;
meta: Record<string, number>;

faq?: number;
errata?: number;
}
30 changes: 30 additions & 0 deletions search/operators/errata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type ICardHelp } from '../../interfaces';
import { numericalOperator } from './_helpers';

export const errata = numericalOperator(['errata', 'e'], 'errata');

export const errataDescription: ICardHelp = {
name: 'Errata',
id: 'errata',

icon: 'flask-outline',

color: '#aa3c06',

help: `
You can find cards that have errata entries with the \`errata:\` or \`e:\` operator.
This operator is a numeric operator, meaning you'll by default want to search for \`errata:>0\` to find entries with any errata entries.
`,

examples: [
{
example: '`errata:>0`',
explanation: 'Cards that have any number of errata entries.',
},
{
example: '`errata:=0`',
explanation: 'Cards with no errata entries.',
},
],
};
30 changes: 30 additions & 0 deletions search/operators/faq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type ICardHelp } from '../../interfaces';
import { numericalOperator } from './_helpers';

export const faq = numericalOperator(['faq', 'f'], 'faq');

export const faqDescription: ICardHelp = {
name: 'FAQ',
id: 'faq',

icon: 'chatbubbles-outline',

color: '#aa063c',

help: `
You can find cards that have FAQ entries with the \`faq:\` or \`f:\` operator.
This operator is a numeric operator, meaning you'll by default want to search for \`faq:>0\` to find entries with any FAQs.
`,

examples: [
{
example: '`faq:>0`',
explanation: 'Cards that have any number of FAQ entries.',
},
{
example: '`faq:=0`',
explanation: 'Cards with no FAQ entries.',
},
],
};
2 changes: 2 additions & 0 deletions search/operators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './bare';
export * from './card';
export * from './errata';
export * from './faq';
export * from './name';
export * from './product';
export * from './subproduct';
Expand Down
58 changes: 48 additions & 10 deletions search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ import * as parser from 'search-query-parser';

import { type ICard } from '../interfaces';

import { bare, card, name, product, subproduct, tag, text } from './operators';
import {
bare,
card,
errata,
faq,
name,
product,
subproduct,
tag,
text,
} from './operators';

const allKeywords = [
['id'], // exact text
['name', 'n'], // loose text
['cardtext', 't'], // loose text
['game', 'g'], // exact text
['faq', 'f'], // numerical
['errata', 'e'], // numerical
['product', 'expansion', 'p', 'e'], // exact text
['tag'], // array search
];
Expand All @@ -24,6 +36,8 @@ const operators: ParserOperator[] = [
card,
name,
text,
faq,
errata,
product,
subproduct,
tag,
Expand Down Expand Up @@ -80,6 +94,24 @@ const allQueryFormatters = [
return `"${value}"`;
},
},
{
key: 'errata',
includes: 'is',
excludes: 'is not',
formatter: (result: Record<string, any>) => {
const value = result['errata'] ?? 0;
return `${value}`;
},
},
{
key: 'faq',
includes: 'is',
excludes: 'is not',
formatter: (result: Record<string, any>) => {
const value = result['faq'] ?? 0;
return `${value}`;
},
},
/*
{
key: 'game',
Expand Down Expand Up @@ -138,35 +170,41 @@ export function queryToText(query: string, isPlural = true): string {

const result = properOperatorsInsteadOfAliases(firstResult);

const text = [];
const textArrayEntries = [];

const gameResult = result['game'];
if (gameResult) {
text.push(`in ${gameResult}`);
textArrayEntries.push(`in ${gameResult}`);
}

allQueryFormatters.forEach((queryFormatter) => {
const { key, includes, excludes, formatter } = queryFormatter;

if (result[key]) {
text.push(`${key} ${includes} ${formatter(result)}`);
textArrayEntries.push(`${key} ${includes} ${formatter(result)}`);
}
if (result.exclude?.[key]) {
text.push(`${key} ${excludes} ${formatter(result.exclude)}`);
textArrayEntries.push(`${key} ${excludes} ${formatter(result.exclude)}`);
}
});

if (result['in']) {
text.push(`in ${result['in']}`);
textArrayEntries.push(`in ${result['in']}`);
}

if (result['text']) {
text.push(`"${result['text']}" is in name or card id`);
textArrayEntries.push(`"${result['text']}" is in name or card id`);
}

if (query.includes('game:')) {
return `${cardText} ${textArrayEntries[0]} ${
textArrayEntries.length > 1 ? 'where' : ''
} ${textArrayEntries.slice(1).join(' and ')}`;
}

return `${cardText} ${text[0]} ${text.length > 1 ? 'where' : ''} ${text
.slice(1)
.join(' and ')}`;
return `${cardText} ${
textArrayEntries.length > 0 ? 'where' : ''
} ${textArrayEntries.join(' and ')}`;
}

export function getGameFromQuery(query: string): string | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h2>{{ 'Pages.SearchResults.NoResults.Header' | translate }}</h2>
<ion-row>
<ion-col class="search-descriptor">
<span>{{ searchService.displayCurrent() | number }} - {{ searchService.displayMaximum() | number }}
of&nbsp;</span> {{ searchService.displayTotal() | number }} {{ searchService.queryDesc() }}
of</span> {{ searchService.displayTotal() | number }} {{ searchService.queryDesc() }}
</ion-col>
</ion-row>
</ion-grid>
Expand Down
1 change: 0 additions & 1 deletion src/app/_shared/helpers/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export function tryNavigateToHash() {
console.log(document.location.hash);
if (!document.location.hash) return;

setTimeout(() => {
Expand Down
16 changes: 16 additions & 0 deletions src/app/advanced/advanced.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ export class AdvancedPage implements OnInit {
if (this.searchQuery.meta[filter.prop]) return;

if (filter.type === 'number') {
if (['FAQ', 'Errata'].includes(filter.name)) {
this.searchQuery.meta[filter.prop] = {
operator: '>',
value: undefined,
};

return;
}

this.searchQuery.meta[filter.prop] = {
operator: '=',
value: undefined,
Expand Down Expand Up @@ -165,6 +174,13 @@ export class AdvancedPage implements OnInit {
this.visibleFilters = this.metaService.getFiltersByProductId(product);
this.visibleTags = this.tagsByProduct[product];
}

this.visibleFilters.unshift(
...([
{ name: 'FAQ', prop: 'faq', type: 'number' },
{ name: 'Errata', prop: 'errata', type: 'number' },
] as IProductFilter[])
);
}

getSearchQuery() {
Expand Down
16 changes: 15 additions & 1 deletion src/app/cards.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { type ICard, type IProductFilter } from '../../interfaces';
import { numericalOperator } from '../../search/operators/_helpers';
import { parseQuery, type ParserOperator } from '../../search/search';
import { environment } from '../environments/environment';
import { ErrataService } from './errata.service';
import { FAQService } from './faq.service';
import { LocaleService } from './locale.service';
import { MetaService } from './meta.service';

Expand All @@ -23,6 +25,8 @@ export class CardsService {
private http = inject(HttpClient);
private localeService = inject(LocaleService);
private metaService = inject(MetaService);
private faqService = inject(FAQService);
private errataService = inject(ErrataService);

public get allCards(): ICard[] {
return this.cards;
Expand Down Expand Up @@ -59,14 +63,24 @@ export class CardsService {
}

// card utilities
private reformatCardsWithErrataAndFAQ(): ICard[] {
return this.cards.map((card) => ({
...card,
faq: this.faqService.getCardFAQ(card.game, card.name).length ?? 0,
errata:
this.errataService.getCardErrata(card.game, card.name).length ?? 0,
}));
}

public getCardByIdOrName(codeOrName: string): ICard | undefined {
return (
this.cardsById[codeOrName] ?? this.cardsByName[codeOrName] ?? undefined
);
}

public searchCards(query: string): ICard[] {
return parseQuery(this.cards, query, this.getExtraFilterOperators());
const formattedCards = this.reformatCardsWithErrataAndFAQ();
return parseQuery(formattedCards, query, this.getExtraFilterOperators());
}

public getExtraFilterOperators() {
Expand Down
4 changes: 4 additions & 0 deletions src/app/syntax/syntax.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { type ICardHelp } from '../../../interfaces';
import { TranslateService } from '@ngx-translate/core';
import {
cardDescription,
errataDescription,
faqDescription,
nameDescription,
productDescription,
subproductDescription,
Expand All @@ -28,6 +30,8 @@ export class SyntaxPage implements OnInit {
public allOperators: ICardHelp[] = [
cardDescription,
textDescription,
errataDescription,
faqDescription,
nameDescription,
productDescription,
subproductDescription,
Expand Down

0 comments on commit 9fbe72d

Please sign in to comment.