LeagueStats/server/app/Services/SummonerService.ts

121 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-10-04 20:05:44 +00:00
import Jax from './Jax'
2024-11-07 19:43:18 +00:00
import { SummonerDTOExtended } from 'App/Services/Jax/src/Endpoints/SummonerEndpoint'
2020-10-04 20:05:44 +00:00
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'
2024-01-14 13:59:54 +00:00
import { ACCOUNT_NAME_DELIMITER } from 'App/helpers'
2024-11-07 18:57:59 +00:00
import { AccountDto } from 'App/Services/Jax/src/Endpoints/AccountEndpoint'
2020-10-04 20:05:44 +00:00
export interface LeagueEntriesByQueue {
soloQ?: LeagueEntryByQueue
flex5v5?: LeagueEntryByQueue
}
export interface LeagueEntryByQueue extends LeagueEntryDTO {
fullRank: string
winrate: string
shortName: string | number
}
2020-10-04 20:05:44 +00:00
class SummonerService {
private readonly uniqueLeagues = ['CHALLENGER', 'GRANDMASTER', 'MASTER']
public readonly leaguesNumbers = { I: 1, II: 2, III: 3, IV: 4 }
public getRankedShortName(rank: PlayerRankParsed | MatchPlayerRank) {
return this.uniqueLeagues.includes(rank.tier) ? rank.lp : rank.tier[0] + rank.rank
}
public getWinrate(wins: number, losses: number) {
return +((wins * 100) / (wins + losses)).toFixed(1) + '%'
}
2020-10-04 20:05:44 +00:00
/**
* Helper to transform League Data from the Riot API
* @param league raw data of the league from Riot API
*/
2024-04-14 18:57:27 +00:00
private getLeagueData(league?: LeagueEntryDTO): LeagueEntryByQueue | null {
2020-10-04 20:05:44 +00:00
if (!league) {
return null
}
const fullRank = this.uniqueLeagues.includes(league.tier)
? league.tier
: `${league.tier} ${league.rank}`
const winrate = this.getWinrate(league.wins, league.losses)
const shortName = this.uniqueLeagues.includes(league.tier)
? league.leaguePoints
: league.tier[0] + this.leaguesNumbers[league.rank]
2020-10-04 20:05:44 +00:00
return {
...league,
fullRank,
winrate,
shortName,
}
}
/**
2024-04-14 18:57:27 +00:00
* Get summoner account infos for a searched summoner name
* @param summonerName
* @param region
2020-10-04 20:05:44 +00:00
*/
2024-04-14 18:57:27 +00:00
public async getSummoner(
summonerName: string,
region: string
): Promise<SummonerDTOExtended | null> {
let name = summonerName.toLowerCase()
2020-10-04 20:05:44 +00:00
2024-04-14 18:57:27 +00:00
// Get old way
2024-01-14 13:59:54 +00:00
if (!name.includes(ACCOUNT_NAME_DELIMITER)) {
2024-04-14 18:57:27 +00:00
name = `${name}-${region}`
2024-01-14 13:59:54 +00:00
}
2024-01-14 13:59:54 +00:00
// Get new way: gameName#tagLine
const [gameName, tagLine] = name.split(ACCOUNT_NAME_DELIMITER)
const account = await Jax.Account.byRiotId(gameName, tagLine, region)
2024-04-14 18:57:27 +00:00
if (account) {
const summoner = await Jax.Summoner.summonerPuuid(account.puuid, region)
2024-11-07 19:40:35 +00:00
return { ...summoner, name: account.gameName, tagLine: account.tagLine }
2024-04-14 18:57:27 +00:00
}
return null
}
2024-11-07 18:57:59 +00:00
public async getAccount(puuid: string, region: string): Promise<AccountDto | null> {
return Jax.Account.byPuuid(puuid, region)
}
2020-10-04 20:05:44 +00:00
/**
* Return the full list of old and actual summoner names
* @param account of the summoner
* @param summonerDB summoner in the database
*/
2024-04-14 18:57:27 +00:00
public async getAllSummonerNames(account: SummonerDTOExtended, summonerDB: Summoner) {
await summonerDB.related('names').firstOrCreate({
2024-11-07 19:40:35 +00:00
name: account.name + ACCOUNT_NAME_DELIMITER + account.tagLine,
})
return summonerDB.related('names').query().select('name', 'created_at')
2020-10-04 20:05:44 +00:00
}
/**
* Get ranked data for a specific Summoner
* @param account
* @param region
2020-10-04 20:05:44 +00:00
*/
public async getRanked(summonerId: string, region: string): Promise<LeagueEntriesByQueue> {
const ranked = await Jax.League.summonerID(summonerId, region)
const result: LeagueEntriesByQueue = {}
if (ranked && ranked.length) {
result.soloQ =
2024-04-14 18:57:27 +00:00
this.getLeagueData(ranked.find((e) => e.queueType === 'RANKED_SOLO_5x5')) || undefined
result.flex5v5 =
2024-04-14 18:57:27 +00:00
this.getLeagueData(ranked.find((e) => e.queueType === 'RANKED_FLEX_SR')) || undefined
2020-10-04 20:05:44 +00:00
}
return result
}
}
export default new SummonerService()