User:Blackwolfe/common.js

From IdleOn MMO Wiki
< User:Blackwolfe
Revision as of 16:27, 20 May 2021 by imported>Blackwolfe

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.
/* Any JavaScript here will be loaded for all users on every page load. */
function nFormatter(num, digits) {
  var si = [
    { value: 1, symbol: "" },
    { value: 1E3, symbol: "k" },
    { value: 1E6, symbol: "M" },
    { value: 1E9, symbol: "G" },
    { value: 1E12, symbol: "T" },
    { value: 1E15, symbol: "P" },
    { value: 1E18, symbol: "E" }
  ];
  var rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
  var i;
  for (i = si.length - 1; i > 0; i--) {
    if (num >= si[i].value) {
      break;
    }
  }
  return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
}

function getCostTotal(base,level,cLevel) {
	var res = base
	for (i = 1; i < level;i++){
		res += base + i*cLevel;
	console.log(res)
	}
	if (level == 0){
		return 0;
	} else {
		return res;
	}
}


function coindisplay(coins) {
  var x = parseInt(coins);
  var copper = x%100;
  var silver = (x%10000 - copper) / 100;
  var gold = (x%1000000 - copper - (100 * silver)) / 10000;
  var plat = (x%100000000 - copper - (100 * silver) - (10000 * gold)) / 1000000;
  var dem = (x - copper - (100 * silver) - (10000 * gold) - (1000000 * plat)) / 100000000;
  var copperimg = "<img src=https://static.miraheze.org/idleonwiki/4/43/Copper_Coin.png>";
  var silverimg = "<img src=https://static.miraheze.org/idleonwiki/4/4e/Silver_Coin.png>";
  var goldimg = "<img src=https://static.miraheze.org/idleonwiki/1/1f/Gold_Coin.png>";
  var platimg = "<img src=https://static.miraheze.org/idleonwiki/2/2c/Platinum_Coin.png>";
  var demimg = "<img src=https://static.miraheze.org/idleonwiki/5/53/Dementia_Coin.png>";
  var out = "";
  if (dem) {out += demimg  + dem + " " }
  if (plat) {out +=  platimg + plat + " "}
  if (gold) {out +=  goldimg + gold + " "}
  if (silver) {out +=  silverimg + silver + " "}
  if (copper) {out +=  copperimg + copper}
  return out;
}

function calcBubbleMatCost(bubbleLvl, baseCost,isLiquid, cauldCostReduxLvl, bubbleCostBubbleLvl, bubbleCostVialLvl) {
	if (isLiquid == '1'){
		console.log(baseCost,bubbleLvl,baseCost + Math.floor(bubbleLvl/20));
		return baseCost + Math.floor(bubbleLvl / 20);
	} else {
		first = baseCost * Math.pow( 1.35 - (0.3 * bubbleLvl)/ (50 + bubbleLvl), bubbleLvl);
		const costReduxBoost = Math.round(10 * lavaFunc("decay",cauldCostReduxLvl, 90, 100)) / 10;
		const oa = Math.max(0.1, 1 - costReduxBoost / 100);
		// for any material besides liquid
		return Math.round(first * oa * Math.max(0.05, 1 - (lavaFunc("decay",bubbleCostBubbleLvl, 40, 70) + lavaFunc("add",bubbleCostVialLvl, 1, 0)) / 100));
	}
}
function round(num){
	return Math.round((num + Number.EPSILON) * 100) / 100;
}
function lavaLog(num){
	return Math.log(Math.max(num, 1)) / 2.303;
}

function goldFoodBonus(amount,stack){
	return round(amount*0.05*lavaLog(1+stack)*(1+ lavaLog(1+stack)/2.14));
}


function lavaFunc(func,level,x1,x2) {
	var result = 0;
	switch(func) {
    	case 'add':
			if (x2 != 0) {
			  result = (((x1+x2)/x2 +0.5*(level-1))/(x1/x2))*level*x1;
			} else {
				result = level*x1;
			}
			break;
		case 'decay':
			result = (level*x1)/(level+x2);
			break;
		case 'intervalAdd':
			result = x1 + Math.floor(level/x2);
			break;
		case 'decayMulti':
			result = 1 + (level*x1)/(level+x2);
			break;
		case 'bigBase':
			result = x1 + x2*level;
			break;
		default:
			result = 0;
	}
	return round(result);
}

