lol-pairing-stat-fetcher/index.mjs

159 lines
3.8 KiB
JavaScript

// @ts-check
import { RiotAPI, RiotAPITypes, PlatformId } from "@fightmegg/riot-api";
import "dotenv/config";
/** @type {string} */
const RG_API_KEY = process.env.RG_API_KEY || "no api key found";
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(RG_API_KEY, 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),
);
/** @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();
let gameData = commonGames
.map(({ info, metadata }) => ({
id: metadata.matchId,
data: info.participants
.filter(({ puuid }) => puuid === ice.puuid || puuid === val.puuid)
.map(
({
championName,
summonerName,
individualPosition,
teamPosition,
win,
}) => ({
championName,
summonerName,
individualPosition,
teamPosition,
win,
}),
)
.map(({ individualPosition, teamPosition, ...rest }) => {
const estimates = [...new Set([individualPosition, teamPosition])];
/** @type {string} */
let positionEstimate;
if (estimates.length === 1) {
positionEstimate = estimates[0];
} else if (estimates.length > 1) {
positionEstimate =
estimates.find((e) => e === "BOTTOM" || e === "UTILITY") ||
estimates[0];
} else {
positionEstimate = "NONE";
}
return {
...rest,
positionEstimate,
};
}),
}))
.filter(({ data }) =>
data
.map(({ positionEstimate }) => positionEstimate)
.every((estimates) => estimates === "BOTTOM" || estimates === "UTILITY"),
)
.map(({ data, ...rest }) => {
return {
...rest,
win: data[0].win,
data: data.reduce((acc, playerData) => {
acc[playerData.summonerName] = playerData.positionEstimate;
return acc;
}, {}),
};
});
gameData.forEach((g) => console.log(JSON.stringify(g, null, " ")));
/**
* @param {string} puuid
* @returns {Promise<string[]>}
*/
async function get_games_by_puuid(puuid) {
const COUNT = 100;
let games = [];
let start = 0;
while (start >= 0) {
let newGames = await rAPI.matchV5.getIdsByPuuid({
cluster: PlatformId.AMERICAS,
puuid,
params: {
start,
count: COUNT,
queue: 400,
},
});
if (newGames.length < 20) {
break;
}
games.push.apply(games, newGames);
start += COUNT;
}
return games;
}