Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. function updateTable(pairs) {
  2. pairs.forEach(function(pair) {
  3. var hash = pair[0]
  4. var mult = pair[1]
  5. var color = 'gray'
  6. if (mult == 3) color = 'green'
  7. if (mult == 5) color = 'purple'
  8. if (mult == 50) color = 'orange'
  9. var tableBody = document.getElementById("tbody");
  10. tableBody.innerHTML += "<tr><td>" + hash + "</td><td style='background:" + color + "'>" + mult + "</td></tr>";
  11. })
  12. }
  13.  
  14. var maxHex = 13
  15. var maxInt = Math.pow(2, maxHex * 4)
  16. var clientSeed = '000000000000000007a9a31ff7f07463d91af6b5454241d5faf282e5e0fe1b3a'
  17. var pocketTypes = [{
  18. count: 1,
  19. multiplier: 50
  20. }, {
  21. count: 10,
  22. multiplier: 5
  23. }, {
  24. count: 17,
  25. multiplier: 3
  26. }, {
  27. count: 26,
  28. multiplier: 2
  29. }, ]
  30.  
  31. function previousHash(hash) {
  32. return CryptoJS.SHA256(hash).toString()
  33. };
  34.  
  35. function hmac(key, v) {
  36. var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
  37. return hmacHasher.finalize(v).toString();
  38. }
  39.  
  40. function toInt(hash) {
  41. return parseInt(hash.slice(0, maxHex), 16)
  42. }
  43.  
  44. function randomInt(hash, max) {
  45. hash = hmac(hash, clientSeed)
  46. var integer = toInt(hash)
  47. return Math.floor(max * (integer / maxInt))
  48. }
  49.  
  50. function createPockets(pocketTypes) {
  51. return pocketTypes.reduce(function(result, next) {
  52. return result.concat(Array(next.count).fill(next.multiplier))
  53. }, [])
  54. }
  55.  
  56. function pickRandom(hash, pockets) {
  57. var index = randomInt(hash, pockets.length)
  58. return pockets[index]
  59. }
  60.  
  61. function nPrevious(hash, n, result) {
  62. if(result == null) result = []
  63. if(n == 0) return result
  64. var previous = previousHash(hash)
  65. result.push(previous)
  66. return nPrevious(previous,--n,result)
  67. }
  68.  
  69. function getParams(name, url) {
  70. if (!url) url = window.location.href;
  71. name = name.replace(/[\[\]]/g, "\\$&");
  72. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  73. results = regex.exec(url);
  74. if (!results) return null;
  75. if (!results[2]) return '';
  76. return decodeURIComponent(results[2].replace(/\+/g, " "));
  77. }
  78.  
  79. function run() {
  80. var currentHash = getParams('hash')
  81. if (currentHash == null) {
  82. return console.log('must supply hash as url parameter')
  83. }
  84. var previousHashes = nPrevious(currentHash, 20)
  85. var hashes = [currentHash].concat(previousHashes)
  86.  
  87. var pockets = createPockets(pocketTypes)
  88. var results = hashes.map(function(hash) {
  89. return [hash, pickRandom(hash, pockets)]
  90. })
  91. updateTable(results)
  92. }
  93. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement