User:Joeytje50/Dropadd.js

//

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

//--Monster drops--//

var rdt = /uncut (sapphire|emerald|ruby|diamond|dragonstone)|dragonstone|(loop|tooth) half (of a? ?)?key|(adamant|rune) javelin|(rune|dragon) spear|rune kiteshield|shield left half|dragon med helm|rune arrow|(adamant|onyx) bolts|battlestaff|(air|fire) orb|vecna skull|pure essence|(nature|soul|blood) rune|(water|earth|fire|chaos|nature) talisman|raw (swordfish|shark)|big bones|coal|(silver|adamantite|runite) ore|(adamant|rune) bar|clean (ranarr|toadflax|snapdragon|torstol)|saradomin brew( \(4\))?|(watermelon|yew|magic|palm|snapdragon|torstol|fellstalk) seed|yew logs/i

$('.dropstable tr:last-child').not('.collapsible tr:last-child').after(' Add to this drops section ') $('.dropadd').click(function {	var section = $(this).parents('dl').prev('h3').find('a').attr('href').replace(/.*section=/,'')	$(this).parents('td').prepend(' Item name:  Quantity:   Rarity:    Sold on Grand Exchange Image:  ')	$('.gemw').click(function { if ($(this).is(':checked')) { $(this).parent.next.css('display','none').find('input').val('') } else { $(this).parent.next.css('display','inline') }	}) if (!($(this).prev.is('button'))) {	$(this).before('Submit ')	$('button.submit').click(function { $(this).attr('disabled','disabled').html('Submitting edit...') var section = $(this).attr('data-section'); var templates = ''; for (i=0;i<$(this).parents('td').find('div.dropsubmission').length;i++) { var thisdiv = $(this).parents('td').find('div.dropsubmission:nth-child('+(i+1)+')') if (!(thisdiv.find('.itemname').val.match(rdt))&&thisdiv.find('.itemname').val!='') { templates += '{{DropsLine|Name='+thisdiv.find('.itemname').val+'|Quantity='+thisdiv.find('.quantity').val+'|Rarity='+thisdiv.find('.rarity').val + (thisdiv.find('.gemw').is(':checked')?:'|gemw=no'+(thisdiv.find('.image').val!=?'|image='+thisdiv.find('.image').val:''))+'\}\}\n' } else { var matchedRDT = thisdiv.find('.itemname').val.match(rdt)||matchedRDT?true:false; }		}		if (templates=='') { alert((matchedRDT?'The drops you entered are already in the Rare drop table. Please see the Rare drop table drops section below to see those drops, or go to the RDT article.':'Please enter something before pressing submit.')) $(this).removeAttr('disabled').html('Submit') } else { callAPI({				'action': 'query',				'prop': 'info|revisions',				'intoken': 'edit',				'titles': wgPageName,				'rvprop': 'content',				'rvsection': section,				'rvlimit': '1',				'indexpageids': 'true',			}, 'GET', function(response) {				var page = response.query.pages[response.query.pageids[0]];				var content = page.revisions[0]['*'];				content = content.replace('|}', templates+'|}')				callAPI({ 'minor': 'yes', 'summary': 'Added monster drop using the boxes on the page.', 'action': 'edit', 'section': section, 'title': wgPageName, 'basetimestamp': page.revisions[0].timestamp, 'startimestamp': page.starttimestamp, 'token': page.edittoken, 'text': content }, 'POST', function(response) { alert('thank you for your submission. Please reload this page to see the changes.') $('button.submit[data-section="'+section+'"]').parents('td').find('*:not(a)').remove });			});		}	}) } })

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

function submitCharmLog(charmpage) { 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 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 { if (curtot>1000&&(diff(curgold/curtot,gold/total)>0.15 || diff(curgreen/curtot,green/total)>0.15 || diff(curcrimson/curtot,crimson/total)>0.15 || diff(curblue/curtot,blue/total)>0.15)) { summary += ' /*(Possible vandalism/spam)*/' }			var template = '\n{{Charm log submission\n|kills='+total+'\n|gold='+gold+'\n|green='+green+'\n|crimson='+crimson+'\n|blue='+blue+'\n\}\}'

$('#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))			}); }	}) }

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])		}) }	$('a[href$="&action=edit&section=new&preload=Template:Charm_log_submission_preload&editintro=Template:CharmLogPreloadedUpdateGuide&useeditor=mediawiki"]').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'		waterfiend?$('*[id^="charmLogSubmit"]:not([id$="'+wfiendadd+'"]), hr.chHr').remove:		$(this).parent.after(submitTable+'')		waterfiend?$(this).parent.next.attr('id', $(this).parent.next.attr('id')+'-'+wfiendadd):		$('button#clSubmit').click(function { if (parseInt($('#chTotalKills').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.') } else { $(this).attr('disabled','disabled').html('Loading...').after(' Loading current charm log data for comparison') submitCharmLog(charmpage) }		})	}	}) } else if (wgPageName.match('/Charm_log') && $('#charmguide').length) { $(document).ready(function {		$('#charmguide').html(submitTable)		var charmpage = wgPageName;		$('button#clSubmit').click(function { if (parseInt($('#chTotalKills').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.') } else { $(this).attr('disabled','disabled').html('Loading...').after(' Loading current charm log data for comparison') submitCharmLog(charmpage) }		})	}) }