aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/data/HySkyBlockStats.java
blob: 493a3e8bf74fa934e1d5120a264a5116329a8032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package de.cowtipper.cowlection.data;

import com.google.common.collect.ComparisonChain;
import com.mojang.realmsclient.util.Pair;
import com.mojang.util.UUIDTypeAdapter;
import de.cowtipper.cowlection.config.MooConfig;
import de.cowtipper.cowlection.util.Utils;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StringUtils;
import net.minecraftforge.common.util.Constants;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.*;

@SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal", "unused", "MismatchedQueryAndUpdateOfCollection"})
public class HySkyBlockStats {
    private boolean success;
    private String cause;
    private List<Profile> profiles;

    /**
     * No-args constructor for GSON
     */
    private HySkyBlockStats() {
    }

    /**
     * ConcurrentHashMaps don't allow null values, so this acts as a null object
     */
    public HySkyBlockStats(boolean fakeNullElement) {
        success = false;
        cause = "Hypixel API down? Check status.hypixel.net";
    }

    public boolean isSuccess() {
        return success;
    }

    public String getCause() {
        return cause;
    }

    public Profile getActiveProfile(UUID uuid) {
        if (profiles == null) {
            return null;
        }
        for (Profile profile : profiles) {
            if (profile.selected) {
                return profile;
            }
        }
        // no selected profile
        return null;
    }

    public static class Profile {
        public boolean selected;
        private String cute_name;
        private Map<String, Member> members;
        private String game_mode;
        private Banking banking;

        /**
         * No-args constructor for GSON
         */
        private Profile() {
        }

        public String getCuteName() {
            return cute_name;
        }

        public Member getMember(UUID uuid) {
            return members.get(UUIDTypeAdapter.fromUUID(uuid));
        }

        public String getGameModeIcon() {
            if (StringUtils.isNullOrEmpty(game_mode)) {
                return "";
            }
            switch (game_mode) {
                case "bingo":
                    return EnumChatFormatting.DARK_PURPLE + "Ⓑ";
                case "ironman":
                    return EnumChatFormatting.GRAY + "♲";
                case "island": // stranded
                    return EnumChatFormatting.GREEN + "☀";
                default:
                    return EnumChatFormatting.ITALIC + Utils.fancyCase(game_mode);
            }
        }

        public double getCoinBank() {
            return (banking != null) ? banking.balance : -1;
        }

        public int coopCount() {
            return members.size() - 1;
        }

        public double getCoopCoinPurses(UUID stalkedUuid) {
            double coopCoinPurses = 0;
            for (Map.Entry<String, Member> memberEntry : members.entrySet()) {
                if (memberEntry.getKey().equals(UUIDTypeAdapter.fromUUID(stalkedUuid))) {
                    // don't include stalked player's purse again, only coops' purse
                    continue;
                }
                coopCoinPurses += memberEntry.getValue().getCoinPurse();
            }
            return coopCoinPurses;
        }

        public Pair<Integer, Integer> getUniqueMinions() {
            int uniqueMinions = 0;
            int membersWithDisabledApi = 0;
            for (Member member : members.values()) {
                if (member.crafted_generators != null) {
                    if (uniqueMinions > 0) {
                        --uniqueMinions; // subtract duplicate COBBLESTONE_1 minion
                    }
                    uniqueMinions += member.crafted_generators.size();
                } else {
                    ++membersWithDisabledApi;
                }
            }
            return Pair.of(uniqueMinions, membersWithDisabledApi);
        }

        public static class Member {
            private long first_join;
            private double coin_purse;
            private NbtData inv_armor;
            private List<String> crafted_generators;
            private int fairy_souls_collected = -1;
            private double experience_skill_farming = -1;
            private double experience_skill_mining = -1;
            private double experience_skill_combat = -1;
            private double experience_skill_foraging = -1;
            private double experience_skill_fishing = -1;
            private double experience_skill_enchanting = -1;
            private double experience_skill_alchemy = -1;
            private double experience_skill_carpentry = -1;
            private double experience_skill_runecrafting = -1;
            private double experience_skill_social2 = -1;
            private double experience_skill_taming = -1;
            private Map<String, SlayerBossDetails> slayer_bosses;
            private List<Pet> pets;
            private JacobData jacob2;
            private Dungeons dungeons;

            /**
             * No-args constructor for GSON
             */
            private Member() {
            }

            public Pair<String, String> getFancyFirstJoined() {
                return Utils.getDurationAsWords(first_join);
            }

            public double getCoinPurse() {
                return coin_purse;
            }

