lol-pairing-stat-fetcher/index.mjs

111 lines
2.7 KiB
JavaScript

// @ts-check
import { RiotAPI, RiotAPITypes, PlatformId } from "@fightmegg/riot-api";
const METHOD_KEY = RiotAPITypes.METHOD_KEY;
/** @type {RiotAPITypes.Config} */
const config = {
debug: false,
cache: {
cacheType: "ioredis", // local or ioredis
client: "redis://redis:6379", // leave null if client is local
ttls: {
byMethod: {
[METHOD_KEY.SUMMONER.GET_BY_SUMMONER_NAME]: 24 * 60 * 60 * 100, // 1 day
[METHOD_KEY.MATCH_V5.GET_IDS_BY_PUUID]: 24 * 60 * 60 * 100, // 1 day
[METHOD_KEY.MATCH_V5.GET_MATCH_BY_ID]: 24 * 60 * 60 * 100, // 1 day
// TODO: Figure out if I can get more games with old API?
// [METHOD_KEY.MATCH.GET_MATCHLIST_BY_ACCOUNT]: 24 * 60 * 60 * 100, // ms
},
},
},
};
const rAPI = new RiotAPI("RGAPI-8f41294d-7bb1-4fd5-83d4-773a8a7b25f5", config);
let val = await rAPI.summoner
.getBySummonerName({
region: PlatformId.NA1,
summonerName: "RavenShade",
})
.then(({ name, puuid }) => ({ name, puuid }));
const ice = await rAPI.summoner
.getBySummonerName({
region: PlatformId.NA1,
summonerName: "IcePhoenix05",
})
.then(({ name, puuid }) => ({ name, puuid }));
const valGameIds = await get_games_by_puuid(val.puuid);
const iceGamesIds = await get_games_by_puuid(ice.puuid);
const commonGameIds = valGameIds.filter(
Set.prototype.has,
new Set(iceGamesIds),
);
// console.log(`${JSON.stringify(commonGameIds, null, " ")}`);
/** @type {RiotAPITypes.MatchV5.MatchDTO[]} */
let commonGames = [];
for (const matchId of commonGameIds) {
const game = await rAPI.matchV5.getMatchById({
cluster: PlatformId.AMERICAS,
matchId,
});
commonGames.push(game);
}
console.log(`Common Game Data Found: ${commonGameIds.length}`);
commonGames.reverse();
commonGames
.map(({ info, metadata }) => ({
id: metadata.matchId,
data: info.participants
.filter(({ puuid }) => puuid === ice.puuid || puuid === val.puuid)
.map(({ championId, championName, summonerName, lane }) => ({
championId,
championName,
summonerName,
lane,
})),
}))
// .filter(({ data }) => {
// const lanes = data.map((s) => s.lane);
// return lanes.includes("BOTTOM") && lanes.includes("SUPPORT");
// })
.forEach((g) => console.log(g));
/**
* @param {string} puuid
* @returns {Promise<string[]>}
*/
async function get_games_by_puuid(puuid) {
let games = [];
let start = 0;
while (start >= 0) {
let newGames = await rAPI.matchV5.getIdsByPuuid({
cluster: PlatformId.AMERICAS,
puuid,
params: {
start,
},
});
if (newGames.length < 20) {
break;
}
games.push.apply(games, newGames);
start += 20;
}
return games;
}