diff options
author | Linnea Gräf <nea@nea.moe> | 2025-01-29 00:26:56 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-01-29 00:26:56 +0100 |
commit | 29857bff0875c43e055e89d34bc0d616d5d7dc4f (patch) | |
tree | 5875aed4144c0c1f004a86f6768eb5d6013d68f6 /src/main/kotlin/datamodel | |
parent | ad03f074c603f38f9319411d785e598e8a92e42c (diff) | |
download | ultra-notifier-master.tar.gz ultra-notifier-master.tar.bz2 ultra-notifier-master.zip |
Diffstat (limited to 'src/main/kotlin/datamodel')
-rw-r--r-- | src/main/kotlin/datamodel/ChatType.kt | 116 |
1 files changed, 70 insertions, 46 deletions
diff --git a/src/main/kotlin/datamodel/ChatType.kt b/src/main/kotlin/datamodel/ChatType.kt index b157de9..47abe2a 100644 --- a/src/main/kotlin/datamodel/ChatType.kt +++ b/src/main/kotlin/datamodel/ChatType.kt @@ -1,15 +1,18 @@ package moe.nea.ultranotifier.datamodel -import jdk.jfr.Category +import moe.nea.ultranotifier.UltraNotifier import moe.nea.ultranotifier.event.SubscriptionTarget import moe.nea.ultranotifier.event.TickEvent import moe.nea.ultranotifier.event.UltraSubscribe import moe.nea.ultranotifier.event.VisibleChatMessageAddedEvent +import moe.nea.ultranotifier.util.GsonUtil +import moe.nea.ultranotifier.util.duplicatesBy import moe.nea.ultranotifier.util.minecrat.MC import moe.nea.ultranotifier.util.minecrat.category import moe.nea.ultranotifier.util.minecrat.getFormattedTextCompat import moe.nea.ultranotifier.util.minecrat.removeFormattingCodes import net.minecraft.text.Text +import util.KSerializable import java.util.function.Predicate import java.util.regex.Pattern @@ -17,8 +20,8 @@ data class ChatTypeId( val id: String ) +@KSerializable data class ChatType( - val id: ChatTypeId, val name: String, val patterns: List<ChatPattern>, ) @@ -36,6 +39,8 @@ data class ChatPattern( } data class CategoryId(val id: String) + +@KSerializable data class ChatCategory( val id: CategoryId, val label: String, @@ -44,7 +49,7 @@ data class ChatCategory( data class ChatUniverse( val name: String, - val types: List<ChatType>, + val types: Map<ChatTypeId, ChatType>, val categories: List<ChatCategory>, ) { fun categorize( @@ -53,18 +58,16 @@ data class ChatUniverse( val types = this.types .asSequence() .filter { - it.patterns.any { + it.value.patterns.any { it.predicate.test(text) } } .map { - it.id + it.key } .toSet() - // TODO: potentially allow recalculating categories on the fly - val categories = categories.filterTo(mutableSetOf()) { it.chatTypes.any { it in types } } return CategorizedChatLine( - text, types, categories + text, types ) } } @@ -72,54 +75,78 @@ data class ChatUniverse( data class CategorizedChatLine( val text: String, val types: Set<ChatTypeId>, - val categories: Set<ChatCategory>, +// val categories: Set<ChatCategory>, +) + +@KSerializable +data class UniverseMeta( + // TODO: implement the ip filter + val ipFilter: List<ChatPattern> = listOf(), + val name: String, ) interface HasCategorizedChatLine { val categorizedChatLine_ultraNotifier: CategorizedChatLine } +data class UniverseId( + val id: String +) + +private fun loadAllUniverses(): Map<UniverseId, ChatUniverse> = buildMap { + for (file in UltraNotifier.configFolder + .resolve("universes/") + .listFiles() ?: emptyArray()) { + runCatching { + val meta = GsonUtil.read<UniverseMeta>(file.resolve("meta.json")) + val types = GsonUtil.read<Map<ChatTypeId, ChatType>>(file.resolve("chattypes.json")) + val categories = GsonUtil.read<List<ChatCategory>>(file.resolve("categories.json")) + // Validate categories linking properly + for (category in categories) { + for (chatType in category.chatTypes) { + if (chatType in types.keys) { + UltraNotifier.logger.warn("Missing definition for $chatType required by ${category.id} in $file") + } + } + } + for (category in categories.asSequence().duplicatesBy { it.id }) { + UltraNotifier.logger.warn("Found duplicate category ${category.id} in $file") + } + + put( + UniverseId(file.name), + ChatUniverse( + meta.name, + types, + categories, + )) + }.getOrElse { + UltraNotifier.logger.warn("Could not load universe at $file", it) + } + } +} + object ChatCategoryArbiter : SubscriptionTarget { val specialAll = CategoryId("special-all") - val universe = ChatUniverse( - "Hypixel SkyBlock", - listOf( - ChatType( - ChatTypeId("bazaar"), - "Bazaar", - listOf( - ChatPattern("(?i).*Bazaar.*") - ) - ), - ChatType( - ChatTypeId("auction-house"), - "Auction House", - listOf( - ChatPattern("(?i).*Auction House.*") - ) - ), - ), - listOf( - ChatCategory( - specialAll, - "All", - setOf() - ), - ChatCategory( - CategoryId("economy"), - "Economy", - setOf(ChatTypeId("bazaar"), ChatTypeId("auction-house")) - ) - ) + + var allUniverses = loadAllUniverses() + + var activeUniverse: ChatUniverse? = allUniverses.values.single() + private val allCategoryList = listOf( + ChatCategory(specialAll, "All", setOf()) ) - val categories get() = universe.categories + val categories // TODO: memoize + get() = (activeUniverse?.categories ?: listOf()) + allCategoryList + var selectedCategoryId = specialAll set(value) { field = value selectedCategory = findCategory(value) } - var lastSelectedId = selectedCategoryId + private var lastSelectedId = selectedCategoryId + var selectedCategory: ChatCategory = findCategory(selectedCategoryId) + private set @UltraSubscribe fun onTick(event: TickEvent) { @@ -129,15 +156,12 @@ object ChatCategoryArbiter : SubscriptionTarget { } } - var selectedCategory: ChatCategory = findCategory(selectedCategoryId) - private set - @UltraSubscribe fun onVisibleChatMessage(event: VisibleChatMessageAddedEvent) { val cl = event.chatLine.category if (selectedCategory.id == specialAll) return - if (selectedCategory !in cl.categories) + if (cl.types.none { it in selectedCategory.chatTypes }) event.cancel() } @@ -145,7 +169,7 @@ object ChatCategoryArbiter : SubscriptionTarget { fun categorize(content: Text): CategorizedChatLine { val stringContent = content.getFormattedTextCompat().removeFormattingCodes() - return universe.categorize(stringContent) + return activeUniverse?.categorize(stringContent) ?: CategorizedChatLine(stringContent, setOf()) } } |