MediaWiki:BreedingCalculator.js

$(document).ready(function {       if (skin == "oasis")   {            	setMenuOptions;   }        });

function setMenuOptions {	var menu1 = document.getElementById("breeddragons_dragon1choices"); var menu2 = document.getElementById("breeddragons_dragon2choices"); var menu3 = document.getElementById("findparents_dragonchoices");

var dragon_names = getDragonNames; for (var i = 0; i < dragon_names.length; i++) {		var dragonName = dragon_names[i].substring(0, 1).toUpperCase + dragon_names[i].substring(1, dragon_names[i].length).toLowerCase; var newOption1 = document.createElement("option"); newOption1.text = dragonName; menu1.add(newOption1); var newOption2 = document.createElement("option"); newOption2.text = dragonName; menu2.add(newOption2);

var newOption3 = document.createElement("option"); newOption3.text = dragonName; menu3.add(newOption3); }

$('form#breeddragons').toggle; $('form#findparents').toggle; }

function printText(text, id) {  var newNode = document.createElement("p"); newNode.innerHTML = text; document.getElementById(id).appendChild(newNode); }

function addTableRow(tableName, rowData, alignData) {	var table = document.getElementById(tableName); var data = rowData.split("@coldelim@"); table.insertRow(-1); var rows = table.rows; for (var i = 0; i < data.length; i++) {		rows[rows.length - 1].insertCell(-1); }	var cells = rows[rows.length - 1].cells; for (var i = 0; i < cells.length; i++) {		cells[i].style.textAlign = alignData[i]; cells[i].innerHTML = data[i]; } }

function clearContentField(name) {	var contentField = document.getElementById(name); while (contentField.hasChildNodes) {		contentField.removeChild(contentField.firstChild); } }

function clearTable(name) {	var table = document.getElementById(name); var rows = table.rows; for (var i = rows.length - 1; i >= 0; i--) {		table.deleteRow(i); }	}

function getMergedList(list1, list2) {		var data = ""; for (var i = 0; i < list1.length; i++) {		data += data == "" ? list1[i] : "_" + list1[i]; }

for (var i = 0; i < list2.length; i++) {		if (!(data.indexOf("_" + list2[i] + "_") > -1 || data.indexOf(list2[i]) > -1 && data.indexOf(list2[i]) == 0 || data.indexOf("_" + list2[i]) > - 1 && data.indexOf("_" + list2[i]) + list2[i].length + 1 == data.length)) {			data += "_" + list2[i]; }	}	return data.split("_"); }

function breedButtonClicked {	getBreedingOutcome; }

function getBreedingOutcome {		var contentSection = "content_breeddragons"; clearContentField(contentSection);

var table_name = "breeddragons_table"; clearTable(table_name); var menu1 = document.getElementById("breeddragons_dragon1choices"); var menu2 = document.getElementById("breeddragons_dragon2choices"); var dragon1Name = menu1.options[menu1.selectedIndex].text; var dragon2Name = menu2.options[menu2.selectedIndex].text; printText(" Breeding " + dragon1Name + " with " + dragon2Name + " can result in: ", contentSection); var possible_offspring = buildBreedListByRules(dragon1Name, dragon2Name); for (var i = 0; i < possible_offspring.length; i++) {		var formattedText = "&nbsp &nbsp &nbsp &nbsp " + possible_offspring[i] + " Dragon&nbsp &nbsp &nbsp &nbsp @coldelim@"; var colors = dragon_data[possible_offspring[i].toLowerCase].colors; for (var j = 0; j < colors.length; j++) {						formattedText += " "; }		var incubation = dragon_data[possible_offspring[i].toLowerCase].incubation.split("_"); formattedText += "@coldelim@&nbsp &nbsp " + ((incubation.length > 1) ? ("" + incubation[0] + " (Display time of " + incubation[1] + ")") : ("" + incubation[0] + ""))+ " "; addTableRow(table_name, formattedText, ["right", "left", "left"]); }

var bottomCoordinate = document.getElementById("content_breeddragons").offsetTop; window.scroll(0, bottomCoordinate) }

