Skip to content

Commit

Permalink
fix rune page upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rerago authored and Rerago committed May 26, 2021
1 parent b96d588 commit 9ad77f0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 79 deletions.
6 changes: 2 additions & 4 deletions plugins/blitzgg.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const rp = require('request-promise-native');
const {
sortRunes
} = require('./utils');
const { removePerkIds } = require('./utils');

// #region Settings
const supported_modes = [{
Expand Down Expand Up @@ -95,7 +93,7 @@ function getPage(runesJson, champInfo, gameMode) {
const statShards = runesJson["stats"]["most_common_rune_stat_shards"]["build"];

// Determine selected perk ids
const selectedPerkIds = sortRunes(perksData).concat(statShards);
const selectedPerkIds = removePerkIds(perksData).concat(statShards);

// Return rune page
return {
Expand Down
4 changes: 2 additions & 2 deletions plugins/lolalytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const rp = require('request-promise-native');
const { sortRunes } = require('./utils');
const { removePerkIds } = require('./utils');

// #region Settings
const supported_modes = [{
Expand Down Expand Up @@ -86,7 +86,7 @@ function getPage(runesJson, champInfo, gameMode, position, runesMode) {
const perksData = [].concat(perksPriData, perksSecData);

// Determine selected perk ids
const selectedPerkIds = sortRunes(perksData).concat(statShards);
const selectedPerkIds = removePerkIds(perksData).concat(statShards);

// Determine the name of the position
let positionString = gameMode.positions.length <= 1 ? '' : position;
Expand Down
6 changes: 2 additions & 4 deletions plugins/ugg.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const rp = require('request-promise-native');
const {
sortRunes
} = require('./utils');
const { removePerkIds } = require('./utils');

// #region U.GG API consts
const u = {
Expand Down Expand Up @@ -144,7 +142,7 @@ function getPage(runesJson, champInfo, position, gameMode) {
const statShards = runesJsonModed[u.stats.statShards][u.shards.stats].map(str => parseInt(str, 10));

// Determine selected perk ids
const selectedPerkIds = sortRunes(perksData[u.perks.perks]).concat(statShards);
const selectedPerkIds = removePerkIds(perksData[u.perks.perks]).concat(statShards);

// Return rune page
return {
Expand Down
66 changes: 6 additions & 60 deletions plugins/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const rp = require('request-promise-native');
const { groupBy } = require('lodash');

/**
* Gets a JSON object from a URI via 'request-promise-native'.
Expand Down Expand Up @@ -43,67 +42,14 @@ function getPerksMap(usedKeyStr = 'name') {
}

/**
* Sorts the runes as they are in the game.
* Filters the perk ids from a list of runes ids.
* @param {Array} runes - A list of runes ids
* @return {Array} Sorted list of runes
* @return {Array} A list of runes ids without the ids of the perks.
*/
function sortRunes(runes) {
// Index map for later sorting
const indexes = new Map();

// Sorting function that sorts the runes based on the index in the tree
const sortingFunc = (a, b) => indexes.get(a) - indexes.get(b);

// Creates a tree of style id and the matching runes
const tree = freezer.get().runesreforgedinfo.reduce((obj, curr) => {
obj[curr.id] = [].concat(...curr.slots.map(row => row.runes.map(i => i.id)));
return obj;
}, {});

// Creates a list of style ids based on the tree
const styleIds = Object.keys(tree).map(Number);

// Filters style ids from the runes
const filteredRunes = runes.filter(rune => !styleIds.includes(rune));

// Groups the passed runes fit to the respective style id
const groupedRunes = groupBy(filteredRunes, (rune) => {
for (const style of styleIds) {
const runeIndex = tree[style].indexOf(rune);
if (runeIndex !== -1) {
indexes.set(rune, runeIndex);
return style;
}
}
});

// Variables for determining the 'primaryStyleId' and 'secondaryStyleId
let primaryStyleLength = -1;
let primaryStyleId = -1;
let secondaryStyleLength = -1;
let secondaryStyleId = -1;

// Get 'primaryStyleId' and 'secondaryStyleId' based on the number of runes
for (const styleId in groupedRunes) {
if(styleId !== 'undefined'){
if(groupedRunes[styleId].length > primaryStyleLength){
secondaryStyleId = primaryStyleId;
primaryStyleId = styleId;
primaryStyleLength = groupedRunes[styleId].length;
}else if(groupedRunes[styleId].length >= secondaryStyleLength){
secondaryStyleId = styleId;
secondaryStyleLength = groupedRunes[styleId].length;
}
}
}

// Sorts the groups for the respective style
groupedRunes[primaryStyleId].sort(sortingFunc);
groupedRunes[secondaryStyleId].sort(sortingFunc);

// Merges primary and secondary runes in the correct order
return groupedRunes[primaryStyleId].concat(groupedRunes[secondaryStyleId], groupedRunes['undefined'] ?? []);
function removePerkIds(runes) {
const perkIds = freezer.get().runesreforgedinfo.map(runeInfo => runeInfo.id);
return runes.filter(el => !perkIds.includes(el));
}

// Transfer of functions for use in other modules
module.exports = { getStylesMap, getPerksMap, getJson, sortRunes };
module.exports = { getStylesMap, getPerksMap, getJson, removePerkIds };
94 changes: 85 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var settings = require('./settings');
var freezer = require('./state');
const isDev = require('electron-is-dev');
const { groupBy } = require('lodash');

if(settings.get("darktheme") == null){
const { nativeTheme } = require('electron').remote.require('electron')
Expand Down Expand Up @@ -293,8 +294,7 @@ freezer.on('page:upload', (champion, pagename) => {
var state = freezer.get();
console.log("Upload:", pagename);
console.log("State pages", state.current.champ_data.pages);
var page = state.current.champ_data.pages[pagename].toJS();
page.current = true;
var page = prepareRunePage(pagename, state.current.champ_data.pages[pagename].selectedPerkIds);
console.log('upload2', page);

console.log("page.id, page.isEditable", state.connection.page.id, state.connection.page.isEditable);
Expand All @@ -303,12 +303,6 @@ freezer.on('page:upload', (champion, pagename) => {
freezer.get().lastuploadedpage.set({ champion, page: pagename, loading: true });
api.del("/lol-perks/v1/pages/" + freezer.get().connection.page.id).then((res) => {
console.log("api delete current page", res);

// stat shards check
if(!page.selectedPerkIds[6] && !page.selectedPerkIds[7] && !page.selectedPerkIds[8]) {
page.selectedPerkIds = page.selectedPerkIds.concat([5008, 5002, 5003]);
}

api.post("/lol-perks/v1/pages/", page).then((res) => {
if(!res) {
console.log("Error: no response after page upload request.");
Expand Down Expand Up @@ -510,4 +504,86 @@ connector.on('disconnect', () => {
});

// Start listening for the LCU client
connector.start();
connector.start();

// #region Helper
/**
* Creates a rune page based on the rune id and the name as it has to be passed to LoL.
* @param {string} pageName - Name of the rune page
* @param {Array} runes - A list of runes ids
* @return {Array} A rune page as expected by LoL.
*/
function prepareRunePage(pageName, runes) {
// Minimal rune page as LoL expects it as a handover
let runePageMeta = {
name: pageName,
current: true,
primaryStyleId: -1,
selectedPerkIds: [],
subStyleId: -1,
}

// Index map for later sorting
const indexes = new Map();

// Sorting function that sorts the runes based on the index in the tree
const sortingFunc = (a, b) => indexes.get(a) - indexes.get(b);

// Creates a tree of style id and the matching runes
const tree = freezer.get().runesreforgedinfo.reduce((obj, curr) => {
obj[curr.id] = [].concat(...curr.slots.map(row => row.runes.map(i => i.id)));
return obj;
}, {});

// Creates a list of style ids based on the tree
const styleIds = Object.keys(tree).map(Number);

// Filters style ids from the runes
const filteredRunes = runes.filter(rune => !styleIds.includes(rune));

// Groups the passed runes fit to the respective style id
const groupedRunes = groupBy(filteredRunes, (rune) => {
for (const style of styleIds) {
const runeIndex = tree[style].indexOf(rune);
if (runeIndex !== -1) {
indexes.set(rune, runeIndex);
return style;
}
}
});

// Variables for determining the 'primaryStyleId' and 'secondaryStyleId
let primaryStyleLength = -1;
let primaryStyleId = -1;
let secondaryStyleLength = -1;
let secondaryStyleId = -1;

// Get 'primaryStyleId' and 'secondaryStyleId' based on the number of runes
for (const styleId in groupedRunes) {
if (styleId !== 'undefined') {
if (groupedRunes[styleId].length > primaryStyleLength) {
secondaryStyleId = primaryStyleId;
primaryStyleId = styleId;
primaryStyleLength = groupedRunes[styleId].length;
} else if (groupedRunes[styleId].length >= secondaryStyleLength) {
secondaryStyleId = styleId;
secondaryStyleLength = groupedRunes[styleId].length;
}
}
}

// Sorts the groups for the respective style
groupedRunes[primaryStyleId].sort(sortingFunc);
groupedRunes[secondaryStyleId].sort(sortingFunc);

// Merges primary and secondary runes in the correct order
runePageMeta.selectedPerkIds = groupedRunes[primaryStyleId].concat(groupedRunes[secondaryStyleId], groupedRunes['undefined'] ?? []);

// Set StyleIds
runePageMeta.primaryStyleId = parseInt(primaryStyleId);
runePageMeta.subStyleId = parseInt(secondaryStyleId);

// Return rune page
return runePageMeta;
}
// #endregion

0 comments on commit 9ad77f0

Please sign in to comment.