User:Joeytje50/Dropadd.js

//

function callAPI(data, method, callback) { data['format'] = 'json'; $.ajax({		data: data,		dataType: 'json',		url: wgScriptPath + '/api.php',		type: method,		cache: false,		success: function(response) {			if (response.error)				alert('API error: ' + response.error.info);			else 				callback(response);		},		error: function(xhr, error) { alert('AJAX error: ' + error); }	}); }

//--Charm logs--// //To disable charm log submissions on the monster page itself, add   var onPage = false;   just before the import. function isInt(n) { return parseInt(n) % 1 == 0; } function diff(x,y,z) { z=z?z:1; x=parseFloat(x)/z, y=parseFloat(y)/z var diff = Math.abs(x-y); return Math.round(diff*100)/100; }

var onPage = onPage?onPage:true; var waterfiend = wgPageName=='Waterfiend' var submitTable = ' '

if (onPage && !wgPageName.match('/Charm_log') && $('.charmtable').length) { if (waterfiend) { $(document).ready(function{			$('#charmLogSubmit').attr('id','charmLogSubmit'+$('a[href$="action=edit&section=new&preload=Template:Charm_log_submission_preload&editintro=Template:CharmLogPreloadedUpdateGuide&useeditor=mediawiki"]').parents('.charmpage').attr('class').split(' ')[1])		}) }	$(function {	$('a[href$="action=edit&section=new&preload=Template:Charm_log_submission_preload&editintro=Template:CharmLogPreloadedUpdateGuide&useeditor=mediawiki"]').attr('href', function(i, val) { return val.replace(/.*\/wiki\/([^\?\&]*).*/, '/wiki/$1') }).click(function(e) { e.preventDefault; var wfiendadd = waterfiend?$(this).parents('.charmpage').attr('class').split(' ')[1]:''; if (!$('table#charmLogSubmit'+(waterfiend?'-':'')+wfiendadd).length) { var charmpage = wgPageName+(waterfiend?(wfiendadd=='ghorrock'?' (Ghorrock)':):)+'/Charm log'; if(waterfiend) $('*[id^="charmLogSubmit"]:not([id$="'+wfiendadd+'"]), hr.chHr').remove; $(this).parent.after(submitTable+''); if(waterfiend) $(this).parent.next.attr('id', $(this).parent.next.attr('id')+'-'+wfiendadd); $('button#clSubmit').click(processSubmit); }	})	}) } else if (wgPageName.match('/Charm_log') && $('#charmguide').length) { $(document).ready(function {		$('#charmguide').html('Submit charm data: '+submitTable)		var charmpage = wgPageName;		$('button#clSubmit').click(processSubmit);	}) }

function processSubmit { var total=$('#chTotalKills'), gold=$('#chGold'), green=$('#chGreen'), crimson=$('#chCrimson'), blue=$('#chBlue'); if (parseInt(total.val)<50) { alert('The total amount of kills you submitted is below the required amount of 50. Please only submit charm data for 50 kills or more.'); return; } else if (parseInt(total.val)>10000) { alert('The total amount of kills you submitted it above the allowed amount. Please do not try to add made up results.'); return; }	if ((parseInt(gold.val)||gold.val.length)+(parseInt(green.val)||green.val.length)+(parseInt(crimson.val)||crimson.val.length)+(parseInt(blue.val)||blue.val.length) == 0) { alert('The total amount of charms you received was 0. Please only submit when you\'ve received charms in your monster kills (increasing the sample size by killing more monsters).'); return false; }	if (gold.val.length==0||green.val.length==0||crimson.val.length==0||blue.val.length==0) { if (confirm('The inputs that were left empty will default to 0. Continue?')) { gold.val||gold.val('0'); green.val||green.val('0'); crimson.val||crimson.val('0'); blue.val||blue.val('0'); submitCharmLog(charmpage) }	} else { submitCharmLog(charmpage) } }

