User:King Dragonhoff/wikia.js

/*

<<<<<< This script is under construction. >>>>>>

Clash Royale Deck Builder tool
By King Dragonhoff Special thanks to Blaster Niceshot Dependencies - The Statistics section on card articles must have the appropriate ID's.   - id="unit-statistics" should be applied to the div element surrounding the Statistics section. - id="unit-statistics-table" should be applied to the table with the level-dependent statistics. - id="unit-attributes-table" should be applied to the table with the statistics that are the same for all levels of the card. - Remember that an ID must never be used more than once per page. - Cells in the unit-statistics-table must contain a number. If the exact statistic is unknown, enter an estimate along with Template:fact. - The tables on card articles must not contain cells that use the colspan or rowspan attributes. - The header cells in the card tables must remain consistant across the site. (e.g. The cell must remain "Damage per second". It cannot be changed to "DPS"    on a page.) - Template:Card is used to display the card images. - Template:MyBattleDeck has code that allows users to save the deck shown to   one of the save slots on the tool. - MediaWiki:Common.js/CountUp.js is used for the number count up animations. Maintainance Since this tool retrieves the card statistics and attributes from the wiki articles, this script will typically only need to be modified when new cards are added to the game. - Add new cards to the ccalcInputs function (the first function) and the card role objects (the first 2 variables; see User:King Dragonhoff/Card Roles) - Add new cards that spawn units on death to the operations for average hitpoints - Add new damaging spells to the Damaging Spell Counters section code - If the card has any strange exceptions, have the calc check those. - The following templates will need to be updated to account for new cards. - Card - LookupCardRarity - LookupCardMaxLevel (if the max level of a rarity is changed) - Add more error catches - what level of certain troops can the ballistic spells defeat - add way to add Substitution suggestions to input battle deck - refine suggested substitutions - make Substitutions table 2 columns of 4 cards? - add ability to collapse each section of the result - reduce number of global variables

// *********************** Variables and functions ************************* // Card Role objects (visualized on User:King Dragonhoff/Card Roles) var cardRole = { 1:["Giant", "Giant Skeleton", "Golem", "Lava Hound", "P.E.K.K.A."], 2:["Bomber", "Bowler", "Dark Prince", "Sparky", "Valkyrie" ], 3:["Goblin Barrel", "Goblins", "Guards", "Skeleton Army", "Skeletons"], 4:["Barbarians", "Knight", "Miner", "Mini P.E.K.K.A.", "Prince"], 5:["Baby Dragon", "Fire Spirits", "Princess", "Wizard"], 6:["Archers", "Minion Horde", "Minions", "Musketeer", "Spear Goblins", "Three Musketeers"], 7:["Bomb Tower", "Cannon", "Inferno Tower", "Mortar", "Tesla", "X-Bow"], 8:["Barbarian Hut", "Furnace", "Goblin Hut", "Tombstone"], 9:["Balloon", "Hog Rider", "Royal Giant"], 10:["Arrows", "Fireball", "Poison", "The Log", "Zap"], 11:["Lightning", "Rocket"], 12:["Ice Spirit", "Ice Wizard", "Lumberjack", "Witch"], 13:["Elixir Collector", "Freeze", "Mirror", "Rage"] }; var subCards = { 1:["Giant", "Giant Skeleton", "Golem", "Lava Hound", "P.E.K.K.A."], 2:["Bomber", "Bowler", "Dark Prince", "Sparky", "Valkyrie" ], 3:["Goblin Barrel", "Goblins", "Guards", "Knight", "Skeleton Army", "Skeletons"], 4:["Barbarians", "Knight", "Lumberjack", "Miner", "Mini P.E.K.K.A.", "Prince"], 5:["Baby Dragon", "Fire Spirits", "Ice Wizard", "Princess", "Wizard"], 6:["Archers", "Minion Horde", "Minions", "Musketeer", "Spear Goblins", "Three Musketeers"], 7:["Bomb Tower", "Cannon", "Inferno Tower", "Mortar", "Tesla", "X-Bow"], 8:["Barbarian Hut", "Furnace", "Goblin Hut", "Tombstone", "Witch"], 9:["Balloon", "Hog Rider", "Royal Giant"], 10:["Arrows", "Fireball", "Fire Spirits", "Poison", "The Log", "Zap"], 11:["Fireball", "Fire Spirits", "Lightning", "Rocket", "Zap"], 12:["Ice Spirit", "Ice Wizard", "Lumberjack", "Witch"], 13:["Elixir Collector", "Freeze", "Ice Spirit", "Mirror", "Rage"] }; // Variables var lookupResult = {}; var deckCardImages = []; var i; var savedDeck, selectedDeck = "1", retrievedInputString; var dataTimeout, errorMsgSent, saveDeckMsgSent; var elixirTotal, elixirAverage, elixirBar, elixirBarDiff; var hitpointsTotal, hitpointsAverage, hitpointsBar, hitpointsBarDiff, dCount, dCountNoX; var dpsTotal, dpsAverage, dpsBar, dpsBarDiff; var cardTypeTroop, cardTypeSpell, cardTypeBuilding; var dmgTypeSplash, dmgTypeSingle, dmgTypeNone; var targetsNone, targetsBuilding, targetsGround, targetsAny; var standardizedRange, stringRange, standardizedHitSpeed, stringHitSpeed; // Define input elements function ccalcInputs(type,number) { if (type === "Common") { return 'Common Rare Epic Legendary Archers Arrows Barbarians Bomber Cannon Fire Spirits Goblins Ice Spirit Knight Minion Horde <option value="Minions">Minions <option value="Mortar">Mortar <option value="Royal Giant">Royal Giant <option value="Skeletons">Skeletons <option value="Spear Goblins">Spear Goblins <option value="Tesla">Tesla <option value="Zap">Zap  <select id="ccalc-level' + number + '" name="ccalc-level' + number + '" class="ccalc-level-input"> 1  2  3  4  5  6  7  8  9  10  11  12  13  '; } else if (type === "Rare") { return '<select id="ccalc-rarity' + number + '" name="ccalc-rarity' + number + '"class="ccalc-rarity-input"><option value="Common">Common <option value="Rare" selected>Rare <option value="Epic">Epic <option value="Legendary">Legendary <select id="ccalc-card' + number + '" name="ccalc-card' + number + '" class="ccalc-card-input"><option value="Barbarian Hut" selected>Barbarian Hut <option value="Bomb Tower">Bomb Tower <option value="Elixir Collector">Elixir Collector <option value="Fireball">Fireball <option value="Furnace">Furnace <option value="Giant">Giant <option value="Goblin Hut">Goblin Hut <option value="Hog Rider">Hog Rider <option value="Inferno Tower">Inferno Tower <option value="Mini P.E.K.K.A.">Mini P.E.K.K.A. <option value="Musketeer">Musketeer <option value="Rocket">Rocket <option value="Three Musketeers">Three Musketeers <option value="Tombstone">Tombstone <option value="Valkyrie">Valkyrie <option value="Wizard">Wizard  <select id="ccalc-level' + number + '" name="ccalc-level' + number + '" class="ccalc-level-input"> 1  2  3  4  5  6  7  8  9  10  11  '; } else if (type === "Epic") { return '<select id="ccalc-rarity' + number + '" name="ccalc-rarity' + number + '"class="ccalc-rarity-input"><option value="Common">Common <option value="Rare">Rare <option value="Epic" selected>Epic <option value="Legendary">Legendary <select id="ccalc-card' + number + '" name="ccalc-card' + number + '" class="ccalc-card-input"><option value="Baby Dragon" selected>Baby Dragon <option value="Balloon">Balloon <option value="Bowler">Bowler <option value="Dark Prince">Dark Prince <option value="Freeze">Freeze <option value="Giant Skeleton">Giant Skeleton <option value="Goblin Barrel">Goblin Barrel <option value="Golem">Golem <option value="Guards">Guards <option value="Lightning">Lightning <option value="Mirror">Mirror <option value="P.E.K.K.A.">P.E.K.K.A. <option value="Poison">Poison <option value="Prince">Prince <option value="Rage">Rage <option value="Skeleton Army">Skeleton Army <option value="Witch">Witch <option value="X-Bow">X-Bow  <select id="ccalc-level' + number + '" name="ccalc-level' + number + '" class="ccalc-level-input"> 1  2  3  4  5  6  7  8  '; } else if (type === "Legendary") { return '<select id="ccalc-rarity' + number + '" name="ccalc-rarity' + number + '"class="ccalc-rarity-input"><option value="Common">Common <option value="Rare">Rare <option value="Epic">Epic <option value="Legendary" selected>Legendary <select id="ccalc-card' + number + '" name="ccalc-card' + number + '" class="ccalc-card-input"><option value="Ice Wizard" selected>Ice Wizard <option value="Lava Hound">Lava Hound <option value="Lumberjack">Lumberjack <option value="Miner">Miner <option value="Princess">Princess <option value="Sparky">Sparky <option value="The Log">The Log  <select id="ccalc-level' + number + '" name="ccalc-level' + number + '" class="ccalc-level-input"> 1  2  3  4  5  '; } else { return '<select id="ccalc-rarity' + number + '" name="ccalc-rarity' + number + '"class="ccalc-rarity-input"><option value="Common" selected>Common <option value="Rare">Rare <option value="Epic">Epic <option value="Legendary">Legendary <select id="ccalc-card' + number + '" name="ccalc-card' + number + '" class="ccalc-card-input"><option value="Archers" selected>Archers <option value="Arrows">Arrows <option value="Barbarians">Barbarians <option value="Bomber">Bomber <option value="Cannon">Cannon <option value="Fire Spirits">Fire Spirits <option value="Goblins">Goblins <option value="Knight">Knight <option value="Minion Horde">Minion Horde <option value="Minions">Minions <option value="Mortar">Mortar <option value="Royal Giant">Royal Giant <option value="Skeletons">Skeletons <option value="Spear Goblins">Spear Goblins <option value="Tesla">Tesla <option value="Zap">Zap  <select id="ccalc-level' + number + '" name="ccalc-level' + number + '" class="ccalc-level-input"> 1  2  3  4  5  6  7  8  9  10  11  12  13  '; } } // Data Retrieval Function function getPageData(page) { var property = ''; var attribute = ''; // Code that will execute while AJAX request is running clearTimeout(dataTimeout); dataTimeout = setTimeout(function {       alert("It is taking a long time to retrieve the card's statistics. If the form is not re-enabled soon, check your Internet connection and refresh the page. If this does not seem to be the problem, check the card's wiki page to ensure that it has not been vandalized. If you are unable to fix the problem, contact a member of the wiki's staff.");    }, 15000); $('#ccalc-submit').html(' ').prop("disabled", true); $('#ccalc-save').html(' ').prop("disabled", true); $(".ccalc-rarity-input, .ccalc-card-input").prop("disabled", true); // Test if data for this page has already been retrieved if (lookupResult[page]) { // data already exists clearTimeout(dataTimeout); $('#ccalc-submit').html('Submit').prop("disabled", false); $('#ccalc-save').html('Save Deck to Slot ' + selectedDeck + ' ').prop("disabled", false); $(".ccalc-rarity-input, .ccalc-card-input").prop("disabled", false); } else { // data does not exist // AJAX Request $.get('http://clashroyale.wikia.com/index.php?title=' + page) .done(function(statData) {           lookupResult[page] = {};            $('#unit-attributes-table tbody tr:eq(0) th',statData).each(function(b) { attribute = $(this).clone.children.remove.end.text.trim.toLowerCase; lookupResult[page][attribute] = $('td:eq(' + b + ')',$("#unit-attributes-table tbody tr:eq(1)",statData)).text.trim; });           $('#unit-statistics-table tbody tr:eq(0) th',statData).each(function(i) { property = $(this).clone.children.remove.end.text.trim.toLowerCase; if (i > 0) { lookupResult[page][property] = [0]; $('#unit-statistics-table tbody tr',statData).each(function(a) {                       if (a > 0) {                            lookupResult[page][property].push($('td:eq(' + i + ')',$(this)).text.trim.replace(/\D/g, ""));                            if (a === $('#unit-statistics-table tbody tr',statData).length - 1 && i === $('#unit-statistics-table tbody tr:eq(0) th',statData).length - 1) {                                // Code that will execute                                // after AJAX request has finished and                                // data has been added to lookupResult object                                clearTimeout(dataTimeout);                                $('#ccalc-submit').html('Submit').prop("disabled", false);                                $('#ccalc-save').html('Save Deck to Slot ' + selectedDeck + ' ').prop("disabled", false);                                $(".ccalc-rarity-input, .ccalc-card-input").prop("disabled", false); }                       }                    });                }            });        });    } } // Get basic statistics table of a card function getStatisticsSection(page, rowNum) {    // Code that will execute while AJAX request is running    clearTimeout(dataTimeout);    dataTimeout = setTimeout(function { alert("It is taking a long time to retrieve the card's statistics. If the statistics do not appear soon, check your Internet connection and refresh the page. If this does not seem to be the problem, check the card's wiki page to ensure that it has not been vandalized. If you are unable to fix the problem, contact a member of the wiki's staff."); }, 15000);   $('#ccalc-individual-stats-result').html('<center class="fa fa-spinner fa-pulse" style="color: white; font-size: 24px;"> ');    // AJAX Request    $.get('http://clashroyale.wikia.com/index.php?title=' + page)    .done(function(pageContent) { $("div#unit-statistics",pageContent).children.hide.appendTo("#ccalc-individual-stats-result").fadeIn("slow"); $("#ccalc-individual-stats-result h2").remove; if (rowNum < 100) { $("#ccalc-individual-stats #unit-statistics-table tr:nth-of-type(" + rowNum + ")").css("background-color", "#ffffb3"); }       clearTimeout(dataTimeout); $('#ccalc-individual-stats-result center.fa-spinner').remove; }); } // Deck image updating function function refreshDeck(card,index) {   $.getJSON('/api.php?action=parse&text=&format=json', function(n) { var addCardTemplate = n.parse.text['*']; $('#ccalc-deck td.ccalc-card[data-card-index="' + index + '"]').html(addCardTemplate); deckCardImages[index] = addCardTemplate; }); } // Place card image in the Selected Card column of the Substitutions section of the result function substitutionsCardImage(index) {   $('#ccalc-substitutions td.ccalc-subSelected-card[data-card-index="' + index + '"]').html(deckCardImages[index]); } // Get an inline card image and add it to the Similar Cards column of the Substitutions table function getSubstitutionImage(card,row) {    if (card !== cardName(row)) {        $.getJSON('/api.php?action=parse&text=&format=json', function(n) { var retrievedCardImage = n.parse.text['*']; $("#ccalc-substitutions td#similarCards" + row).append(retrievedCardImage); });   } } // Get deck data from a MyBattleDeck display function userDeckData(attribute,cardNum) {    return $(".ccalc-user-deck-data-selected .user-deck-" + attribute + cardNum).text; } // Card name by number function function cardName(number) {    return $("#ccalc-card" + number).val; } // Card rarity by number function function cardRarity(number) {    return $("#ccalc-rarity" + number).val; } // Card level by number function function cardLevel(number) {    return $("#ccalc-level" + number).val; } // Get max card level function maxCardLevel(rarity) {    if (rarity === "Common") {        return 13;    } else if (rarity === "Rare") {        return 11;    } else if (rarity === "Epic") {        return 8;    } else if (rarity === "Legendary") {        return 5;    } else {        return 1;    } } // Error Message function function ccerrorMsg {    if (errorMsgSent !== "sent") { alert("The calculator was unable to retrieve one or more of the cards' statistics. Please check your Internet connection and submit again or refresh the page. If this does not seem to be the problem, check the card's wiki page to ensure that it has not been vandalized. If you are unable to fix the problem, contact a member of the wiki's staff."); }   errorMsgSent = "sent"; } // Card comparison table manipulator function function cardCompareTable(row,cell,mode,input) { var rowPlusOne = Number(row) + 1; var rowNumString = String(rowPlusOne); if (mode == "text") { $("#ccalc-cardComparison tr:nth-of-type(" + rowNumString + ") td:nth-of-type(" + cell + ")").text(input); } else if (mode == "html") { $("#ccalc-cardComparison tr:nth-of-type(" + rowNumString + ") td:nth-of-type(" + cell + ")").html(input); } else if (mode == "get") { return $("#ccalc-cardComparison tr:nth-of-type(" + rowNumString + ") td:nth-of-type(" + cell + ")").text; } else if (mode == "color") { return $("#ccalc-cardComparison tr:nth-of-type(" + rowNumString + ") td:nth-of-type(" + cell + ")").css("background-color", input); } } // Damaging Spell Counters section HTML function function displaySpellCounter(spell,spellLvl,troop,troopLvl) { $.getJSON('/api.php?action=parse&text=&format=json', function(n) {       var retrievedSpellImage = n.parse.text['*'];    }); $.getJSON('/api.php?action=parse&text=&format=json', function(n) {       var retrievedTroopImage = n.parse.text['*'];    }); return ' '; } // ****************************** Get Started ****************************** $(document).ready(function {   // Check for saved deck and place inputs accordingly    $('#ccalc-harness1').html(ccalcInputs("Common","1"));    $('#ccalc-harness2').html(ccalcInputs("Common","2"));    $('#ccalc-harness3').html(ccalcInputs("Common","3"));    $('#ccalc-harness4').html(ccalcInputs("Common","4"));    $('#ccalc-harness5').html(ccalcInputs("Common","5"));    $('#ccalc-harness6').html(ccalcInputs("Common","6"));    $('#ccalc-harness7').html(ccalcInputs("Common","7"));    $('#ccalc-harness8').html(ccalcInputs("Common","8"));    $('#ccalc-card1').val("Archers").attr({"data-fallback-val": "Archers", "data-fallback-rarity": "Common"});    $('#ccalc-card2').val("Arrows").attr({"data-fallback-val": "Arrows", "data-fallback-rarity": "Common"});    $('#ccalc-card3').val("Barbarians").attr({"data-fallback-val": "Barbarians", "data-fallback-rarity": "Common"});    $('#ccalc-card4').val("Bomber").attr({"data-fallback-val": "Bomber", "data-fallback-rarity": "Common"}); $('#ccalc-card5').val("Cannon").attr({"data-fallback-val": "Cannon", "data-fallback-rarity": "Common"}); $('#ccalc-card6').val("Fire Spirits").attr({"data-fallback-val": "Fire Spirits", "data-fallback-rarity": "Common"}); $('#ccalc-card7').val("Goblins").attr({"data-fallback-val": "Goblins", "data-fallback-rarity": "Common"}); $('#ccalc-card8').val("Knight").attr({"data-fallback-val": "Knight", "data-fallback-rarity": "Common"}); // Place form buttons $('#ccalc-submit-harness').html('<button type="submit" id="ccalc-submit">Submit '); $('#ccalc-save-harness').html('<button type="button" id="ccalc-save">Save Deck to Slot '); // Deck selection buttons $("#deck-selection .deck-select-button").click(function {       $(this).siblings.removeClass("selected-deck");        $(this).addClass("selected-deck");        selectedDeck = $(this).text;        $(".displaySelectedDeckNum").text(selectedDeck);    }); $(".displaySelectedDeckNum").text(selectedDeck); // ****************************** Save Inputs... ****************************   // For MyBattleDeck displays: show save buttons $(".ccalc-save-user-deck").click(function {       $(this).siblings(".save-user-deck-buttons").slideToggle;    }); // Save from the tool $('#ccalc-save').click(function {       savedDeck = [            {},            {rarity: cardRarity(1), card: cardName(1), level: cardLevel(1)},            {rarity: cardRarity(2), card: cardName(2), level: cardLevel(2)},            {rarity: cardRarity(3), card: cardName(3), level: cardLevel(3)},            {rarity: cardRarity(4), card: cardName(4), level: cardLevel(4)},            {rarity: cardRarity(5), card: cardName(5), level: cardLevel(5)},            {rarity: cardRarity(6), card: cardName(6), level: cardLevel(6)},            {rarity: cardRarity(7), card: cardName(7), level: cardLevel(7)},            {rarity: cardRarity(8), card: cardName(8), level: cardLevel(8)}        ];        localStorage.setItem("ccalcSavedDeck" + selectedDeck, JSON.stringify(savedDeck));    }); // Save from a MyBattleDeck display $('.deck-save-button').click(function {       $(".ccalc-user-deck-data-selected").removeClass("ccalc-user-deck-data-selected");        $(this).parents(".user-battle-deck").children(".ccalc-user-deck-data").addClass("ccalc-user-deck-data-selected");        // Check that all cards are valid and not duplicated        if (userDeckData("rarity","1") == "invalid" || userDeckData("rarity","2") == "invalid" || userDeckData("rarity","3") == "invalid" || userDeckData("rarity","4") == "invalid" || userDeckData("rarity","5") == "invalid" || userDeckData("rarity","6") == "invalid" || userDeckData("rarity","7") == "invalid" || userDeckData("rarity","8") == "invalid") {            alert("Cannot save! One or more of the cards in this deck is invalid.");        } else if (userDeckData("card","1") == userDeckData("card","2") || userDeckData("card","1") == userDeckData("card","3") || userDeckData("card","1") == userDeckData("card","4") || userDeckData("card","1") == userDeckData("card","5") || userDeckData("card","1") == userDeckData("card","6") || userDeckData("card","1") == userDeckData("card","7") || userDeckData("card","1") == userDeckData("card","8") || userDeckData("card","2") == userDeckData("card","3") || userDeckData("card","2") == userDeckData("card","4") || userDeckData("card","2") == userDeckData("card","5") || userDeckData("card","2") == userDeckData("card","6") || userDeckData("card","2") == userDeckData("card","7") || userDeckData("card","2") == userDeckData("card","8") || userDeckData("card","3") == userDeckData("card","4") || userDeckData("card","3") == userDeckData("card","5") || userDeckData("card","3") == userDeckData("card","6") || userDeckData("card","3") == userDeckData("card","7") || userDeckData("card","3") == userDeckData("card","8") || userDeckData("card","4") == userDeckData("card","5") || userDeckData("card","4") == userDeckData("card","6") || userDeckData("card","4") == userDeckData("card","7") || userDeckData("card","4") == userDeckData("card","8") || userDeckData("card","5") == userDeckData("card","6") || userDeckData("card","5") == userDeckData("card","7") || userDeckData("card","5") == userDeckData("card","8") || userDeckData("card","6") == userDeckData("card","7") || userDeckData("card","6") == userDeckData("card","8") || userDeckData("card","7") == userDeckData("card","8")) { alert("Cannot save! One or more of the cards in this deck is repeated."); } else { selectedDeck = $(this).text; savedDeck = [ {},               {rarity: userDeckData("rarity","1"), card: userDeckData("card","1"), level: userDeckData("level","1")}, {rarity: userDeckData("rarity","2"), card: userDeckData("card","2"), level: userDeckData("level","2")}, {rarity: userDeckData("rarity","3"), card: userDeckData("card","3"), level: userDeckData("level","3")}, {rarity: userDeckData("rarity","4"), card: userDeckData("card","4"), level: userDeckData("level","4")}, {rarity: userDeckData("rarity","5"), card: userDeckData("card","5"), level: userDeckData("level","5")}, {rarity: userDeckData("rarity","6"), card: userDeckData("card","6"), level: userDeckData("level","6")}, {rarity: userDeckData("rarity","7"), card: userDeckData("card","7"), level: userDeckData("level","7")}, {rarity: userDeckData("rarity","8"), card: userDeckData("card","8"), level: userDeckData("level","8")} ];           localStorage.setItem("ccalcSavedDeck" + selectedDeck, JSON.stringify(savedDeck)); alert("This deck has been saved to deck slot " + selectedDeck + " on the Deck Builder Tool."); }   });    // ************************* ...and restore them ****************************    $('#deck-selection .deck-select-button').click(function { if (localStorage.getItem("ccalcSavedDeck" + selectedDeck) !== null) { retrievedInputString = localStorage.getItem("ccalcSavedDeck" + selectedDeck); savedDeck = JSON.parse(retrievedInputString); for (i = 1; i < 9; i++) { $('#ccalc-harness' + String(i)).html(ccalcInputs(savedDeck[i].rarity,String(i))); $('#ccalc-card' + String(i)).val(savedDeck[i].card).attr({"data-fallback-val": savedDeck[i].card, "data-fallback-rarity": savedDeck[i].rarity}); $('#ccalc-level' + String(i)).val(savedDeck[i].level); getPageData(savedDeck[i].card); refreshDeck(savedDeck[i].card,i); }       } else { if (saveDeckMsgSent !== "sent") { alert("You have not saved anything to deck slot " + selectedDeck + ". Change the inputs and click the save button to save a deck."); saveDeckMsgSent = "sent"; }       }    });    // ******************* On rarity change, replace inputs *****************    $('#cardCalculator').on('change', '.ccalc-rarity-input', function { var newRarity = this.value; var cardIndex = $(this).parent.attr("data-card-index"); var oldCard = $("#ccalc-card" + cardIndex).attr("data-fallback-val"); var oldCardRarity = $("#ccalc-card" + cardIndex).attr("data-fallback-rarity"); $(this).parent.html(ccalcInputs(newRarity,cardIndex)); // Perform card change functions as if the card selection was changed var changedInput = $("#ccalc-card" + cardIndex); var newCard = $(changedInput).val; var newCardIndex = cardIndex; // If user selects an already-selected card, swap them. var dupCardIndex = $(".ccalc-card-input[data-fallback-val='" + newCard + "']").parent.attr("data-card-index"); if (dupCardIndex) { refreshDeck(oldCard,dupCardIndex); }       $(".ccalc-card-input[data-fallback-val='" + newCard + "']").parent.html(ccalcInputs(oldCardRarity,dupCardIndex)).children(".ccalc-card-input").val(oldCard).attr({"data-fallback-val": oldCard, "data-fallback-rarity": oldCardRarity}); // Replace fallback value, get data, replace card image $(changedInput).attr({"data-fallback-val": newCard, "data-fallback-rarity": $("#ccalc-rarity" + newCardIndex).val}); getPageData(newCard); refreshDeck(newCard,newCardIndex); });   // ******** On card change, get card statistics and change deck image ********    $('#cardCalculator').on('change', '.ccalc-card-input', function { var changedInput = this; var newCard = this.value; var oldCard = $(this).attr("data-fallback-val"); var oldCardRarity = $(this).attr("data-fallback-rarity"); var newCardIndex = $(this).parent.attr('data-card-index'); // If user selects an already-selected card, swap them. var dupCardIndex = $(".ccalc-card-input[data-fallback-val='" + newCard + "']").parent.attr("data-card-index"); if (dupCardIndex) { refreshDeck(oldCard,dupCardIndex); }       $(".ccalc-card-input[data-fallback-val='" + newCard + "']").parent.html(ccalcInputs(oldCardRarity,dupCardIndex)).children(".ccalc-card-input").val(oldCard).attr({"data-fallback-val": oldCard, "data-fallback-rarity": oldCardRarity}); // Replace fallback value, get data, replace card image $(this).attr({"data-fallback-val": newCard, "data-fallback-rarity": $("#ccalc-rarity" + newCardIndex).val}); getPageData(newCard); refreshDeck(newCard,newCardIndex); });   $('.ccalc-card-input').change;    // ******** Show basic statistics table when card in deck is clicked ********    // For deck    $("#ccalc-deck td.ccalc-card").click(function { var chosenCardNum, chosenCardLvlPlusOne, chosenCard; $("#ccalc-individual-stats-result").children.remove; $("#ccalc-individual-stats").slideDown("slow"); chosenCardNum = $(this).attr("data-card-index"); chosenCardLvlPlusOne = Number(cardLevel(chosenCardNum)) + 1; chosenCard = cardName(chosenCardNum); $("#individual-stats-card").text(chosenCard); getStatisticsSection(chosenCard,chosenCardLvlPlusOne); });   // For substitutions table    $("#ccalc-substitutions .subSimilarCards").on("click", ".cr-display-card", function { var chosenCardName; $("#ccalc-individual-stats-result").children.remove; $("#ccalc-individual-stats").slideDown("slow"); chosenCardName = $(this).attr("data-card-name"); $("#individual-stats-card").text(chosenCardName); getStatisticsSection(chosenCardName,100); // 100 so that no row is highlighted // Fade "Scroll Up" alert in and out $("#ccalc-scrollup-alert").show; $("#ccalc-scrollup-alert").animate({opacity: "1"}, 800); $("#ccalc-scrollup-alert").animate({opacity: "0"}, 800); $("#ccalc-scrollup-alert").animate({opacity: "1"}, 800); $("#ccalc-scrollup-alert").animate({opacity: "0"}, 800, function {            $("#ccalc-scrollup-alert").hide;        }); });   // Close the result window for basic statisitcs    $("#ccalc-individual-stats-hide").click(function { $("#ccalc-individual-stats").slideUp("slow"); });   // ************************* Submit and show results ************************    $("#ccalc-submit").click(function { // Check for card stat objects if ($.isEmptyObject(lookupResult[$("#ccalc-card1").val]) === false && $.isEmptyObject(lookupResult[$("#ccalc-card2").val]) === false && $.isEmptyObject(lookupResult[$("#ccalc-card3").val]) === false && $.isEmptyObject(lookupResult[$("#ccalc-card4").val]) === false && $.isEmptyObject(lookupResult[$("#ccalc-card5").val]) === false && $.isEmptyObject(lookupResult[$("#ccalc-card6").val]) === false && $.isEmptyObject(lookupResult[$("#ccalc-card7").val]) === false && $.isEmptyObject(lookupResult[$("#ccalc-card8").val]) === false) { // Successful Output $("#cardCalculator-result").slideDown("slow"); // Define countUp animations var countUpAverageElixir = new CountUp("ccalc-bar-value-averageElixir", 0, 0, 2, 2); countUpAverageElixir.start; var countUpAverageHitpoints = new CountUp("ccalc-bar-value-averageHitpoints", 0, 0, 2, 2); countUpAverageHitpoints.start; var countUpAverageDPS = new CountUp("ccalc-bar-value-averageDPS", 0, 0, 2, 2); countUpAverageDPS.start; // **************** Bar: Average Elixir Cost ************************* elixirTotal = 0; for (i = 1; i < 9; i++) { if (isNaN(lookupResult[cardName(i)].cost) === true) { lookupResult[cardName(i)].cost = 2; }               elixirTotal += Number(lookupResult[cardName(i)].cost); }           elixirAverage = elixirTotal / 8; elixirBar = (elixirAverage * 100) / 8; // 8 is the max capcity of the bar elixirBarDiff = 100 - elixirBar; $("#ccalc-averageElixir .bar1").css("width", elixirBar + "%"); countUpAverageElixir.update(round(2,elixirAverage)); $("#ccalc-averageElixir .bar2").css("width", elixirBarDiff + "%"); // ********************* Bar: Average Hitpoints *********************** hitpointsTotal = 0; // Sum card hitpoint values for (i = 1; i < 9; i++) { // Check for count value dCount = String(lookupResult[cardName(i)].count); dCountNoX = dCount.replace(/x/g, ""); if (isNaN(dCountNoX) === true || lookupResult[cardName(i)].count === undefined) { lookupResult[cardName(i)].count = 1; } else { lookupResult[cardName(i)].count = Number(dCountNoX); }               // Make sure the card can have hitpoints if (lookupResult[cardName(i)].type == "Troop" || lookupResult[cardName(i)].type == "Building") { // Check for a card that spawns units on death then check for missing value if (cardName(i) == "Golem") { if (lookupResult[cardName(i)]["golem hitpoints"] !== undefined && lookupResult[cardName(i)]["golemite hitpoints"] !== undefined) { hitpointsTotal += Number(lookupResult[cardName(i)]["golem hitpoints"][cardLevel(i)]) + (2 * Number(lookupResult[cardName(i)]["golemite hitpoints"][cardLevel(i)])); lookupResult[cardName(i)].hitpoints = []; lookupResult[cardName(i)].hitpoints[cardLevel(i)] = Number(lookupResult[cardName(i)]["golem hitpoints"][cardLevel(i)]) + (2 * Number(lookupResult[cardName(i)]["golemite hitpoints"][cardLevel(i)])); } else { ccerrorMsg; }                   } else if (cardName(i) == "Lava Hound") { if (lookupResult[cardName(i)]['lava hound hitpoints'] !== undefined && lookupResult[cardName(i)]['lava pup hitpoints'] !== undefined) { hitpointsTotal += Number(lookupResult[cardName(i)]['lava hound hitpoints'][cardLevel(i)]) + (6 * Number(lookupResult[cardName(i)]['lava pup hitpoints'][cardLevel(i)])); lookupResult[cardName(i)].hitpoints = []; lookupResult[cardName(i)].hitpoints[cardLevel(i)] = Number(lookupResult[cardName(i)]['lava hound hitpoints'][cardLevel(i)]) + (6 * Number(lookupResult[cardName(i)]['lava pup hitpoints'][cardLevel(i)])); } else { ccerrorMsg; }                   } else { if (lookupResult[cardName(i)].hitpoints !== undefined) { hitpointsTotal += Number(lookupResult[cardName(i)].hitpoints[cardLevel(i)]) * Number(lookupResult[cardName(i)].count); } else { ccerrorMsg; }                   }                } else { lookupResult[cardName(i)].hitpoints = []; lookupResult[cardName(i)].hitpoints[cardLevel(i)] = 0; }           }            hitpointsAverage = hitpointsTotal / 8; hitpointsBar = (hitpointsAverage * 100) / 4500; // 4500 is the max capacity of the bar hitpointsBarDiff = 100 - hitpointsBar; $("#ccalc-averageHitpoints .bar1").css("width", hitpointsBar + "%"); countUpAverageHitpoints.update(round(2,hitpointsAverage)); $("#ccalc-averageHitpoints .bar2").css("width", hitpointsBarDiff + "%"); // ************************ Bar: Average DPS *********************** dpsTotal = 0; // Sum card DPS values for (i = 1; i < 9; i++) { // (Count value was checked and saved to the object by the average hitpoints loop) // Make sure the card can have DPS if (lookupResult[cardName(i)].type == "Troop" || lookupResult[cardName(i)].type == "Building") { // Check for an abnormal card then check for missing value // Golem and Lava Hound spawn units that do damage if (cardName(i) == "Golem") { if (lookupResult[cardName(i)]["golem damage per second"] !== undefined && lookupResult[cardName(i)]["golemite damage per second"] !== undefined) { dpsTotal += Number(lookupResult[cardName(i)]["golem damage per second"][cardLevel(i)]) + (2 * Number(lookupResult[cardName(i)]["golemite damage per second"][cardLevel(i)])); lookupResult[cardName(i)]['damage per second'] = []; lookupResult[cardName(i)]['damage per second'][cardLevel(i)] = Number(lookupResult[cardName(i)]["golem damage per second"][cardLevel(i)]) + (2 * Number(lookupResult[cardName(i)]["golemite damage per second"][cardLevel(i)])); } else { ccerrorMsg; }                   } else if (cardName(i) == "Lava Hound") { if (lookupResult[cardName(i)]['lava hound damage per second'] !== undefined && lookupResult[cardName(i)]['lava pup damage per second'] !== undefined) { dpsTotal += Number(lookupResult[cardName(i)]['lava hound damage per second'][cardLevel(i)]) + (6 * Number(lookupResult[cardName(i)]['lava pup damage per second'][cardLevel(i)])); lookupResult[cardName(i)]['damage per second'] = []; lookupResult[cardName(i)]['damage per second'][cardLevel(i)] = Number(lookupResult[cardName(i)]['lava hound damage per second'][cardLevel(i)]) + (6 * Number(lookupResult[cardName(i)]['lava pup damage per second'][cardLevel(i)])); } else { ccerrorMsg; }                   // Fire Spirits only do damage on death } else if (cardName(i) == "Fire Spirits") { if (lookupResult['Fire Spirits']['area damage'] !== undefined) { dpsTotal += Number(lookupResult['Fire Spirits']['area damage'][cardLevel(i)]) * Number(lookupResult['Fire Spirits'].count); lookupResult[cardName(i)]['damage per second'] = []; lookupResult[cardName(i)]['damage per second'][cardLevel(i)] = Number(lookupResult['Fire Spirits']['area damage'][cardLevel(i)]) * Number(lookupResult['Fire Spirits'].count); } else { ccerrorMsg; }                   // Ice Spirits only do damage on death } else if (cardName(i) == "Ice Spirit") { if (lookupResult['Ice Spirit']['area damage'] !== undefined) { dpsTotal += Number(lookupResult['Ice Spirit']['area damage'][cardLevel(i)]) * Number(lookupResult['Ice Spirit'].count); lookupResult[cardName(i)]['damage per second'] = []; lookupResult[cardName(i)]['damage per second'][cardLevel(i)] = Number(lookupResult['Ice Spirit']['area damage'][cardLevel(i)]) * Number(lookupResult['Ice Spirit'].count); } else { ccerrorMsg; }                   // Inferno Tower damage charges } else if (cardName(i) == "Inferno Tower") { dpsTotal += 200 + (Number(cardLevel(i)) * 20); lookupResult[cardName(i)]['damage per second'] = []; lookupResult[cardName(i)]['damage per second'][cardLevel(i)] = 200 + (Number(cardLevel(i)) * 20); // Spawner building DPS ignored, Elixir Collector does no damage } else if (cardName(i) == "Barbarian Hut" || cardName(i) == "Goblin Hut" || cardName(i) == "Elixir Collector" || cardName(i) == "Furnace" || cardName(i) == "Tombstone") { dpsTotal += 0; lookupResult[cardName(i)]['damage per second'] = []; lookupResult[cardName(i)]['damage per second'][cardLevel(i)] = 0; } else if (lookupResult[cardName(i)]['damage per second'] !== undefined) { dpsTotal += Number(lookupResult[cardName(i)]['damage per second'][cardLevel(i)]) * Number(lookupResult[cardName(i)].count); } else { ccerrorMsg; }               } else { lookupResult[cardName(i)]['damage per second'] = []; lookupResult[cardName(i)]['damage per second'][cardLevel(i)] = 0; }           }            dpsAverage = dpsTotal / 8; dpsBar = (dpsAverage * 100) / 500; // 500 is the max capacity of the bar dpsBarDiff = 100 - dpsBar; $("#ccalc-averageDPS .bar1").css("width", dpsBar + "%"); countUpAverageDPS.update(round(2,dpsAverage)); $("#ccalc-averageDPS .bar2").css("width", dpsBarDiff + "%"); // ********************** Bar: Card Type ********************** cardTypeTroop = 0; cardTypeSpell = 0; cardTypeBuilding = 0; // Count number of each card type for (i = 1; i < 9; i++) { if (lookupResult[cardName(i)].type === "Troop") { cardTypeTroop++; } else if (lookupResult[cardName(i)].type === "Spell") { cardTypeSpell++; } else { cardTypeBuilding++; }           }            cardTypeTroop *= 12.5; cardTypeSpell *= 12.5; cardTypeBuilding *= 12.5; $("#ccalc-cardType .bar1").css("width", cardTypeTroop + "%").attr("data-bar-fill", cardTypeTroop); $("#ccalc-cardType .bar2").css("width", cardTypeSpell + "%").attr("data-bar-fill", cardTypeSpell); $("#ccalc-cardType .bar3").css("width", cardTypeBuilding + "%").attr("data-bar-fill", cardTypeBuilding); // ********************** Bar: Can Target ********************** targetsAny = 0; targetsGround = 0; targetsBuilding = 0; targetsNone = 0; // Count number of each target type for (i = 1; i < 9; i++) { if (lookupResult[cardName(i)].target === "Air & Ground" || lookupResult[cardName(i)].target === "Air and Ground") { targetsAny++; } else if (lookupResult[cardName(i)].target === "Ground") { targetsGround++; } else if (lookupResult[cardName(i)].target === "Building" || lookupResult[cardName(i)].target === "Buildings") { targetsBuilding++; } else { targetsNone++; }           }            targetsAny *= 12.5; targetsGround *= 12.5; targetsBuilding *= 12.5; targetsNone *= 12.5; $("#ccalc-canTarget .bar1").css("width", targetsAny + "%").attr("data-bar-fill", targetsAny); $("#ccalc-canTarget .bar2").css("width", targetsGround + "%").attr("data-bar-fill", targetsGround); $("#ccalc-canTarget .bar3").css("width", targetsBuilding + "%").attr("data-bar-fill", targetsBuilding); $("#ccalc-canTarget .bar4").css("width", targetsNone + "%").attr("data-bar-fill", targetsNone); // ********************** Bar: Damage Type ********************** dmgTypeSplash = 0; dmgTypeSingle = 0; dmgTypeNone = 0; // Count number of each damage type for (i = 1; i < 9; i++) { if ($.isEmptyObject(lookupResult[cardName(i)]['area damage']) === false) { dmgTypeSplash++; } else if ($.isEmptyObject(lookupResult[cardName(i)].damage) === false) { dmgTypeSingle++; } else { dmgTypeNone++; }           }            dmgTypeSplash *= 12.5; dmgTypeSingle *= 12.5; dmgTypeNone *= 12.5; $("#ccalc-damageType .bar1").css("width", dmgTypeSplash + "%").attr("data-bar-fill", dmgTypeSplash); $("#ccalc-damageType .bar2").css("width", dmgTypeSingle + "%").attr("data-bar-fill", dmgTypeSingle); $("#ccalc-damageType .bar3").css("width", dmgTypeNone + "%").attr("data-bar-fill", dmgTypeNone); // *********** Remove certain bars if they are zero. ************           $("#ccalc-analysis-bars .bar-fully-hide").each(function {                if ($(this).attr("data-bar-fill") === "0") {                    $(this).children.css("opacity", "0");                } else {                    $(this).children.css("opacity", "1");                }            }); // **************************** Card Comparison Table **************************** // Add Card Names and color them for (i = 1; i < 9; i++) { var cardComparisonNameLoop = i + 1; if (cardRarity(i) == "Common") { $("#ccalc-cardComparison tr:nth-of-type(" + cardComparisonNameLoop + ") td:nth-of-type(1)").html(' ' + cardName(i) + ' '); } else if (cardRarity(i) == "Rare") { $("#ccalc-cardComparison tr:nth-of-type(" + cardComparisonNameLoop + ") td:nth-of-type(1)").html(' ' + cardName(i) + ' '); } else if (cardRarity(i) == "Epic") { $("#ccalc-cardComparison tr:nth-of-type(" + cardComparisonNameLoop + ") td:nth-of-type(1)").html(' ' + cardName(i) + ' '); } else if (cardRarity(i) == "Legendary") { $("#ccalc-cardComparison tr:nth-of-type(" + cardComparisonNameLoop + ") td:nth-of-type(1)").html(' ' + cardName(i) + ' '); }           }            // Elixir Cost column var deckElixir = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"2","text",lookupResult[cardName(i)].cost); deckElixir.push(cardCompareTable(i,"2","get")); }           deckElixir.sort(function(a, b){return b-a}); $("#ccalc-cardComparison tr td:nth-of-type(2)").each(function {               if ($(this).text == deckElixir[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckElixir[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckElixir[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckElixir[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckElixir[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckElixir[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckElixir[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckElixir[7]) {$(this).css("background-color", "#45DE68");}            }); // HP column var deckHP = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"4","text",round(0,lookupResult[cardName(i)].hitpoints[cardLevel(i)] * Number(lookupResult[cardName(i)].count))); deckHP.push(cardCompareTable(i,"4","get")); }           deckHP.sort(function(a, b){return a-b}); $("#ccalc-cardComparison tr td:nth-of-type(4)").each(function {               if ($(this).text == deckHP[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckHP[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckHP[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckHP[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckHP[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckHP[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckHP[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckHP[7]) {$(this).css("background-color", "#45DE68");}            }); // HP/Elixir column var deckHPbyCost = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"3","text",round(1,cardCompareTable(i,"4","get") / cardCompareTable(i,"2","get"))); deckHPbyCost.push(cardCompareTable(i,"3","get")); }           deckHPbyCost.sort(function(a, b){return a-b}); $("#ccalc-cardComparison tr td:nth-of-type(3)").each(function {               if ($(this).text == deckHPbyCost[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckHPbyCost[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckHPbyCost[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckHPbyCost[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckHPbyCost[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckHPbyCost[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckHPbyCost[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckHPbyCost[7]) {$(this).css("background-color", "#45DE68");}            }); // DPS column var deckDPS = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"6","text",round(0,lookupResult[cardName(i)]['damage per second'][cardLevel(i)] * Number(lookupResult[cardName(i)].count))); deckDPS.push(cardCompareTable(i,"6","get")); }           deckDPS.sort(function(a, b){return a-b}); $("#ccalc-cardComparison tr td:nth-of-type(6)").each(function {               if ($(this).text == deckDPS[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckDPS[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckDPS[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckDPS[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckDPS[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckDPS[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckDPS[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckDPS[7]) {$(this).css("background-color", "#45DE68");}            }); // DPS/Elixir column var deckDPSbyCost = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"5","text",round(1,cardCompareTable(i,"6","get") / cardCompareTable(i,"2","get"))); deckDPSbyCost.push(cardCompareTable(i,"5","get")); }           deckDPSbyCost.sort(function(a, b){return a-b}); $("#ccalc-cardComparison tr td:nth-of-type(5)").each(function {               if ($(this).text == deckDPSbyCost[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckDPSbyCost[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckDPSbyCost[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckDPSbyCost[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckDPSbyCost[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckDPSbyCost[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckDPSbyCost[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckDPSbyCost[7]) {$(this).css("background-color", "#45DE68");}            }); // Range column /* Remove non-numerical characters and assign range as 0 if it is undefined */ for (i = 1; i < 9; i++) { stringRange = String(lookupResult[cardName(i)].range); standardizedRange = stringRange.replace(/[^0-9$.,]/g, ""); if (isNaN(standardizedRange) === true) { lookupResult[cardName(i)].range = 0; } else { lookupResult[cardName(i)].range = Number(standardizedRange); }           }            /* Set the column */ var deckRange = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"7","text",lookupResult[cardName(i)].range); deckRange.push(cardCompareTable(i,"7","get")); }           deckRange.sort(function(a, b){return a-b}); $("#ccalc-cardComparison tr td:nth-of-type(7)").each(function {               if ($(this).text == deckRange[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckRange[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckRange[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckRange[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckRange[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckRange[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckRange[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckRange[7]) {$(this).css("background-color", "#45DE68");}            }); // Hit Speed column /* Remove non-numerical characters and assign hit speed as 0 if it is undefined */ for (i = 1; i < 9; i++) { stringHitSpeed = String(lookupResult[cardName(i)]['hit speed']); standardizedHitSpeed = stringHitSpeed.replace(/[^0-9$.,]/g, ""); if (isNaN(standardizedHitSpeed) === true) { lookupResult[cardName(i)]['hit speed'] = 0; } else { lookupResult[cardName(i)]['hit speed'] = Number(standardizedHitSpeed); }           }            /* Set the column */ var deckHitSpeed = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"8","text",lookupResult[cardName(i)]['hit speed']); deckHitSpeed.push(cardCompareTable(i,"8","get")); }           deckHitSpeed.sort(function(a, b){return b-a}); $("#ccalc-cardComparison tr td:nth-of-type(8)").each(function {               if ($(this).text == deckHitSpeed[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckHitSpeed[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckHitSpeed[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckHitSpeed[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckHitSpeed[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckHitSpeed[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckHitSpeed[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckHitSpeed[7]) {$(this).css("background-color", "#45DE68");}            }); // Count column var deckCount = []; for (i = 1; i < 9; i++) { cardCompareTable(i,"9","text",lookupResult[cardName(i)].count); deckCount.push(cardCompareTable(i,"9","get")); }           deckCount.sort(function(a, b){return a-b}); $("#ccalc-cardComparison tr td:nth-of-type(9)").each(function {               if ($(this).text == deckCount[1]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckCount[2]) {$(this).css("background-color", "#FFC054");}                if ($(this).text == deckCount[5]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckCount[6]) {$(this).css("background-color", "#B5FF8A");}                if ($(this).text == deckCount[3]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckCount[4]) {$(this).css("background-color", "#F6FF78");}                if ($(this).text == deckCount[0]) {$(this).css("background-color", "#FF9E9E");}                if ($(this).text == deckCount[7]) {$(this).css("background-color", "#45DE68");}            }); // Target column for (i = 1; i < 9; i++) { if (lookupResult[cardName(i)].target == "Air & Ground" || lookupResult[cardName(i)].target == "Air and Ground") { cardCompareTable(i,"10","html"," "); cardCompareTable(i,"10","color","#45DE68"); } else if (lookupResult[cardName(i)].target == "Ground") { cardCompareTable(i,"10","html"," "); cardCompareTable(i,"10","color","#F6FF78"); } else if (lookupResult[cardName(i)].target == "Buildings" || lookupResult[cardName(i)].target == "Building") { cardCompareTable(i,"10","html"," "); cardCompareTable(i,"10","color","#FFC054"); } else { cardCompareTable(i,"10","html"," "); cardCompareTable(i,"10","color","#FF9E9E"); }           }            // Damage Type column for (i = 1; i < 9; i++) { if ($.isEmptyObject(lookupResult[cardName(i)]['area damage']) === false) { cardCompareTable(i,"11","html"," "); cardCompareTable(i,"11","color","#45DE68"); } else if ($.isEmptyObject(lookupResult[cardName(i)].damage) === false) { cardCompareTable(i,"11","html"," "); cardCompareTable(i,"11","color","#F6FF78"); } else { cardCompareTable(i,"11","html"," "); cardCompareTable(i,"11","color","#FF9E9E"); }           }            // ******************************* Substitutions Table ******************************* // Clear table from previous submission $("#ccalc-substitutions td").html(""); // Add selected card images substitutionsCardImage("1"); substitutionsCardImage("2"); substitutionsCardImage("3"); substitutionsCardImage("4"); substitutionsCardImage("5"); substitutionsCardImage("6"); substitutionsCardImage("7"); substitutionsCardImage("8"); // Determine each card's role number and save it to the lookupResult object for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("1")) > -1) { lookupResult[cardName("1")].role = String(i); break; }           }            for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("2")) > -1) { lookupResult[cardName("2")].role = String(i); break; }           }            for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("3")) > -1) { lookupResult[cardName("3")].role = String(i); break; }           }            for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("4")) > -1) { lookupResult[cardName("4")].role = String(i); break; }           }            for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("5")) > -1) { lookupResult[cardName("5")].role = String(i); break; }           }            for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("6")) > -1) { lookupResult[cardName("6")].role = String(i); break; }           }            for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("7")) > -1) { lookupResult[cardName("7")].role = String(i); break; }           }            for (i = 1; i < 14; i++) { if (cardRole[i].indexOf(cardName("8")) > -1) { lookupResult[cardName("8")].role = String(i); break; }           }            // Add similar cards to the Substitutions table based on role number for (i = 0; i < subCards[lookupResult[cardName("1")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("1")].role][i],"1"); }           for (i = 0; i < subCards[lookupResult[cardName("2")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("2")].role][i],"2"); }           for (i = 0; i < subCards[lookupResult[cardName("3")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("3")].role][i],"3"); }           for (i = 0; i < subCards[lookupResult[cardName("4")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("4")].role][i],"4"); }           for (i = 0; i < subCards[lookupResult[cardName("5")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("5")].role][i],"5"); }           for (i = 0; i < subCards[lookupResult[cardName("6")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("6")].role][i],"6"); }           for (i = 0; i < subCards[lookupResult[cardName("7")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("7")].role][i],"7"); }           for (i = 0; i < subCards[lookupResult[cardName("8")].role].length; i++) { getSubstitutionImage(subCards[lookupResult[cardName("8")].role][i],"8"); }           // ******************************* Damaging Spell Counters ******************************* // Get damaging spell data getPageData("Arrows"); getPageData("Fireball"); getPageData("Lightning"); getPageData("Rocket"); getPageData("The Log"); getPageData("Zap"); var x;           // Arrows for (i = 1; i < 9; i++) { if (lookupResult[cardName(i)].type == "Troop" && lookupResult.Arrows["area damage"][1] < lookupResult[cardName(i)].hitpoints[maxCardLevel(cardRarity(i))] && lookupResult.Arrows["area damage"][maxCardLevel("Common")] >= lookupResult[cardName(i)].hitpoints[1]) { for (x = 1; x <= maxCardLevel(cardRarity(i)); x++) { if (lookupResult.Arrows["area damage"][x] >= lookupResult[cardName(i)].hitpoints[cardLevel(i)]) { $("#ccalc-damagingSpellCounters").append(displaySpellCounter("Arrows",x,cardName(i),cardLevel(i))); break; }                   }                }            }        } else { // This is the else of the original "check for objects" if statement. ccerrorMsg; }   }); });