MediaWiki:BattleArenaSelection.js

/* * Initial setup functions */ $(document).ready(function {                  eval(document.getElementById("battlearenadata").innerHTML);        buildDataLists;  	setMenuOptions;      });

function populateDataLists {	window.dragon_ids = []; for (var id in breeds) {		dragon_ids.push(id); } } function setMenuOptions {	var opponent_dragon_menu = document.getElementById("select_opponent"); var opponent_level_menu = document.getElementById("select_opponent_level"); var names = []; for (var index in dragon_ids) {		var id = dragon_ids[index]; var name = breeds[id].name; names.push(name); }	names = mergeSort(names, "alphabetical"); for (var index in names) {		var option = document.createElement("option"); option.text = names[index]; opponent_dragon_menu.add(option); }	for (var i = OPPONENT_MAX_LEVEL; i >= OPPONENT_MIN_LEVEL; i--) {		var option = document.createElement("option"); option.text = i;		opponent_level_menu.add(option); }	document.getElementById("form_opponent").style.display = "inline"; } /*  * Content display functions */

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

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

/* * Button events */ function calculateScoreButtonClicked {    calculateScores; } function calculateScores {	var content_field = "content_scores"; clearContentField(content_field); var opponent_id = dragon_ids[document.getElementById("select_opponent").selectedIndex]; var opponent_colors = breeds[opponent_id].colors; var opponent_level = OPPONENT_MAX_LEVEL - document.getElementById("select_opponent_level").selectedIndex; var dragon_scores_list = []; for (var id in breeds) {		dragon_scores_list.push(getDragonScores(id, opponent_colors, opponent_level)); }	var handler = new HtmlOutput(content_field); var output = renderDragonScores(handler, mergeSort(dragon_scores_list, "dragon_scores")); handler.output(output); }

/* * Scoring functions */ function getCritRate(crit_score) {	var crit_index = Math.min(Math.max(crit_score, MIN_TOTAL_SCORE), MAX_TOTAL_SCORE); return win_rates[crit_index]; } function getDragonScores(challenger_id, opponent_colors, opponent_level) {	var challenger = breeds[challenger_id]; var type_score = getTypeScore(challenger.colors, opponent_colors); var rarity_score = getRarityScore(challenger.rarity); var level_scores = []; for (var i = CHALLENGER_MIN_LEVEL; i <= CHALLENGER_MAX_LEVEL; i++) {		level_scores.push(getLevelScore(i, opponent_level)); }	return new DragonScores(challenger_id, type_score, rarity_score, level_scores); } function getTypeScore(challenger_colors, opponent_colors) {	var advantages = 0; for (var i in challenger_colors) {		var color = challenger_colors[i];

if (type_advantages[color]) {			var strengths = type_advantages[color].strengths; var weaknesses = type_advantages[color].weaknesses; for (var j in opponent_colors) {				if (listContainsElements(strengths, [opponent_colors[j]])) {					advantages += 1; }				if (listContainsElements(weaknesses, [opponent_colors[j]])) {					advantages -= 1; }			}		}		else {			displayHTML("Warning: The " + color + " color is invalid. " +			"Please report immediately if you see this message.", "content_messages"); }	}	advantages = Math.min(Math.max(advantages, MIN_TYPE_SCORE), MAX_TYPE_SCORE); var index_offset = (MIN_TYPE_SCORE < 0) ? (0 - MIN_TYPE_SCORE) : (-1 * MIN_TYPE_SCORE); return type_advantage_points[advantages + index_offset]; }

function getRarityScore(rarity) {	if (rarity_points[rarity] !== null) {		return rarity_points[rarity]; }

displayHTML("WARNING: Rarity \"" + rarity + "\" is not a valid rarity. " +				"Please report immediately if you see this message.", "content_messages"); return 0; } function getLevelScore(challenger_level, opponent_level) {	var difference = Math.min(Math.max(challenger_level - opponent_level, MIN_LEVEL_SCORE), 	MAX_LEVEL_SCORE); var index_offset = MAX_LEVEL_SCORE - MIN_LEVEL_SCORE; return level_points[difference + index_offset]; }

/* * Validation functions */ function listContainsElements(list, required_elements) {	if (required_elements && required_elements.length > 0) {		for (var i = 0; i < required_elements.length; i++) {			var found = false; for (var j = 0; j < list.length; j++) {				if (list[j] === required_elements[i]) {					found = true; break; }				else if (list[j] instanceof Object && required_elements[i] instanceof Object &&						 typeof list[j] === typeof required_elements[i] &&						 typeof list[j].equals === 'function' && typeof required_elements[i].equals === 'function') {					if (list[j].equals(required_elements[i])) {										found = true; break; }				}			}			if (!found) {							return false; }		}		return true; }	return false; }

/* * List manipulation functions */

