MediaWiki:BreedingCalculator.js

$(document).ready(function {       if (skin == "oasis")   {                  var data = document.getElementById("breedingcalculatordata"); // Required by Internet Explorer        eval(data.innerHTML);  	setMenuOptions;   }        });

function setMenuOptions {	var menu1 = document.getElementById("breeddragons_dragon1choices"); var menu2 = document.getElementById("breeddragons_dragon2choices"); var menu3 = document.getElementById("findparents_dragonchoices"); for (var i = 0; i < dragon_ids.length; i++) {		var dragonName = dragon_data[dragon_ids[i].toLowerCase].name; 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 addTableHeader(tableName, height, color, rowData, alignData, spanData) {	var table = document.getElementById(tableName); var data = rowData.split("@coldelim@"); var header = ""; for (var i = 0; i < data.length; i++) {		header += "" + data[i] + " ";	}	header += " ";	return header; }

function addTableRow(tableName, rowData, alignData) {	var table = document.getElementById(tableName); var data = rowData.split("@coldelim@"); var row = " "; for (var i = 0; i < data.length; i++) {		row += "" + data[i] + " " }	row += " ";

return row; }

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

function clearTable(name) {	var table = document.getElementById(name); table.innerHTML = ""; }

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 = dragon_data[dragon_ids[menu1.selectedIndex]].name; var dragon2Name = dragon_data[dragon_ids[menu2.selectedIndex]].name; printText(" Breeding " + dragon1Name + " with " + dragon2Name + " can result in: ", contentSection); var possible_offspring = sortListByRarity(buildBreedListByRules(dragon_ids[menu1.selectedIndex].toLowerCase, dragon_ids[menu2.selectedIndex].toLowerCase)); var row_format = ""; for (var i = 0; i < possible_offspring.length; i++) {				var offspring_name = dragon_data[possible_offspring[i]].name; var formattedText = "&nbsp &nbsp " + offspring_name + " Dragon&nbsp &nbsp @coldelim@&nbsp &nbsp "; var colors = dragon_data[possible_offspring[i].toLowerCase].colors; for (var j = 0; j < colors.length; j++) {						formattedText += " "; }		var rarity_index = parseInt(dragon_data[possible_offspring[i].toLowerCase].rarity, 10) - 1; formattedText += "&nbsp &nbsp @coldelim@&nbsp &nbsp " + rarities[rarity_index] + "&nbsp &nbsp "; var incubation = dragon_data[possible_offspring[i].toLowerCase].incubation.split("_"); formattedText += "@coldelim@&nbsp &nbsp " + incubation[0] + "" + "@coldelim@ " + incubation[1] + "" + "@coldelim@ &nbsp" + incubation[2] + "@coldelim@ "; row_format += addTableRow(table_name, formattedText, ["center", "center", "center", "right", "left", "right", "left"]); }			var header_format = addTableHeader(table_name, 50, "#8EC3CD", "Dragon Name@coldelim@Colors@coldelim@Rarity@coldelim@&nbsp &nbsp Game Display Time&nbsp &nbsp @coldelim@&nbsp &nbsp Cost to Complete &nbsp &nbsp ", 	             ["center", "center", "center", "center", "center"], ["1", "1", "1", "2", "2"]); var table = document.getElementById(table_name); // Required by Internet Explorer table.innerHTML = header_format + row_format; var bottomCoordinate = document.getElementById(contentSection).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++) {										possible_offspring.push(pure_outcomes[i]); }			}			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 sortListByRarity(list_to_sort) {	var sorted_list = new Array; var separated_list = new Array; separated_list.push(new Array); // Common index = 0 separated_list.push(new Array); // Rare index = 1 separated_list.push(new Array); // Super Rare index = 2 separated_list.push(new Array); // Ultra Rare index = 3 for (var i = 0; i < list_to_sort.length; i++) {		var rarity = parseInt(dragon_data[list_to_sort[i].toLowerCase].rarity, 10); separated_list[rarity - 1].push(list_to_sort[i]); }	for (var i = 0; i < separated_list.length; i++) {				for (var j = 0; j < separated_list[i].length; j++) {			sorted_list.push(separated_list[i][j]); }	}			return sorted_list; }

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]) {								possible_offspring.push(pure_outcomes[j]); 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++) {						possible_offspring.push(diamond_outcomes[j]); }	}	for (var i = 0; i < hybrid_outcomes.length; i++) {					if (comboContainsColors(colorPool, dragon_data[hybrid_outcomes[i]].requiredcolors)) {						possible_offspring.push(hybrid_outcomes[i]); }	}		return possible_offspring; }

function findParentsButtonClicked {	getPossibleParents; }

function getPossibleParents {  		// build a distinct color pool list of keys, give each color an index and sort ascending // then merge the color into a spaceless string // this forms the key for the dictionary containing parents of a color group // the dictionary is built once, at runtime, by running through the dragon list and getting the key from the colors var contentSection = "content_findparents"; clearContentField(contentSection); var table_name = "findparents_table"; clearTable(table_name); var menu = document.getElementById("findparents_dragonchoices"); var dragonName = dragon_data[dragon_ids[menu.selectedIndex]].name; printText(" &nbsp &nbsp &nbsp &nbspBreeding the " + dragonName + " Dragon is possible by pairing the following parents:", contentSection); var possible_parents = buildParentListByRules(dragon_ids[menu.selectedIndex]); var row_format = ""; 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; parentNames[0] = dragon_data[parentNames[0].toLowerCase].name; parentNames[1] = dragon_data[parentNames[1].toLowerCase].name;

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 += " "; }						row_format += addTableRow(table_name, formattedText, ["right", "left", "center", "right", "left"]); }				       var table = document.getElementById(table_name); // Required by Internet Explorer table.innerHTML = row_format; var bottomCoordinate = document.getElementById(contentSection).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 basicColors = [dragonColors, "diamond"]; var diamond_parents = new Array; for (var i = 0; i < diamond_outcomes.length; i++) {						diamond_parents.push(dragonName + "," + diamond_outcomes[i]); }		for (var i = 0; i < dragon_ids.length; i++) {						if (dragon_data[dragon_ids[i]].breedtype == "special_diamond" &&			listsMatch(basicColors, dragon_data[dragon_ids[i]].colors)) {								diamond_parents.push(dragonName + "," + dragon_ids[i]); }		}		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++) {								possible_parents.push(dragonName + "," + diamond_outcomes[i]); }		}		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++) {					   						possible_parents.push(diamond_outcomes[i] + "," + diamond_outcomes[j]); }				}				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 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_ids.length; i++) {		if (listsMatch(dragon_data[dragon_ids[i]].colors, dragonColorReq)) {								same_color_dragons.push(dragon_ids[i]); }					for (var j = i; j < dragon_ids.length; j++) {						if (comboContainsColors(getMergedList(dragon_data[dragon_ids[i]].colors, dragon_data[dragon_ids[j]].colors), dragonColorReq)) {												single_list_traversal_pairs.push(dragon_ids[i] + "," + dragon_ids[j]); }					}	}	for (var i = 0; i < same_color_dragons.length; i++) {		for (var j = i; j < same_color_dragons.length; j++) {						same_color_pairs.push(same_color_dragons[i] + "," + same_color_dragons[j]); }	}	possible_parents = getMergedList(same_color_pairs, single_list_traversal_pairs); return possible_parents; }

function listsMatch(list1, list2) {	if (list1.length == list2.length) {		var num_to_match = list1.length; var matches = 0; for (var i = 0; i < num_to_match; i++) {			for (var j = 0; j < num_to_match; j++) {				if (list1[i] == list2[j]) {					matches++; break; }			}		}		if (matches == num_to_match) {			return true; }	}

return false; }

function getNDistinctColorPairs(numColors) {	var possible_parents = new Array; for (var i = 0; i < dragon_ids.length; i++) {		for (var j = i; j < dragon_ids.length; j++) {			var dragon1Colors = dragon_data[dragon_ids[i]].colors; var dragon2Colors = dragon_data[dragon_ids[j]].colors; var colorPool = getMergedList(dragon1Colors, dragon2Colors); if (colorPool.length >= numColors) {								possible_parents.push(dragon_ids[i] + "," + dragon_ids[j]); }					}	}	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; }