|
| 1 | +package net.hypixel.api.util; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public interface IGuildLeveling { |
| 8 | + |
| 9 | + // TODO: 6/6/20 Finish javadocs and add examples |
| 10 | + |
| 11 | + /** |
| 12 | + * An unmodifiable list containing the experience needed to get from each level to the next. For |
| 13 | + * example, the value at index 0 is the amount of guild experience needed to progress from guild |
| 14 | + * level 0 to 1. The value at index 7 is the guild exp needed to progress from level 7 -> 8. |
| 15 | + * Etc. |
| 16 | + * <p> |
| 17 | + * The last element in this list is the value used for any levels beyond the size of this list. |
| 18 | + * For example, if this list has 15 values, then the last value is used for levels 14 -> 15 and |
| 19 | + * any levels after that. |
| 20 | + */ |
| 21 | + List<Integer> EXP_NEEDED = Collections.unmodifiableList(Arrays.asList( |
| 22 | + 100000, // Lvl 0 -> Lvl 1 |
| 23 | + 150000, // Lvl 1 -> Lvl 2 |
| 24 | + 250000, // Lvl 2 -> Lvl 3 |
| 25 | + 500000, // Etc |
| 26 | + 750000, |
| 27 | + 1000000, |
| 28 | + 1250000, |
| 29 | + 1500000, |
| 30 | + 2000000, |
| 31 | + 2500000, |
| 32 | + 2500000, |
| 33 | + 2500000, |
| 34 | + 2500000, |
| 35 | + 2500000, |
| 36 | + 3000000 |
| 37 | + )); |
| 38 | + |
| 39 | + /** |
| 40 | + * The last value in {@link #EXP_NEEDED}. This represents exp difference between any two levels |
| 41 | + * >= {@link #EXP_NEEDED}.size() - 1. |
| 42 | + * |
| 43 | + * @see #EXP_NEEDED |
| 44 | + */ |
| 45 | + int MAX_EXP_NEEDED = EXP_NEEDED.get(EXP_NEEDED.size() - 1); |
| 46 | + |
| 47 | + /** |
| 48 | + * This method returns the full level of a guild with that amount of guild experience. This |
| 49 | + * method does not take into account the guild's progress to the next level, but will return an |
| 50 | + * integer representing the last whole guild level reached by the guild. If the experience |
| 51 | + * parameter is less than 0, an {@link IllegalArgumentException} will be thrown. |
| 52 | + * |
| 53 | + * @param exp The total experience gathered by a guild; should be >= 0 |
| 54 | + * @return An integer representing the guild's current whole level |
| 55 | + */ |
| 56 | + static double getLevel(double exp) { |
| 57 | + if (exp < 0) { |
| 58 | + throw new IllegalArgumentException("Experience value must be >= 0"); |
| 59 | + } |
| 60 | + |
| 61 | + for (int level = 0; ; level++) { |
| 62 | + double needed = getExpFromLevelToNext(level); |
| 63 | + exp -= needed; |
| 64 | + |
| 65 | + if (exp < 0) { |
| 66 | + return level; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * This method returns the precise guild level for that amount of guild experience. This is the |
| 73 | + * equivalent of adding up the result of {@link #getLevel(double)} and {@link |
| 74 | + * #getPercentageToNextLevel(double)}. The value returned by this method is a floating point |
| 75 | + * number greater than or equal to 0, representing the guild's previse level. If the experience |
| 76 | + * parameter is less than 0, an {@link IllegalArgumentException} may be thrown. |
| 77 | + * |
| 78 | + * @param exp The total experience gathered by a guild; should be >= 0 |
| 79 | + * @return Exact level of a guild with that much experience |
| 80 | + */ |
| 81 | + static double getExactLevel(double exp) { |
| 82 | + return getLevel(exp) + getPercentageToNextLevel(exp); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * This method returns the amount of experience needed to go from that level to the next. If the |
| 87 | + * level parameter is less than 0, an {@link IllegalArgumentException} will be thrown. |
| 88 | + * |
| 89 | + * @param level The starting level |
| 90 | + * @return The amount of guild exp needed to progress from that level to the next level |
| 91 | + * @see #EXP_NEEDED |
| 92 | + * @see #MAX_EXP_NEEDED |
| 93 | + */ |
| 94 | + static double getExpFromLevelToNext(double level) { |
| 95 | + if (level < 0) { |
| 96 | + throw new IllegalArgumentException("Level value must be >= 0"); |
| 97 | + } |
| 98 | + |
| 99 | + return level >= EXP_NEEDED.size() ? MAX_EXP_NEEDED : EXP_NEEDED.get((int) level); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * This method returns the amount of guild experience needed to reach a precise level. For |
| 104 | + * example, passing in a level of 10.5 will return the amount of exp needed for level 10 plus |
| 105 | + * half the amount of exp needed between levels 10 and 11. If the level parameter is less than |
| 106 | + * 0, an {@link IllegalArgumentException} may be thrown. |
| 107 | + * |
| 108 | + * @param level The precise level reached with the returned amount of experience; should be >= |
| 109 | + * 0 |
| 110 | + * @return The total experience needed to reach that precise level |
| 111 | + */ |
| 112 | + static double getTotalExpToLevel(double level) { |
| 113 | + double progress = level - (int) level; |
| 114 | + return getTotalExpToFullLevel(level) + (progress * getExpFromLevelToNext(level)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * This method returns the total amount of exp needed for a guild to reach a whole level |
| 119 | + * (integer). For example, if a guild had 0 experience, this method would return how much |
| 120 | + * experience they would need before they reached level 5.0. If the level parameter is less than |
| 121 | + * 0, an {@link IllegalArgumentException} may be thrown. |
| 122 | + * |
| 123 | + * @param level The level reached with the returned amount of exp; should be an integer |
| 124 | + * @return The total amount of experience needed to reach that level |
| 125 | + */ |
| 126 | + static double getTotalExpToFullLevel(double level) { |
| 127 | + double expNeeded = 0; |
| 128 | + |
| 129 | + for (int i = 0; i < (int) level; i++) { |
| 130 | + expNeeded += getExpFromLevelToNext(i); |
| 131 | + } |
| 132 | + |
| 133 | + return expNeeded; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * This method returns a guild's current progress to the next level as a floating-point number |
| 138 | + * between 0 (inclusively) and 1 (exclusively). For example, if a guild has an experience value |
| 139 | + * exactly halfway between the exp needed for their current level (floored) and the exp needed |
| 140 | + * for the next level (floored), this method will return 0.5. If the experience parameter is |
| 141 | + * less than 0, an {@link IllegalArgumentException} will be thrown. |
| 142 | + * |
| 143 | + * @param exp The total experience gathered by a guild; should be >= 0 |
| 144 | + * @return The guild's progress to the next level as a percentage between 0 and 1 |
| 145 | + */ |
| 146 | + static double getPercentageToNextLevel(double exp) { |
| 147 | + if (exp < 0) { |
| 148 | + throw new IllegalArgumentException("Experience value must be >= 0"); |
| 149 | + } |
| 150 | + |
| 151 | + double currentLvl = getLevel(exp); |
| 152 | + // Exp needed for the current whole level (excluding progress) |
| 153 | + double totalExpForCurrentLvl = getTotalExpToFullLevel(currentLvl); |
| 154 | + // Exp diff between current whole level and next whole level |
| 155 | + double expToNextLvl = getExpFromLevelToNext(currentLvl); |
| 156 | + |
| 157 | + return (exp - (totalExpForCurrentLvl)) / expToNextLvl; |
| 158 | + } |
| 159 | +} |
0 commit comments