Skip to content

Commit

Permalink
feat(rework): mais valores
Browse files Browse the repository at this point in the history
todo o projeto foi refeito, menos linhas e mais otimizado
agora é possível comprar campeões de todos os valores
  • Loading branch information
controlado committed Jul 15, 2023
1 parent 53072a4 commit 90efa00
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.br.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[![english](https://img.shields.io/badge/-English-blue)](README.md)
[![portuguese](https://img.shields.io/badge/-Português%20Brasileiro-blue)](README.br.md)

Compre todos os campeões que custam 450 de essência! <br>
Esse é o meu segundo projeto utilizando JavaScript :)
Compre todos os campeões pelo seu custo automaticamente! <br>
Clique no botão para mudar o custo selecionado :)

<img src="https://github.com/controlado/buy-champions/assets/71716568/0f808848-5651-4f46-96f7-9f7b14a6b220" width="700" alt="Plugin preview">

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[![english](https://img.shields.io/badge/-English-blue)](README.md)
[![portuguese](https://img.shields.io/badge/-Português%20Brasileiro-blue)](README.br.md)

Buy all champions that cost 450 automatically! <br>
This is my second project using JavaScript :)
Buy all champions by their cost automatically! <br>
Click on the button to change the selected cost :)

<img src="https://github.com/controlado/buy-champions/assets/71716568/0f808848-5651-4f46-96f7-9f7b14a6b220" width="700" alt="Plugin preview">

Expand Down
88 changes: 51 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { addRoutines } from "../controladoUtils";
import { Store } from "./requests";
import { Champion, StoreBase, addRoutines } from "../controladoUtils";

/**
* @author balaclava
Expand All @@ -8,43 +7,58 @@ import { Store } from "./requests";
* @description Buy champions automatically! 🐧
*/

export const plugin = {
"name": "Buy Champions",
"url": "https://github.com/controlado/buy-champions",
"version": "1.0.1",
};
const buttonId = "buy-450-champions-button";

const onMutation = () => {
const frameStore = document.querySelector("#rcp-fe-lol-store-iframe > iframe");
const storeDocument = frameStore?.contentDocument.documentElement;
if (!frameStore || storeDocument.querySelector(`#${buttonId}`)) { return; }

const store = new Store(); // autenticando a loja

// criação do botão para comprar os campeões
const buyChampionButton = document.createElement("lol-uikit-flat-button");
buyChampionButton.id = buttonId;
buyChampionButton.textContent = "450 EA";
buyChampionButton.style.marginRight = "18px";

// callback do botão de compra
buyChampionButton.onclick = async () => {
buyChampionButton.setAttribute("disabled", "true");
try {
const availableChampions = await store.getAvailableChampionsByCost(450);
if (availableChampions.length > 0) { await store.buyChampions(...availableChampions); }
class Store extends StoreBase {
async getAvailableChampions(ipCost, currentCost = 0) {
const response = await this.request("GET", "/storefront/v3/view/champions");
return response.data.catalog
.filter(champion => champion.ip === ipCost && !champion.owned)
.filter(champion => (currentCost += champion.ip) <= response.data.player.ip)
.map(champion => new Champion(champion.itemId, champion.ip));
}
catch (error) { console.error(`${plugin.name}:`, error); }
finally { buyChampionButton.removeAttribute("disabled"); }
};
}

async function setupElements(selector) {
const storeIFrame = document.querySelector("#rcp-fe-lol-store-iframe > iframe");
const storeContainer = storeIFrame?.contentDocument.documentElement?.querySelector(selector);
if (!storeContainer || storeContainer.hasAttribute("buy-button")) { return; }

const store = new Store();
storeContainer.setAttribute("buy-button", "true");
storeContainer.insertBefore(newButton(store), storeContainer.firstChild);
}

// selecionar o local onde os botões vão ser colocados
const navBar = storeDocument.querySelector(".nav.nav-right");
navBar.insertBefore(buyChampionButton, navBar.firstChild);
function newButton(store) {
const costsGenerator = championsCosts();
let currentCost = costsGenerator.next().value;

const button = document.createElement("lol-uikit-flat-button");
button.classList.add("lol-uikit-flat-button-normal", "title-on-hover");
button.ariaLabel = "Buy champions that cost...";
button.textContent = `${currentCost} BE`;
button.onclick = async () => {
button.setAttribute("disabled", "true");
try {
const champions = await store.getAvailableChampions(currentCost);
if (champions.length) { await store.buyChampions(...champions); }
} finally {
button.removeAttribute("disabled");
}
};
button.oncontextmenu = () => {
currentCost = costsGenerator.next().value;
button.textContent = `${currentCost} BE`;
};
return button;
}

function* championsCosts() {
const costs = [450, 1350, 3150, 4800, 6300];
while (true) { // Infinite generator
yield* costs;
}
};

window.addEventListener("load", () => {
console.debug(`${plugin.name}: Report bugs to Balaclava#1912`);
addRoutines(onMutation);
});
addRoutines(() => setupElements("nav.navbar > .nav-right"));
console.debug("buy-champions: Report bugs to Balaclava#1912");
});
37 changes: 0 additions & 37 deletions requests.js

This file was deleted.

0 comments on commit 90efa00

Please sign in to comment.