function buildBreedListByRules(dragon1Name, dragon2Name) {	var possible_offspring = new Array; var dragon1Type = dragon_data[dragon1Name.toLowerCase].breedtype; var dragon2Type = dragon_data[dragon2Name.toLowerCase].breedtype; var dragon1Colors = dragon_data[dragon1Name.toLowerCase].colors; var dragon2Colors = dragon_data[dragon2Name.toLowerCase].colors; var rules = ""; if (dragon1Type.indexOf("special") > -1 || dragon2Type.indexOf("special") > -1) {		rules = "special"; }	else if (dragon1Type == "basic" && dragon2Type == "basic") {		if (dragon1Colors[0] == dragon2Colors[0]) {			rules = "basic"; }		else {			rules = "hybrid"; }	}	else if (dragon1Type == "hybrid" || dragon2Type == "hybrid") {		rules = "hybrid"; }	if (rules == "hybrid") {		possible_offspring = getValidHybridOutcomes(dragon1Colors, dragon2Colors, true); }	else if (rules == "basic") {		possible_offspring.push(dragon1Name); }	else if (rules == "special") {		if (dragon1Type == "special_diamond" || dragon2Type == "special_diamond") {			if ((dragon1Type == dragon2Type) && dragon1Colors.length == 1 && dragon2Colors.length == 1) {				for (var i = 0; i < pure_outcomes.length; i++) {					var name = pure_outcomes[i].substring(0, 1).toUpperCase + pure_outcomes[i].substring(1, pure_outcomes[i].length).toLowerCase; possible_offspring.push(name); }			}			else {					var colorPool = getMergedList(dragon1Colors, dragon2Colors); if (colorPool.length > 2) {					possible_offspring = getValidHybridOutcomes(dragon1Colors, dragon2Colors, true); }				else {					possible_offspring = getMergedList(getValidBasicOutcomes(colorPool), getValidHybridOutcomes(dragon1Colors, dragon2Colors, false)); }			}		}	}	return possible_offspring; }

function getValidBasicOutcomes(colorPool) {	var possible_offspring = new Array; var basicColors = new Array; for (var i = 0; i < colorPool.length; i++) {		if (!(colorPool[i] == "diamond")) {						basicColors.push(colorPool[i]); }	}	for (var i = 0; i < basicColors.length; i++) {		for (var j = 0; j < pure_outcomes.length; j++) {			if (basicColors[i] == dragon_data[pure_outcomes[j]].colors[0]) {				var name = pure_outcomes[j].substring(0, 1).toUpperCase + pure_outcomes[j].substring(1, pure_outcomes[j].length).toLowerCase; possible_offspring.push(name); break; }		}	}		return possible_offspring; }

function getValidHybridOutcomes(dragon1Colors, dragon2Colors, considerDiamond) {	var possible_offspring = new Array; var colorPool = getMergedList(dragon1Colors, dragon2Colors);

if (considerDiamond && colorPool.length >= 4) {		for (var j = 0; j < diamond_outcomes.length; j++) {			var name = diamond_outcomes[j].substring(0, 1).toUpperCase + diamond_outcomes[j].substring(1, diamond_outcomes[j].length).toLowerCase; possible_offspring.push(name); }	}

for (var i = 0; i < hybrid_outcomes.length; i++) {				if (comboContainsColors(colorPool, dragon_data[hybrid_outcomes[i]].requiredcolors)) {			var name = hybrid_outcomes[i].substring(0, 1).toUpperCase + hybrid_outcomes[i].substring(1, hybrid_outcomes[i].length).toLowerCase; possible_offspring.push(name); }	}		return possible_offspring; }

function findParentsButtonClicked {	getPossibleParents; }

function getPossibleParents {           var contentSection = "content_findparents"; clearContentField(contentSection); var table_name = "findparents_table"; clearTable(table_name); var menu = document.getElementById("findparents_dragonchoices"); var dragonName = menu.options[menu.selectedIndex].text; printText(" &nbsp &nbsp &nbsp &nbspBreeding the " + dragonName + " Dragon is possible by pairing the following parents:", contentSection); var possible_parents = buildParentListByRules(dragonName); for (var i = 0; i < possible_parents.length; i++) {	   var parentNames = possible_parents[i].split(","); var parent1Colors = dragon_data[parentNames[0].toLowerCase].colors; var parent2Colors = dragon_data[parentNames[1].toLowerCase].colors;

var formattedText = "&nbsp &nbsp &nbsp &nbsp " + parentNames[0] + " Dragon&nbsp &nbsp &nbsp &nbsp @coldelim@"; for (var j = 0; j < parent1Colors.length; j++) {		formattedText += " "; }	   formattedText += "@coldelim@&nbsp &nbsp &nbsp &nbsp \& &nbsp &nbsp @coldelim@&nbsp &nbsp " + parentNames[1] + " Dragon&nbsp &nbsp &nbsp &nbsp @coldelim@"; for (var j = 0; j < parent2Colors.length; j++) {		formattedText += " "; }					   addTableRow(table_name, formattedText, ["right", "left", "center", "right", "left"]); }

var bottomCoordinate = document.getElementById("content_findparents").offsetTop; window.scroll(0, bottomCoordinate); }

