User:The JoTS/wikia.js

// Calculates a user's badge score // ... except wikia already does that automatically // by https://dev.wikia.com/wiki/User:The_JoTS

// CSV representation of Google Doc table // dern wikia not supporting template strings var BADGE_EXP_VALUES = ",Type,Category,W-Points,Description,XP Reward" + ",Bronze,Special Achievements,10,Awarded for joining the wiki,10" + ",Bronze,,10,Awarded for making 1 edit on an article,10" + ",Bronze,,10,Awarded for making 5 edits on articles,10" + ",Bronze,,10,Awarded for making 10 edits on articles,10" + ",Silver,,50,Awarded for making 25 edits on articles,50" + ",Silver,,50,Awarded for making 50 edits on articles,50" + ",Silver,,50,Awarded for making 100 edits on articles,50" + ",Gold,,100,Awarded for making 250 edits on articles,100" + ",Gold,,100,Awarded for making 500 edits on articles,100" + ",Bronze,,10,Awarded for adding 1 picture to an article,5" + ",Bronze,,10,Awarded for adding 5 pictures to articles,10" + ",Bronze,,10,Awarded for adding 10 pictures to articles,15" + ",Silver,,50,Awarded for adding 25 pictures to articles,30" + ",Silver,,50,Awarded for adding 50 pictures to articles,50" + ",Silver,,50,Awarded for adding 100 pictures to articles,60" + ",Gold,,100,Awarded for adding 250 pictures to articles,100" + ",Gold,,100,Awarded for adding 500 pictures to articles,130" + ",Bronze,,10,Awarded for adding 1 article to a category,10" + ",Bronze,,10,Awarded for adding 5 articles to categories,10" + ",Bronze,,10,Awarded for adding 10 articles to categories,10" + ",Silver,,50,Awarded for adding 25 articles to categories,50" + ",Silver,,50,Awarded for adding 50 articles to categories,50" + ",Silver,,50,Awarded for adding 100 articles to categories,50" + ",Gold,,100,Awarded for adding 250 articles to categories,100" + ",Bronze,Blog Post Track,10,Awarded for writing 1 blog post,10" + ",Bronze,Blog Comment Track,10,Awarded for writing a comment on 3 different blog posts,10" + ",Silver,Blog Comment Track,50,Awarded for writing a comment on 10 different blog posts,50" + ",Silver,Wiki Love Track,50,Awarded for contributing to the wiki every day for 5 days,50" + ",Gold,Wiki Love Track,100,Awarded for contributing to the wiki every day for 14 days,100" + ",Gold,Wiki Love Track,100,Awarded for contributing to the wiki every day for 30 days,100" + ",Gold,Wiki Love Track,100,Awarded for contributing to the wiki every day for 60 days,100" + ",Gold,Wiki Love Track,100,Awarded for contributing to the wiki every day for 100 days,100" + ",Gold,Wiki Love Track,100,Awarded for contributing to the wiki every day for 200 days,100" + ",Platinum,Wiki Love Track,250,Awarded for contributing to the wiki every day for 365 days,250" + ",Bronze,Special Achievements,10,Awarded for adding to your own user page,10" + ",Bronze,Special Achievements,10,Awarded for leaving a message on someone else's talk page,10" + ",Gold,Special Achievements,100,Awarded for creating the wiki,100" + ",Silver,Secret Achievements,50,Awarded for making edits on 100 pages within an hour of the page's creation,50" + ",Silver,Secret Achievements,50,Awarded for making 100 edits on pages in a single day,50" + ",Gold,Secret Achievements,100,\"Awarded for making the Lucky 1,000th edit on the wiki\",100";

if(mw.config.get("wgNamespaceNumber") === 2) $(function {	var $badgesList = $("ul.badges:first");	var $prev = $badgesList.nextAll(".badges-prev"),		$next = $badgesList.nextAll(".badges-next");	var $calcButton = $(" ");	var points = 0;	var flags = {		nav: false,		calc: false	};	/** Calculates EXP of badges on the current page then calls itself	* (via onNextPageDo) to calculate exp on the next pages	*/	function calcPageBadgePts(stop) {		flags.calc = true;		for(var $badge = $("").prependTo($badgesList); ($badge = $badge.next("li[class|=badge]")).length;)		{			var badgeDex = $badge.find(".profile-hover-text p:first")				.text				.replace('\n',' ')				.replace('!','');

points += parseInt(					// Half-assed way to retrieve. Must be changed if columns change.					// Consider using table headings in the CSV to find dex column.					(new RegExp(badgeDex + '"?,([0-9]+)','mg').exec(BADGE_EXP_VALUES)||[])[1]				|| console.error("An undefined badge was found") || 0			);		}		if(!onNextPageDo(calcPageBadgePts)) flags.calc = false;	} // end calcPageBadgePts	/**	* Returns to the very first page of awarded badges	*/	function gotoFirstPage {		var clicker;		flags.nav = true;		clicker = setInterval(function {			if($prev.is(":visible")) {				if(!$badgesList.children(".wikiaThrobber").length) // wait for load					$prev.click; // else yield			} else {				clearInterval(clicker);				flags.nav = false;			}		}, 800);	}	/**	* Loads the next page of badges and then executes the provided callback	*/	function onNextPageDo(func) {		if($next.is(":visible")) {			var throbWait; $next.click; throbWait = setInterval(function {				if(!$badgesList.children(".wikiaThrobber").length) {					clearInterval(throbWait);					func;				}			}, 800); return true; } else return false; }		/**	* Toggles the visibility of the badge page navigation buttons */	function showNavButtons(visible) { $prev.children.css("visibility", visible ? "visible" : "hidden"); $next.children.css("visibility", visible ? "visible" : "hidden"); }	/**	* Checks whether or not a flag has met the necessary condition, * else yields until the desired state is met. Executes callback after yield. */	function checkFlag(flag, cond, f) { if(flags[flag] != cond) setTimeout(checkFlag, 100, flag, cond, f); else f; }	/**	* Onclick function */	function calcAndDisplayPts { // Start var $status = $(" ") .text("Calculating...") .insertAfter($badgesList); $calcButton .off .hide; showNavButtons(false); // Navigate to first page and yield until done gotoFirstPage; checkFlag("nav", false, function {			// Calculate points and yield until done			calcPageBadgePts;			checkFlag("calc", false, function { // Output results $status.text("User's Points: " + String(points)); showNavButtons(true); });		});	}	// Style & Insert button var insertButton = function { $calcButton .text("Calculate") .css("clear", "both") .click(calcAndDisplayPts) .insertAfter($badgesList); };	if($("#WikiaRail").length) insertButton; else $('#WikiaRail').on('afterLoad.rail', insertButton); });