User:King Dragonhoff/wikia.js

// ******************** Card name arrays ******************** // All Cards (remember to copy this array into the rebuildPool function below when updating) var cardPool = ['Archers','Arrows','Baby Dragon','Balloon','Barbarian Hut','Barbarians','Bomb Tower','Bomber','Bowler','Cannon','Dark Prince','Elixir Collector','Fire Spirits','Fireball','Freeze','Furnace','Giant','Giant Skeleton','Goblin Barrel','Goblin Hut','Goblins','Golem','Graveyard','Guards','Hog Rider','Ice Golem','Ice Spirit','Ice Wizard','Inferno Dragon','Inferno Tower','Knight','Lava Hound','Lightning','Lumberjack','Mega Minion','Miner','Mini P.E.K.K.A.','Minion Horde','Minions','Mirror','Mortar','Musketeer','P.E.K.K.A.','Poison','Prince','Princess','Rage','Rocket','Royal Giant','Skeleton Army','Skeletons','Sparky','Spear Goblins','Tesla','The Log','Three Musketeers','Tombstone','Tornado','Valkyrie','Witch','Wizard','X-Bow','Zap']; // By elixir cost var cardsByCost = [ /* 0 */[],   /* 1 */['Ice Spirit','Skeletons'], /* 2 */['Fire Spirits','Goblins','Ice Golem','Mirror','Rage','Spear Goblins','The Log','Zap'], /* 3 */['Archers','Arrows','Bomber','Cannon','Goblin Barrel','Guards','Ice Wizard','Knight','Mega Minion','Miner','Minions','Princess','Skeleton Army','Tombstone','Tornado'], /* 4 */['Baby Dragon','Dark Prince','Fireball','Freeze','Furnace','Hog Rider','Inferno Dragon','Lumberjack','Mini P.E.K.K.A.','Mortar','Musketeer','Poison','Tesla','Valkyrie'], /* 5 */['Balloon','Barbarians','Bomb Tower','Bowler','Giant','Goblin Hut','Graveyard','Inferno Tower','Minion Horde','Prince','Witch','Wizard'], /* 6 */['Elixir Collector','Giant Skeleton','Lightning','Rocket','Royal Giant','Sparky','X-Bow'], /* 7 */['Barbarian Hut','Lava Hound','P.E.K.K.A.'], /* 8 */['Golem'], /* 9 */['Three Musketeers'] ]; // By rarity var cardsByRarity = { common:['Archers','Arrows','Barbarians','Bomber','Cannon','Fire Spirits','Goblins','Ice Spirit','Knight','Minion Horde','Minions','Mortar','Royal Giant','Skeletons','Spear Goblins','Tesla','Zap'], rare:['Barbarian Hut','Bomb Tower','Elixir Collector','Fireball','Furnace','Giant','Goblin Hut','Hog Rider','Ice Golem','Inferno Tower','Mega Minion','Mini P.E.K.K.A.','Musketeer','Rocket','Three Musketeers','Tombstone','Valkyrie','Wizard'], epic:['Baby Dragon','Balloon','Bowler','Dark Prince','Freeze','Giant Skeleton','Goblin Barrel','Golem','Guards','Lightning','Mirror','P.E.K.K.A.','Poison','Prince','Rage','Skeleton Army','Tornado','Witch','X-Bow'], legendary:['Graveyard','Ice Wizard','Inferno Dragon','Lava Hound','Lumberjack','Miner','Princess','Sparky','The Log'] }; // By type var cardsByType = { troop:{ ground:['Archers','Barbarians','Bomber','Bowler','Dark Prince','Fire Spirits','Giant','Giant Skeleton','Goblins','Golem','Guards','Hog Rider','Ice Golem','Ice Spirit','Ice Wizard','Knight','Lumberjack','Miner','Mini P.E.K.K.A.','Musketeer','P.E.K.K.A.','Prince','Princess','Royal Giant','Skeleton Army','Skeletons','Sparky','Spear Goblins','Three Musketeers','Valkyrie','Witch','Wizard'], air:['Baby Dragon','Balloon','Inferno Dragon','Lava Hound','Mega Minion','Minion Horde','Minions'] },   spell:['Arrows','Fireball','Freeze','Goblin Barrel','Graveyard','Lightning','Mirror','Poison','Rage','Rocket','The Log','Tornado','Zap'], building:['Barbarian Hut','Bomb Tower','Cannon','Elixir Collector','Furnace','Goblin Hut','Inferno Tower','Mortar','Tesla','Tombstone','X-Bow'] }; // By arena var cardsByArena = [ /* 0 */['Archers','Arrows','Baby Dragon','Bomber','Fireball','Giant','Knight','Mini P.E.K.K.A.','Musketeer','Prince','Skeleton Army','Witch'], /* 1 */['Goblin Barrel','Goblin Hut','Goblins','Lightning','Spear Goblins','Valkyrie'], /* 2 */['Balloon','Bomb Tower','Giant Skeleton','Minions','Skeletons','Tombstone'], /* 3 */['Barbarian Hut','Barbarians','Cannon','Rage','Rocket','X-Bow'], /* 4 */['Freeze','Hog Rider','Inferno Dragon','Inferno Tower','Lava Hound','Minion Horde','P.E.K.K.A.','Tesla'], /* 5 */['Fire Spirits','Furnace','Graveyard','Ice Wizard','Mirror','Poison','Wizard','Zap'], /* 6 */['Elixir Collector','Golem','Miner','Mortar','Sparky','The Log','Tornado'], /* 7 */['Dark Prince','Guards','Mega Minion','Princess','Royal Giant','Three Musketeers'], /* 8 */['Bowler','Ice Golem','Ice Spirit','Lumberjack'] ]; // ***************** Other global variables ***************** var generatedDeck = []; // *********************** Functions ************************ function rebuildPool { var x, i;   cardPool = ['Archers','Arrows','Baby Dragon','Balloon','Barbarian Hut','Barbarians','Bomb Tower','Bomber','Bowler','Cannon','Dark Prince','Elixir Collector','Fire Spirits','Fireball','Freeze','Furnace','Giant','Giant Skeleton','Goblin Barrel','Goblin Hut','Goblins','Golem','Graveyard','Guards','Hog Rider','Ice Golem','Ice Spirit','Ice Wizard','Inferno Dragon','Inferno Tower','Knight','Lava Hound','Lightning','Lumberjack','Mega Minion','Miner','Mini P.E.K.K.A.','Minion Horde','Minions','Mirror','Mortar','Musketeer','P.E.K.K.A.','Poison','Prince','Princess','Rage','Rocket','Royal Giant','Skeleton Army','Skeletons','Sparky','Spear Goblins','Tesla','The Log','Three Musketeers','Tombstone','Tornado','Valkyrie','Witch','Wizard','X-Bow','Zap']; // Elixir Cost Restriction for (x in cardsByCost) { if (x < Number($("#dcalc-restrict-cost-min").val) || x > Number($("#dcalc-restrict-cost-max").val)) { for (i in cardsByCost[x]) { if (Number(cardPool.indexOf(cardsByCost[x][i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByCost[x][i]), 1); }           }        }    }    // Arena Restriction for (x in cardsByArena) { if (x > Number($("#dcalc-restrict-arena").val)) { for (i in cardsByArena[x]) { if (Number(cardPool.indexOf(cardsByArena[x][i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByArena[x][i]), 1); }           }        }    }    // Common Restriction if ($("#dcalc-restrict-common").prop("checked") === false) { for (i in cardsByRarity.common) { if (Number(cardPool.indexOf(cardsByRarity.common[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.common[i]), 1); }       }    }    // Rare Restriction if ($("#dcalc-restrict-rare").prop("checked") === false) { for (i in cardsByRarity.rare) { if (Number(cardPool.indexOf(cardsByRarity.rare[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.rare[i]), 1); }       }    }    // Epic Restriction if ($("#dcalc-restrict-epic").prop("checked") === false) { for (i in cardsByRarity.epic) { if (Number(cardPool.indexOf(cardsByRarity.epic[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.epic[i]), 1); }       }    }    // Legendary Restriction if ($("#dcalc-restrict-legendary").prop("checked") === false) { for (i in cardsByRarity.legendary) { if (Number(cardPool.indexOf(cardsByRarity.legendary[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.legendary[i]), 1); }       }    }    // Troop Restriction if ($("#dcalc-restrict-troops").prop("checked") === true && $("#dcalc-restrict-troops-ground").prop("checked") === false) { for (i in cardsByType.troop.ground) { if (Number(cardPool.indexOf(cardsByType.troop.ground[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.ground[i]), 1); }       }    } else if ($("#dcalc-restrict-troops").prop("checked") === true && $("#dcalc-restrict-troops-air").prop("checked") === false) { for (i in cardsByType.troop.air) { if (Number(cardPool.indexOf(cardsByType.troop.air[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.air[i]), 1); }       }    } else if ($("#dcalc-restrict-troops").prop("checked") === false) { for (i in cardsByType.troop.ground) { if (Number(cardPool.indexOf(cardsByType.troop.ground[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.ground[i]), 1); }       }        for (i in cardsByType.troop.air) { if (Number(cardPool.indexOf(cardsByType.troop.air[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.air[i]), 1); }       }    } } // Random Integer Generating Function function getRandomInt(min, max) { return Math.floor(Math.random * (max - min + 1)) + min; } // ********************** Get Started *********************** $(document).ready(function {   // ************************ Form ************************    // Toggle ground and air options if the troop option is changed    $("#deckGenerator").on("change", "#dcalc-restrict-troops", function { if ($(this).prop('checked') === true) { $("#dcalc-restrict-troops-ground, #dcalc-restrict-troops-air").prop("checked", true).prop("disabled", false); } else { $("#dcalc-restrict-troops-ground, #dcalc-restrict-troops-air").prop("checked", false).prop("disabled", true); }   });    // Uncheck troop option if both the ground and air options are unchecked    $("#deckGenerator").on("change", "#dcalc-restrict-troops-ground, #dcalc-restrict-troops-air", function { if ($("#dcalc-restrict-troops-ground").prop("checked") === false && $("#dcalc-restrict-troops-air").prop("checked") === false) { $("#dcalc-restrict-troops").prop("checked", false); $("#deckGenerator #dcalc-restrict-troops").change; }   });    // Min Elixir Cost Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-cost-min", function { // Make sure max cost is not lower than the min cost selected if (Number(this.value) > Number($("#dcalc-restrict-cost-max").val)) { $(this).val($("#dcalc-restrict-cost-max").val); }       var i, x;        if (Number($(this).attr("data-previous-value")) > Number(this.value)) { rebuildPool; } else if (Number($(this).attr("data-previous-value")) < Number(this.value)) { for (x in cardsByCost) { if (x < Number(this.value)) { for (i in cardsByCost[x]) { if (Number(cardPool.indexOf(cardsByCost[x][i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByCost[x][i]), 1); }                   }                }            }        }        $(this).attr("data-previous-value", this.value); });   // Max Elixir Cost Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-cost-max", function { // Make sure max cost is not lower than the min cost selected if (Number(this.value) < Number($("#dcalc-restrict-cost-min").val)) { $(this).val($("#dcalc-restrict-cost-min").val); }       var i, x;        if (Number($(this).attr("data-previous-value")) < Number(this.value)) { rebuildPool; } else if (Number($(this).attr("data-previous-value")) > Number(this.value)) { for (x in cardsByCost) { if (x > Number(this.value)) { for (i in cardsByCost[x]) { if (Number(cardPool.indexOf(cardsByCost[x][i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByCost[x][i]), 1); }                   }                }            }        }        $(this).attr("data-previous-value", this.value); });   // Arena Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-arena", function { var i, x;       if (Number($(this).attr("data-previous-value")) < Number(this.value)) { rebuildPool; } else if (Number($(this).attr("data-previous-value")) > Number(this.value)) { for (x in cardsByArena) { if (x <= Number($(this).attr("data-previous-value")) && x > Number(this.value)) { for (i in cardsByArena[x]) { if (Number(cardPool.indexOf(cardsByArena[x][i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByArena[x][i]), 1); }                   }                }            }        }        $(this).attr("data-previous-value", this.value); });   // Common Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-common", function { var i;       if ($(this).prop("checked") === true) { rebuildPool; } else if ($(this).prop("checked") === false) { for (i in cardsByRarity.common) { if (Number(cardPool.indexOf(cardsByRarity.common[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.common[i]), 1); }           }        }    });    // Rare Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-rare", function { var i;       if ($(this).prop("checked") === true) { rebuildPool; } else if ($(this).prop("checked") === false) { for (i in cardsByRarity.rare) { if (Number(cardPool.indexOf(cardsByRarity.rare[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.rare[i]), 1); }           }        }    });    // Epic Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-epic", function { var i;       if ($(this).prop("checked") === true) { rebuildPool; } else if ($(this).prop("checked") === false) { for (i in cardsByRarity.epic) { if (Number(cardPool.indexOf(cardsByRarity.epic[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.epic[i]), 1); }           }        }    });    // Legendary Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-legendary", function { var i;       if ($(this).prop("checked") === true) { rebuildPool; } else if ($(this).prop("checked") === false) { for (i in cardsByRarity.legendary) { if (Number(cardPool.indexOf(cardsByRarity.legendary[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByRarity.legendary[i]), 1); }           }        }    });    // Troop Inclusion/Exclusion    $("#deckGenerator").on("change", "#dcalc-restrict-troops, #dcalc-restrict-troops-ground, #dcalc-restrict-troops-air", function { var i;       if ($(this).prop("checked") === true) { rebuildPool; } else if ($(this).prop("checked") === false) { if ($("#dcalc-restrict-troops").prop("checked") === true && $("#dcalc-restrict-troops-ground").prop("checked") === false) { for (i in cardsByType.troop.ground) { if (Number(cardPool.indexOf(cardsByType.troop.ground[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.ground[i]), 1); }               }            } else if ($("#dcalc-restrict-troops").prop("checked") === true && $("#dcalc-restrict-troops-air").prop("checked") === false) { for (i in cardsByType.troop.air) { if (Number(cardPool.indexOf(cardsByType.troop.air[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.air[i]), 1); }               }            } else if ($("#dcalc-restrict-troops").prop("checked") === false) { for (i in cardsByType.troop.ground) { if (Number(cardPool.indexOf(cardsByType.troop.ground[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.ground[i]), 1); }               }                for (i in cardsByType.troop.air) { if (Number(cardPool.indexOf(cardsByType.troop.air[i])) > -1) { cardPool.splice(cardPool.indexOf(cardsByType.troop.air[i]), 1); }               }            }        }    });    // Display and color card pool count    $("#deckGenerator").on("change", "select, input", function { $("#dcalc-card-pool").text(cardPool.length); if (Number($("#dcalc-card-pool").text) < 8) { $("#dcalc-card-pool").css("color", "red"); } else if (Number($("#dcalc-card-pool").text) < 16) { $("#dcalc-card-pool").css("color", "gold"); } else { $("#dcalc-card-pool").css("color", "chartreuse"); }   });    $("#deckGenerator input, #deckGenerator select").change;    // Change "Roll" button to say "Re-Roll" after being clicked once    $("#deckGenerator").on("click", "#dcalc-roll-deck", function { $(this).text("Re-Roll"); }); });