function buildParentListByRules(dragonName) {       var possible_parents = new Array; var dragonType = dragon_data[dragonName.toLowerCase].breedtype; var dragonColors = dragon_data[dragonName.toLowerCase].colors; var dragonColorReq = dragon_data[dragonName.toLowerCase].requiredcolors; var rules = ""; if (dragonType.indexOf("special") > -1) {		rules = "special"; }	else if (dragonType == "basic") {		if (dragonColorReq.length > 1) {		 			rules = "hybrid"; }		else {			rules = "basic"; }	}	else if (dragonType == "hybrid") {		rules = "hybrid"; }	if (rules == "basic") {		possible_parents.push(dragonName + "," + dragonName); var dragon_names = getDragonNames; var basicColors = [dragonColors, "diamond"]; var diamond_parents = new Array; for (var i = 0; i < diamond_outcomes.length; i++) {			var name = diamond_outcomes[i].substring(0, 1).toUpperCase + diamond_outcomes[i].substring(1, diamond_outcomes[i].length).toLowerCase; diamond_parents.push(dragonName + "," + name); }		for (var i = 0; i < dragon_names.length; i++) {						if (dragon_data[dragon_names[i]].breedtype == "special_diamond" &&			listsMatch(basicColors, dragon_data[dragon_names[i]].colors)) {				var name = dragon_names[i].substring(0, 1).toUpperCase + dragon_names[i].substring(1, dragon_names[i].length).toLowerCase; diamond_parents.push(dragonName + "," + name); }		}		possible_parents = getMergedList(possible_parents, diamond_parents); }	else if (rules == "hybrid") {		possible_parents.push(dragonName + "," + dragonName);

if (dragonColors.length == 1) {							for (var i = 0; i < diamond_outcomes.length; i++) {				var name = diamond_outcomes[i].substring(0, 1).toUpperCase + diamond_outcomes[i].substring(1, diamond_outcomes[i].length).toLowerCase; possible_parents.push(dragonName + "," + name); }		}

possible_parents = getMergedList(possible_parents, getValidParentPairs(dragonColorReq)); }	else if (rules == "special") {		if (dragonType == "special_diamond") {			if (dragonColors.length > 1) {				possible_parents.push(dragonName + "," + dragonName); possible_parents = getMergedList(possible_parents, getValidParentPairs(dragonColorReq)); }								else {								for (var i = 0; i < diamond_outcomes.length; i++) {					for (var j = i; j < diamond_outcomes.length; j++) {					   var name1 = diamond_outcomes[i].substring(0, 1).toUpperCase + diamond_outcomes[i].substring(1, diamond_outcomes[i].length).toLowerCase; var name2 = diamond_outcomes[j].substring(0, 1).toUpperCase + diamond_outcomes[j].substring(1, diamond_outcomes[j].length).toLowerCase; possible_parents.push(name1 + "," + name2); }				}				var four_color_pairs = getNDistinctColorPairs(4); for (var i = 0; i < four_color_pairs.length; i++) {					possible_parents.push(four_color_pairs[i]); }				var named_dragon_pair = new Array; named_dragon_pair.push(dragonName + "," + dragonName); possible_parents = getMergedList(named_dragon_pair, possible_parents); }		}	}	return possible_parents; }

function getValidParentPairs(dragonColorReq) {       var dragon_names = getDragonNames; var possible_parents = new Array; var single_list_traversal_pairs = new Array; var same_color_dragons = new Array; var same_color_pairs = new Array; for (var i = 0; i < dragon_names.length; i++) {		if (listsMatch(dragon_data[dragon_names[i]].colors, dragonColorReq)) {			var name = dragon_names[i].substring(0, 1).toUpperCase + dragon_names[i].substring(1, dragon_names[i].length).toLowerCase; same_color_dragons.push(name); }					for (var j = i; j < dragon_names.length; j++) {						if (comboContainsColors(getMergedList(dragon_data[dragon_names[i]].colors, dragon_data[dragon_names[j]].colors), dragonColorReq)) {								var name1 = dragon_names[i].substring(0, 1).toUpperCase + dragon_names[i].substring(1, dragon_names[i].length).toLowerCase; var name2 = dragon_names[j].substring(0, 1).toUpperCase + dragon_names[j].substring(1, dragon_names[j].length).toLowerCase; single_list_traversal_pairs.push(name1 + "," + name2); }					}	}	for (var i = 0; i < same_color_dragons.length; i++) {		for (var j = i; j < same_color_dragons.length; j++) {			var name1 = same_color_dragons[i].substring(0, 1).toUpperCase + same_color_dragons[i].substring(1, same_color_dragons[i].length).toLowerCase; var name2 = same_color_dragons[j].substring(0, 1).toUpperCase + same_color_dragons[j].substring(1, same_color_dragons[j].length).toLowerCase; same_color_pairs.push(name1 + "," + name2); }	}	possible_parents = getMergedList(same_color_pairs, single_list_traversal_pairs); return possible_parents; }

