MediaWiki:Gadget-mmgkc-core.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
 
Line 3: Line 3:
// KPH is saved in local storage with the key "{Subpage name}-mmg-kph"
// KPH is saved in local storage with the key "{Subpage name}-mmg-kph"
// For ex. "Money making guide/Drinking irresponsibly" would go to the key "Drinking irresponsibly-mmg-kph"
// For ex. "Money making guide/Drinking irresponsibly" would go to the key "Drinking irresponsibly-mmg-kph"
// If the page is NOT a subpage, we will not save LS. This might be confusing but is preferable to all TL mw.pages using the same LS key.
// If the page is NOT a subpage, we will not save LS. This might be confusing but is preferable to all TL pages using the same LS key.


function parseMmgFloat(x) {
function parseMmgFloat(x) {

Latest revision as of 17:15, 17 October 2024

// Gets and sets items in local storage for the KPH amount
// This allows for repeat visits to use the same KPH.
// KPH is saved in local storage with the key "{Subpage name}-mmg-kph"
// For ex. "Money making guide/Drinking irresponsibly" would go to the key "Drinking irresponsibly-mmg-kph"
// If the page is NOT a subpage, we will not save LS. This might be confusing but is preferable to all TL pages using the same LS key.

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('.scp')[0].lastChild.nodeValue = ' ' +  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');
	
	var pageName = mw.config.get('wgTitle');
	
	defaultKPH = $this.attr('data-default-kph');
	defaultKPHname = $this.attr('data-default-kph-name');

	kphField = new OO.ui.NumberInputWidget({
		min: 0,
		input: { value: getKphLocalStorage(pageName) || defaultKPH },
		classes: ['mmg-kph-selector-field']
	});
	// We need to updateEverything since the KPH might not be the default
	updateEverything();
	
	var resetButton = new OO.ui.ButtonWidget( {
		icon: 'reload',
		label: 'Reset',
		invisibleLabel: true,
		classes: ['mmg-kph-refresh-field'],
		disabled: (getKphLocalStorage(pageName) || defaultKPH) === defaultKPH
	});
	resetButton.on('click', function() { 
		// Reset the key AFTER setValue since setValue would otherwise write default_kph to LS
		kphField.setValue(defaultKPH);
		resetButton.setDisabled( true )
		resetKphLocalStorage(pageName);
	});
	
	function saveToLocalStorageAndUpdate() {
		setKphLocalStorage(pageName, kphField.getValue());
		resetButton.setDisabled( kphField.getValue() === defaultKPH );
		updateEverything();
	}
	kphField.on('change', saveToLocalStorageAndUpdate ).on('enter', saveToLocalStorageAndUpdate);
	
	layout = new OO.ui.ActionFieldLayout( kphField, resetButton, {
		label: defaultKPHname,
		classes: ['mmg-kph-div'],
		help: 'Change the amount per hour you get here to update the numbers in the guide below.'
	});

	$this.before(layout.$element);
}

function getLSKeyNameForMmg(mmgName) {
	// mmgName should always have a "Money making guide/" prefix
	// This will work for anything that is a subpage and we could have some default for a top level page, but I don't want to pollute LS
	var mmg = mmgName.split('/')[1];
	if (mmg === undefined)
		return undefined;
	return mmg + '-mmg-kph';
}

function getKphLocalStorage(mmgName) {
	var lsKey = getLSKeyNameForMmg(mmgName);
	if (lsKey !== undefined)
		return localStorage.getItem(lsKey);
}

function setKphLocalStorage(mmgName, valueToUse) {
	var lsKey = getLSKeyNameForMmg(mmgName);
	if (lsKey !== undefined)
		localStorage.setItem(lsKey, valueToUse);
}

function resetKphLocalStorage(mmgName) {
	var lsKey = getLSKeyNameForMmg(mmgName);
	if (lsKey !== undefined)
		localStorage.removeItem(lsKey);
}

$(init);