1

(31 replies, posted in Site Discussion)

Wasn't working for me so I tinkered with it to get it to work. Also moved the popup so it doesn't hide the "Home" button anymore smile

// ==UserScript==
    // @name        deckboxCMC
    // @match       https://deckbox.org/sets/*
    // @description Calculates CMC in deckbox sets
    // @version     0.6.1
    // ==/UserScript==

    // Initialize array and hash to hold mana costs
    var manaCosts = {};
    var totalCosts = [];

    // Iterate through rows to get mana cost totals for each card
    var x = document.getElementsByClassName("mtg_mana");
//     console.log("mtg_mana element count: " + x.length);


    for (i = 0; i < x.length; i++) {
        var re = /mtg_mana_[0-9URGBWCX]{1,2}/i;
        //console.log("This is a mana cost item: " + x[i].outerHTML);
        var row = x[i].parentElement.parentElement.id;
//         console.log("Parent element is " + row);
        //console.log("Card element looks like this: " + document.getElementById(row).innerHTML);
        var cardName = document.getElementById(row).innerHTML.match(/(?:a class="simple" href="https:\/\/deckbox\.org\/mtg\/.*" target="_blank">)(.*)(?:<\/a>)/)[1];
        //console.log("Card name is " + cardName);
        var stripped = x[i].outerHTML.match(re).toString();
        //console.log("This its stripped mana name: " + stripped);
        var strippedMore = stripped.replace("mtg_mana_", "");
        //console.log("Mana cost indicated: " + strippedMore);
        var converted = strippedMore.replace(/[URGBWC]([URGBWC])?/, "1");
         converted = converted.replace(/X/, "0");

        if (!manaCosts[row]) {
//             console.log("Adding new item to manaCosts hash for card " + cardName);
//             console.log("Found mana cost " + converted + " for card " + cardName);
            manaCosts[row] = Number(converted);
        } else {
            manaCosts[row] += Number(converted);
//             console.log("Found mana cost " + converted + " for card " + cardName);
        }
//         console.log("Total mana cost for " + cardName + " is " + manaCosts[row]);
    }

    // Summary of card costs by table row
    console.log("Here's what we found in the main deck: ");
    for (var card in manaCosts) {
        // use hasOwnProperty to filter out keys from the Object.prototype
        if (manaCosts.hasOwnProperty(card)) {
             cardName = document.getElementById(card).innerHTML.match(/(?:a class="simple" href="https:\/\/deckbox\.org\/mtg\/.*" target="_blank">)(.*)(?:<\/a>)/)[1];
            if (document.getElementById(card).innerHTML.indexOf("wishlist") !== -1) {
                //console.log("Deck list has a wishlist.  I think this is YOUR deck.");
                var cardCount = document.getElementById(card).innerHTML.match(/(?:<td class="card_count deck_count.*>(\n)?)([0-9]{1,2})(?:(\n)?<\/td>\n<script>)/);
                cardCount = Number(cardCount[2]);
                //console.log("Raw cardCount is " + cardCount);
                //console.log("I think there are " + cardCount + " copies of " + cardName);
            } else {
                //console.log("Deck list DOESN'T have a wishlist.  I think this is someone elses deck.");
                 cardCount = document.getElementById(card).innerHTML.match(/(?:<td class="card_count">)([0-9]*)(?:<\/td>\n<td class="card_name">)/)[1];
                //console.log("I think there are " + cardCount + " copies of " + cardName);
            }
            if (document.getElementById(card).innerHTML.indexOf("main") !== -1) {
                console.log(cardCount + " copies of " + cardName + " (" + manaCosts[card] + ")");
                for(var i = 0; i < cardCount; i++) {
                    console.log("Adding mana cost " + manaCosts[card] + " for card " + cardName + " to the total.");
                    totalCosts.push(manaCosts[card]);
                }
            } else {
//                 console.log("Card " + cardName + " with value of " + card + " is not in main deck.  Will NOT use to calculate CMC average.");
            }
        }
    }

    // Get average
    var total = 0;
    console.log("Found " + totalCosts.length + " cards in the main deck.");
    for(var i = 0; i < totalCosts.length; i++) {
        //console.log("Adding mana cost number " + i + " to the total.");
        total += totalCosts[i];
        //console.log("Total is now: " + total);
    }
    console.log("Dividing total mana cost of " + total + " by " + totalCosts.length + " total cards to find the average.");
    var avg = total / totalCosts.length;
    console.log("Agerage CMC: " + avg);

    // Create pop up with CMC average
    var box = document.createElement( 'div' );
    box.id = 'myAlertBox';
    box.style.background = 'white';
    box.style.border = '2px solid red';
    box.style.padding = '4px';
    box.style.position = 'absolute';
    box.style.top = '30px';
    box.style.left = '100px';
    box.style.maxWidth = '400px';
    box.textContent = "Avg. CMC: " + avg;
    document.body.appendChild( box );