// ==UserScript==
// @name         Skinrave Rakeback Collector
// @namespace    https://pube.tk
// @version      2025-06-03
// @description  Claims rewards
// @author       Abhorrent_Anger
// @match        https://skinrave.gg/en/rewards
// @icon         https://www.google.com/s2/favicons?sz=64&domain=skinrave.gg
// @grant        none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @require https://cdn.jsdelivr.net/gh/CoeJoder/waitForKeyElements.js@v1.3/waitForKeyElements.js
// @run-at document-end
// ==/UserScript==

const RECHECK_TIMER = 60000;
const INITIAL_TIMER = 2000;
const MAIN_WINDOW_SELECTOR = "div.flex.overflow-y-auto.overflow-x-hidden";
const RAKEBACK_AMOUNT_SELECTOR = 'div[data-testid="rakeback-amount"]';
const TICKET_SELECTOR = 'div[data-testid="reward-ticket-claim-button"] button:not(:disabled)'
const EMPTY_CLAIM = 'Claim 0.00 T';

function runRoutines() {
    collectRakeback();
    collectTickets();
    setTimeout(runRoutines, RECHECK_TIMER);
}

function collectRakeback() {
    $(RAKEBACK_AMOUNT_SELECTOR).each(function () {
        let button = $(this).parent().children('button').eq(0);
        if (button.text() == EMPTY_CLAIM || button.prop('disabled')) {
            console.log('Skipped an empty rakeback');
            return;
        }
        button.click();
        console.log('Attempted to claim a rakeback');
    });
}

function collectTickets() {
    let ticketButton = $(TICKET_SELECTOR);
    if (ticketButton.length == 0) {
        console.log('No tickets to collect');
        return false;
    }
    ticketButton.click();
    console.log('Attempted to collect tickets');
}

waitForKeyElements(MAIN_WINDOW_SELECTOR, () => {
    setTimeout(runRoutines, INITIAL_TIMER);
});