Re: Feature Request - Average CMC

Awesome.  Can you edit your first post to include that version of the script.  Use the code button above the post edit box (https://deckbox.org/forum/extensions/pun_deckbox_bbcode/buttons/Oxygen/code.png) to format the code correctly.

Re: Feature Request - Average CMC

cybrosis wrote:

Awesome.  Can you edit your first post to include that version of the script.  Use the code button above the post edit box (https://deckbox.org/forum/extensions/pun_deckbox_bbcode/buttons/Oxygen/code.png) to format the code correctly.


Done.

Re: Feature Request - Average CMC

I need to make a fix here for Phyrexian mana.  Will do so this weekend.

Re: Feature Request - Average CMC

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 );

Re: Feature Request - Average CMC

I was going to make a new post a request for Automatic CMC but seems that we've got some interest here. So ditto for me on implementing something like this so I don't have to count symbols manually.

PROFILETradelistWishlist

Re: Feature Request - Average CMC

Hello, I edited the script a bit more to place the average CMC under the mana curve chart and make it look more like it's part of the website. Also commented out the console log and did some general tidying up of the formatting.

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

// Function to append a new DOM element after another
function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

// 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 element with average CMC
var ElCMC = document.createElement('div');
ElCMC.id = 'avg_cmc';
ElCMC.style.textAlign = 'center';
ElCMC.style.fontSize = '10px';
ElCMC.style.color = '#484848';
ElCMC.textContent = "Average CMC: " + avg.toFixed(2);

// Insert the element after the mana curve chart
var ref = document.querySelector('#chart_curve');
insertAfter(ElCMC, ref);

Re: Feature Request - Average CMC

LucasVon wrote:

Hello, I edited the script a bit more to place the average CMC under the mana curve chart and make it look more like it's part of the website. Also commented out the console log and did some general tidying up of the formatting.

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

// Function to append a new DOM element after another
function insertAfter(newNode, referenceNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

// 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 element with average CMC
var ElCMC = document.createElement('div');
ElCMC.id = 'avg_cmc';
ElCMC.style.textAlign = 'center';
ElCMC.style.fontSize = '10px';
ElCMC.style.color = '#484848';
ElCMC.textContent = "Average CMC: " + avg.toFixed(2);

// Insert the element after the mana curve chart
var ref = document.querySelector('#chart_curve');
insertAfter(ElCMC, ref);

Awesome, thanks!

Last edited by tjdrake719 (2019-05-20 23:35:33)