-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
CarryTracker.kt
346 lines (304 loc) · 13.1 KB
/
CarryTracker.kt
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
package at.hannibal2.skyhanni.features.misc
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.commands.CommandRegistrationEvent
import at.hannibal2.skyhanni.data.jsonobjects.repo.CarryTrackerJson
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.events.entity.slayer.SlayerDeathEvent
import at.hannibal2.skyhanni.features.slayer.SlayerType
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.CollectionUtils.addString
import at.hannibal2.skyhanni.utils.HypixelCommands
import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.formatDouble
import at.hannibal2.skyhanni.utils.NumberUtil.formatDoubleOrUserError
import at.hannibal2.skyhanni.utils.NumberUtil.formatIntOrUserError
import at.hannibal2.skyhanni.utils.NumberUtil.shortFormat
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.StringUtils.cleanPlayerName
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.renderables.Renderable
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds
/**
* TODO more carry features
* save on restart
* support for Dungeon, Kuudra, crimson minibosses
* average spawn time per slayer customer
* change customer name color if offline, online, on your island
* show time since last boss died next to slayer customer name
* highlight slayer bosses for slayer customers
* automatically mark customers with /shmarkplayers
* show a line behind them
*/
@SkyHanniModule
object CarryTracker {
private val config get() = SkyHanniMod.feature.misc
private val customers = mutableListOf<Customer>()
private val carryTypes = mutableMapOf<String, CarryType>()
private var slayerNames = emptyMap<SlayerType, List<String>>()
private var display = listOf<Renderable>()
private val patternGroup = RepoPattern.group("carry")
/**
* REGEX-TEST:
* §6Trade completed with §r§b[MVP§r§c+§r§b] ClachersHD§r§f§r§6!
*/
private val tradeCompletedPattern by patternGroup.pattern(
"trade.completed",
"§6Trade completed with (?<name>.*)§r§6!",
)
/**
* REGEX-TEST:
* §r§a§l+ §r§6500k coins
*/
private val rawNamePattern by patternGroup.pattern(
"trade.coins.gained",
" §r§a§l\\+ §r§6(?<coins>.*) coins",
)
@HandleEvent
fun onSlayerDeath(event: SlayerDeathEvent) {
val slayerType = event.slayerType
val tier = event.tier
val owner = event.owner
for (customer in customers) {
if (!customer.name.equals(owner, ignoreCase = true)) continue
for (carry in customer.carries) {
val type = carry.type as? SlayerCarryType ?: return
if (type.slayerType != slayerType) continue
if (type.tier != tier) continue
carry.done++
if (carry.done == carry.requested) {
ChatUtils.chat("Carry done for ${customer.name}!")
LorenzUtils.sendTitle("§eCarry done!", 3.seconds)
}
update()
}
}
}
// TODO create trade event with player name, coins and items
var lastTradedPlayer = ""
@SubscribeEvent
fun onChat(event: LorenzChatEvent) {
tradeCompletedPattern.matchMatcher(event.message) {
lastTradedPlayer = group("name").cleanPlayerName()
}
rawNamePattern.matchMatcher(event.message) {
val coinsGained = group("coins").formatDouble()
getCustomer(lastTradedPlayer).alreadyPaid += coinsGained
update()
}
}
@SubscribeEvent
fun onRepoReload(event: RepositoryReloadEvent) {
val data = event.getConstant<CarryTrackerJson>("CarryTracker")
slayerNames = data.slayerNames.mapKeys { SlayerType.valueOf(it.key) }
}
@SubscribeEvent
fun onRenderOverlay(event: GuiRenderEvent) {
if (!LorenzUtils.inSkyBlock) return
config.carryPosition.renderRenderables(display, posLabel = "Carry Tracker")
}
@HandleEvent
fun onCommandRegister(event: CommandRegistrationEvent) {
event.register("shcarry") {
description = "Keep track of carries you do."
callback { onCommand(it) }
}
}
private fun onCommand(args: Array<String>) {
if (args.size < 2 || args.size > 3) {
ChatUtils.userError(
"Usage:\n" +
"§c/shcarry <customer name> <type> <amount requested>\n" +
"§c/shcarry <type> <price per>\n" +
"§c/shcarry remove <costumer name>",
)
return
}
if (args.size == 2) {
if (args[0] == "remove") {
val customerName = args[1]
for (customer in customers) {
if (customer.name.equals(customerName, ignoreCase = true)) {
customers.remove(customer)
update()
ChatUtils.chat("Removed customer: §b$customerName")
return
}
}
ChatUtils.userError("Customer not found: §b$customerName")
return
}
setPrice(args[0], args[1])
return
}
addCarry(args[0], args[1], args[2])
}
private fun addCarry(customerName: String, rawType: String, amount: String) {
val carryType = getCarryType(rawType) ?: return
val amountRequested = amount.formatIntOrUserError() ?: return
val newCarry = Carry(carryType, amountRequested)
for (customer in customers) {
if (!customer.name.equals(customerName, ignoreCase = true)) continue
val carries = customer.carries
for (carry in carries.toList()) {
if (!newCarry.type.sameType(carry.type)) continue
val newAmountRequested = carry.requested + amountRequested
if (newAmountRequested < 1) {
ChatUtils.userError("New carry amount requested must be positive!")
return
}
carries.remove(carry)
val updatedCarry = Carry(carryType, newAmountRequested)
updatedCarry.done = carry.done
carries.add(updatedCarry)
update()
ChatUtils.chat("Updated carry: §b$customerName §8x$newAmountRequested ${newCarry.type}")
return
}
}
if (amountRequested < 1) {
ChatUtils.userError("Carry amount requested must be positive!")
return
}
getCustomer(customerName).carries.add(newCarry)
update()
ChatUtils.chat("Started carry: §b$customerName §8x$amountRequested ${newCarry.type}")
}
private fun getCarryType(rawType: String): CarryType? = carryTypes.getOrPut(rawType) {
createCarryType(rawType) ?: run {
ChatUtils.userError("Unknown carry type: '$rawType'! Use e.g. rev5, sven4, eman3, blaze2..")
return null
}
}
private fun setPrice(rawType: String, rawPrice: String) {
val carryType = getCarryType(rawType) ?: return
val price = rawPrice.formatDoubleOrUserError() ?: return
carryType.pricePer = price
update()
ChatUtils.chat("Set carry price for $carryType §eto §6${price.shortFormat()} coins.")
}
private fun getCustomer(customerName: String): Customer =
customers.firstOrNull { it.name.equals(customerName, ignoreCase = true) } ?: Customer(customerName).also { customers.add(it) }
private fun CarryType.sameType(other: CarryType): Boolean = name == other.name && tier == other.tier
private fun createDisplay(
carry: Carry,
customer: Customer,
carries: MutableList<Carry>,
): Renderable {
val requested = carry.requested
val done = carry.done
val missing = requested - done
val color = if (done > requested) "§c" else if (done == requested) "§a" else "§e"
val cost = formatCost(carry.type.pricePer?.let { it * requested })
val text = "$color$done§8/$color$requested $cost"
return Renderable.clickAndHover(
Renderable.string(" ${carry.type} $text"),
tips = buildList<String> {
add("§b${customer.name}' ${carry.type} §cCarry")
add("")
add("§7Requested: §e$requested")
add("§7Done: §e$done")
add("§7Missing: §e$missing")
add("")
if (cost != "") {
add("§7Total cost: §e$cost")
add("§7Cost per carry: §e${formatCost(carry.type.pricePer)}")
} else {
add("§cNo price set for this carry!")
add("§7Set a price with §e/shcarry <type> <price>")
}
add("")
add("§7Run §e/shcarry remove ${customer.name} §7to remove the whole customer!")
add("§eClick to send current progress in the party chat!")
add("§eControl-click to remove this carry!")
},
onClick = {
if (KeyboardManager.isModifierKeyDown()) {
carries.remove(carry)
update()
} else {
HypixelCommands.partyChat(
"${customer.name} ${carry.type.toString().removeColor()} carry: $done/$requested",
)
}
},
)
}
private fun update() {
val list = mutableListOf<Renderable>()
if (customers.none { it.carries.isNotEmpty() }) {
display = emptyList()
return
}
list.addString("§c§lCarries")
for (customer in customers) {
if (customer.carries.isEmpty()) continue
addCustomerName(customer, list)
val carries = customer.carries
for (carry in carries) {
list.add(createDisplay(carry, customer, carries))
}
}
display = list
}
private fun addCustomerName(customer: Customer, list: MutableList<Renderable>) {
val customerName = customer.name
val totalCost = customer.carries.sumOf { it.getCost() ?: 0.0 }
val totalCostFormat = formatCost(totalCost)
if (totalCostFormat == "") {
list.addString("§b$customerName")
return
}
val paidFormat = "§6${customer.alreadyPaid.shortFormat()}"
val missingFormat = formatCost(totalCost - customer.alreadyPaid)
Renderable.clickAndHover(
Renderable.string("§b$customerName $paidFormat§8/$totalCostFormat"),
tips = listOf(
"§7Carries for §b$customerName",
"",
"§7Total cost: $totalCostFormat",
"§7Already paid: $paidFormat",
"§7Still missing: $missingFormat",
"",
"§eClick to send missing coins in party chat!",
),
onClick = {
HypixelCommands.partyChat(
"$customerName Carry: already paid: ${paidFormat.removeColor()}, still missing: ${missingFormat.removeColor()}",
)
},
).also { list.add(it) }
}
private fun Carry.getCost(): Double? = type.pricePer?.let {
requested * it
}?.takeIf { it != 0.0 }
private fun formatCost(totalCost: Double?): String = if (totalCost == 0.0 || totalCost == null) "" else "§6${totalCost.shortFormat()}"
private fun createCarryType(input: String): CarryType? {
if (input.length == 1) return null
val rawName = input.dropLast(1).lowercase()
val tier = input.last().digitToIntOrNull() ?: return null
getSlayerType(rawName)?.let {
return SlayerCarryType(it, tier)
}
return null
}
private fun getSlayerType(name: String): SlayerType? = slayerNames.entries.find { name in it.value }?.key
class Customer(
val name: String,
var alreadyPaid: Double = 0.0,
val carries: MutableList<Carry> = mutableListOf(),
)
class Carry(val type: CarryType, val requested: Int, var done: Int = 0)
abstract class CarryType(val name: String, val tier: Int, var pricePer: Double? = null) {
override fun toString(): String = "§d$name $tier"
}
class SlayerCarryType(val slayerType: SlayerType, tier: Int) : CarryType(slayerType.displayName, tier)
// class DungeonCarryType(val floor: DungeonFloor, masterMode: Boolean) : CarryType(floor.name, tier)
}