            public int getFairySoulsCollected() {
                return fairy_souls_collected;
            }

            public Map<XpTables.Skill, Integer> getSkills() {
                Map<XpTables.Skill, Integer> skills = new TreeMap<>();
                if (experience_skill_farming >= 0) {
                    skills.put(XpTables.Skill.FARMING, XpTables.Skill.FARMING.getLevel(experience_skill_farming, getMaxFarmingLevel()));
                }
                if (experience_skill_mining >= 0) {
                    skills.put(XpTables.Skill.MINING, XpTables.Skill.MINING.getLevel(experience_skill_mining, 60));
                }
                if (experience_skill_combat >= 0) {
                    skills.put(XpTables.Skill.COMBAT, XpTables.Skill.COMBAT.getLevel(experience_skill_combat, 60));
                }
                if (experience_skill_foraging >= 0) {
                    skills.put(XpTables.Skill.FORAGING, XpTables.Skill.FORAGING.getLevel(experience_skill_foraging));
                }
                if (experience_skill_fishing >= 0) {
                    skills.put(XpTables.Skill.FISHING, XpTables.Skill.FISHING.getLevel(experience_skill_fishing));
                }
                if (experience_skill_enchanting >= 0) {
                    skills.put(XpTables.Skill.ENCHANTING, XpTables.Skill.ENCHANTING.getLevel(experience_skill_enchanting, 60));
                }
                if (experience_skill_alchemy >= 0) {
                    skills.put(XpTables.Skill.ALCHEMY, XpTables.Skill.ALCHEMY.getLevel(experience_skill_alchemy));
                }
                if (experience_skill_carpentry >= 0) {
                    skills.put(XpTables.Skill.CARPENTRY, XpTables.Skill.CARPENTRY.getLevel(experience_skill_carpentry));
                }
                if (experience_skill_runecrafting >= 0) {
                    skills.put(XpTables.Skill.RUNECRAFTING, XpTables.Skill.RUNECRAFTING.getLevel(experience_skill_runecrafting));
                }
                if (experience_skill_social2 >= 0) {
                    skills.put(XpTables.Skill.SOCIAL, XpTables.Skill.SOCIAL.getLevel(experience_skill_social2));
                }
                if (experience_skill_taming >= 0) {
                    skills.put(XpTables.Skill.TAMING, XpTables.Skill.TAMING.getLevel(experience_skill_taming));
                }
                return skills;
            }

            public Map<XpTables.Slayer, Integer> getSlayerLevels() {
                Map<XpTables.Slayer, Integer> slayerLevels = new EnumMap<>(XpTables.Slayer.class);
                for (XpTables.Slayer slayerBoss : XpTables.Slayer.values()) {
                    SlayerBossDetails bossDetails = slayer_bosses.get(slayerBoss.name().toLowerCase());
                    int slayerLevel = bossDetails != null ? slayerBoss.getLevel(bossDetails.xp) : 0;
                    slayerLevels.put(slayerBoss, slayerLevel);
                }
                return slayerLevels;
            }

            public Dungeons getDungeons() {
                return dungeons;
            }

            public List<Pet> getPets() {
                if (pets == null) {
                    pets = Collections.emptyList();
                } else {
                    pets.sort((p1, p2) -> ComparisonChain.start().compare(p2.active, p1.active).compare(p2.getRarity(), p1.getRarity()).compare(p2.exp, p1.exp).result());
                }
                return pets;
            }

            public Pet getActivePet() {
                for (Pet pet : getPets()) {
                    if (pet.isActive()) {
                        return pet;
                    }
                }
                return null;
            }

            public Pet getPet(String type) {
                for (Pet pet : getPets()) {
                    if (type.equals(pet.type)) {
                        return pet;
                    }
                }
                return null;
            }

            public int getMaxFarmingLevel() {
                int farmingLevelCap = 50;
                if (jacob2 != null && jacob2.perks != null) {
                    farmingLevelCap += jacob2.perks.getOrDefault("farming_level_cap", 0);
                }
                return farmingLevelCap;
            }

            public List<String> getArmor() {
                NBTTagCompound nbt = inv_armor.getDecodedData();
                List<String> armorList = new ArrayList<>();
                if (nbt.hasKey("i", Constants.NBT.TAG_LIST)) {
                    NBTTagList armor = nbt.getTagList("i", Constants.NBT.TAG_COMPOUND);
                    for (int i = 0; i < armor.tagCount(); i++) {
                        NBTTagCompound armorPiece = armor.getCompoundTagAt(i);
                        NBTTagCompound nbtDisplay = armorPiece.getCompoundTag("tag").getCompoundTag("display");
                        if (nbtDisplay != null && nbtDisplay.hasKey("Name", Constants.NBT.TAG_STRING)) {
                            String itemName = nbtDisplay.getString("Name");
                            armorList.add(0, itemName);
                        } else {
                            armorList.add(0, "" + EnumChatFormatting.GRAY + EnumChatFormatting.ITALIC + "(empty slot)");
                        }
                    }
                }
                return armorList;
            }

