Skip to content

Commit

Permalink
feat: buyChampions() adicionado a Store
Browse files Browse the repository at this point in the history
  • Loading branch information
controlado committed Jul 5, 2023
1 parent 33cca27 commit 53072a4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const onMutation = () => {
buyChampionButton.setAttribute("disabled", "true");
try {
const availableChampions = await store.getAvailableChampionsByCost(450);
if (availableChampions.length > 0) { await store.buyChampions(availableChampions); }
if (availableChampions.length > 0) { await store.buyChampions(...availableChampions); }
}
catch (error) { console.error(`${plugin.name}:`, error); }
finally { buyChampionButton.removeAttribute("disabled"); }
Expand Down
30 changes: 3 additions & 27 deletions requests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StoreBase } from "../controladoUtils";
import { Champion, StoreBase } from "../controladoUtils";

/**
* @author balaclava
Expand All @@ -8,46 +8,22 @@ import { StoreBase } from "../controladoUtils";
*/

export class Store extends StoreBase {
/**
* Compra os campeões que da array recebida.
*
* @async
* @function
* @summary Deve ser chamada após a conclusão do {@link auth}.
* @param {Object[]} champions - Objetos devem possuir `itemId` e `ip`.
* @return {Promise<Response>} Resposta da solicitação.
*/
async buyChampions(champions) {
const items = champions.map(
champion => (
{
"inventoryType": "CHAMPION",
"itemId": champion.itemId,
"ipCost": champion.ip,
"quantity": 1,
}
),
);
const requestBody = { "accountId": this.summoner.accountId, "items": items };
return await this.request("POST", "/storefront/v3/purchase", requestBody);
}

/**
* Retorna os campeões disponíveis na loja, que possuem o custo recebido.
*
* @async
* @function
* @summary Deve ser chamada após a conclusão do {@link auth}.
* @param {number} cost - Custo dos campeões que devem ser retornados.
* @return {Promise<Object[]>} Array de campeões disponíveis.
* @return {Promise<Champion[]>} Array de campeões disponíveis.
*/
async getAvailableChampionsByCost(cost) {
const playerChampions = await this.getAvailableChampions();
const availableChampions = [];

for (const champion of playerChampions.catalog) {
if (!champion.owned && champion.ip == cost) {
availableChampions.push(champion);
availableChampions.push(new Champion(champion.itemId, champion.ip));
}
}

Expand Down

0 comments on commit 53072a4

Please sign in to comment.