match_collector: better typing in index.ts

This commit is contained in:
2026-04-17 15:02:15 +02:00
parent b7435f0884
commit 0f84b9a707

View File

@@ -5,6 +5,7 @@ const sleep_minutes = 12
import { MongoClient } from 'mongodb' import { MongoClient } from 'mongodb'
import champion_stat from './champion_stat' import champion_stat from './champion_stat'
import { Match } from './api'
main() main()
@@ -32,7 +33,7 @@ async function main() {
const challengerLeague = await fetchChallengerLeague() const challengerLeague = await fetchChallengerLeague()
console.log('ChallengerLeague: got ' + challengerLeague.entries.length + ' entries') console.log('ChallengerLeague: got ' + challengerLeague.entries.length + ' entries')
const gameList = [] const gameList : string[] = []
let i = 0 let i = 0
for (const challenger of challengerLeague.entries) { for (const challenger of challengerLeague.entries) {
console.log('Entry ' + i + '/' + challengerLeague.entries.length + ' ...') console.log('Entry ' + i + '/' + challengerLeague.entries.length + ' ...')
@@ -105,11 +106,11 @@ async function connectToDatabase() {
return client return client
} }
async function fetchLatestPatchDate(client) { async function fetchLatestPatchDate(client: MongoClient) {
const database = client.db('patches') const database = client.db('patches')
const patches = database.collection('patches') const patches = database.collection('patches')
const latestPatch = await patches.find().limit(1).sort({ date: -1 }).next() const latestPatch = await patches.find().limit(1).sort({ date: -1 }).next()
return [latestPatch.patch, Math.floor(latestPatch.date.valueOf() / 1000)] return [latestPatch!.patch, Math.floor(latestPatch!.date.valueOf() / 1000)]
} }
async function fetchChallengerLeague() { async function fetchChallengerLeague() {
@@ -125,7 +126,7 @@ async function fetchChallengerLeague() {
return challengerLeague return challengerLeague
} }
async function summonerGameList(puuid, startTime) { async function summonerGameList(puuid: string, startTime: string) {
const base = 'https://europe.api.riotgames.com' const base = 'https://europe.api.riotgames.com'
const endpoint = `/lol/match/v5/matches/by-puuid/${puuid}/ids` const endpoint = `/lol/match/v5/matches/by-puuid/${puuid}/ids`
const url = `${base}${endpoint}?queue=420&type=ranked&startTime=${startTime}&api_key=${api_key}` const url = `${base}${endpoint}?queue=420&type=ranked&startTime=${startTime}&api_key=${api_key}`
@@ -137,7 +138,7 @@ async function summonerGameList(puuid, startTime) {
return gameList return gameList
} }
async function match(matchId) { async function match(matchId: string) {
const base = 'https://europe.api.riotgames.com' const base = 'https://europe.api.riotgames.com'
const endpoint = `/lol/match/v5/matches/${matchId}` const endpoint = `/lol/match/v5/matches/${matchId}`
const url = `${base}${endpoint}?api_key=${api_key}` const url = `${base}${endpoint}?api_key=${api_key}`
@@ -149,7 +150,7 @@ async function match(matchId) {
return match return match
} }
async function matchTimeline(matchId) { async function matchTimeline(matchId: string) {
const base = 'https://europe.api.riotgames.com' const base = 'https://europe.api.riotgames.com'
const endpoint = `/lol/match/v5/matches/${matchId}/timeline` const endpoint = `/lol/match/v5/matches/${matchId}/timeline`
const url = `${base}${endpoint}?api_key=${api_key}` const url = `${base}${endpoint}?api_key=${api_key}`
@@ -161,7 +162,7 @@ async function matchTimeline(matchId) {
return timeline return timeline
} }
async function alreadySeenGames(client, latestPatch) { async function alreadySeenGames(client: MongoClient, latestPatch: string) {
const database = client.db('matches') const database = client.db('matches')
const matches = database.collection(latestPatch) const matches = database.collection(latestPatch)
@@ -169,7 +170,7 @@ async function alreadySeenGames(client, latestPatch) {
return alreadySeen return alreadySeen
} }
async function saveMatch(client, match, latestPatch) { async function saveMatch(client: MongoClient, match: Match, latestPatch: string) {
const database = client.db('matches') const database = client.db('matches')
const matches = database.collection(latestPatch) const matches = database.collection(latestPatch)
await matches.insertOne(match) await matches.insertOne(match)