Padel Net Cost Calculator

Calculate and display net cost of padel matches in the header

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Padel Net Cost Calculator
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Calculate and display net cost of padel matches in the header
// @author       You
// @match        *://*.ballejaune.com/account*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to calculate net cost from reservation history
    function calculateNetCost() {
        const rows = document.querySelectorAll('tr[data-rowid]');
        let valid_reservations = 0
        let friends_paied_for = 0
        rows.forEach(row => {
            const badge = row.querySelector('div.badge');
            const originTd = row.querySelector('td:nth-child(3)');
            if (badge && originTd) {
                const text = badge.textContent.trim();
                const match = text.match(/([+-]\s*\d+)/);
                if (match) {
                    const K_str = match[1].replace(/\s+/g, ''); // e.g., "- 1" -> "-1"
                    const K = parseInt(K_str);
                    if (!isNaN(K) && K !== 100) { // Exclude initial +100 purchase
                        const originText = originTd.innerHTML.trim();
                        // Reservation for yourself
                        if (originText.includes('Réservation') && K < 0) {
                            valid_reservations++
                        }
                        // Cancellation
                        else if (originText.includes('Réservation') && K > 0) {
                            valid_reservations--
                        }
                        // Paying for friends
                        else if (originText.includes('Club')) {
                            friends_paied_for -= K
                        }
                    }
                }
            }
        });
        const totalNetCost = valid_reservations * 15 - friends_paied_for * 5
        return totalNetCost

    }

    // Function to inject net cost into the header
    function injectNetCost(netCost) {
        const menu = document.querySelector('ul#widget-menu');
        if (menu) {
            const net_cost_item_refreach = document.querySelector('#widget-netcost span');
            if (net_cost_item_refreach){
                net_cost_item_refreach.textContent = `Net Cost: ${netCost} dt`;
            }else{
                const netCostItem = document.createElement('li');
                netCostItem.className = 'navbar-widget-nav-line';
                netCostItem.id = 'widget-netcost';

                const netCostLink = document.createElement('a');
                netCostLink.className = 'navbar-widget-nav-link';
                netCostLink.href = '#'; // Non-clickable link
                netCostLink.setAttribute('data-container', '#main-tooltips');
                netCostLink.setAttribute('data-placement', 'bottom');
                netCostLink.setAttribute('data-delay', '500');
                netCostLink.setAttribute('title', 'Net Cost');
                netCostLink.style.color = '#fff'; // Match navbar text color
                netCostLink.style.cursor = 'default'; // Indicate non-interactive

                const netCostText = document.createElement('span');
                netCostText.textContent = `Net Cost: ${netCost} dt`;

                netCostLink.appendChild(netCostText);
                netCostItem.appendChild(netCostLink);
                menu.insertBefore(netCostItem, menu.firstChild);
            }
            console.log(netCost);
        } else {
            console.error('Widget menu not found!');
        }
    }

    // Wait for the page to fully load
    window.addEventListener('load', function() {
        setInterval(()=>{
        const netCost = calculateNetCost();
        injectNetCost(netCost)}, 5000)
    });
})();