Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opgg add item sets #117

Merged
merged 11 commits into from
Mar 17, 2022
61 changes: 17 additions & 44 deletions plugins/blitzgg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ const { removePerkIds } = require('./utils');

// #region Settings
const supported_modes = [{
key: 420,
name: "Ranked",
tier: 'PLATINUM_PLUS'
},
{
key: 450,
name: 'ARAM',
tier: '',
}
key: 420,
name: "Ranked",
tier: 'PLATINUM_PLUS'
},
{
key: 450,
name: 'ARAM',
tier: '',
}
];

const baseApiUrl = 'https://league-champion-aggregate.iesdev.com';
Expand Down Expand Up @@ -48,15 +48,15 @@ async function getChampionsJsonAsync(championId, gameMode, position = null, useA

// Query URL and get the result
var result = await rp({
uri: requestUri,
json: true
})
.then(function(response) {
uri: requestUri,
json: true
})
.then(function (response) {
// precheck if data is present (currently blitz.gg sends an empty array if no data is present)
if(response["data"] && response["data"].length > 0)
if (response["data"] && response["data"].length > 0)
return response;
})
.catch(function(err) {
.catch(function (err) {
if (err.statusCode === 400 || err.statusCode === 403 || err.statusCode === 404 || err.statusCode === 500)
console.log("JSON was not found => " + err);
else if (err.statusCode === 418) {
Expand Down Expand Up @@ -94,10 +94,6 @@ function getPage(runesJson, champInfo, gameMode) {

// Determine selected perk ids
const selectedPerkIds = removePerkIds(perksData).concat(statShards);
//gets an array for with items name for every category
const start_items = getItemArray(runesJson, "most_common_starting_items");
const core_items = getItemArray(runesJson, "most_common_core_builds");
const big_items = getItemArray(runesJson, "most_common_big_item_builds");
// Return rune page
return {
name: `[${gameMode.name}] ${champInfo.name} ${runesJson.role}`.trim(),
Expand All @@ -112,38 +108,15 @@ function getPage(runesJson, champInfo, gameMode) {
}
},
itemSet: {
start_items: start_items,
core_items: core_items,
big_items: big_items,
raw_data: {
start_items: runesJson["stats"]["most_common_starting_items"],
core_items: runesJson["stats"]["most_common_core_builds"],
big_items: runesJson["stats"]["most_common_big_item_builds"],
},
},
};
} catch (e) {
throw Error(e);
}
}
/**
* A helper methode to get the name of an item per id
* @param {object} runesJson The object that contains the page and item set info
* @param {string} key The key to get from the object
* @returns An array of item names or an empty array if the key wasn't found
*/
function getItemArray(runesJson, key) {
const itemsmap = freezer.get().itemsinfo;
if (runesJson["stats"][key].build != null) {
return runesJson["stats"][key].build.map((item) => {
try {
return itemsmap[item].name;
} catch (error) {
return "Unknown Item";
}
});
} else return [];
}
/**
* Determines all possible rune pages for a given champion for the specified game mode.
*
Expand Down Expand Up @@ -201,7 +174,7 @@ async function _getPagesAsync(champion, callback) {

// sort rune pages based on the key (name)
const ordered = {};
Object.keys(runePages.pages).sort().forEach(function(key) {
Object.keys(runePages.pages).sort().forEach(function (key) {
ordered[key] = runePages.pages[key];
});
runePages.pages = ordered;
Expand Down Expand Up @@ -241,7 +214,7 @@ async function _getPagesAsync(champion, callback) {

// sort rune pages based on the key (name)
const ordered = {};
Object.keys(runePages.pages).sort().forEach(function(key) {
Object.keys(runePages.pages).sort().forEach(function (key) {
ordered[key] = runePages.pages[key];
});
runePages.pages = ordered;
Expand Down
156 changes: 98 additions & 58 deletions plugins/opgg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,130 @@ const urlModes = 'http://www.op.gg/modes/';
const positions = ["top", 'mid', "jungle", "adc", "support"];
const gameModes = ["aram", "urf"];

function extractSinglePage(html, champion, name,pageIndex, buildIndex, src){
function extractSinglePage(html, champion, name, pageIndex, buildIndex, src) {
const runeIds = [];
const data = extractJSON(html); data["rune_pages"][pageIndex]["builds"][buildIndex]["primary_rune_ids"].forEach((id) => runeIds.push(id));
data["rune_pages"][pageIndex]["builds"][buildIndex]["secondary_rune_ids"].forEach((id) => runeIds.push(id));
data["rune_pages"][pageIndex]["builds"][buildIndex]["stat_mod_ids"].forEach((id) => runeIds.push(id));
return buildPluginObject(name,src,pageIndex, buildIndex,runeIds, champion);
return buildPluginObject(name, src, pageIndex, buildIndex, runeIds, buildItemSet(data), champion);
}

function extractPages(data, champion, position,gameMode,src,callback) {
function extractPages(data, champion, position, gameMode, src, callback) {
let runeIds = [];
if(data["rune_pages"].length){
const limitPages = data["rune_pages"].length < 2? data["rune_pages"].length : 2;
for (let i = 0; i < limitPages; i++) {
for (let j = 0; j < 1; j++) {
try {
data["rune_pages"][i]["builds"][j]["primary_rune_ids"].forEach((id) => runeIds.push(id));
data["rune_pages"][i]["builds"][j]["secondary_rune_ids"].forEach((id) => runeIds.push(id));
data["rune_pages"][i]["builds"][j]["stat_mod_ids"].forEach((id) => runeIds.push(id));
callback(buildPluginObject((gameMode? "["+ toUpper(gameMode) + "]" : "[NORMAL]" ) + champion +" "+ toUpper(position) +" Wins "+ data["rune_pages"][i]["builds"][j]["win"] +" "+ data["meta"]['runePages'].filter((page) => page["id"] ==data["rune_pages"][i]["builds"][j].primary_page_id )[0]["name"],src,i, j,runeIds, champion));
} catch (error) {
callback(undefined);
}
runeIds = [];
if (data["rune_pages"].length) {
for (let i = 0; i < 1; i++) {
for (let j = 0; j < 1; j++) {
try {
data["rune_pages"][i]["builds"][j]["primary_rune_ids"].forEach((id) => runeIds.push(id));
data["rune_pages"][i]["builds"][j]["secondary_rune_ids"].forEach((id) => runeIds.push(id));
data["rune_pages"][i]["builds"][j]["stat_mod_ids"].forEach((id) => runeIds.push(id));
callback(buildPluginObject((gameMode ? "[" + toUpper(gameMode) + "]" : "[NORMAL]") + champion + " " + toUpper(position) + " Wins " + data["rune_pages"][i]["builds"][j]["win"] + " " + data["meta"]['runePages'].filter((page) => page["id"] == data["rune_pages"][i]["builds"][j].primary_page_id)[0]["name"], src, i, j, runeIds, buildItemSet(data), champion));
} catch (error) {
buildItemSet(data)
callback(undefined);
}
runeIds = [];
}
}
} else {
callback(undefined);
}
}

function _getPages(champion, callback) {
function _getPages(champion, callback) {
const runePages = { pages: {} };
positions.forEach((pos) =>{
let url =urlChamp +"/" + champion +"/" + pos + "/build";
request.get(url, (error, response, html) =>{
positions.forEach((pos) => {
let url = urlChamp + "/" + champion + "/" + pos + "/build";
request.get(url, (error, response, html) => {
if (!error && response.statusCode === 200) {
extractPages(extractJSON(html), champion,pos,"",url,page => {
if( page != undefined) runePages.pages[page.name] = page;
callback(runePages);
extractPages(extractJSON(html), champion, pos, "", url, page => {
if (page != undefined) runePages.pages[page.name] = page;
callback(runePages);
})
} else {
callback(runePages);
throw Error('rune page not loaded');
}});
callback(runePages);
throw Error('rune page not loaded');
}
});
});
gameModes.forEach((mode) => {
let url = urlModes +"/"+ mode+"/"+ champion + "/build"
request.get(url, (error, response, html) =>{
let url = urlModes + "/" + mode + "/" + champion + "/build"
request.get(url, (error, response, html) => {
if (!error && response.statusCode === 200) {
extractPages(extractJSON(html), champion,"",mode,url ,page => {
if( page != undefined) runePages.pages[page.name] = page;
callback(runePages);
extractPages(extractJSON(html), champion, "", mode, url, page => {
if (page != undefined) runePages.pages[page.name] = page;
callback(runePages);
})
}else {
callback(runePages);
throw Error('rune page not loaded');
}});
} else {
callback(runePages);
throw Error('rune page not loaded');
}
});
});
}
function extractJSON(html){
let str = html.slice(html.indexOf('>{') + 1, html.indexOf('}</script>') +1)
str = '{ "'+str.slice(str.indexOf('"props'), str.length -1).slice(str.indexOf("__N_SSP"), str.length -1).slice(str.indexOf('pageProps'), str.length - 1);
str = str.substring(0, str.indexOf('}},"page') + 1).slice(str.indexOf(':{')+1, str.length -1);
str = str.substring(0,str.indexOf('"_nextI18Next"') - 1);
function extractJSON(html) {
let str = html.slice(html.indexOf('>{') + 1, html.indexOf('}</script>') + 1)
str = '{ "' + str.slice(str.indexOf('"props'), str.length - 1).slice(str.indexOf("__N_SSP"), str.length - 1).slice(str.indexOf('pageProps'), str.length - 1);
str = str.substring(0, str.indexOf('}},"page') + 1).slice(str.indexOf(':{') + 1, str.length - 1);
str = str.substring(0, str.indexOf('"_nextI18Next"') - 1);
return JSON.parse(str);
}
function buildPluginObject(name, src,pageIndex,buildIndex, runeIds,champion){
function buildPluginObject(name, src, pageIndex, buildIndex, runeIds, itemSetObj, champion) {
return {
name,
selectedPerkIds: runeIds,
bookmark: {
name,
src,
meta: {
pageIndex,
buildIndex,
champion
},
remote: {
name: 'OP.GG',
id: 'opgg'
}
}
}
name,
selectedPerkIds: runeIds,
bookmark: {
name,
src,
meta: {
pageIndex,
buildIndex,
champion
},
remote: {
name: 'OP.GG',
id: 'opgg'
}
},
itemSet: itemSetObj
}
}
function buildItemSet(data) {
let startItemBuild = [];
let coreItemBuild = [];
let bigItemBuild = [];
for (let i = 0; i < (data["starter_items"].length > 2 ? 2 : data["starter_items"].length); i++) {
console.log(data["starter_items"][i])
data["starter_items"][i]["ids"].forEach((itemID) => startItemBuild.push(itemID));
};
for (let i = 0; i < (data["core_items"].length > 4 ? 4 : data["core_items"].length); i++) {
data["core_items"][i]["ids"].forEach((itemID) => coreItemBuild.push(itemID));
};
for (let i = 0; i < (data["last_items"].length > 3 ? 3 : data["last_items"].length); i++) {
data["last_items"][i]["ids"].forEach((itemID) => bigItemBuild.push(itemID))
};
return {
start_items: {
build: uniq(startItemBuild)
},
core_items: {
build: uniq(coreItemBuild),
},
big_items: {
build: uniq(bigItemBuild),
}
}
}
/**
* Takes an array and removes duplicates
* @param {array} a Input array
* @returns an array
*/
function uniq(a) {
var seen = {};
return a.filter(function (item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}
const plugin = {
id: 'opgg',
Expand All @@ -102,7 +142,7 @@ const plugin = {
syncBookmark(bookmark, callback) {
request.get(bookmark.src, (error, response, html) => {
if (!error && response.statusCode == 200) {
callback(extractSinglePage(html, bookmark.meta.champion, bookmark.name , bookmark.meta.pageIndex,bookmark.meta.buildIndex, bookmark.src));
callback(extractSinglePage(html, bookmark.meta.champion, bookmark.name, bookmark.meta.pageIndex, bookmark.meta.buildIndex, bookmark.src));
} else {
throw Error('rune page not loaded');
}
Expand Down
Loading