function comboContainsColors(colorList, requiredColors) {	for (var i = 0; i < requiredColors.length; i++) {		var found = false; for (var j = 0; j < colorList.length; j++) {			if (colorList[j] == requiredColors[i]) {				found = true; break; }		}		if (!found) {			return false; }	}	return true; }

function listsMatch(list1, list2) {	if (list1.length == list2.length) {		var list1Data = ""; for (var i = 0; i < list1.length; i++) {			list1Data += (list1Data == "") ? list1[i] : "_" + list1[i]; }		for (var i = 0; i < list1.length; i++) {			var prefixIndex = list1Data.indexOf(list2[i] + "_"); var postfixIndex = list1Data.indexOf("_" + list2[i]); if (!((list1Data == list2[i]) || (list1Data.indexOf("_" + list2[i] + "_") > -1) || ((prefixIndex > -1) || (prefixIndex == 0)) || ((postfixIndex > -1) || ((postfixIndex + list2[i].length + 1) == list1Data.length)))) {							return false; }		}	}	else {		return false; }	return true; }

function getNDistinctColorPairs(numColors) {       var dragon_names = getDragonNames; var possible_parents = new Array; for (var i = 0; i < dragon_names.length; i++) {		for (var j = i; j < dragon_names.length; j++) {			var dragon1Colors = dragon_data[dragon_names[i]].colors; var dragon2Colors = dragon_data[dragon_names[j]].colors; var colorPool = getMergedList(dragon1Colors, dragon2Colors); if (colorPool.length >= numColors) {				var name1 = dragon_names[i].substring(0, 1).toUpperCase + dragon_names[i].substring(1, dragon_names[i].length).toLowerCase; var name2 = dragon_names[j].substring(0, 1).toUpperCase + dragon_names[j].substring(1, dragon_names[j].length).toLowerCase; possible_parents.push(name1 + "," + name2); }					}	}	return possible_parents; }

function getDragonNames {   var dragon_names = ["air", "athletic", "atlantis", "charm", "coral", "crusader", "diamond", "eagle", "fairy", "familiar", "fire", "firestorm", "forest", "forestfire", "fruitful", "genie", "gold", "infinity", "island", "life", "magic", "mindvolt", "mist", "mistmoth", "parakeet", "planet", "poison", "quetzal", "scorpion", "seabreeze", "serpent", "titan", "trickster", "water", "wild", "wizard"];

return dragon_names; }

