MediaWiki:Gadget-mmgkc-core.js

From Old School Near-Reality Wiki
Revision as of 19:20, 3 October 2022 by Jacmob (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.
 1 function parseMmgFloat(x) {
 2 	var _x = parseFloat(x);
 3 	if (isNaN(_x)) {
 4 		return 0;
 5 	}
 6 	return _x;
 7 }
 8 
 9 function formatNum(x) {
10 	var _x = Math.abs(x);
11 	if (_x > 99) {
12 		// if over 100
13 		// round to 0 dp and format with commas if needed
14 		_x = Math.round(x);
15 		_x = _x.toLocaleString('en');
16 	} else if (_x < 0.1) {
17 		// if under 0.1
18 		// round to 2 sf
19 		var n = Math.floor(Math.log10(x)) - 1;
20 		_x = Math.pow(10, n) * Math.round(x/Math.pow(10,n));
21 		
22 		// cull binary representation error
23 		// probably a better way to do this
24 		_x = String(_x);
25 		_x = _x.replace(/([1-9])0000+\d$/, '$1');
26 	} else {
27 		// if between 99 and 0.1 (inclusive)
28 		// round to 2 dp 
29 		_x = Math.round(x*100)/100;
30 	}
31 	return _x;
32 }
33 
34 function coinsClasses($e, x) {
35 	$e.removeClass('coins-pos coins-neg');
36 	if (x > 0) {
37 		$e.addClass('coins-pos');
38 	} else if (x < 0) {
39 		$e.addClass('coins-neg');
40 	}
41 }
42 
43 var $this, defaultKPH, kphField, layout;
44 
45 function updateEverything() {
46 	var val = kphField.getNumericValue();
47 	if (isNaN(val)) val = defaultKPH;
48 	
49 	$('.mmg-kph.mmg-variable').each(function(i,e){
50 		$(e).text(formatNum(val));
51 	});
52 	
53 	$('.mmg-varieswithkph').each(function(i,e) {
54 		var $e = $(e);
55 		if ($e.hasClass('mmg-itemline')) {
56 			var newValue = parseMmgFloat($e.find('.mmg-cost').attr('data-mmg-cost-pk'))*val;
57 			$e.find('.mmg-quantity').text(formatNum(parseMmgFloat($e.find('.mmg-quantity').attr('data-mmg-qty'))*val));
58 			$e.find('.mmg-cost > span.coins').text(formatNum(newValue));
59 			coinsClasses($e.find('.mmg-cost > span.coins'), newValue);
60 		} else if ($e.hasClass('mmg-xpline')) {
61 			$e.find('.SkillClickPicXP').text(formatNum(parseMmgFloat($e.attr('data-mmg-xp-ph')) + parseMmgFloat($e.attr('data-mmg-xp-pk')) * val));
62 		} else {
63 			var $e2 = $e.find('> span.coins'),
64 				newValue = parseMmgFloat($e.attr('data-mmg-cost-ph')) + parseMmgFloat($e.attr('data-mmg-cost-pk')) * val,
65 				fNewVal = formatNum(newValue);
66 			
67 			if ($e2.length) {
68 				 $e2.text(fNewVal);
69 				 coinsClasses($e2, newValue);
70 			} else {
71 				$e.text(fNewVal);
72 			}
73 		}
74 	});
75 }
76 
77 function init() {
78 	$this = $('.mmg-table.mmg-isperkill');
79 	defaultKPH = $this.attr('data-default-kph');
80 	defaultKPHname = $this.attr('data-default-kph-name');
81 	
82 	kphField = new OO.ui.NumberInputWidget({
83 		min: 0,
84 		input: { value: defaultKPH },
85 		classes: ['mmg-kph-selector']
86 	});
87 	
88 	kphField.on('change', updateEverything).on('enter', updateEverything);
89 	
90 	layout = new OO.ui.FieldLayout(kphField, { 
91 		label: defaultKPHname,
92 		classes: ['mmg-kph-selector-field'],
93 		help: 'Change the amount per hour you get here to update the numbers in the guide below.'
94 	});
95 	$this.before(layout.$element);
96 }
97 
98 $(init);