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
|
package at.hannibal2.skyhanni.data
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.data.ElectionCandidate.Companion.getMayorFromPerk
import at.hannibal2.skyhanni.data.ElectionCandidate.Companion.setAssumeMayorJson
import at.hannibal2.skyhanni.data.Perk.Companion.getPerkFromName
import at.hannibal2.skyhanni.data.jsonobjects.other.MayorCandidate
import at.hannibal2.skyhanni.data.jsonobjects.other.MayorElection
import at.hannibal2.skyhanni.data.jsonobjects.other.MayorJson
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.features.fame.ReminderUtils
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.APIUtils
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.CollectionUtils.nextAfter
import at.hannibal2.skyhanni.utils.CollectionUtils.put
import at.hannibal2.skyhanni.utils.ConditionalUtils.onToggle
import at.hannibal2.skyhanni.utils.HypixelCommands
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark
import at.hannibal2.skyhanni.utils.SkyBlockTime
import at.hannibal2.skyhanni.utils.SkyBlockTime.Companion.SKYBLOCK_YEAR_MILLIS
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.json.fromJson
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
@SkyHanniModule
object ElectionAPI {
private val group = RepoPattern.group("mayorapi")
/**
* REGEX-TEST: Schedules an extra §bFishing Festival §7event during the year.
*/
val foxyExtraEventPattern by group.pattern(
"foxy.extraevent",
"Schedules an extra §.(?<event>.*) §.event during the year\\.",
)
/**
* REGEX-TEST: The election room is now closed. Clerk Seraphine is doing a final count of the votes...
*/
private val electionOverPattern by group.pattern(
"election.over",
"§eThe election room is now closed\\. Clerk Seraphine is doing a final count of the votes\\.\\.\\.",
)
/**
* REGEX-TEST: Calendar and Events
*/
val calendarGuiPattern by group.pattern(
"calendar.gui",
"Calendar and Events",
)
/**
* REGEX-TEST: §dMayor Jerry
* REGEX-TEST: §cMayor Aatrox
*/
private val mayorHeadPattern by group.pattern(
"mayor.head",
"§.Mayor (?<name>.*)",
)
/**
* REGEX-TEST: §9Perkpocalypse Perks:
*/
private val perkpocalypsePerksPattern by group.pattern(
"perkpocalypse",
"§9Perkpocalypse Perks:",
)
var currentMayor: ElectionCandidate? = null
private set
var currentMinister: ElectionCandidate? = null
private set
private var lastMayor: ElectionCandidate? = null
var jerryExtraMayor: Pair<ElectionCandidate?, SimpleTimeMark> = null to SimpleTimeMark.farPast()
private set
private var lastJerryExtraMayorReminder = SimpleTimeMark.farPast()
private var lastUpdate = SimpleTimeMark.farPast()
private val dispatcher = Dispatchers.IO
private var rawMayorData: MayorJson? = null
private var candidates = mapOf<Int, MayorCandidate>()
var nextMayorTimestamp = SimpleTimeMark.farPast()
private set
private const val ELECTION_END_MONTH = 3 // Late Spring
private const val ELECTION_END_DAY = 27
/**
* @param input: The name of the mayor
* @return: The NotEnoughUpdates color of the mayor; If no mayor was found, it will return "§c"
*/
fun mayorNameToColorCode(input: String): String = ElectionCandidate.getMayorFromName(input)?.color ?: "§c"
/**
* @param input: The name of the mayor
* @return: The NotEnoughUpdates color of the mayor with the name of the mayor; If no mayor was found, it will return "§c[input]"
*/
fun mayorNameWithColorCode(input: String) = mayorNameToColorCode(input) + input
@SubscribeEvent
fun onSecondPassed(event: SecondPassedEvent) {
if (!LorenzUtils.onHypixel) return
if (event.repeatSeconds(2)) {
checkHypixelAPI()
getTimeTillNextMayor()
}
if (!LorenzUtils.inSkyBlock) return
if (!ElectionCandidate.JERRY.isActive()) return
if (jerryExtraMayor.first != null && jerryExtraMayor.second.isInPast()) {
jerryExtraMayor = null to SimpleTimeMark.farPast()
ChatUtils.clickableChat(
"The Perkpocalypse Mayor has expired! Click here to update the new temporary Mayor.",
onClick = { HypixelCommands.calendar() },
)
}
val misc = SkyHanniMod.feature.misc
if (jerryExtraMayor.first == null && misc.unknownPerkpocalypseMayorWarning) {
if (lastJerryExtraMayorReminder.passedSince() < 5.minutes) return
if (ReminderUtils.isBusy()) return
lastJerryExtraMayorReminder = SimpleTimeMark.now()
ChatUtils.clickableChat(
"The Perkpocalypse Mayor is not known! Click here to update the temporary Mayor.",
onClick = { HypixelCommands.calendar() },
replaceSameMessage = true,
)
}
}
@SubscribeEvent
fun onChat(event: LorenzChatEvent) {
if (!LorenzUtils.inSkyBlock) return
if (electionOverPattern.matches(event.message)) {
lastMayor = currentMayor
currentMayor = ElectionCandidate.UNKNOWN
currentMinister = null
}
}
@SubscribeEvent
fun onInventory(event: InventoryFullyOpenedEvent) {
if (!LorenzUtils.inSkyBlock) return
if (!calendarGuiPattern.matches(event.inventoryName)) return
val stack: ItemStack = event.inventoryItems.values.firstOrNull {
mayorHeadPattern.matchMatcher(it.displayName) {
group("name") == "Jerry"
} ?: false
} ?: return
val perk = stack.getLore().nextAfter({ perkpocalypsePerksPattern.matches(it) }, 2) ?: return
// This is the first Perk of the Perkpocalypse Mayor
val jerryMayor = getMayorFromPerk(getPerkFromName(perk.removeColor()) ?: return)?.addAllPerks() ?: return
val lastMayorTimestamp = nextMayorTimestamp - SKYBLOCK_YEAR_MILLIS.milliseconds
val expireTime = (1..21)
.map { lastMayorTimestamp + (6.hours * it) }
.firstOrNull { it.isInFuture() }
?.coerceAtMost(nextMayorTimestamp) ?: return
ChatUtils.debug("Jerry Mayor found: ${jerryMayor.name} expiring at: ${expireTime.timeUntil()}")
jerryExtraMayor = jerryMayor to expireTime
}
fun SkyBlockTime.getElectionYear(): Int {
var mayorYear = year
// Check if this year's election has not happened yet
if (month < ELECTION_END_MONTH || (day < ELECTION_END_DAY && month == ELECTION_END_MONTH)) {
// If so, the current mayor is still from last year's election
mayorYear--
}
return mayorYear
}
private fun calculateNextMayorTime(): SimpleTimeMark {
val now = SkyBlockTime.now()
return SkyBlockTime(now.getElectionYear() + 1, ELECTION_END_MONTH, day = ELECTION_END_DAY).asTimeMark()
}
private fun getTimeTillNextMayor() {
nextMayorTimestamp = calculateNextMayorTime()
}
private fun checkHypixelAPI(forceReload: Boolean = false) {
if (!forceReload) {
if (lastUpdate.passedSince() < 20.minutes) return
if (currentMayor == ElectionCandidate.UNKNOWN && lastUpdate.passedSince() < 1.minutes) return
}
lastUpdate = SimpleTimeMark.now()
SkyHanniMod.coroutineScope.launch {
val url = "https://api.hypixel.net/v2/resources/skyblock/election"
val jsonObject = withContext(dispatcher) { APIUtils.getJSONResponse(url) }
rawMayorData = ConfigManager.gson.fromJson<MayorJson>(jsonObject)
val data = rawMayorData ?: return@launch
val map = mutableMapOf<Int, MayorCandidate>()
map put data.mayor.election.getPairs()
data.current?.let {
map put data.current.getPairs()
}
candidates = map
val currentMayorName = data.mayor.name
if (lastMayor?.name != currentMayorName) {
Perk.resetPerks()
currentMayor = setAssumeMayorJson(currentMayorName, data.mayor.perks)
currentMinister = data.mayor.minister?.let { setAssumeMayorJson(it.name, listOf(it.perk)) }
}
}
}
private fun MayorElection.getPairs() = year + 1 to candidates.bestCandidate()
private fun List<MayorCandidate>.bestCandidate() = maxBy { it.votes }
@SubscribeEvent
fun onConfigReload(event: ConfigLoadEvent) {
val config = SkyHanniMod.feature.dev
|