From 346273ef2d4dee623258835365b1b01383822b9c Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Sun, 14 Jan 2024 14:59:54 +0100 Subject: [PATCH] refactor: quickly adapt merged PR --- client/src/components/Form/SearchForm.vue | 8 ++--- .../Controllers/Http/SummonersController.ts | 26 +--------------- .../Jax/src/Endpoints/AccountEndpoint.ts | 31 +++++++++++++++++++ .../Jax/src/Endpoints/SummonerEndpoint.ts | 13 -------- server/app/Services/Jax/src/Jax.ts | 4 ++- server/app/Services/Jax/src/JaxRequest.ts | 2 +- server/app/Services/SummonerService.ts | 21 +++++++------ server/app/helpers.ts | 5 +++ server/commands/LoadV4Matches.ts | 2 +- 9 files changed, 57 insertions(+), 55 deletions(-) create mode 100644 server/app/Services/Jax/src/Endpoints/AccountEndpoint.ts diff --git a/client/src/components/Form/SearchForm.vue b/client/src/components/Form/SearchForm.vue index 3b95c0e..e46105b 100644 --- a/client/src/components/Form/SearchForm.vue +++ b/client/src/components/Form/SearchForm.vue @@ -110,15 +110,15 @@ export default { } }, $route(newRoute) { - this.summoner = newRoute.params.name + this.summoner = newRoute.params.name.replaceAll('-', '#') this.dropdown = false this.open = false }, }, created() { - if (!this.summoner.length && !this.homepage) { - this.summoner = this.$route.params.name + if (!this.summoner.length && !this.homepage && this.$route.params.name) { + this.summoner = this.$route.params.name.replaceAll('-', '#') } window.addEventListener('blur', this.windowBlur) window.addEventListener('keydown', this.handleEscape) @@ -148,7 +148,7 @@ export default { document.body.style.overflow = 'hidden' }, formSubmit() { - const search = this.summoner.split(' ').join('').replace('+', ' ').replace('#', '-') + const search = this.summoner.split(' ').join('').replaceAll('+', ' ').replaceAll('#', '-') if (search.length) { this.$emit('formSubmit', search, this.selectedRegion) } diff --git a/server/app/Controllers/Http/SummonersController.ts b/server/app/Controllers/Http/SummonersController.ts index a31de1b..0d65c70 100644 --- a/server/app/Controllers/Http/SummonersController.ts +++ b/server/app/Controllers/Http/SummonersController.ts @@ -35,31 +35,7 @@ export default class SummonersController { const { summoner, region } = await request.validate(SummonerBasicValidator) try { - // Coming from C++ this seems completely fine. Which suggests to me that it likely isn't. - var account - - // Checks if searching for Riot tag. Frontend is currently replacing `#` with `-`. - if (!summoner.includes(`-`)) { - account = await SummonerService.getAccount(summoner, region) - } else { - const [name, tagline] = summoner.split(`-`) - account = await SummonerService.getRiotAccountByName(name, tagline, region) - if (!account) { - return response.json(null) - } - - const additionalInfo = await SummonerService.getSummonerByPuuid(account.puuid, region) - - if (!additionalInfo) { - return response.json(null) - } - - account = { - ...account, - ...additionalInfo, - } - } - + const account = await SummonerService.getSummoner(summoner, region) // Check if the summoner is found if (!account) { return response.json(null) diff --git a/server/app/Services/Jax/src/Endpoints/AccountEndpoint.ts b/server/app/Services/Jax/src/Endpoints/AccountEndpoint.ts new file mode 100644 index 0000000..61ba92b --- /dev/null +++ b/server/app/Services/Jax/src/Endpoints/AccountEndpoint.ts @@ -0,0 +1,31 @@ +import RiotRateLimiter from 'riot-ratelimiter' +import { JaxConfig } from '../../JaxConfig' +import JaxRequest from '../JaxRequest' +import { getRiotRegion } from 'App/helpers' + +export interface AccountDto { + puuid: string + gameName: string + tagLine: string +} + +export default class AccountEndpoint { + private config: JaxConfig + private limiter: RiotRateLimiter + + constructor(config: JaxConfig, limiter: RiotRateLimiter) { + this.config = config + this.limiter = limiter + } + + public byRiotId(name: string, tagline: string, region: string): Promise { + return new JaxRequest( + getRiotRegion(region), + this.config, + `account/v1/accounts/by-riot-id/${name}/${tagline}`, + this.limiter, + 36000, + 'riot' + ).execute() + } +} diff --git a/server/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts b/server/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts index e62226f..a3400bf 100644 --- a/server/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts +++ b/server/app/Services/Jax/src/Endpoints/SummonerEndpoint.ts @@ -1,8 +1,6 @@ -// import { RiotRateLimiter } from '@fightmegg/riot-rate-limiter' import RiotRateLimiter from 'riot-ratelimiter' import { JaxConfig } from '../../JaxConfig' import JaxRequest from '../JaxRequest' -import { getRiotRegion } from 'App/helpers' export interface SummonerDTO { accountId: string @@ -53,17 +51,6 @@ export default class SummonerEndpoint { ).execute() } - public puuidFromRiotTag(name: string, tagline: string, region: string): Promise { - return new JaxRequest( - getRiotRegion(region), - this.config, - `account/v1/accounts/by-riot-id/${name}/${tagline}`, - this.limiter, - 36000, - `riot` - ).execute() - } - public summonerPuuid(puuid: string, region: string): Promise { return new JaxRequest( region, diff --git a/server/app/Services/Jax/src/Jax.ts b/server/app/Services/Jax/src/Jax.ts index fc3735a..cef61d8 100644 --- a/server/app/Services/Jax/src/Jax.ts +++ b/server/app/Services/Jax/src/Jax.ts @@ -1,3 +1,4 @@ +import AccountEndpoint from './Endpoints/AccountEndpoint' import LeagueEndpoint from './Endpoints/LeagueEndpoint' import MatchEndpoint from './Endpoints/MatchEndpoint' import MatchlistEndpoint from './Endpoints/MatchlistEndpoint' @@ -5,7 +6,6 @@ import SummonerEndpoint from './Endpoints/SummonerEndpoint' import SpectatorEndpoint from './Endpoints/SpectatorEndpoint' import CDragonEndpoint from './Endpoints/CDragonEndpoint' import { JaxConfig } from '../JaxConfig' -// import { RiotRateLimiter } from '@fightmegg/riot-rate-limiter' import RiotRateLimiter from 'riot-ratelimiter' import { STRATEGY } from 'riot-ratelimiter/dist/RateLimiter' import MatchV4Endpoint from './Endpoints/MatchV4Endpoint' @@ -15,6 +15,7 @@ export default class Jax { public key: string public limiter: RiotRateLimiter public config: JaxConfig + public Account: AccountEndpoint public League: LeagueEndpoint public Match: MatchEndpoint public MatchV4: MatchV4Endpoint @@ -36,6 +37,7 @@ export default class Jax { }) this.config = config + this.Account = new AccountEndpoint(this.config, this.limiter) this.League = new LeagueEndpoint(this.config, this.limiter) this.Match = new MatchEndpoint(this.config, this.limiter) this.MatchV4 = new MatchV4Endpoint(this.config, this.limiter) diff --git a/server/app/Services/Jax/src/JaxRequest.ts b/server/app/Services/Jax/src/JaxRequest.ts index 3f0a74a..6c40f61 100644 --- a/server/app/Services/Jax/src/JaxRequest.ts +++ b/server/app/Services/Jax/src/JaxRequest.ts @@ -21,7 +21,7 @@ export default class JaxRequest { endpoint: string, limiter: RiotRateLimiter, cacheTime: number, - riotApiPath = `lol` + riotApiPath = 'lol' ) { this.region = region this.config = config diff --git a/server/app/Services/SummonerService.ts b/server/app/Services/SummonerService.ts index 6e38dc6..93935ee 100644 --- a/server/app/Services/SummonerService.ts +++ b/server/app/Services/SummonerService.ts @@ -4,6 +4,7 @@ import { LeagueEntryDTO } from './Jax/src/Endpoints/LeagueEndpoint' import Summoner from 'App/Models/Summoner' import { PlayerRankParsed } from 'App/Parsers/ParsedType' import MatchPlayerRank from 'App/Models/MatchPlayerRank' +import { ACCOUNT_NAME_DELIMITER } from 'App/helpers' export interface LeagueEntriesByQueue { soloQ?: LeagueEntryByQueue @@ -53,22 +54,22 @@ class SummonerService { } /** - * Get account infos for a searched summoner name + * Get summnoner account infos for a searched summoner name * @param summonerName * @param region */ - public async getAccount(summonerName: string, region: string) { + public async getSummoner(summonerName: string, region: string): Promise { const name = summonerName.toLowerCase() - const account = await Jax.Summoner.summonerName(name, region) - return account - } - public async getRiotAccountByName(name: string, tagline: string, region: string) { - return await Jax.Summoner.puuidFromRiotTag(name, tagline, region) - } + // Get old way: summonerName + if (!name.includes(ACCOUNT_NAME_DELIMITER)) { + return Jax.Summoner.summonerName(name, region) + } - public async getSummonerByPuuid(puuid: string, region: string) { - return await Jax.Summoner.summonerPuuid(puuid, region) + // Get new way: gameName#tagLine + const [gameName, tagLine] = name.split(ACCOUNT_NAME_DELIMITER) + const account = await Jax.Account.byRiotId(gameName, tagLine, region) + return account ? Jax.Summoner.summonerPuuid(account.puuid, region) : null } /** diff --git a/server/app/helpers.ts b/server/app/helpers.ts index e4f2558..fd94d68 100644 --- a/server/app/helpers.ts +++ b/server/app/helpers.ts @@ -1,3 +1,8 @@ +/** + * Delimiter used instead of '#' in account names from the Frontend + */ +export const ACCOUNT_NAME_DELIMITER = '-' + /** * All League of Legends regions used in Riot API */ diff --git a/server/commands/LoadV4Matches.ts b/server/commands/LoadV4Matches.ts index 87dd2ff..a335d3b 100644 --- a/server/commands/LoadV4Matches.ts +++ b/server/commands/LoadV4Matches.ts @@ -37,7 +37,7 @@ export default class LoadV4Matches extends BaseCommand { this.logger.info(`Trying to find ${this.summoner} from ${this.region}`) // ACCOUNT - const account = await SummonerService.getAccount(this.summoner, this.region) + const account = await SummonerService.getSummoner(this.summoner, this.region) if (account) { this.logger.success('League account found.') } else {