-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
240 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import {Arg, Args, Query, Resolver} from 'type-graphql'; | ||
import AbilityEntry from '../structures/AbilityEntry'; | ||
import AbilityService from '../services/AbilityService'; | ||
import {GraphQLJSONObject} from 'graphql-type-json'; | ||
import PaginatedArgs from '../arguments/PaginatedArgs'; | ||
|
||
@Resolver(AbilityEntry) | ||
export default class AbilityResolver { | ||
private abilityService: AbilityService; | ||
|
||
constructor() { | ||
this.abilityService = new AbilityService(); | ||
} | ||
|
||
@Query(() => GraphQLJSONObject, { | ||
description: [ | ||
'Gets details on a single ability based on a fuzzy search.', | ||
'You can supply skip and take to paginate the fuzzy search and reverse to show the least likely matched on top.', | ||
'Reversal is applied before pagination!' | ||
].join(''), | ||
}) | ||
async getAbilityDetailsByFuzzy(@Args() { | ||
query, skip, take, reverse, | ||
}: PaginatedArgs) { | ||
const entry = this.abilityService.findByName(query); | ||
|
||
if (!entry) { | ||
const fuzzyEntry = this.abilityService.findByFuzzy({ | ||
query, skip, take, reverse, | ||
}); | ||
if (fuzzyEntry === undefined) { | ||
throw new Error(`Failed to get data for Pokemon: ${query}`); | ||
} | ||
query = fuzzyEntry[0].name; | ||
} | ||
|
||
const detailsEntry = this.abilityService.findByNameWithDetails(query); | ||
if (detailsEntry === undefined) { | ||
throw new Error(`Failed to get data for Pokemon: ${query}`); | ||
} | ||
|
||
return detailsEntry; | ||
} | ||
|
||
@Query(() => GraphQLJSONObject, { | ||
description: [ | ||
'Gets details on a single ability based on an exact name match.' | ||
].join(''), | ||
}) | ||
async getAbilityDetailsByName(@Arg('ability') ability: string) { | ||
const entry = this.abilityService.findByNameWithDetails(ability); | ||
|
||
if (entry === undefined) { | ||
throw new Error(`Failed to get data for Pokemon: ${ability}`); | ||
} | ||
|
||
return entry; | ||
} | ||
|
||
@Query(() => GraphQLJSONObject, { | ||
description: [ | ||
'Gets entries of multiple ability based on a fuzzy search.', | ||
'You can supply skip and take to limit the amount of flavour texts to return and reverse to show latest games on top.', | ||
'Reversal is applied before pagination!' | ||
].join(''), | ||
}) | ||
getAbilityByFuzzy(@Args() { | ||
query, skip, take, reverse, | ||
}: PaginatedArgs) { | ||
const abilityEntries = this.abilityService.findByFuzzy({ | ||
query, skip, take, reverse, | ||
}); | ||
|
||
if (abilityEntries === undefined) { | ||
throw new Error(`Failed to get data for ability: ${name}`); | ||
} | ||
|
||
return abilityEntries; | ||
} | ||
|
||
@Query(() => GraphQLJSONObject, {description: 'Gets details on a single ability based on name.'}) | ||
getAbilityByName(@Arg('name') name: string) { | ||
const abilityEntry = this.abilityService.findByName(name); | ||
|
||
if (abilityEntry === undefined) { | ||
throw new Error(`Failed to get data for ability: ${name}`); | ||
} | ||
|
||
return abilityEntry; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {Arg, Args} from 'type-graphql'; | ||
import abilities from '../assets/abilities'; | ||
import PaginatedArgs from '../arguments/PaginatedArgs'; | ||
import {GraphQLJSONObject} from 'graphql-type-json'; | ||
import {SimpleFuseOptions} from '../typings/common'; | ||
import AbilityEntry from '../structures/AbilityEntry'; | ||
import FuzzySearch from '../utils/FuzzySearch'; | ||
import {abilityAliases} from '../assets/aliases'; | ||
import {sentencecase} from '../utils/util'; | ||
|
||
export default class AbilityService { | ||
public findByName(@Arg('name') name: string) { | ||
return abilities.get(name); | ||
} | ||
|
||
public findByFuzzy(@Args() { | ||
query, skip, take, reverse, | ||
}: PaginatedArgs, @Arg('fuseOptions', () => GraphQLJSONObject) fuseOptions?: SimpleFuseOptions) { | ||
const fuzzyAbility = new FuzzySearch(abilities, [ 'name', 'num' ], {threshold: 0.3, ...fuseOptions}); | ||
|
||
let fuzzyResult = fuzzyAbility.run(query); | ||
|
||
if (!fuzzyResult.length) { | ||
const fuzzyAliasResult = new FuzzySearch(abilityAliases, [ 'alias', 'ability' ], {threshold: 0.4}).run(query); | ||
|
||
if (fuzzyAliasResult.length) { | ||
fuzzyResult = fuzzyAbility.run(fuzzyAliasResult[0].ability); | ||
} | ||
} | ||
|
||
if (reverse) { | ||
fuzzyResult.reverse(); | ||
} | ||
|
||
return fuzzyResult.slice(skip, skip + take); | ||
} | ||
|
||
public findByNameWithDetails(@Arg('ability') ability: string) { | ||
const abilityData = this.findByName(ability); | ||
|
||
if (!abilityData) { | ||
throw new Error(`No ability found for ${ability}`); | ||
} | ||
|
||
const abilityEntry = new AbilityEntry(); | ||
abilityEntry.desc = abilityData.desc; | ||
abilityEntry.name = abilityData.name; | ||
abilityEntry.num = abilityData.num; | ||
abilityEntry.shortDesc = abilityData.shortDesc; | ||
abilityEntry.bulbapediaPage = `https://bulbapedia.bulbagarden.net/wiki/${sentencecase(abilityData.name.replace(/ /g, '_'))}_(Ability)`; | ||
abilityEntry.serebiiPage = `https://www.serebii.net/abilitydex/${abilityData.name.replace(/ /g, '_').toLowerCase()}.shtml`; | ||
abilityEntry.smogonPage = `https://www.smogon.com/dex/sm/abilities/${abilityData.name.toLowerCase().replace(/ /g, '_')}`; | ||
|
||
return abilityEntry; | ||
} | ||
} |
Oops, something went wrong.