aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/handler/DungeonCache.java
blob: d7fd5da5374584764eebb1e78eedaf2a687a583d (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
package de.cowtipper.cowlection.handler;

import de.cowtipper.cowlection.Cowlection;
import de.cowtipper.cowlection.util.TickDelay;
import net.minecraft.client.Minecraft;
import net.minecraft.scoreboard.Score;
import net.minecraft.scoreboard.ScoreObjective;
import net.minecraft.scoreboard.ScorePlayerTeam;
import net.minecraft.scoreboard.Scoreboard;
import net.minecraft.util.EnumChatFormatting;

import java.util.*;
import java.util.stream.Collectors;

public class DungeonCache {
    private final Cowlection main;
    private final Map<String, Integer> deathCounter;
    private final Set<String> deadPlayers;
    private final Set<String> failedPuzzles;
    private final Set<UUID> destroyedCrypts;

    private boolean isInDungeon;
    private int elapsedMinutes;
    private int classMilestone;
    private long lastScoreboardCheck;
    private long nextPerformanceSend;
    private String queuedFloor;

    public DungeonCache(Cowlection main) {
        this.main = main;
        deathCounter = new HashMap<>();
        deadPlayers = new HashSet<>();
        failedPuzzles = new HashSet<>();
        destroyedCrypts = new HashSet<>();
    }

    public boolean isInDungeon() {
        return isInDungeon;
    }

    public void onDungeonEnterOrLeave(boolean isInDungeonNow) {
        boolean wasInDungeon = isInDungeon;

        if (!wasInDungeon && isInDungeonNow) {
            onDungeonEntered();
        } else if (wasInDungeon && !isInDungeonNow) {
            onDungeonLeft();
        }
    }

    public void onDungeonEntered() {
        main.getLogger().info("Entered SkyBlock Dungeon!");
        isInDungeon = true;
        resetCounters();
    }

    public void onDungeonLeft() {
        main.getLogger().info("Leaving SkyBlock Dungeon!");
        isInDungeon = false;
        resetCounters();
    }

    public void sendDungeonPerformance() {
        if (System.currentTimeMillis() < nextPerformanceSend) {
            // already sent dungeon performance less than 260ms ago
            return;
        }
        String dungeonPerformance;
        boolean hasPointPenalty = false;
        if (deathCounter.isEmpty()) {
            dungeonPerformance = EnumChatFormatting.GOLD + "☠ Deaths: " + EnumChatFormatting.WHITE + "none \\o/";
        } else {
            hasPointPenalty = true;
            String deaths = deathCounter.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(deathEntry -> "  " + EnumChatFormatting.WHITE + deathEntry.getKey() + ": " + EnumChatFormatting.LIGHT_PURPLE + deathEntry.getValue())
                    .collect(Collectors.joining("\n"));
            dungeonPerformance = EnumChatFormatting.RED + "☠ " + EnumChatFormatting.BOLD + "Deaths: " + EnumChatFormatting.DARK_RED + "-" + (getTotalDeaths() * 2) + " Skill points\n" + deaths;
        }
        if (failedPuzzles.size() > 0) {
            hasPointPenalty = true;
            dungeonPerformance += "\n" + EnumChatFormatting.RED + "Failed puzzles: " + EnumChatFormatting.LIGHT_PURPLE + failedPuzzles.size() + EnumChatFormatting.RED + " (" + EnumChatFormatting.DARK_RED + "-" + failedPuzzles.size() * 14 + " Skill points" + EnumChatFormatting.RED + ")";
        }

        if (hasPointPenalty) {
            dungeonPerformance += "\n" + EnumChatFormatting.LIGHT_PURPLE + "➜ " + EnumChatFormatting.RED + EnumChatFormatting.BOLD + "Skill " + EnumChatFormatting.RED + "score penalty: " + EnumChatFormatting.DARK_RED + getSkillScorePenalty() + " points";
        }
        main.getChatHelper().sendMessage(EnumChatFormatting.WHITE, dungeonPerformance);
        nextPerformanceSend = System.currentTimeMillis() + 260;
    }

    public void updateElapsedMinutesFromScoreboard() {
        long now = System.currentTimeMillis();
        if (now - lastScoreboardCheck > 10000) { // run every 10 seconds
            lastScoreboardCheck = now;
            Scoreboard scoreboard = Minecraft.getMinecraft().theWorld.getScoreboard();
            ScoreObjective scoreboardSidebar = scoreboard.getObjectiveInDisplaySlot(1);
            if (scoreboardSidebar != null) {
                Collection<Score> scoreboardLines = scoreboard.getSortedScores(scoreboardSidebar);
                for (Score line : scoreboardLines) {
                    ScorePlayerTeam scorePlayerTeam = scoreboard.getPlayersTeam(line.getPlayerName());
                    if (scorePlayerTeam != null) {
                        String lineWithoutFormatting = EnumChatFormatting.getTextWithoutFormattingCodes(scorePlayerTeam.getColorPrefix() + scorePlayerTeam.getColorSuffix());

                        String timeElapsed = "Time Elapsed: ";
                        if (lineWithoutFormatting.startsWith(timeElapsed)) {
                            // dungeon timer: 05m 22s
                            String timeString = lineWithoutFormatting.substring(timeElapsed.length());
                            try {
                                int indexOfMinute = timeString.indexOf('m');
                                if (indexOfMinute > -1) {
                                    elapsedMinutes = (Integer.parseInt(timeString.substring(0, indexOfMinute)));
                                }
                            } catch (NumberFormatException ex) {
                                // couldn't parse dungeon time from scoreboard
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }

    // setter/adder
    public void setQueuedFloor(String floorNr) {
        this.queuedFloor = floorNr;
    }

    public void addDeath(String playerName, boolean ghostByDisconnecting) {
        if (!deadPlayers.add(playerName) && ghostByDisconnecting) {
            // dead player disconnected from the game; don't count again!
            return;
        }
        int previousPlayerDeaths = deathCounter.getOrDefault(playerName, 0);
        deathCounter.put(playerName, previousPlayerDeaths + 1);

        new TickDelay(this::sendDungeonPerformance, 1);
    }

    public void revivedPlayer(String playerName) {
        deadPlayers.remove(playerName);
    }

    public void addFailedPuzzle(String text) {
        failedPuzzles.add(text);
    }

    public void setClassMilestone(int classMilestone) {
        this.classMilestone = classMilestone;
    }

    public boolean addDestroyedCrypt(UUID uuid) {
        return destroyedCrypts.add(uuid);
    }

    // getter
    public String getQueuedFloor() {
        return queuedFloor;
    }

    public int getMaxSkillScore() {
        return 100 - getSkillScorePenalty();
    }

    private int getSkillScorePenalty() {
        return getTotalDeaths() * 2 + failedPuzzles.size() * 14;
    }

    public int getTotalDeaths() {
        int totalDeaths = 0;
        for (int deathCount : deathCounter.values()) {
            totalDeaths += deathCount;
        }
        return totalDeaths;
    }

    public int getFailedPuzzles() {
        return failedPuzzles.size();
    }

    public int getClassMilestone() {
        return classMilestone;
    }

    public int getDestroyedCrypts() {
        return destroyedCrypts.size();
    }

    public int getElapsedMinutes() {
        return elapsedMinutes;
    }

    // resetter
    private void resetCounters() {
        deathCounter.clear();
        deadPlayers.clear();
        failedPuzzles.clear();
        destroyedCrypts.clear();
        elapsedMinutes = 0;
        classMilestone = 0;
        nextPerformanceSend = 0;
        queuedFloor = null;
    }
}