MediaWiki:Gadget-mmgkc-core.js

From Zeah RSPS - Wiki
Revision as of 08:13, 16 February 2022 by Soulgazer (talk | contribs) (Created page with "function parseMmgFloat(x) { var _x = parseFloat(x); if (isNaN(_x)) { return 0; } return _x; } function formatNum(x) { var _x = Math.abs(x); if (_x > 99) { // if over 100 // round to 0 dp and format with commas if needed _x = Math.round(x); _x = _x.toLocaleString('en'); } else if (_x < 0.1) { // if under 0.1 // round to 2 sf var n = Math.floor(Math.log10(x)) - 1; _x = Math.pow(10, n) * Math.round(x/Math.pow(10,n)); // cull binary representat...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
function parseMmgFloat(x) {
	var _x = parseFloat(x);
	if (isNaN(_x)) {
		return 0;
	}
	return _x;
}

function formatNum(x) {
	var _x = Math.abs(x);
	if (_x > 99) {
		// if over 100
		// round to 0 dp and format with commas if needed
		_x = Math.round(x);
		_x = _x.toLocaleString('en');
	} else if (_x < 0.1) {
		// if under 0.1
		// round to 2 sf
		var n = Math.floor(Math.log10(x)) - 1;
		_x = Math.pow(10, n) * Math.round(x/Math.pow(10,n));
		
		// cull binary representation error
		// probably a better way to do this
		_x = String(_x);
		_x = _x.replace(/([1-9])0000+\d$/, '$1');
	} else {
		// if between 99 and 0.1 (inclusive)
		// round to 2 dp 
		_x = Math.round(x*100)/100;
	}
	return _x;
}

function coinsClasses($e, x) {
	$e.removeClass('coins-pos coins-neg');
	if (x > 0) {
		$e.addClass('coins-pos');
	} else if (x < 0) {
		$e.addClass('coins-neg');
	}
}

var $this, defaultKPH, kphField, layout;

function updateEverything() {
	var val = kphField.getNumericValue();
	if (isNaN(val)) val = defaultKPH;
	
	$('.mmg-kph.mmg-variable').each(function(i,e){
		$(e).text(formatNum(val));
	});
	
	$('.mmg-varieswithkph').each(function(i,e) {
		var $e = $(e);
		if ($e.hasClass('mmg-itemline')) {
			var newValue = parseMmgFloat($e.find('.mmg-cost').attr('data-mmg-cost-pk'))*val;
			$e.find('.mmg-quantity').text(formatNum(parseMmgFloat($e.find('.mmg-quantity').attr('data-mmg-qty'))*val));
			$e.find('.mmg-cost > span.coins').text(formatNum(newValue));
			coinsClasses($e.find('.mmg-cost > span.coins'), newValue);
		} else if ($e.hasClass('mmg-xpline')) {
			$e.find('.SkillClickPicXP').text(formatNum(parseMmgFloat($e.attr('data-mmg-xp-ph')) + parseMmgFloat($e.attr('data-mmg-xp-pk')) * val));
		} else {
			var $e2 = $e.find('> span.coins'),
				newValue = parseMmgFloat($e.attr('data-mmg-cost-ph')) + parseMmgFloat($e.attr('data-mmg-cost-pk')) * val,
				fNewVal = formatNum(newValue);
			
			if ($e2.length) {
				 $e2.text(fNewVal);
				 coinsClasses($e2, newValue);
			} else {
				$e.text(fNewVal);
			}
		}
	});
}

function init() {
	$this = $('.mmg-table.mmg-isperkill');
	defaultKPH = $this.attr('data-default-kph');
	defaultKPHname = $this.attr('data-default-kph-name');
	
	kphField = new OO.ui.NumberInputWidget({
		min: 0,
		input: { value: defaultKPH },
		classes: ['mmg-kph-selector']
	});
	
	kphField.on('change', updateEverything).on('enter', updateEverything);
	
	layout = new OO.ui.FieldLayout(kphField, { 
		label: defaultKPHname,
		classes: ['mmg-kph-selector-field'],
		help: 'Change the amount per hour you get here to update the numbers in the guide below.'
	});
	$this.before(layout.$element);
}

$(init);