dragon_data = { air: { colors: ["yellow"], breedtype: "basic", requiredcolors: ["yellow"], incubation: "2 hours" },		athletic: { colors: ["red", "blue"], breedtype: "hybrid", requiredcolors: ["red", "blue"], incubation: "11 hours" },		atlantis: { colors: ["blue", "green"], breedtype: "hybrid", requiredcolors: ["blue", "green"], incubation: "15 hours" },		charm: { colors: ["red", "purple"], breedtype: "hybrid", requiredcolors: ["red", "purple"], incubation: "16 hours" },		coral: { colors: ["blue", "purple"], breedtype: "hybrid", requiredcolors: ["blue", "purple"], incubation: "10 hours" },		crusader: { colors: ["diamond", "red"], breedtype: "special_diamond", requiredcolors: ["diamond", "red"], incubation: "44 hours_2 days" },		diamond: { colors: ["diamond"], breedtype: "special_diamond", incubation: "44 hours_2 days" },		eagle: { colors: ["yellow", "red"], breedtype: "hybrid", requiredcolors: ["yellow", "red"], incubation: "14 hours" },		fairy: { colors: ["green", "yellow"], breedtype: "hybrid", requiredcolors: ["green", "yellow"], incubation: "8 hours" },		familiar: { colors: ["green", "purple"], breedtype: "hybrid", requiredcolors: ["green", "purple"], incubation: "18 hours" },		fire: { colors: ["red"], breedtype: "basic", requiredcolors: ["red"], incubation: "10 seconds" },		firestorm: { colors: ["red", "yellow"], breedtype: "hybrid", requiredcolors: ["red", "yellow"], incubation: "12 hours" },		forest: { colors: ["green"], breedtype: "basic", requiredcolors: ["green"], incubation: "30 seconds" },			forestfire: { colors: ["red", "green"], breedtype: "hybrid", requiredcolors: ["red", "green"], incubation: "7 hours" },		fruitful: { colors: ["green", "yellow"], breedtype: "hybrid", requiredcolors: ["green", "yellow"], incubation: "10 hours" },		genie: { colors: ["yellow", "purple"], breedtype: "hybrid", requiredcolors: ["yellow", "purple"], incubation: "8 hours" },		gold: { colors: ["diamond", "yellow"], breedtype: "special_diamond", requiredcolors: ["diamond", "yellow"], incubation: "44 hours_2 days" },			infinity: { colors: ["diamond"], breedtype: "special_diamond", incubation: "40 hours_2 days" },		island: { colors: ["blue", "green"], breedtype: "hybrid", requiredcolors: ["blue", "green"], incubation: "20 hours" },		life: { colors: ["green", "red"], breedtype: "hybrid", requiredcolors: ["green", "red"], incubation: "3 hours" },		magic: { colors: ["purple"], breedtype: "basic", requiredcolors: ["red", "blue"], incubation: "20 hours" },		mindvolt: { colors: ["yellow", "blue"], breedtype: "hybrid", requiredcolors: ["yellow", "blue"], incubation: "22 hours" },		mist: { colors: ["blue", "yellow"], breedtype: "hybrid", requiredcolors: ["blue", "yellow"], incubation: "14 hours" },		mistmoth: { colors: ["blue", "purple"], breedtype: "hybrid", requiredcolors: ["blue", "purple"], incubation: "12 hours" },		parakeet: { colors: ["yellow", "green"], breedtype: "hybrid", requiredcolors: ["yellow", "green"], incubation: "5 hours" },		planet: { colors: ["red", "purple"], breedtype: "hybrid", requiredcolors: ["red", "purple"], incubation: "19 hours" },		poison: { colors: ["green", "purple"], breedtype: "hybrid", requiredcolors: ["green", "purple"], incubation: "21 hours" },		quetzal: { colors: ["diamond", "green"], breedtype: "special_diamond", requiredcolors: ["diamond", "green"], incubation: "32 hours_2 days" },		scorpion: { colors: ["red", "yellow"], breedtype: "hybrid", requiredcolors: ["red", "yellow"], incubation: "13 hours" },		seabreeze: { colors: ["yellow", "blue"], breedtype: "hybrid", requiredcolors: ["yellow", "blue"], incubation: "9 hours" },		serpent: { colors: ["blue", "green"], breedtype: "hybrid", requiredcolors: ["blue", "green"], incubation: "16 hours" },		titan: { colors: ["red", "purple"], breedtype: "hybrid", requiredcolors: ["red", "purple"], incubation: "32 hours_2 days" },		trickster: { colors: ["purple", "yellow"], breedtype: "hybrid", requiredcolors: ["purple", "yellow"], incubation: "18 hours" },			water: { colors: ["blue"], breedtype: "basic", requiredcolors: ["blue"], incubation: "12 hours" },			wild: { colors: ["green", "red"], breedtype: "hybrid", requiredcolors: ["green", "red"], incubation: "6 hours" },		wizard: { colors: ["blue", "purple"], breedtype: "hybrid", requiredcolors: ["blue", "purple"], incubation: "22 hours" }	};

pure_outcomes = ["air", "diamond", "fire", "forest", "infinity", "magic", "water"];

hybrid_outcomes = ["athletic", "atlantis", "charm", "coral", "crusader", "eagle", "fairy", "familiar", "firestorm", "forestfire", "fruitful", "genie", "gold", "island", "life", "magic", "mindvolt", "mist", "mistmoth", "parakeet", "planet", "poison", "quetzal", "scorpion", "seabreeze", "serpent", "titan", "trickster", "wild", "wizard"];

diamond_outcomes = ["diamond", "infinity"];

dragonPageSource = "http://dragon-story.wikia.com/wiki/"; colorImagePageSource = { red: "http://images.wikia.com/dragon-story/images/9/9a/Red30px.png", green: "http://images.wikia.com/dragon-story/images/3/3d/Green30px.png", yellow: "http://images.wikia.com/dragon-story/images/7/70/Yellow30px.png", blue: "http://images.wikia.com/dragon-story/images/f/f1/Blue30px.png", purple: "http://images.wikia.com/dragon-story/images/3/3c/Purple30px.png", diamond: "http://images.wikia.com/dragon-story/images/1/14/Diamond30px.png" };