function submitCharmLog(charmpage) { $(this).attr('disabled','disabled').html('Loading...').after(' Loading current charm log data for comparison') callAPI({		'action': 'query',		'prop': 'info|revisions',		'intoken': 'edit',		'titles': charmpage.replace(/(.*)\/Charm[ _]log/, 'Charm:$1')+'|'+charmpage,		'rvprop': 'content',		'indexpageids': 'true',	}, 'GET', function(response) { console.log('test')		var summary = 'Submitted charm data using the box on the '+(wgPageName.match('/Charm_log')?'submission ':)+'page.'		var submitpage = response.query.pages[response.query.pageids[0]]		var datapage = response.query.pages[response.query.pageids[1]]		var curdata = datapage.revisions[0]['*'].replace(/(.|\n)*\|charms=/i, '|charms=').replace(/\n\}\}(.|\n)*/, ).split('\n')		datapage = datapage.title=='Charm:'+wgPageName.replace('_',' ')?submitpage:datapage		submitpage = datapage.title=='Charm:'+wgPageName.replace('_',' ')?submitpage:datapage		var total=$('#chTotalKills').val, gold=$('#chGold').val, green=$('#chGreen').val, crimson=$('#chCrimson').val, blue=$('#chBlue').val var total=parseInt(total), gold=parseInt(gold), green=parseInt(green), crimson=parseInt(crimson), blue=parseInt(blue) var perkill = parseInt(curdata[0].replace('|charms=','')) var ea = perkill; var curtot=curdata[1].replace('|kills=',), curgold=curdata[2].replace('|gold=',), curgreen=curdata[3].replace('|green=',), curcrimson=curdata[4].replace('|crimson=',), curblue=curdata[5].replace('|blue=','') curtot=parseInt(curtot), curgold=parseInt(curgold), curgreen=parseInt(curgreen), curcrimson=parseInt(curcrimson), curblue=parseInt(curblue) if (!isInt(gold/perkill)||!isInt(green/perkill)||!isInt(crimson/perkill)||!isInt(blue/perkill)||(gold+green+crimson+blue)/perkill>total) { alert('It is not possible to get that amount of charms. You '+((gold+green+crimson+blue)/perkill>total?'submitted more charm drops than kills':"submitted an amount of charms that can't be divided by "+perkill+', the amount of charms you get per kill')) $('button#clSubmit').removeAttr('disabled').html('Submit').next.remove } else { var pd = 1/(ea*total), //percent divisor cpd = 1/(ea*curtot); //cur percent divisor var goldprob = Math.abs((gold*pd-curgold*cpd)/Math.sqrt(((gold*pd*total + curgold*cpd*curtot)*(1-((gold*cpd*total + curgold*cpd*curtot)/(total+curtot)))*((1/total)+(1/curtot)))),			   greenprob = Math.abs((green*pd-curgreen*cpd)/Math.sqrt(((green*pd*total + curgreen*cpd*curtot)*(1-((green*cpd*total + curgreen*cpd*curtot)/(total+curtot)))*((1/total)+(1/curtot)))), crimsonprob = Math.abs((crimson*pd-curcrimson*cpd)/Math.sqrt(((crimson*pd*total + curcrimson*cpd*curtot)*(1-((crimson*cpd*total + curcrimson*cpd*curtot)/(total+curtot)))*((1/total)+(1/curtot)))),			   blueprob = Math.abs((blue*pd-curblue*cpd)/Math.sqrt(((blue*pd*total + curblue*cpd*curtot)*(1-((blue*pd*total + curblue*cpd*curtot)/(total+curtot)))*((1/total)+(1/curtot)))); if (goldprob>2||greenprob>2||crimsonprob>2||blueprob>2) { if (goldprob>3||greenprob>3||crimsonprob>3||blueprob>3) { alert('Please do not try to use this script to vandalise the wiki. If you do vandalise this wiki, you will be blocked.'); return false; } else { summary += ' /*(Possible vandalism/spam)*/' }			}			var template = '{{Charm log submission\n|kills='+total+'\n|gold='+gold+'\n|green='+green+'\n|crimson='+crimson+'\n|blue='+blue+'\n\}\}' if (submitpage.revisions[0]['*'].match(new RegExp(template))) { alert('thank you for your submission. The page will now be reloaded.') document.location.replace(wgArticlePath.replace('$1', wgPageName)) return false; }			$('#loadmsg').html('Submitting edit...') callAPI({				'minor': 'yes',				'summary': summary,				'action': 'edit',				'title': charmpage,				'basetimestamp': submitpage.revisions[0].timestamp,				'startimestamp': submitpage.starttimestamp,				'token': submitpage.edittoken,				'appendtext': template			}, 'POST', function(response) {				alert('thank you for your submission. The page will now be reloaded.')				$('button#clSubmit').removeAttr('disabled').html('Submit').next.remove				document.location.replace(wgArticlePath.replace('$1', wgPageName))			}); }	}) }