A Pen by Dave Campion

Thumbnail
This awesome code was written by gasolinewaltz, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright gasolinewaltz ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>A Pen by  Dave Campion</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/gasolinewaltz/a-pen-by-dave-campion-XVjajK */
(function(console){

    console.save = function(data, filename){

        if(!data) {
            console.error('Console.save: No data')
            return;
        }

        if(!filename) filename = 'console.json'

        if(typeof data === "object"){
            data = JSON.stringify(data, undefined, 4)
        }

        var blob = new Blob([data], {type: 'text/json'}),
            e    = document.createEvent('MouseEvents'),
            a    = document.createElement('a')

        a.download = filename
        a.href = window.URL.createObjectURL(blob)
        a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
        e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
        a.dispatchEvent(e)
    }
})(console)
let sumSequence = function(ratio, terms){
  
  return (1 * (1-Math.pow(ratio, terms))) / (1 - ratio);  
}

let nthInSequence = function(n, ratio){
  return ( 1 * (Math.pow(ratio, n - 1 )) );
}

let weights = {
  'common' : [],
  'uncommon' : [],
  'rare' : [],
  'legend' : [],
  'myth' : [0.0000153]
}

let weightSums = {
  'common' : 0,
  'uncommon' : 0,
  'rare' : 0,
  'legend' : 0,
  'myth' : 0
}

let generateWeightTable = function(table, nItems, ratio){
  let totalWeight = sumSequence(ratio, nItems);
  weights[table] = new Array(nItems);
  weights[table][nItems+1] = 1;

  for(let i = nItems; i >= 0; i -=1){
    weights[table][i] = weights[table][i+1] + nthInSequence((nItems - i)+1, ratio);
  }
}

let pickRandomWeightedItem = function(pool){
  let totalWeight = weightSums[pool];
  let randomWeight = Math.random() * totalWeight;
  let item = 0;
  for(; item < weights[pool].length; item += 1){
    randomWeight -= weights[pool][item];
    if( randomWeight <= 0){
      break
    }
  }
  
  return item+1 
}

let getItemClass = function(epochModifier){
  let r = Math.random();
  r = (r + epochModifier) % 1.2;
  let itemType = '';
  if( r <= 0.68-epochModifier){
    itemType = 'common';
  }else if(r > 0.68-epochModifier  && r <= 0.99-epochModifier) {
    itemType = 'uncommon';
  }else if(r > 0.99-epochModifier && r <= 0.997+epochModifier){
    itemType = 'rare';
  }else if( r > 0.997+epochModifier && r <= 0.9999-epochModifier){
    itemType = 'legend';
  }else{
    itemType = 'myth';
  }
  
  return itemType;
}


let ItemPool = function(){
  this.pool = {
    'common' : [],
    'uncommon' : [],
    'rare' : [],
    'legend' : [],
    'myth' : []
  }
  
}

ItemPool.prototype.toString = function(){
  let s = "";
  Object.keys(this.pool).forEach( type =>{
    s += type + " : " + this.pool[type].length + "\t | \t";
  });
  
  return s;
}

console.clear();
generateWeightTable('common', 136, 1.002);
generateWeightTable('uncommon', 60, 1.0024);
generateWeightTable('rare', 30, 1.0018);
generateWeightTable('legend', 20, 1.00018);

Object.keys(weights).forEach( key => {
  weightSums[key] = weights[key].reduce( (acc, val) => val + acc);
});

let pools = [];
let samples = 100
for( let poolNum = 0; poolNum < samples; poolNum += 1){
  pools.push( new ItemPool() );
  //let epochMod = (samples/5)/(samples-poolNum+1)*(Math.random()/1000);
  let useEpoch = 1;
 
  //let epochMod = (Math.random() * (0.003 - 0.00001) + 0.00001) * useEpoch;
    let epochMod = 0.007;
  
  for( let i = 0; i < 1000; i += 1){
  
    let item = getItemClass(epochMod);
    let rank =  pickRandomWeightedItem(item);
    pools[poolNum].pool[item].push(rank);
  }
  
  console.log("hour: ", poolNum+1+"", "epochMod: ",  epochMod+"", "\n",  pools[poolNum].toString() );
  
}
let averages = {};
pools.forEach(pool => {
  Object.keys(pool.pool).forEach(key =>{1
    if(averages[key]){
      averages[key] += pool.pool[key].length;
    }else{
      averages[key] = pool.pool[key].length;
    }
  });
  
});
console.log(averages);
let total = Object.keys(averages).reduce( (acc, val) => acc+averages[val], 0);
console.log(total);
let percents = Object.keys(averages).map( (val) => +(averages[val]/total).toFixed(5) );
console.log(percents);
console.log(percents.reduce( (acc, val) => acc+val));
Object.keys(averages).forEach( a => averages[a] /=  samples)
console.log(averages);

Comments