$(document).ready(function(){
	console.log("UPDATED22");
    $.each($('.skillDiv'), function(){
    	var container = $(this);
    	var x1 = parseFloat($(this).data('x1'));
    	var x2 = parseFloat($(this).data('x2'));
    	var max = parseFloat($(this).data('max'));
    	var func1 = $(this).data('func1');
    	var y1 = parseFloat($(this).data('y1'));
    	var y2 = parseFloat($(this).data('y2'));
    	var func2 = $(this).data('func2');
    	$('.result1', container).text(0);
    	$('.result2', container).text(0);
    	$('.valInput', this).html('(<input id="inField" type="number" min="0" max="'+max+'" placeholder=" Level "/>) ');
    	$("#inField", this).change(function(){
    		var inVal = parseInt($(this).val());
    		$('.result1', container).html('<p> '+lavaFunc(func1,inVal,x1,x2)+'</p>');
    		$('.result2', container).html('<p> '+lavaFunc(func2,inVal,y1,y2)+'</p>');
    	});
    });
    $.each($('.guildBonusDiv'), function(){
    	var container = $(this);
    	var x1 = parseFloat($(this).data('x1'));
    	var x2 = parseFloat($(this).data('x2'));
    	var max = parseFloat($(this).data('max'));
    	var func1 = $(this).data('func');
    	var baseC = parseFloat($(this).data('basec'));
    	var incC = parseFloat($(this).data('incc'));
    	$('.result1', container).text(0);
    	$('.result2', container).text(0);
    	$('.cost', container).text(baseC);
    	$('.valInput', this).html('(<input id="inField" type="number" min="0" max="'+max+'" placeholder=" Level "/>) ');
    	$("#inField", this).change(function(){
    		var inVal = parseInt($(this).val());
    		$('.result1', container).text(lavaFunc(func1,inVal,x1,x2));
    		$('.result2', container).text(10+inVal);
    		$('.cost', container).text(baseC + inVal*incC);
    		$('.tcost', container).text(getCostTotal(baseC,inVal,incC));
    	});
    });
	$.each($('.boxDiv'), function(){
        var container = $(this);
        var func1 = $(this).data('func1');
        var x1 = $(this).data('x1');
        var x2 = $(this).data('x2');
        var func2 = $(this).data('func2');
        var y1 = $(this).data('y1');
        var y2 = $(this).data('y2');
        var func3 = $(this).data('func3');
        var z1 = $(this).data('z1');
        var z2 = $(this).data('z2');
        $('.result1', container).text(0);
        $('.result2', container).text(0);
        $('.result3', container).text(0);
        $('.valInput', this).html('<input id="inField" type="number" min="0" placeholder=" Level " style="width: 10%px;"/>');
        $("#inField", this).change(function(){
            var inVal = parseInt($(this).val());
            $('.result1', container).text(lavaFunc(func1,inVal,x1,x2));
            $('.result2', container).text(Math.max(0,lavaFunc(func2,inVal-25,y1,y2)));
            $('.result3', container).text(Math.max(0,lavaFunc(func3,inVal-100,z1,z2)));
        });
	});
    $.each($('.stampDiv'), function(){
    	var container = $(this);
    	var x1 = parseFloat($(this).data('x1'));
    	var x2 = parseFloat($(this).data('x2'));
    	var func = $(this).data('func');
    	var i4 = parseFloat($(this).data('i4'));
    	var i6 = parseFloat($(this).data('i6'));
    	var i7 = parseFloat($(this).data('i7'));
    	var i8 = parseFloat($(this).data('i8'));
    	var i9 = parseFloat($(this).data('i9'));
    	$('.resultB', container).text(0);
    	$('.resultC', container).text(0);
    	$('.resultM', container).text(0);
    	$('.valInput', this).html('<input id="inField" type="number" min="0" placeholder=" Level " style="width: 10%px;"/>');
    	$("#inField", this).change(function(){
    		var inVal = parseInt($(this).val());
    		var goldCost = i8 * Math.pow(i9 - (inVal / (inVal + 5 * i4)) * 0.25, inVal * (10 / i4));
			var materialCost = i6 * Math.pow(i7, Math.pow(Math.round(inVal / i4) - 1, 0.8));
    		$('.resultB', container).text(lavaFunc(func,inVal,x1,x2));
    		$('.resultC', container).html(coindisplay(Math.floor(goldCost)));
    		if (inVal % i4 == 0){
    			$('.resultM', container).text(Math.floor(materialCost).toLocaleString());
    		} else {
    			$('.resultM', container).text(i4 - (inVal % i4) + " Upgrades left before you need to increase max level.");
    		}
    	});
    });
    $.each($('.gfoodDiv'), function(){
    	var container = $(this);
    	var amount = parseFloat($(this).data('amount'));
    	$('.result', container).text(0);
    	$('.valInput', this).html('<input id="inField" type="number" min="0" placeholder=" Quantity " style="width: 10%px;"/>');
    	$("#inField", this).change(function(){
    		var inVal = parseInt($(this).val());
    		$('.result', container).text(goldFoodBonus(amount,inVal));
    	});
    });
    $.each($('.statueDiv'), function(){
    	var container = $(this);
    	var perLv = parseFloat($(this).data('amount'));
    	$('.result1', container).text(0);
    	$('.result2', container).text(0);
    	$('.valInput', this).html('<input id="inField" type="number" min="0" placeholder=" Level " style="width: 10%px;"/>');
    	$("#inField", this).change(function(){
    		var inVal = parseInt($(this).val());
    		$('.result1', container).text(round(inVal*perLv));
    		$('.result2', container).text(Math.round(Math.pow(inVal,1.17)*Math.pow(1.35,inVal/10)+1) + " Required statues to level this bonus.");
    	});
    });
    $.each($('.cauldDiv'), function(){
    	var cauldCostReduxLvl = 0
    	var bubbleCostBubbleLvl = 0
    	var bubbleCostVialLvl = 0
    	$('.cauldCostReduxLvlInput', this).html('<input id="inCauldCostReduxLvl" type="number" min="0" placeholder=" Level "/>');
    	$('.bubbleCostBubbleInput', this).html('<input id="inBubbleCostBubbleLvl" type="number" min="0" placeholder=" Level "/>');
    	$('.bubbleCostVialInput', this).html('<input id="inBubbleCostVialLvl" type="number" min="0" placeholder=" Level "/>');
    	$("#inCauldCostReduxLvl", this).change(function(){
    		cauldCostReduxLvl = parseInt($(this).val());
    	});
    	$("#inBubbleCostBubbleLvl", this).change(function(){
    		bubbleCostBubbleLvl = parseInt($(this).val());
    	});
    	$("#inBubbleCostVialLvl", this).change(function(){
    		bubbleCostVialLvl = parseInt($(this).val());
    	});
    	$.each($('.bubbleDiv'), function(){
    		var container = $(this);
    		var baseCost = $(this).data('basecost').split(',')
    		var isLiquid = $(this).data('isliquid').split(',')
    		var x1 = parseFloat($(this).data('x1'));
    		var x2 = parseFloat($(this).data('x2'));
    		var func = $(this).data('func');
    		$('.valInput', this).html('<input id="inField" type="number" min="0" placeholder=" Level "/>');
    		for(i=0;i<baseCost.length;i++){
				var div = '.material' +(i+1);
				$(div, container).text(0);
			}
			$(".bonusOut", container).text(0);
    		$("#inField", this).change(function(){
    			var inVal = parseInt($(this).val());
				for(i=0;i<baseCost.length;i++){
					var div = '.material' +(i+1);
					var cost = calcBubbleMatCost(inVal, parseInt(baseCost[i]),isLiquid[i], cauldCostReduxLvl, bubbleCostBubbleLvl, bubbleCostVialLvl);
					$(div, container).text(nFormatter(cost,1))
				}
				$(".bonusOut", container).text(round(lavaFunc(func,inVal,x1,x2)))
    		});	
    	});
    });
});

