User:King Dragonhoff/wikia.js

// Variables var lookupResult = {}; // Define input elements function ccalcInputs(type,number) { if (type === "Common") { return 'Common Rare Epic Legendary Archers Arrows Barbarians Bomber Cannon Fire Spirits Goblins Knight Minion Horde 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="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="Miner">Miner <option value="Princess">Princess <option value="Sparky">Sparky  <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 = ''; // Code that will execute while AJAX request is running $('#ccalc-submit').html(' ').prop("disabled", true); // Test if data for this page has already been retrieved if (lookupResult[page]) { // data already exists $('#ccalc-submit').html('Submit').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-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                                $('#ccalc-submit').html('Submit').prop("disabled", false);                            }                        }                    }); }           });        });    } } // Get Started $(document).ready(function {   // ********** Build Form **********    // Place inputs    $('#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"));    // Set default card inputs and initial fallback values    $('#ccalc-card1').val("Archers").attr("data-fallback-val", "Archers");    $('#ccalc-card2').val("Arrows").attr("data-fallback-val", "Arrows");    $('#ccalc-card3').val("Barbarians").attr("data-fallback-val", "Barbarians");    $('#ccalc-card4').val("Bomber").attr("data-fallback-val", "Bomber"); $('#ccalc-card5').val("Cannon").attr("data-fallback-val", "Cannon"); $('#ccalc-card6').val("Fire Spirits").attr("data-fallback-val", "Fire Spirits"); $('#ccalc-card7').val("Goblins").attr("data-fallback-val", "Goblins"); $('#ccalc-card8').val("Knight").attr("data-fallback-val", "Knight"); // Place Submit button $('#ccalc-submit-harness').html('<button type="submit" id="ccalc-submit">Submit '); // On rarity change, replace inputs $('#cardCalculator').on('change', '.ccalc-rarity-input', function {       var newRarity = this.value;        var cardIndex = $(this).parent.attr("data-card-index");        $(this).parent.html(ccalcInputs(newRarity,cardIndex));        $('.ccalc-card-input').change;    }); // On card change, get card statistics and change deck image $('#cardCalculator').on('change', '.ccalc-card-input', function {       var newCard = this.value;        var newCardIndex = $(this).parent.attr('data-card-index');        getPageData(newCard);        $.getJSON('/api.php?action=parse&text=&format=json', function(n) { var addCardTemplate = n.parse.text['*']; $('td.ccalc-card[data-card-index="' + newCardIndex + '"]').html(addCardTemplate); });   });    $('.ccalc-card-input').change; // Submit and show results $("#ccalc-submit").click(function {       $("#unit-stats-1").text(lookupResult[$('#ccalc-card1').val]['hitpoints'][$('#ccalc-level1').val]);        $("#unit-stats-2").text(lookupResult[$('#ccalc-card2').val]['hitpoints'][$('#ccalc-level2').val]);    }); });