MediaWiki:Common.js/charmadd.js

/** * Charm log add script * * @author Joeytje50 * @author Cqm */


 * ( function {

function diff( x, y, z ) { z = z ? z : 1; x = parseFloat( x ) / z;   y = parseFloat( y ) / z;    var d = Math.abs( x - y );

return Math.round( diff * 100 ) / 100; }

// To disable charm log submissions on the monster page itself, add   var onPage = false;   just before the import. var onPage = window.onPage ? window.onPage : true; var waterfiend = wgPageName === 'Waterfiend'; var charmpage; var submitTable = ' ';

if ( onPage && !wgPageName.match( '/Charm_log' ) && $( '.charmtable' ).length ) { if ( waterfiend ) { $( function {            $( '#charmLogSubmit' ).attr( 'id', 'charmLogSubmit' + ( $( 'a[href*="log_edit=true"]' ).parents( '.charmpage' ).attr( 'class' ) || '' ).split( ' ' )[1] );       } );    }

$( function {        $('a[href*="log_edit=true"]')            .attr('href', function(i, val) { // @todo why is this needed? return val.replace( /.*\/wiki\/([^\?\&]*).*/, '/wiki/$1'); } )           .click( function ( e ) { e.preventDefault; var $this = $( this ), $parent = $this.parent, $next = $parent.next, wfiendadd = waterfiend ? $this.parents( '.charmpage' ).attr( 'class' ).split( ' ' )[1] : '';

if ( !$( 'table#charmLogSubmit' + ( waterfiend ? '-' : '' ) + wfiendadd ).length) { charmpage = wgPageName + ( waterfiend ? ( wfiendadd === 'ghorrock' ? ' (Ghorrock)' :  ) :  ) + '/Charm log'; if ( waterfiend ) { $( '*[id^="charmLogSubmit"]:not([id$="' + wfiendadd + '"]), hr.chHr' ).remove; }

$parent.after(submitTable+'');

if ( waterfiend ) { $next.attr( 'id', $next.attr( 'id' ) + '-' + wfiendadd ); }

$( 'button#clSubmit' ).click( processSubmit ); }           } );    } ); } else if ( wgPageName.match( '/Charm_log' ) && $( '#charmguide' ).length ) { $( function {       $( '#charmguide' ).html( 'Submit charm data: ' + submitTable )        charmpage = wgPageName;        $( 'button#clSubmit' ).click( processSubmit );    } ); }

function processSubmit { var total = $( '#chTotalKills' ), gold = $( '#chGold' ), green = $( '#chGreen' ), crimson = $( '#chCrimson' ), blue = $( '#chBlue' );

if ( parseInt( total.val ) > 5000 ) { alert( 'The number of kills you submitted is above the allowed amount per submission.' ); return; }   if (        ( parseInt( gold.val, 10 ) || gold.val.length ) +        ( parseInt( green.val, 10 ) || green.val.length ) +        ( parseInt( crimson.val, 10 ) || crimson.val.length ) +        ( parseInt( blue.val, 10 ) || blue.val.length ) === 0    ) { alert(           'The total amount of charms you received was 0. ' +            'You can 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) { $('#clSubmit').prop('disabled',true).html('Loading...').after(' Loading current charm log data for comparison'); var monster = charmpage.replace(/(.*)\/Charm[ _]log/, 'Charm:$1'); callAPI({       'action': 'query',        'prop': 'info|revisions',        'intoken': 'edit',        'titles': monster+'|'+charmpage,        'rvprop': 'content',        'indexpageids': 'true',    }, 'GET', function(response) {        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)*/, ).replace(/\n\|rare=.*/,'').split('\n')        var rare = datapage.revisions[0]['*'].replace(/.*|rare=(.*).*/, "$1").trim;        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); var minKills = rare.indexOf("rare=yes") == -1 ? 50 : 15;       function resetForm { $('button#clSubmit').prop('disabled', false).html('Submit').next.remove } if (total < minKills) { alert('The total amount of kills you submitted is below the required amount of '+minKills+'. You can only submit charm data for '+minKills+' kills or more.'); resetForm; return false; } else if (gold%perkill!=0||green%perkill!=0||crimson%perkill!=0||blue%perkill!=0||((gold+green+crimson+blue)/perkill>total && mw.config.get('wgPageName').indexOf("Vorago") == -1)||isNaN(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')) resetForm; } else if (gold+green+crimson+blue == 0) { alert('Please only submit to the log when you received charms in your kills. Try killing more monsters before submitting the kills to the log.'); resetForm; } else { var pd = 1/(ea*total), //percent cpd = 1/(ea*curtot), //cur percent divisor div = Math.sqrt((1/total) + (1/curtot)), gp = ((gold+curgold)/(total+curtot)/ea), grp = ((green+curgreen)/(total+curtot)/ea), cp = ((crimson+curcrimson)/(total+curtot)/ea), bp = ((blue+curblue)/(total+curtot)/ea); var goldprob = Math.abs((gold*pd-curgold*cpd)/Math.sqrt(gp*(1-gp)*((1/total)+(1/curtot)))), greenprob = Math.abs((green*pd-curgreen*cpd)/Math.sqrt(grp*(1-grp)*((1/total)+(1/curtot)))), crimsonprob = Math.abs((crimson*pd-curcrimson*cpd)/Math.sqrt(cp*(1-cp)*((1/total)+(1/curtot)))), blueprob = Math.abs((blue*pd-curblue*cpd)/Math.sqrt(bp*(1-bp)*((1/total)+(1/curtot)))); if ((goldprob>2||greenprob>2||crimsonprob>2||blueprob>2) && curtot > 500) { var logLineAdd = [total, gold, goldprob, , green, greenprob, , crimson, crimsonprob, , blue, blueprob, ] if ((goldprob>3||greenprob>3||crimsonprob>3||blueprob>3) && curtot > 1000) { addLogLine(logLineAdd, monster.replace('Charm:',''), true) alert('Do not attempt to vandalise the wiki\'s data by submitting erroneous data. Continuing to vandalise or attempting to vandalise will get you blocked.'); $('#chGold, #chGreen, #chCrimson, #chBlue, #chTotalKills').val(''); return false; } else { addLogLine(logLineAdd, monster.replace('Charm:',''), false) 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\}\}' $('#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.')               resetForm;                document.location.replace(wgArticlePath.replace('$1', wgPageName))            }); }   }) }

function addLogLine(data, monster, blocked) { callAPI({       'action': 'query',        'prop': 'info|revisions',        'intoken': 'edit',        'titles': 'RuneScape:Counter-Vandalism Unit/Charms',        'indexpageids': 'true',    }, 'GET', function(response) {        var logLine = '\n|-\n|Special:Contributions/\n|';        for (var i=0;i<data.length;i++) {            if (typeof data[i] == 'number')                data[i] = Math.round(data[i]*100)/100;        }        logLine += data.join(' ||');        logLine += '\n|'+monster.split('_').join(' ')+' ||'        logLine += blocked?"disallowed":'[ history]';        logLine += ' ||';        callAPI({ 'minor': 'yes', 'summary': '', 'action': 'edit', 'title': 'RuneScape:Counter-Vandalism Unit/Charms', 'token': response.query.pages[response.query.pageids[0]].edittoken, 'appendtext': logLine }, 'POST', function(response)            { });   }); }

} );