Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Only list players that were removed when relisting
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixSchuSi committed Aug 27, 2021
1 parent b5017f1 commit f7c3f66
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/helpers/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
29 changes: 18 additions & 11 deletions src/widgets/re-list.widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const ReList: FunctionComponent = () => {
};

async function reListButtonClick() {
const players: NodeListOf<HTMLElement> = 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) => {
Expand All @@ -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<string | false> = 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) => {
Expand All @@ -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;
}
}

Expand All @@ -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<string | false>) {
const addPlayersButton: HTMLElement | null = select(Selectors.ADD_PLAYERS);
addPlayersButton?.click();

Expand All @@ -108,9 +111,13 @@ async function listAllPlayers() {
// We wait another 100ms so all buttons are rendered
await sleep(2000);

const setPriceFields: NodeListOf<HTMLElement> = selectAll(Selectors.SET_LISTING_PRICE);
setPriceFields.forEach((setPriceField: HTMLElement) => {
setPriceField.click();
const listPlayerRows: NodeListOf<HTMLElement> = 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);
Expand Down

0 comments on commit f7c3f66

Please sign in to comment.