refactor/match_collector: refactor platform handling logic
This commit is contained in:
104
match_collector/platform.ts
Normal file
104
match_collector/platform.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Platform and region configuration for Riot Games API
|
||||
*
|
||||
* Platforms are the server clusters (EUW1, EUN1, NA1, KR)
|
||||
* Regions are the routing values for match API (EUROPE, AMERICAS, ASIA)
|
||||
*/
|
||||
|
||||
// Platform to regional routing value mapping
|
||||
const PLATFORMS: Record<string, string> = {
|
||||
EUW1: 'EUROPE',
|
||||
EUN1: 'EUROPE',
|
||||
NA1: 'AMERICAS',
|
||||
KR: 'ASIA'
|
||||
}
|
||||
|
||||
// Platform counts for tracking item purchases per region
|
||||
type PlatformCounts = {
|
||||
euw: number
|
||||
eun: number
|
||||
na: number
|
||||
kr: number
|
||||
}
|
||||
|
||||
// Platform key mapping for converting platform strings to PlatformCounts keys
|
||||
const PLATFORM_KEYS: Record<string, keyof PlatformCounts> = {
|
||||
euw1: 'euw',
|
||||
eun1: 'eun',
|
||||
na1: 'na',
|
||||
kr: 'kr'
|
||||
}
|
||||
|
||||
// List of all region keys for iteration
|
||||
const REGION_KEYS: Array<keyof PlatformCounts> = ['euw', 'eun', 'na', 'kr']
|
||||
|
||||
/**
|
||||
* Get the base URL for platform-specific API calls (e.g., league-v4)
|
||||
*/
|
||||
function getPlatformBaseUrl(platform: string): string {
|
||||
return `https://${platform.toLowerCase()}.api.riotgames.com`
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the base URL for regional API calls (e.g., match-v5)
|
||||
*/
|
||||
function getRegionalBaseUrl(region: string): string {
|
||||
return `https://${region.toLowerCase()}.api.riotgames.com`
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the regional routing value for a platform
|
||||
* Falls back to the provided default region if platform not found
|
||||
*/
|
||||
function getRegionForPlatform(platform: string): string | undefined {
|
||||
return PLATFORMS[platform]
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize an empty PlatformCounts object
|
||||
*/
|
||||
function initPlatformCounts(): PlatformCounts {
|
||||
return { euw: 0, eun: 0, na: 0, kr: 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge platform counts from source into target
|
||||
*/
|
||||
function mergePlatformCounts(target: PlatformCounts, source: PlatformCounts): void {
|
||||
for (const key of REGION_KEYS) {
|
||||
target[key] += source[key]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a platform count with a single platform set to 1
|
||||
*/
|
||||
function singlePlatformCount(platform: string): PlatformCounts {
|
||||
const counts = initPlatformCounts()
|
||||
const key = PLATFORM_KEYS[platform.toLowerCase()]
|
||||
if (key) {
|
||||
counts[key] = 1
|
||||
}
|
||||
return counts
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PlatformCounts key for a platform string
|
||||
*/
|
||||
function getPlatformKey(platform: string): keyof PlatformCounts | undefined {
|
||||
return PLATFORM_KEYS[platform.toLowerCase()]
|
||||
}
|
||||
|
||||
export {
|
||||
PLATFORMS,
|
||||
PlatformCounts,
|
||||
PLATFORM_KEYS,
|
||||
REGION_KEYS,
|
||||
getPlatformBaseUrl,
|
||||
getRegionalBaseUrl,
|
||||
getRegionForPlatform,
|
||||
initPlatformCounts,
|
||||
mergePlatformCounts,
|
||||
singlePlatformCount,
|
||||
getPlatformKey
|
||||
}
|
||||
Reference in New Issue
Block a user