lol-pairing-stat-fetcher/index.mjs

212 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

2024-02-08 11:18:03 +00:00
// @ts-check
import { RiotAPI, RiotAPITypes, PlatformId } from "@fightmegg/riot-api";
2024-02-09 00:52:26 +00:00
import "dotenv/config";
/** @type {string} */
const RG_API_KEY = process.env.RG_API_KEY || "no api key found";
2024-02-08 12:08:27 +00:00
const METHOD_KEY = RiotAPITypes.METHOD_KEY;
2024-02-08 11:18:03 +00:00
/** @type {RiotAPITypes.Config} */
const config = {
2024-02-08 12:15:34 +00:00
debug: false,
2024-02-08 11:18:03 +00:00
cache: {
cacheType: "ioredis", // local or ioredis
client: "redis://redis:6379", // leave null if client is local
ttls: {
byMethod: {
2024-02-09 03:41:40 +00:00
[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
2024-02-08 12:34:16 +00:00
// TODO: Figure out if I can get more games with old API?
// [METHOD_KEY.MATCH.GET_MATCHLIST_BY_ACCOUNT]: 24 * 60 * 60 * 100, // ms
2024-02-08 11:18:03 +00:00
},
},
},
};
2024-02-09 00:52:26 +00:00
const rAPI = new RiotAPI(RG_API_KEY, config);
2024-02-08 11:18:03 +00:00
2024-02-08 12:08:27 +00:00
let val = await rAPI.summoner
.getBySummonerName({
region: PlatformId.NA1,
summonerName: "RavenShade",
})
.then(({ name, puuid }) => ({ name, puuid }));
2024-02-09 09:53:28 +00:00
// const ice = await rAPI.summoner
// .getBySummonerName({
// region: PlatformId.NA1,
// summonerName: "IcePhoenix05",
// })
// .then(({ name, puuid }) => ({ name, puuid }));
const ice = await rAPI.account
.getByRiotId({
region: PlatformId.AMERICAS,
gameName: "Heartsii",
tagLine: "0000",
2024-02-08 12:08:27 +00:00
})
2024-02-09 09:53:28 +00:00
.then(({ puuid }) => ({ puuid }));
2024-02-08 12:08:27 +00:00
2024-02-08 12:34:16 +00:00
const valGameIds = await get_games_by_puuid(val.puuid);
const iceGamesIds = await get_games_by_puuid(ice.puuid);
2024-02-08 12:08:27 +00:00
2024-02-08 12:34:16 +00:00
const commonGameIds = valGameIds.filter(
Set.prototype.has,
new Set(iceGamesIds),
);
2024-02-08 12:08:27 +00:00
2024-02-08 12:37:48 +00:00
/** @type {RiotAPITypes.MatchV5.MatchDTO[]} */
2024-02-08 12:34:16 +00:00
let commonGames = [];
for (const matchId of commonGameIds) {
const game = await rAPI.matchV5.getMatchById({
cluster: PlatformId.AMERICAS,
matchId,
});
commonGames.push(game);
}
2024-02-08 12:08:27 +00:00
2024-02-09 03:41:40 +00:00
console.log(`\nCommon Game Data Found: ${commonGameIds.length}`);
2024-02-08 13:09:31 +00:00
commonGames.reverse();
2024-02-09 03:41:40 +00:00
let gameData0 = commonGames
2024-02-08 13:09:31 +00:00
.map(({ info, metadata }) => ({
id: metadata.matchId,
data: info.participants
.filter(({ puuid }) => puuid === ice.puuid || puuid === val.puuid)
2024-02-09 02:22:32 +00:00
.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,
};
}),
2024-02-08 13:09:31 +00:00
}))
2024-02-09 02:22:32 +00:00
.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) => {
2024-02-09 03:41:40 +00:00
acc[playerData.summonerName] = {
position: playerData.positionEstimate,
champion: playerData.championName,
};
2024-02-09 02:22:32 +00:00
return acc;
}, {}),
};
});
2024-02-09 03:41:40 +00:00
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, "")));
2024-02-08 12:37:48 +00:00
2024-02-08 12:08:27 +00:00
/**
* @param {string} puuid
* @returns {Promise<string[]>}
*/
async function get_games_by_puuid(puuid) {
2024-02-09 03:41:40 +00:00
const COUNT = 20;
2024-02-09 00:52:26 +00:00
2024-02-08 12:08:27 +00:00
let games = [];
let start = 0;
while (start >= 0) {
let newGames = await rAPI.matchV5.getIdsByPuuid({
cluster: PlatformId.AMERICAS,
2024-02-08 12:15:34 +00:00
puuid,
2024-02-08 12:08:27 +00:00
params: {
start,
2024-02-09 00:52:26 +00:00
count: COUNT,
queue: 400,
2024-02-08 12:08:27 +00:00
},
});
2024-02-09 10:13:38 +00:00
games.push.apply(games, newGames);
2024-02-08 12:08:27 +00:00
if (newGames.length < 20) {
break;
}
2024-02-09 00:52:26 +00:00
start += COUNT;
2024-02-08 12:08:27 +00:00
}
2024-02-08 11:18:03 +00:00
2024-02-08 12:08:27 +00:00
return games;
}