lol-pairing-stat-fetcher/index.mjs

204 lines
5.1 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]: 7 * 24 * 60 * 60 * 1000, // 7 day
[METHOD_KEY.MATCH_V5.GET_IDS_BY_PUUID]: 7 * 24 * 60 * 60 * 1000, // 7 day
[METHOD_KEY.MATCH_V5.GET_MATCH_BY_ID]: 7 * 24 * 60 * 60 * 1000, // 7 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(`\nCommon Game Data Found: ${commonGameIds.length}`);
commonGames.reverse();
let gameData0 = 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] = {
position: playerData.positionEstimate,
champion: playerData.championName,
};
return acc;
}, {}),
};
});
console.log(`\nBot Lane Duo Matches Found: ${gameData0.length}`);
const wins0 = gameData0.filter(({ win }) => win).length;
const losses0 = gameData0.filter(({ win }) => !win).length;
console.log(`Wins: ${wins0}`);
console.log(`Losses: ${losses0}`);
console.log(`W/L: ${(wins0 / (wins0 + losses0)).toFixed(2)}`);
let gameData = gameData0.filter(
({ data }) => data["RavenShade"].champion !== "Smolder",
);
console.log(`\nBot Lane Duo Matches Found (No Smolder): ${gameData.length}`);
const rawWins = gameData.filter(({ win }) => win);
const rawLosses = gameData.filter(({ win }) => !win);
const winData = rawWins.map((win) => win.data);
const lossData = rawLosses.map((loss) => loss.data);
const wins = rawWins.length;
const losses = rawLosses.length;
console.log(`Wins: ${wins}`);
console.log(`Losses: ${losses}`);
console.log(`W/L: ${(wins / (wins + losses)).toFixed(2)}`);
console.log("\nwins");
winData
.map((d) =>
Object.entries(d).map(([summoner, { champion }]) => ({
summoner,
champion,
})),
)
.forEach((g) => console.log(JSON.stringify(g, null, "")));
console.log("\nlosses");
lossData
.map((d) =>
Object.entries(d).map(([summoner, { champion }]) => ({
summoner,
champion,
})),
)
.forEach((g) => console.log(JSON.stringify(g, null, "")));
/**
* @param {string} puuid
* @returns {Promise<string[]>}
*/
async function get_games_by_puuid(puuid) {
const COUNT = 20;
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;
}