function mergeSort (values, type) {   if (values && values.length > 1) // List requires sorting {                   var split_num = parseInt(values.length / 2); var remainder = values.length % 2; var left = new Array; var right = new Array; for (var i = 0; i < split_num; i++) {           left.push(values[i]); right.push(values[i + split_num]); }       if (remainder > 0) // Odd number of values {           right.push(values[values.length - 1]); }       left = mergeSort(left, type); right = mergeSort(right, type); values = merge(values, left, right, type); }   return values; }

function merge(values, left, right, type) {   var n = 0; // index of values var l = 0; // index of left var r = 0; // index of right while (l < left.length && r < right.length) {       if (compareValues(left[l], right[r], type) <= 0) // left < right {                       values[n] = left[l]; l++; }       else // right < left {                   values[n] = right[r]; r++; }       n++; }   // One of the subarrays has been exhausted. Copy over the remaining elements. while (l < left.length) {       values[n] = left[l]; l++; n++; }   while (r < right.length) {       values[n] = right[r]; r++; n++; }   return values; }

/* * Comparison functions */ function compareValues (first_val, second_val, type) {       	if (type === "alphabetical") {		return comparePrimitives(first_val, second_val); }	else if (type == "level_scores") {		return compareLevelScores(first_val, second_val); }	else if (type === "dragon_scores") {		return compareDragonScores(first_val, second_val); }	return 0; }

function comparePrimitives(a,b) { if (a < b) { return -1; }   if (a > b) { return 1; }   return 0; }

function compareLevelScores(first_val, second_val) {	return -1 * comparePrimitives(first_val, second_val); }

function compareDragonScores(first_val, second_val) {	if (first_val instanceof DragonScores && second_val instanceof DragonScores) {		var first_scores = first_val.total_scores; var second_scores = second_val.total_scores; return -1 * comparePrimitives(first_scores[0], second_scores[0]); }	else {		displayHTML("Warning: Invalid types for comparison. The compared objects " + 		"should both be instances of DragonScores.", "content_messages"); }

return 0; }

/* * Output rendering code */ function renderDragonScores(handler, dragon_scores_list) {	var header_color = "#8EC3CD"; var header_pieces = []; header_pieces.push(handler.wrapHeader("Dragon", header_color, 1, 100)); for (var i = CHALLENGER_MAX_LEVEL; i >= CHALLENGER_MIN_LEVEL; i--) {		header_pieces.push(handler.wrapHeader("Lv. " + i, header_color, 1, 50)); }   var headers = handler.joinRows(header_pieces); var rows = handler.joinRows(	   dragon_scores_list.map(function(dragon_scores) { var dragon_name = breeds[dragon_scores.challenger].name; var total_scores = dragon_scores.total_scores; var cells = []; cells.push(handler.wrapCell(handler.renderDragonLink(dragon_name), "left")); for (var index in total_scores) {				cells.push(handler.wrapCell(getCritRate(total_scores[index]) + "%", "center")); }			return handler.wrapRow(handler.joinCells(cells)); })	);	return handler.wrapBorderedTable(handler.joinRows([headers, rows])); }

/* * HTML-specific output handler */ function HtmlOutput(outputElementId) {   this.outputElementId = outputElementId; this.rows = []; }

HtmlOutput.prototype.toUrl = function (link, text, title) { return "" + text + ""; }

HtmlOutput.prototype.renderDragonLink = function (breed_name) { return this.toUrl(site_page_source + breed_name + " Dragon", breed_name, breed_name + " Dragon"); }

HtmlOutput.prototype.wrapBorderedTable = function (content) {       var table_style = "background-color:#CCCCCC; border-collapse: collapse;"; return " "; }

HtmlOutput.prototype.wrapHeader = function (header, color, span, width) {   var header_style = "width:" + width + "px; height:50px; background-color:" + color + "; text-align: center;"; return ""+header+" "; }

HtmlOutput.prototype.wrapCell = function (cell, align) {   var cell_style = "text-align:" + align + ";"; return ""+cell+" "; }

HtmlOutput.prototype.wrapRow = function (row) {   return " "+row+" " }

HtmlOutput.prototype.joinRows = function (rows) {   return rows.join(''); }

HtmlOutput.prototype.joinCells = function (cells) {   return cells.join(''); }

HtmlOutput.prototype.output = function (value) {      document.getElementById(this.outputElementId).innerHTML = value; }

/* * Custom data structures */ function DragonScores(challenger, type_score, rarity_score, level_scores) {	this.challenger = challenger; this.type_score = type_score; this.rarity_score = rarity_score; this.level_scores = mergeSort(level_scores, "level_scores"); this.total_scores = []; for (var i = 0; i < this.level_scores.length; i++) {		this.total_scores.push(this.type_score + this.rarity_score + this.level_scores[i]); } }