            private static class NbtData {
                private int type;
                private String data;

                private NBTTagCompound getDecodedData() {
                    try (ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(this.data))) {
                        return CompressedStreamTools.readCompressed(bis);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return new NBTTagCompound();
                    }
                }
            }
        }

        private static class SlayerBossDetails {
            private int xp;
        }

        public static class Pet {
            private String type;
            private double exp;
            private String tier;
            private boolean active;

            public boolean isActive() {
                return active;
            }

            public String getType() {
                return type;
            }

            public double getExp() {
                return exp;
            }

            public DataHelper.SkyBlockRarity getRarity() {
                return DataHelper.SkyBlockRarity.valueOf(tier);
            }

            public String toFancyString() {
                return getRarity().getColor() + Utils.fancyCase(type) + " " + getLevel();
            }

            private int getLevel() {
                return XpTables.Pet.getLevel(tier, exp, "GOLDEN_DRAGON".equals(type));
            }
        }

        private static class JacobData {
            private Map<String, Integer> perks;
        }

        public static class Dungeons {
            private Map<String, Type> dungeon_types;
            private Map<DataHelper.DungeonClass, ClassDetails> player_classes;
            private DataHelper.DungeonClass selected_dungeon_class;

            public Map<String, Type> getDungeonTypes() {
                return dungeon_types;
            }

            public Map<DataHelper.DungeonClass, Integer> getClassLevels() {
                Map<DataHelper.DungeonClass, Integer> classLevels = new TreeMap<>();
                for (Map.Entry<DataHelper.DungeonClass, ClassDetails> classEntry : player_classes.entrySet()) {
                    classLevels.put(classEntry.getKey(), classEntry.getValue().getLevel());
                }
                return classLevels;
            }

            public DataHelper.DungeonClass getSelectedClass() {
                return selected_dungeon_class;
            }

            public boolean hasPlayed() {
                if (dungeon_types != null) {
                    for (Type dungeonType : dungeon_types.values()) {
                        if (dungeonType != null && dungeonType.hasPlayed()) {
                            return true;
                        }
                    }
                }
                return false;
            }

            public int getSelectedClassLevel() {
                return player_classes.get(selected_dungeon_class).getLevel();
            }

            public StringBuilder getHighestFloorCompletions(int nHighestFloors, boolean indent) {
                StringBuilder output = new StringBuilder();
                String spacer = indent ? "\n  " : "\n";

                // print detail of 4 latest dungeon types (cata + master cata + xx + master xx)
                Map<String, Type> latestDungeonType = Utils.getLastNMapEntries(dungeon_types, 4);
                for (Map.Entry<String, Type> dungeonTypeEntry : latestDungeonType.entrySet()) {
                    if (!indent) {
                        output.append(spacer);
                    }
                    if (dungeonTypeEntry != null) {
                        if (!dungeonTypeEntry.getValue().hasPlayed()) {
                            // never played this dungeon type
                            continue;
                        }
                        Map<String, Integer> highestFloorCompletions = Utils.getLastNMapEntries(dungeonTypeEntry.getValue().getTierCompletions(), nHighestFloors);
                        String latestDungeonTypeName = Utils.fancyCase(dungeonTypeEntry.getKey());
                        if (highestFloorCompletions != null) {
                            // top n highest floor completions:
                            String highest = highestFloorCompletions.size() > 1 ? highestFloorCompletions.size() + " highest " : "Highest ";
                            String pluralS = highestFloorCompletions.size() > 1 ? "s" : "";
                            output.append(spacer).append(EnumChatFormatting.BOLD).append(highest).append(latestDungeonTypeName).append(" floor").append(pluralS).append(":");

                            for (Map.Entry<String, Integer> highestFloorEntry : highestFloorCompletions.entrySet()) {
                                int highestFloorHighestScore = dungeonTypeEntry.getValue().getBestScore().get(highestFloorEntry.getKey());
                                output.append(spacer).append(EnumChatFormatting.GRAY).append("  Floor ").append(EnumChatFormatting.YELLOW).append(highestFloorEntry.getKey()).append(EnumChatFormatting.GRAY).append(": ")
                                        .append(EnumChatFormatting.GOLD).append(highestFloorEntry.getValue()).append(EnumChatFormatting.GRAY).append(" completions (best score: ").append(EnumChatFormatting.LIGHT_PURPLE).append(highestFloorHighestScore).append(EnumChatFormatting.GRAY).append(")");
                            }
                        } else {
                            // no floor completions yet
                            output.append(spacer).append(EnumChatFormatting.ITALIC).append("No ").append(latestDungeonTypeName).append(" floor completions yet");
                        }
                    }
                }
                return output;
            }

