diff --git a/package.json b/package.json index 0ded697..98d01e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "better-kickbase", - "version": "2.1.2", + "version": "2.1.3", "description": "Sei ein effizienterer Manager mit 'better-kickbase'", "main": "index.js", "scripts": { diff --git a/src/helpers/selectors.ts b/src/helpers/selectors.ts index 0cdbee5..2a4902c 100644 --- a/src/helpers/selectors.ts +++ b/src/helpers/selectors.ts @@ -7,6 +7,7 @@ export enum Selectors { REMOVE_PLAYER = '.offerWidget>.cancelButton', ADD_PLAYERS = '#pageContentWrapper > div > div.TransferMarket.inner > div.leftContainer > div > div > div > div.statusBar > div > button', SET_LISTING_PRICE = '.offerWidget > .sellPlayerButton', + LIST_PLAYER_ROW = '.players > .playerGroup', LIST_PLAYERS = '.buttonContainer > .btn.highlighted', LASTNAME = '.playerName>.lastName', FIRSTNAME = '.playerName>.firstName', diff --git a/src/widgets/re-list.widget.tsx b/src/widgets/re-list.widget.tsx index a0d2a77..3e624ab 100644 --- a/src/widgets/re-list.widget.tsx +++ b/src/widgets/re-list.widget.tsx @@ -35,7 +35,7 @@ export const ReList: FunctionComponent = () => { }; async function reListButtonClick() { - const players: NodeListOf = selectAll(Selectors.ALL_PLAYERS); + const players: HTMLElement[] = Array.from(selectAll(Selectors.ALL_PLAYERS)); const settings: Setting[] = await settingsService.get(); const relistSettings: Setting = settings.find((setting: Setting) => { @@ -45,18 +45,19 @@ async function reListButtonClick() { const relistThreshold: number = relistSettings.childOption?.value as number; console.log(relistThreshold); - const x: { removedCount: number } = { removedCount: 0 }; // Take a player off the market when the offer is below // the market value or when the offer is expired - players.forEach((p: HTMLElement) => removePlayerForRelist(p, relistThreshold, x)); + const removedPlayers: Array = players.map((p: HTMLElement) => + removePlayerForRelist(p, relistThreshold) + ); - if (x.removedCount > 0) { - listAllPlayers(); + if (removedPlayers.length > 0) { + listRemovedPlayers(removedPlayers); } } -function removePlayerForRelist(player: HTMLElement, relistThreshold: number, x: { removedCount: number }) { +function removePlayerForRelist(player: HTMLElement, relistThreshold: number) { const selectors: Selectors[] = [Selectors.OFFER, Selectors.MARKET_VALUE]; const [offer, marketValue]: number[] = selectors.map((selector: Selectors) => { @@ -81,7 +82,9 @@ function removePlayerForRelist(player: HTMLElement, relistThreshold: number, x: // Take a player off the market const removePlayerButton: HTMLElement | null = player.querySelector(Selectors.REMOVE_PLAYER); removePlayerButton?.click(); - x.removedCount = x.removedCount + 1; + return player.id; + } else { + return false; } } @@ -98,7 +101,7 @@ function isOfferTooLow(offer: number, marketValue: number, threshold: number): b return marketValue * (1 + threshold / 100) > offer; } -async function listAllPlayers() { +async function listRemovedPlayers(removedPlayers: Array) { const addPlayersButton: HTMLElement | null = select(Selectors.ADD_PLAYERS); addPlayersButton?.click(); @@ -108,9 +111,13 @@ async function listAllPlayers() { // We wait another 100ms so all buttons are rendered await sleep(2000); - const setPriceFields: NodeListOf = selectAll(Selectors.SET_LISTING_PRICE); - setPriceFields.forEach((setPriceField: HTMLElement) => { - setPriceField.click(); + const listPlayerRows: NodeListOf = selectAll(Selectors.LIST_PLAYER_ROW); + listPlayerRows.forEach((listPlayerRow: HTMLElement) => { + const isPlayerToRelist: boolean = !!removedPlayers.find((player: string | false) => player === listPlayerRow.id); + if (isPlayerToRelist) { + const setPriceField: HTMLElement = select(Selectors.SET_LISTING_PRICE, listPlayerRow)!; + setPriceField.click(); + } }); const listPlayersButton: HTMLElement | null = select(Selectors.LIST_PLAYERS);