LeagueStats/server/app/helpers.ts

85 lines
2 KiB
TypeScript
Raw Normal View History

2020-10-11 15:31:16 +00:00
import { ParticipantBasic, ParticipantDetails } from './Models/Match'
/**
* All League of Legends regions used in Riot API
*/
export type Region = 'br1' | 'eun1' | 'euw1' | 'jp1' | 'kr' | 'la1' | 'la2' | 'na1' | 'oc1' | 'tr1' | 'ru'
/**
* New regions used in Riot API >= v5
*/
export type V5Region = 'americas' | 'asia' | 'europe'
/**
* Map old Riot API regions to new ones
* @param region : old region
* @returns new region name
*/
export function getV5Region (region: string): V5Region {
switch (region as Region) { // TODO: remove cast when region is typed to "Region" everywhere instead of string
case 'na1':
case 'br1':
case 'la1':
case 'la2':
case 'oc1':
return 'americas'
case 'kr':
case 'jp1':
return 'asia'
case 'eun1':
case 'euw1':
case 'tr1':
case 'ru':
return 'europe'
}
}
2020-10-04 20:05:44 +00:00
/**
* League of Legends queues with defined role for each summoner
*/
export const queuesWithRole = [
2020-10-04 20:05:44 +00:00
0, // Custom
400, // Draft
420, // Solo/Duo
430, // Blind,
440, // Flex
700, // Clash
]
/**
* League of Legends seasons timestamps
*/
export const seasons = {
2020-10-04 20:05:44 +00:00
0: 9,
1578628800000: 10,
2020-11-13 20:54:29 +00:00
1604970061000: 10.5, // Preseason 11
2021-01-08 09:17:14 +00:00
1610078400000: 11,
2020-10-04 20:05:44 +00:00
}
/**
* League of Legends all support item ids
*/
export const supportItems = [3850, 3851, 3853, 3854, 3855, 3857, 3858, 3859, 3860, 3862, 3863, 3864]
2020-10-04 20:05:44 +00:00
/**
* Get season number for a match
* @param timestamp
*/
2020-10-11 15:31:16 +00:00
export function getSeasonNumber (timestamp: number): number {
const arrSeasons = Object.keys(seasons).map(k => Number(k))
arrSeasons.push(timestamp)
arrSeasons.sort()
const indexSeason = arrSeasons.indexOf(timestamp) - 1
return seasons[arrSeasons[indexSeason]]
}
/**
2020-10-11 15:31:16 +00:00
* Sort array of Players by roles according to a specific order
* @param a first player
* @param b second player
*/
2020-10-11 15:31:16 +00:00
export function sortTeamByRole (a: ParticipantBasic | ParticipantDetails, b: ParticipantBasic | ParticipantDetails) {
const sortingArr = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
return sortingArr.indexOf(a.role) - sortingArr.indexOf(b.role)
2020-10-04 20:05:44 +00:00
}