            public String getDungeonTypesLevels() {
                StringBuilder dungeonTypesLevels = new StringBuilder();
                if (dungeon_types != null && !dungeon_types.isEmpty()) {
                    for (Map.Entry<String, HySkyBlockStats.Profile.Dungeons.Type> dungeonType : dungeon_types.entrySet()) {
                        if (dungeonType.getKey().startsWith("master_")) {
                            // master floors don't have their own experience/levels, skip!
                            continue;
                        }
                        if (dungeonTypesLevels.length() == 0) {
                            dungeonTypesLevels.append(EnumChatFormatting.DARK_GRAY).append("  [").append(EnumChatFormatting.GRAY);
                        } else {
                            dungeonTypesLevels.append(EnumChatFormatting.DARK_GRAY).append(", ").append(EnumChatFormatting.GRAY);
                        }
                        int dungeonTypeLevel = dungeonType.getValue().getLevel();
                        dungeonTypesLevels.append(Utils.fancyCase(dungeonType.getKey().substring(0, 4))).append(" ").append(MooConfig.useRomanNumerals() ? Utils.convertArabicToRoman(dungeonTypeLevel) : dungeonTypeLevel);
                    }
                    dungeonTypesLevels.append(EnumChatFormatting.DARK_GRAY).append("]");
                }
                return dungeonTypesLevels.toString();
            }

            public int getTotalDungeonCompletions() {
                int totalDungeonCompletions = 0;
                if (dungeon_types != null) {
                    for (Type dungeonType : dungeon_types.values()) {
                        if (dungeonType != null && dungeonType.hasPlayed()) {
                            totalDungeonCompletions += dungeonType.getTotalTierCompletions();
                        }
                    }
                }
                return totalDungeonCompletions;
            }

            public static class Type {
                private Map<String, Integer> times_played;
                private Map<String, Integer> tier_completions;
                private Map<String, Integer> best_score;
                private int highest_tier_completed;
                private double experience;

                public Map<String, Integer> getTimesPlayed() {
                    return times_played;
                }

                public Map<String, Integer> getTierCompletions() {
                    return tier_completions;
                }

                public Map<String, Integer> getBestScore() {
                    return best_score;
                }

                public int getLevel() {
                    return XpTables.Dungeoneering.DUNGEON.getLevel(experience);
                }

                public boolean hasPlayed() {
                    return experience > 0 || best_score != null;
                }

                public int getTotalTierCompletions() {
                    int totalTierCompletions = 0;
                    if (tier_completions != null) {
                        for (Integer completions : tier_completions.values()) {
                            totalTierCompletions += completions;
                        }
                    }
                    return totalTierCompletions;
                }

                /**
                 * Level [lvl] ([amount]x Floor [highest])
                 *
                 * @return summary text
                 */
                public String getSummary(boolean isMasterFloor) {
                    String floorCompletion;
                    if (tier_completions != null && tier_completions.size() > 0) {
                        int highestTierCompletions = tier_completions.get(String.valueOf(highest_tier_completed));
                        floorCompletion = "" + highestTierCompletions + EnumChatFormatting.GRAY + "x " + EnumChatFormatting.YELLOW + "Floor " + (MooConfig.useRomanNumerals() ? Utils.convertArabicToRoman(highest_tier_completed) : highest_tier_completed);
                    } else {
                        // played dungeons but never completed a floor
                        floorCompletion = "not a single floor...";
                    }
                    return (isMasterFloor ? "" : "Level " + getLevel()) + EnumChatFormatting.GRAY + " (beaten " + EnumChatFormatting.YELLOW + floorCompletion + EnumChatFormatting.GRAY + ")";
                }
            }

            private static class ClassDetails {
                private double experience;

                public int getLevel() {
                    return XpTables.Dungeoneering.CLASS.getLevel(experience);
                }
            }
        }

        public static class Banking {
            private double balance;

            /**
             * No-args constructor for GSON
             */
            private Banking() {
            }
        }
    }
}