Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1. Save this in ILeveling.js ```// Converted From: https://github.com/HypixelDev/PublicAPI/blob/master/Java/src/main/java/net/hypixel/api/util/ILeveling.java
  2.  
  3. const BASE = 10000;
  4. const GROWTH = 2500;
  5.  
  6. /* Constants to generate the total amount of XP to complete a level */
  7. const HALF_GROWTH = 0.5 * GROWTH;
  8.  
  9. /* Constants to look up the level from the total amount of XP */
  10. const REVERSE_PQ_PREFIX = -(BASE - 0.5 * GROWTH) / GROWTH;
  11. const REVERSE_CONST = REVERSE_PQ_PREFIX * REVERSE_PQ_PREFIX;
  12. const GROWTH_DIVIDES_2 = 2 / GROWTH;
  13.  
  14. /**
  15. * This method returns players network level. Uses direct values from the API.
  16. */
  17. function getTrueLevel(networkExp, networkLevel) {
  18.     return getLevel(networkExp + getTotalExpToLevel(networkLevel + 1))
  19. }
  20.  
  21. /**
  22. * This method returns the level of a player calculated by the current experience gathered. The result is
  23. * a precise level of the player The value is not zero-indexed and represents the absolute visible level
  24. * for the player.
  25. * The result can't be smaller than 1 and negative experience results in level 1.
  26. * <p>
  27. * Examples:
  28. * -        0 XP = 1.0
  29. * -     5000 XP = 1.0
  30. * -    10000 XP = 2.0
  31. * -    50000 XP = 4.0
  32. * - 79342431 XP = 249.0
  33. *
  34. * @param exp Total experience gathered by the player.
  35. * @return number level of player (Smallest value is 1.0)
  36. */
  37. function getLevel(exp) {
  38.     return exp <= 1 ? 1 : Math.floor(1 + REVERSE_PQ_PREFIX + Math.sqrt(REVERSE_CONST + GROWTH_DIVIDES_2 * exp));
  39. }
  40.  
  41. /**
  42. * This method returns the level of a player calculated by the current experience gathered. The result is
  43. * a precise level of the player The value is not zero-indexed and represents the visible level
  44. * for the player.
  45. * The result can't be smaller than 1 and negative experience results in level 1.
  46. * <p>
  47. * Examples:
  48. * -        0 XP = 1.0
  49. * -     5000 XP = 1.5
  50. * -    10000 XP = 2.0
  51. * -    50000 XP = 4.71...
  52. * - 79342431 XP = 249.46...
  53. *
  54. * @param exp Total experience gathered by the player.
  55. * @return Exact level of player (Smallest value is 1.0)
  56. */
  57. function getExactLevel(exp) {
  58.     return getLevel(exp) + getPercentageToNextLevel(exp);
  59. }
  60.  
  61. /**
  62. * This method returns the amount of experience that is needed to progress from level to level + 1. (5 to 6)
  63. * The levels passed must absolute levels with the smallest level being 1. Smaller values always return
  64. * the BASE constant. The calculation is precise and if a decimal is passed it returns the XP from the
  65. * progress of this level to the next level with the same progress. (5.5 to 6.5)
  66. * <p>
  67. * Examples:
  68. * -   1 (to 2)   =  10000.0 XP
  69. * -   2 (to 3)   =  12500.0 XP
  70. * -   3 (to 4)   =  15000.0 XP
  71. * -   5 (to 6)   =  20000.0 XP
  72. * - 5.5 (to 6.5) =  21250.0 XP
  73. * - 130 (to 131) = 332500.0 XP
  74. * - 250 (to 251) = 632500.0 XP
  75. *
  76. * @param level Level from which you want to get the next level with the same level progress
  77. * @return number to reach the next level with same progress
  78. */
  79. function getExpFromLevelToNext(level) {
  80.     return level < 1 ? BASE : GROWTH * (level - 1) + BASE;
  81. }
  82.  
  83. /**
  84. * This method returns the experience it needs to reach that level. If you want to reach the given level
  85. * you have to gather the amount of experience returned by this method. This method is precise, that means
  86. * you can pass any progress of a level to receive the experience to reach that progress. (5.764 to get
  87. * the experience to reach level 5 with 76.4% of level 6.
  88. * <p>
  89. * Examples:
  90. * -    1.0 =        0.0 XP
  91. * -    2.0 =    10000.0 XP
  92. * -    3.0 =    22500.0 XP
  93. * -    5.0 =    55000.0 XP
  94. * -  5.764 =    70280.0 XP
  95. * -  130.0 = 21930000.0 XP
  96. * - 250.43 = 79951975.0 XP
  97. *
  98. * @param level The level and progress of the level to reach
  99. * @return The experience required to reach that level and progress
  100. */
  101. function getTotalExpToLevel(level) {
  102.     const lv = Math.floor(level), x0 = getTotalExpToFullLevel(lv);
  103.     if (level === lv) return x0;
  104.     return (getTotalExpToFullLevel(lv + 1) - x0) * (level % 1) + x0;
  105. }
  106.  
  107. /**
  108. * Helper method that may only be called by full levels and has the same functionality as getTotalExpToLevel()
  109. * but doesn't support progress and returns wrong values for progress due to perfect curve shape.
  110. *
  111. * @param level Level to receive the amount of experience to
  112. * @return Experience to reach the given level
  113. */
  114. function getTotalExpToFullLevel(level) {
  115.     return (HALF_GROWTH * (level - 2) + BASE) * (level - 1);
  116. }
  117.  
  118. /**
  119. * This method returns the current progress of this level to reach the next level. This method is as
  120. * precise as possible due to rounding errors on the mantissa. The first 10 decimals are totally
  121. * accurate.
  122. * <p>
  123. * Examples:
  124. * -     5000.0 XP   (Lv. 1) = 0.5                               (50 %)
  125. * -    22499.0 XP   (Lv. 2) = 0.99992                       (99.992 %)
  126. * -  5324224.0 XP  (Lv. 62) = 0.856763076923077   (85.6763076923077 %)
  127. * - 23422443.0 XP (Lv. 134) = 0.4304905109489051 (43.04905109489051 %)
  128. *
  129. * @param exp Current experience gathered by the player
  130. * @return Current progress to the next level
  131. */
  132. function getPercentageToNextLevel(exp) {
  133.     const lv = getLevel(exp), x0 = getTotalExpToLevel(lv);
  134.     return (exp - x0) / (getTotalExpToLevel(lv + 1) - x0);
  135. }
  136. //xp - getTotalExpToLevel(ILeveling.getLevel(XP))
  137.  
  138. module.exports = {
  139.     getTrueLevel,
  140.     getLevel,
  141.     getExactLevel,
  142.     getExpFromLevelToNext,
  143.     getTotalExpToLevel,
  144.     getTotalExpToFullLevel,
  145.     getPercentageToNextLevel
  146. };```
  147. 2. Load the file in your bot `const ILeveling = require("./ILeveling")`
  148. 3. Call the function `const level = ILeveling.getTrueLevel(networkExp || 0, networkLevel || 0)`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement