Skip to content

Commit

Permalink
feat: use u.gg instead of mobalytics
Browse files Browse the repository at this point in the history
[IMP] use u.gg instead of mobalytics
  • Loading branch information
Stormix authored Dec 20, 2023
2 parents 99be7e0 + cb90bef commit ec83286
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 76 deletions.
150 changes: 118 additions & 32 deletions lib/riot.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,127 @@
import { SummonerData } from '@/types/riot';
import { RecentRoles, SummonerProfile } from '@/types/riot';
import { PlayerRole } from '@prisma/client';

export const summonerData = async (summonerName: string, server: string) => {
const res = await fetch('https://app.mobalytics.gg/api/lol/graphql/v1/query', {
const UGG_ENPOT = 'https://u.gg/api';

export const summonerProfile = async (
riotUserName: string,
riotTagLine: string,
regionId: string,
seasonId: number,
) => {
const resProfile = await fetch(UGG_ENPOT, {
headers: {
accept: '*/*',
'accept-language': 'en_us',
'content-type': 'application/json',
'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'x-moba-client': 'mobalytics-web',
'x-moba-proxy-gql-ops-name': 'LolProfilePageSummonerInfoQuery',
},
referrer: `https://app.mobalytics.gg/lol/profile/${server}/${encodeURIComponent(summonerName)}/overview`,
referrerPolicy: 'strict-origin-when-cross-origin',
body: `{"operationName":"LolProfilePageSummonerInfoQuery","variables":{"summonerName":"${summonerName}","region":"${server}","sQueue":null,"sRole":null,"sChampion":null},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"8ad5d73cd5306b9f5b423a0285e4a078976f125e0424b227e5f244af50954da7"}}}`,
referrer: `https://u.gg/lol/profile/${regionId}/${riotUserName}-${riotTagLine}/overview`,
body: `{\"operationName\":\"getSummonerProfile\",\"variables\":{\"regionId\":\"${regionId}\",\"riotUserName\":\"${riotUserName}\",\"riotTagLine\":\"${riotTagLine}\",\"seasonId\":${seasonId}},\"query\":\"query getSummonerProfile($regionId: String!, $seasonId: Int!, $riotUserName: String!, $riotTagLine: String!) {\\n fetchProfileRanks(\\n riotUserName: $riotUserName\\n riotTagLine: $riotTagLine\\n regionId: $regionId\\n seasonId: $seasonId\\n ) {\\n rankScores {\\n lastUpdatedAt\\n losses\\n lp\\n promoProgress\\n queueType\\n rank\\n role\\n seasonId\\n tier\\n wins\\n __typename\\n }\\n __typename\\n }\\n profileInitSimple(\\n regionId: $regionId\\n riotUserName: $riotUserName\\n riotTagLine: $riotTagLine\\n ) {\\n lastModified\\n memberStatus\\n playerInfo {\\n accountIdV3\\n accountIdV4\\n exodiaUuid\\n iconId\\n puuidV4\\n regionId\\n summonerIdV3\\n summonerIdV4\\n summonerLevel\\n riotUserName\\n riotTagLine\\n __typename\\n }\\n customizationData {\\n headerBg\\n __typename\\n }\\n __typename\\n }\\n}\"}`,
method: 'POST',
mode: 'cors',
credentials: 'include',
});

if (!res.ok) {
if (!resProfile.ok) {
throw new Error('Failed to fetch data');
}

return res.json() as Promise<SummonerData>;
const GQLquery = `
query recentRoleRates(
$queueType: Int!
$regionId: String!
$riotTagLine: String!
$riotUserName: String!
) {
recentRoleRates(
queueType: $queueType
regionId: $regionId
riotTagLine: $riotTagLine
riotUserName: $riotUserName
) {
adc {
gameCount
winCount
}
jungle {
gameCount
winCount
}
mid {
gameCount
winCount
}
top {
gameCount
winCount
}
supp {
gameCount
winCount
}
none {
gameCount
winCount
}
}
}
`;
const resRoles = await fetch(UGG_ENPOT, {
headers: {
accept: '*/*',
'accept-language': 'en_us',
'content-type': 'application/json',
},
referrer: `https://u.gg/lol/profile/${regionId}/${riotUserName}-${riotTagLine}/overview`,
body: `{\"operationName\":\"recentRoleRates\",\"variables\":{\"regionId\":\"${regionId}\",\"riotUserName\":\"${riotUserName}\",\"riotTagLine\":\"${riotTagLine}\",\"queueType\":-1},\"query\":\"${GQLquery}\"}`,
method: 'POST',
});

if (!resRoles.ok) {
throw new Error('Failed to history data');
}

return {
profile: resProfile.json() as Promise<SummonerProfile>,
recentRoles: resRoles.json() as Promise<RecentRoles>,
};
};

const getSeasonId = async () => {
const res = await fetch(
'https://sandu-variables-default-rtdb.europe-west1.firebasedatabase.app/riftmaker-season-id.json',
{ method: 'GET' },
);
if (!res.ok) {
throw new Error('Failed to fetch seasonId');
}

return res.json() as Promise<number>;
};

const getRole = (recentRoles: RecentRoles) => {
const roles = recentRoles.data.recentRoleRates;
let maxGameCount = 0;
let mainRole = '';

Object.entries(roles).forEach(([role, stats]) => {
if (stats.gameCount > maxGameCount) {
maxGameCount = stats.gameCount;
mainRole = role;
}
});

return PlayerRole[(mainRole == 'none' ? 'fill' : mainRole).toUpperCase() as keyof typeof PlayerRole];
};

const getRank = (profile: SummonerProfile) => {
const rankScores = profile.data.fetchProfileRanks.rankScores;

for (const score of rankScores) {
if (score.queueType === 'ranked_solo_5x5') {
return `${score.tier} ${score.rank}`;
}
}

return 'UNRANKED 0';
};

export const validateRiotId = async (
Expand All @@ -39,22 +132,15 @@ export const validateRiotId = async (
role: PlayerRole;
}> => {
try {
const { data } = await summonerData(riotId, 'EUW');
const riotIdList = riotId.split('#');
if (riotIdList.length < 2) throw new Error('riotId must be in format Hamid#Hamid');
const [riotUserName, riotTagLine] = riotIdList;
const seasonId = await getSeasonId();
const { profile, recentRoles } = await summonerProfile(riotUserName, riotTagLine, 'euw1', seasonId);

if (!data.lol.player) {
throw new Error('Please connect your Riot account to Mobalytics and update your account');
}

const { queuesStats, roleStats } = data.lol.player;

const role = roleStats.filters.actual.rolename as PlayerRole;

if (Object.values(PlayerRole).indexOf(role) === -1) {
throw new Error('Invalid role, @Stormix, fix it thx');
}
const role = getRole(await recentRoles);

const { division, tier } = queuesStats.items[0].rank;
const elo = `${tier} ${division}`;
const elo = getRank(await profile);

return {
riotId,
Expand Down
92 changes: 48 additions & 44 deletions types/riot.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,71 @@
export interface SummonerData {
data: Data;
export interface SummonerProfile {
data: SummonerProfileData;
}

export interface Data {
lol: Lol;
export interface SummonerProfileData {
fetchProfileRanks: FetchProfileRanks;
}

export interface Lol {
player: Player;
export interface FetchProfileRanks {
rankScores: RankScore[];
}

export interface Player {
aId: string;
icon: string;
queuesStats: QueuesStats;
roleStats: RoleStats;
export interface RankScore {
lastUpdatedAt: any;
losses: number;
lp: number;
promoProgress: any;
queueType: string;
rank: string;
role: any;
seasonId: number;
tier: string;
wins: number;
}

export interface QueuesStats {
items: QueueStat[];
export interface RecentRoles {
data: Data;
}

export interface QueueStat {
rank: Rank;
queue: string;
lp: number;
wins: number;
winrate: number;
gamesCount: number;
losses: number;
export interface Data {
recentRoleRates: RecentRoleRates;
}

export interface Rank {
tier: string;
division: string;
export interface RecentRoleRates {
adc: Adc;
jungle: Jungle;
mid: Mid;
none: None;
supp: Supp;
top: Top;
}

export interface RoleStats {
filters: Filters;
defaultRole: DefaultRole;
export interface Adc {
gameCount: number;
winCount: number;
}

export interface Filters {
actual: Actual;
export interface Jungle {
gameCount: number;
winCount: number;
}

export interface Actual {
queue: string;
rolename: string;
export interface Mid {
gameCount: number;
winCount: number;
}

export interface DefaultRole {
wins: number;
looses: number;
kda: Kda;
csm: number;
kp: number;
lp: number;
export interface None {
gameCount: number;
winCount: number;
}

export interface Supp {
gameCount: number;
winCount: number;
}

export interface Kda {
k: number;
d: number;
a: number;
__typename: string;
export interface Top {
gameCount: number;
winCount: number;
}

0 comments on commit ec83286

Please sign in to comment.