MediaWiki:Common.js/ExpectedProfitPerKill.js

$( '.expectedProfitPerKill' ).each( function {    var $p = $( ' ' ).attr({title:'Average profit made per kill'});	profitValue;    $( this ).empty.append( $p );	finalSum; });

function finalSum {	var sum = 0; for ( var j in singleExpectedValues ) {		sum += (singleExpectedValues[j]*exchangeArray[j]); }	$( '.expectedProfitPerKill' ).css({ 'color': 'green',										'font-style':'bold'}).text(addCommas(Math.round(sum))+" coins"); }

var singleExpectedValues = [], exchangeArray = [], Names = [];

function addCommas(nStr) {	nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); }	return x1 + x2; } function profitValue {	function ajaxSuccess (data) {		var pages = data.query.pages, pageId = pages; for ( var i in pages ) { pageId = pages[i].pageid; }		var page = pages[pageId], pageContent = page.revisions[0]['*']; parseTemplate(pageContent, 'dropsline'); }	$.ajax( {	/*creates a reference to the url http://runescapefanfiction.wikia.com/api.php?action=query&prop=revisions&titles=itemName+'|Exchange:'+itemName&rvprop=content&redirects=	*/	data: {		'action': 'query',		'prop': 'revisions',		'titles': mw.config.get( 'wgTitle' ),	//possibly wrong		'rvprop': 'content',		'redirects': '',		'format': 'json'	},	dataType: 'json',	success: ajaxSuccess,	error: ajaxError,	url: mw.config.get( 'wgScriptPath' ) + '/api.php',	timeout: 10000 // millisec   }); } function ajaxError( xhr, error ) { showError( 'Error: ' + error ); } function parseTemplate(text, tpl) {	tpl2 = tpl.replace(/[_ ]/g, '[_ ]'); var re = new RegExp('', 'gi');

var data = []; var match; while (match = re.exec(text)) { var params = match[2].split('|'); var j = 1; // Unnamed parameter index var tplData = []; for (var k in params) { var t = params[k].split('='); var name = null; var value = null; if (t.length == 1) { // Unnamed params name = (j++) + ''; // Cast to string value = t[0]; } else { name = t[0]; value = t[1]; }

tplData[$.trim(name)] = $.trim(value); }		data.push(tplData); if ((tpl == 'dropsline')&&(tplData['gemw'] != 'no')) {			Names.push(tplData['Name']); retrieveExchangePrice(tplData['Name']); singleExpectedValues[tplData['Name']] = retrieveQuantity(tplData['Quantity'])*retrieveProbability(tplData['Rarity']); }	}	return data; }

function retrieveProbability (Rarity) {	switch(Rarity) {		case 'always': return 1;break; case 'common': return (1/10);break; case 'uncommon': return (1/30);break; case 'rare': return (1/250);break; case 'very rare': return (1/5000);break; case 'extremely rare': return (1/50000);break; default: return 0; } }

function retrieveQuantity (Quantity) {	if (Quantity.indexOf('/') == -1) {		Quantity = Quantity.replace('(noted)',''); var temp = Quantity.split('-'); if(temp.length == 1) return parseInt(Quantity); else return ( parseInt(temp[0]) + parseInt(temp[1]) )/2; }	else {		var temp = Quantity.split('/'); return temp[(temp.length)/2];	//possibly wrong } }

function retrieveExchangePrice (Name) {	$.ajax( {	/*creates a reference to the url http://runescapefanfiction.wikia.com/api.php?action=query&prop=revisions&titles=itemName+'|Exchange:'+itemName&rvprop=content&redirects=	*/	data: {		'action': 'query',		'prop': 'revisions',		'titles': '|Exchange:' + Name,	//possibly wrong		'rvprop': 'content',		'redirects': '',		'format': 'json'	},	dataType: 'json',	success: ajaxSuccess,	error: ajaxError,	url: mw.config.get( 'wgScriptPath' ) + '/api.php', //possibly wrong	timeout: 10000 // millisec   } ); function ajaxSuccess( data ) { var pages = data.query.pages, result = null, exchangePageId = null; for ( var i in pages ) { if ( pages[i].title.substring(0,9) == 'Exchange:' ) { exchangePageId = pages[i].pageid; }		}

var exchangePage = data.query.pages[exchangePageId], exchangeData = []; if(exchangePage) { var exchangeContent = exchangePage.revisions[0]['*'].replace( ,  ); exchangeData = parseTemplate( exchangeContent, 'exchangeitem' ); }		for (var i in exchangeData) { if( exchangePageId == null ) { exchangeArray[Name] = 0; } else { exchangeArray[Name] = parseInt(exchangeData[i]['Price'].replace(',','')); } }	} }