|
| 1 | +package net.swofty.commons.bedwars; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import lombok.Setter; |
| 5 | + |
| 6 | +import java.time.DayOfWeek; |
| 7 | +import java.time.ZoneOffset; |
| 8 | +import java.time.ZonedDateTime; |
| 9 | +import java.time.temporal.TemporalAdjusters; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +@Getter |
| 14 | +public class BedWarsModeStats { |
| 15 | + private final Map<String, Long> wins; |
| 16 | + private final Map<String, Long> finalKills; |
| 17 | + private final Map<String, Long> bedsBroken; |
| 18 | + private final Map<String, Long> losses; |
| 19 | + private final Map<String, Long> bedsLost; |
| 20 | + private final Map<String, Long> kills; |
| 21 | + private final Map<String, Long> deaths; |
| 22 | + private final Map<String, Long> finalDeaths; |
| 23 | + private final Map<String, Long> winstreaks; |
| 24 | + |
| 25 | + @Setter private long dailyResetTimestamp; |
| 26 | + @Setter private long weeklyResetTimestamp; |
| 27 | + @Setter private long monthlyResetTimestamp; |
| 28 | + |
| 29 | + public BedWarsModeStats() { |
| 30 | + this.wins = new HashMap<>(); |
| 31 | + this.finalKills = new HashMap<>(); |
| 32 | + this.bedsBroken = new HashMap<>(); |
| 33 | + this.losses = new HashMap<>(); |
| 34 | + this.bedsLost = new HashMap<>(); |
| 35 | + this.kills = new HashMap<>(); |
| 36 | + this.deaths = new HashMap<>(); |
| 37 | + this.finalDeaths = new HashMap<>(); |
| 38 | + this.winstreaks = new HashMap<>(); |
| 39 | + initializeResetTimestamps(); |
| 40 | + } |
| 41 | + |
| 42 | + public BedWarsModeStats(Map<String, Long> wins, Map<String, Long> finalKills, Map<String, Long> bedsBroken, |
| 43 | + Map<String, Long> losses, Map<String, Long> bedsLost, Map<String, Long> kills, |
| 44 | + Map<String, Long> deaths, Map<String, Long> finalDeaths, Map<String, Long> winstreaks, |
| 45 | + long dailyResetTimestamp, long weeklyResetTimestamp, long monthlyResetTimestamp) { |
| 46 | + this.wins = new HashMap<>(wins); |
| 47 | + this.finalKills = new HashMap<>(finalKills); |
| 48 | + this.bedsBroken = new HashMap<>(bedsBroken); |
| 49 | + this.losses = new HashMap<>(losses); |
| 50 | + this.bedsLost = new HashMap<>(bedsLost); |
| 51 | + this.kills = new HashMap<>(kills); |
| 52 | + this.deaths = new HashMap<>(deaths); |
| 53 | + this.finalDeaths = new HashMap<>(finalDeaths); |
| 54 | + this.winstreaks = new HashMap<>(winstreaks); |
| 55 | + this.dailyResetTimestamp = dailyResetTimestamp; |
| 56 | + this.weeklyResetTimestamp = weeklyResetTimestamp; |
| 57 | + this.monthlyResetTimestamp = monthlyResetTimestamp; |
| 58 | + } |
| 59 | + |
| 60 | + private void initializeResetTimestamps() { |
| 61 | + this.dailyResetTimestamp = computeNextDailyReset(); |
| 62 | + this.weeklyResetTimestamp = computeNextWeeklyReset(); |
| 63 | + this.monthlyResetTimestamp = computeNextMonthlyReset(); |
| 64 | + } |
| 65 | + |
| 66 | + public static BedWarsModeStats empty() { |
| 67 | + return new BedWarsModeStats(); |
| 68 | + } |
| 69 | + |
| 70 | + public void checkAndResetExpiredPeriods() { |
| 71 | + long now = System.currentTimeMillis(); |
| 72 | + |
| 73 | + if (now >= dailyResetTimestamp) { |
| 74 | + resetPeriod(BedwarsLeaderboardPeriod.DAILY); |
| 75 | + dailyResetTimestamp = computeNextDailyReset(); |
| 76 | + } |
| 77 | + |
| 78 | + if (now >= weeklyResetTimestamp) { |
| 79 | + resetPeriod(BedwarsLeaderboardPeriod.WEEKLY); |
| 80 | + weeklyResetTimestamp = computeNextWeeklyReset(); |
| 81 | + } |
| 82 | + |
| 83 | + if (now >= monthlyResetTimestamp) { |
| 84 | + resetPeriod(BedwarsLeaderboardPeriod.MONTHLY); |
| 85 | + monthlyResetTimestamp = computeNextMonthlyReset(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static long computeNextDailyReset() { |
| 90 | + ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); |
| 91 | + ZonedDateTime nextReset = now.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0); |
| 92 | + return nextReset.toInstant().toEpochMilli(); |
| 93 | + } |
| 94 | + |
| 95 | + private static long computeNextWeeklyReset() { |
| 96 | + ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); |
| 97 | + ZonedDateTime nextReset = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)) |
| 98 | + .withHour(0).withMinute(0).withSecond(0).withNano(0); |
| 99 | + return nextReset.toInstant().toEpochMilli(); |
| 100 | + } |
| 101 | + |
| 102 | + private static long computeNextMonthlyReset() { |
| 103 | + ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); |
| 104 | + ZonedDateTime nextReset = now.with(TemporalAdjusters.firstDayOfNextMonth()) |
| 105 | + .withHour(0).withMinute(0).withSecond(0).withNano(0); |
| 106 | + return nextReset.toInstant().toEpochMilli(); |
| 107 | + } |
| 108 | + |
| 109 | + private String key(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 110 | + return mode.getKey() + ":" + period.getKey(); |
| 111 | + } |
| 112 | + |
| 113 | + public long getWins(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 114 | + return wins.getOrDefault(key(mode, period), 0L); |
| 115 | + } |
| 116 | + |
| 117 | + public long getFinalKills(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 118 | + return finalKills.getOrDefault(key(mode, period), 0L); |
| 119 | + } |
| 120 | + |
| 121 | + public long getBedsBroken(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 122 | + return bedsBroken.getOrDefault(key(mode, period), 0L); |
| 123 | + } |
| 124 | + |
| 125 | + public long getLosses(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 126 | + return losses.getOrDefault(key(mode, period), 0L); |
| 127 | + } |
| 128 | + |
| 129 | + public long getBedsLost(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 130 | + return bedsLost.getOrDefault(key(mode, period), 0L); |
| 131 | + } |
| 132 | + |
| 133 | + public long getKills(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 134 | + return kills.getOrDefault(key(mode, period), 0L); |
| 135 | + } |
| 136 | + |
| 137 | + public long getDeaths(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 138 | + return deaths.getOrDefault(key(mode, period), 0L); |
| 139 | + } |
| 140 | + |
| 141 | + public long getFinalDeaths(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 142 | + return finalDeaths.getOrDefault(key(mode, period), 0L); |
| 143 | + } |
| 144 | + |
| 145 | + public long getWinstreak(BedwarsLeaderboardMode mode) { |
| 146 | + return winstreaks.getOrDefault(mode.getKey(), 0L); |
| 147 | + } |
| 148 | + |
| 149 | + public void addWin(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 150 | + wins.merge(key(mode, period), 1L, Long::sum); |
| 151 | + } |
| 152 | + |
| 153 | + public void addFinalKill(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 154 | + finalKills.merge(key(mode, period), 1L, Long::sum); |
| 155 | + } |
| 156 | + |
| 157 | + public void addBedBroken(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 158 | + bedsBroken.merge(key(mode, period), 1L, Long::sum); |
| 159 | + } |
| 160 | + |
| 161 | + public void addLoss(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 162 | + losses.merge(key(mode, period), 1L, Long::sum); |
| 163 | + } |
| 164 | + |
| 165 | + public void addBedLost(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 166 | + bedsLost.merge(key(mode, period), 1L, Long::sum); |
| 167 | + } |
| 168 | + |
| 169 | + public void addKill(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 170 | + kills.merge(key(mode, period), 1L, Long::sum); |
| 171 | + } |
| 172 | + |
| 173 | + public void addDeath(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 174 | + deaths.merge(key(mode, period), 1L, Long::sum); |
| 175 | + } |
| 176 | + |
| 177 | + public void addFinalDeath(BedwarsLeaderboardMode mode, BedwarsLeaderboardPeriod period) { |
| 178 | + finalDeaths.merge(key(mode, period), 1L, Long::sum); |
| 179 | + } |
| 180 | + |
| 181 | + public void incrementWinstreak(BedwarsLeaderboardMode mode) { |
| 182 | + winstreaks.merge(mode.getKey(), 1L, Long::sum); |
| 183 | + } |
| 184 | + |
| 185 | + public void resetWinstreak(BedwarsLeaderboardMode mode) { |
| 186 | + winstreaks.put(mode.getKey(), 0L); |
| 187 | + } |
| 188 | + |
| 189 | + public void recordWin(BedwarsLeaderboardMode mode) { |
| 190 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 191 | + addWin(mode, period); |
| 192 | + } |
| 193 | + incrementWinstreak(mode); |
| 194 | + } |
| 195 | + |
| 196 | + public void recordFinalKill(BedwarsLeaderboardMode mode) { |
| 197 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 198 | + addFinalKill(mode, period); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + public void recordBedBroken(BedwarsLeaderboardMode mode) { |
| 203 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 204 | + addBedBroken(mode, period); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public void recordLoss(BedwarsLeaderboardMode mode) { |
| 209 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 210 | + addLoss(mode, period); |
| 211 | + } |
| 212 | + resetWinstreak(mode); |
| 213 | + } |
| 214 | + |
| 215 | + public void recordBedLost(BedwarsLeaderboardMode mode) { |
| 216 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 217 | + addBedLost(mode, period); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + public void recordKill(BedwarsLeaderboardMode mode) { |
| 222 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 223 | + addKill(mode, period); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + public void recordDeath(BedwarsLeaderboardMode mode) { |
| 228 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 229 | + addDeath(mode, period); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + public void recordFinalDeath(BedwarsLeaderboardMode mode) { |
| 234 | + for (BedwarsLeaderboardPeriod period : BedwarsLeaderboardPeriod.values()) { |
| 235 | + addFinalDeath(mode, period); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + public void resetPeriod(BedwarsLeaderboardPeriod period) { |
| 240 | + for (BedwarsLeaderboardMode mode : BedwarsLeaderboardMode.values()) { |
| 241 | + String k = key(mode, period); |
| 242 | + wins.remove(k); |
| 243 | + finalKills.remove(k); |
| 244 | + bedsBroken.remove(k); |
| 245 | + losses.remove(k); |
| 246 | + bedsLost.remove(k); |
| 247 | + kills.remove(k); |
| 248 | + deaths.remove(k); |
| 249 | + finalDeaths.remove(k); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + public BedWarsModeStats copy() { |
| 254 | + return new BedWarsModeStats(wins, finalKills, bedsBroken, |
| 255 | + losses, bedsLost, kills, deaths, finalDeaths, winstreaks, |
| 256 | + dailyResetTimestamp, weeklyResetTimestamp, monthlyResetTimestamp); |
| 257 | + } |
| 258 | +} |
0 commit comments