mw.loader.load('https://meta.miraheze.org/w/index.php?title=User:K599/TwitchPlayer.js&action=raw&ctype=text/javascript');
mw.loader.using('mobile.site.styles')

/* Sets the top property for stickyHeader tables */
/* Code borrowed with full permission from Coolrox95 on the Melvor Idle Wiki */
function setStickyHeaderTop() {
  const stickyTables = document.getElementsByClassName('stickyHeader');
  const headHeight = document.getElementById('mw-header-container').offsetHeight;
  for (var i = 0; i < stickyTables.length; i++) {
    const firstRow = stickyTables[i].getElementsByClassName('headerRow-0');
    const secondRow = stickyTables[i].getElementsByClassName('headerRow-1');
    var firstHeight = 0;
    if (firstRow.length > 0) {
      firstHeight = firstRow[0].offsetHeight;
      const firstHeaders = firstRow[0].getElementsByTagName('th');
      for (var j = 0; j < firstHeaders.length; j++) {
        firstHeaders[j].style.top = headHeight + 'px';
      }
      if (secondRow.length > 0) {
        const secondHeaders = secondRow[0].getElementsByTagName('th');
        var secondHeight = headHeight + firstHeight;
        for (var j = 0; j < secondHeaders.length; j++) {
          secondHeaders[j].style.top = secondHeight + 'px';
        }
      }
    }
  }
}
$(document).ready(function () {
  if (document.getElementsByClassName('stickyHeader').length > 0) {
    setStickyHeaderTop();
    $(window).resize(setStickyHeaderTop);
  }
}
);