diff options
author | Linnea Gräf <nea@nea.moe> | 2025-01-16 21:49:20 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-01-16 21:49:20 +0100 |
commit | 305178f5511bbf7b4499d7cf4142a067ab42e2fc (patch) | |
tree | 845b3cca6490fb9c1252215aa310eec108316bf1 | |
parent | 40ed3a1f667d58501fc43fb45f53585315c013d1 (diff) | |
download | LocalTransactionLedger-305178f5511bbf7b4499d7cf4142a067ab42e2fc.tar.gz LocalTransactionLedger-305178f5511bbf7b4499d7cf4142a067ab42e2fc.tar.bz2 LocalTransactionLedger-305178f5511bbf7b4499d7cf4142a067ab42e2fc.zip |
refactor: Extract database models to own modules
19 files changed, 119 insertions, 104 deletions
diff --git a/basetypes/src/main/kotlin/moe/nea/ledger/ItemChange.kt b/basetypes/src/main/kotlin/moe/nea/ledger/ItemChange.kt new file mode 100644 index 0000000..6cadf27 --- /dev/null +++ b/basetypes/src/main/kotlin/moe/nea/ledger/ItemChange.kt @@ -0,0 +1,43 @@ +package moe.nea.ledger + +data class ItemChange( + val itemId: ItemId, + val count: Double, + val direction: ChangeDirection, +) { + + enum class ChangeDirection { + GAINED, + TRANSFORM, + SYNC, + CATALYST, + LOST; + + } + + companion object { + fun gainCoins(number: Double): ItemChange { + return gain(ItemId.COINS, number) + } + + fun unpair(direction: ChangeDirection, pair: Pair<ItemId, Double>): ItemChange { + return ItemChange(pair.first, pair.second, direction) + } + + fun unpairGain(pair: Pair<ItemId, Double>) = unpair(ChangeDirection.GAINED, pair) + fun unpairLose(pair: Pair<ItemId, Double>) = unpair(ChangeDirection.LOST, pair) + + fun gain(itemId: ItemId, amount: Number): ItemChange { + return ItemChange(itemId, amount.toDouble(), ChangeDirection.GAINED) + } + + fun lose(itemId: ItemId, amount: Number): ItemChange { + return ItemChange(itemId, amount.toDouble(), ChangeDirection.LOST) + } + + fun loseCoins(number: Double): ItemChange { + return lose(ItemId.COINS, number) + } + + } +}
\ No newline at end of file diff --git a/mod/src/main/kotlin/moe/nea/ledger/TransactionType.kt b/basetypes/src/main/kotlin/moe/nea/ledger/TransactionType.kt index 33c633d..33c633d 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/TransactionType.kt +++ b/basetypes/src/main/kotlin/moe/nea/ledger/TransactionType.kt diff --git a/database/impl/build.gradle.kts b/database/impl/build.gradle.kts new file mode 100644 index 0000000..17a7a5a --- /dev/null +++ b/database/impl/build.gradle.kts @@ -0,0 +1,12 @@ +plugins { + `java-library` + kotlin("jvm") +} + +dependencies { + api(project(":database:core")) +} + +java { + toolchain.languageVersion.set(JavaLanguageVersion.of(8)) +} diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt b/database/impl/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt index b162c6f..e2530cc 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt +++ b/database/impl/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt @@ -21,4 +21,12 @@ object DBItemEntry : Table("ItemEntry") { val mode = column("mode", DBEnum<ItemChange.ChangeDirection>()) val itemId = column("item", DBString.mapped(ItemId::string, ::ItemId)) val size = column("size", DBDouble) + + fun objMap(result: ResultRow): ItemChange { + return ItemChange( + result[itemId], + result[size], + result[mode], + ) + } } diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt b/database/impl/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt index 7d1782a..7d1782a 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt +++ b/database/impl/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/Database.kt b/database/impl/src/main/kotlin/moe/nea/ledger/database/Database.kt index 025888c..e4b34c8 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/database/Database.kt +++ b/database/impl/src/main/kotlin/moe/nea/ledger/database/Database.kt @@ -1,11 +1,11 @@ package moe.nea.ledger.database -import moe.nea.ledger.Ledger import moe.nea.ledger.database.columns.DBString +import java.io.File import java.sql.Connection import java.sql.DriverManager -class Database { +class Database(val dataFolder: File) { lateinit var connection: Connection object MetaTable : Table("LedgerMeta") { @@ -34,7 +34,7 @@ class Database { val databaseVersion: Long = 1 fun loadAndUpgrade() { - connection = DriverManager.getConnection("jdbc:sqlite:${Ledger.dataFolder.resolve("database.db")}") + connection = DriverManager.getConnection("jdbc:sqlite:${dataFolder.resolve("database.db")}") MetaTable.createIfNotExists(connection) val meta = MetaTable.selectAll(connection).associate { MetaKey(it[MetaTable.key]) to it[MetaTable.value] } val lastLaunch = meta[MetaKey.LAST_LAUNCH]?.toLong() ?: 0L diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt b/database/impl/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt index e83abe7..e83abe7 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt +++ b/database/impl/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/schema.dot b/database/impl/src/main/kotlin/moe/nea/ledger/database/schema.dot index d932f6a..d932f6a 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/database/schema.dot +++ b/database/impl/src/main/kotlin/moe/nea/ledger/database/schema.dot diff --git a/mod/build.gradle.kts b/mod/build.gradle.kts index a989fa5..0ad4aa6 100644 --- a/mod/build.gradle.kts +++ b/mod/build.gradle.kts @@ -66,7 +66,7 @@ dependencies { shadowImpl("org.notenoughupdates.moulconfig:legacy:3.0.0-beta.9") shadowImpl("io.azam.ulidj:ulidj:1.0.4") shadowImpl(project(":dependency-injection")) - shadowImpl(project(":database:core")) + shadowImpl(project(":database:impl")) shadowImpl("moe.nea:libautoupdate:1.3.1") { exclude(module = "gson") } diff --git a/mod/src/main/kotlin/moe/nea/ledger/ItemChange.kt b/mod/src/main/kotlin/moe/nea/ledger/ItemChange.kt deleted file mode 100644 index fda709c..0000000 --- a/mod/src/main/kotlin/moe/nea/ledger/ItemChange.kt +++ /dev/null @@ -1,84 +0,0 @@ -package moe.nea.ledger - -import moe.nea.ledger.database.DBItemEntry -import moe.nea.ledger.database.ResultRow -import net.minecraft.event.HoverEvent -import net.minecraft.util.ChatComponentText -import net.minecraft.util.ChatStyle -import net.minecraft.util.EnumChatFormatting -import net.minecraft.util.IChatComponent - -data class ItemChange( - val itemId: ItemId, - val count: Double, - val direction: ChangeDirection, -) { - fun formatChat(): IChatComponent { - return ChatComponentText(" ") - .appendSibling(direction.chatFormat) - .appendText(" ") - .appendSibling(ChatComponentText("$count").setChatStyle(ChatStyle().setColor(EnumChatFormatting.WHITE))) - .appendSibling(ChatComponentText("x").setChatStyle(ChatStyle().setColor(EnumChatFormatting.DARK_GRAY))) - .appendText(" ") - .appendSibling(ChatComponentText(itemId.string).setChatStyle(ChatStyle().setParentStyle(ChatStyle().setColor( - EnumChatFormatting.WHITE)))) - } - - enum class ChangeDirection { - GAINED, - TRANSFORM, - SYNC, - CATALYST, - LOST; - - val chatFormat by lazy { formatChat0() } - private fun formatChat0(): IChatComponent { - val (text, color) = when (this) { - GAINED -> "+" to EnumChatFormatting.GREEN - TRANSFORM -> "~" to EnumChatFormatting.YELLOW - SYNC -> "=" to EnumChatFormatting.BLUE - CATALYST -> "*" to EnumChatFormatting.DARK_PURPLE - LOST -> "-" to EnumChatFormatting.RED - } - return ChatComponentText(text) - .setChatStyle( - ChatStyle() - .setColor(color) - .setChatHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, - ChatComponentText(name).setChatStyle(ChatStyle().setColor(color))))) - } - } - - companion object { - fun gainCoins(number: Double): ItemChange { - return gain(ItemId.COINS, number) - } - - fun unpair(direction: ChangeDirection, pair: Pair<ItemId, Double>): ItemChange { - return ItemChange(pair.first, pair.second, direction) - } - - fun unpairGain(pair: Pair<ItemId, Double>) = unpair(ChangeDirection.GAINED, pair) - fun unpairLose(pair: Pair<ItemId, Double>) = unpair(ChangeDirection.LOST, pair) - - fun gain(itemId: ItemId, amount: Number): ItemChange { - return ItemChange(itemId, amount.toDouble(), ChangeDirection.GAINED) - } - - fun lose(itemId: ItemId, amount: Number): ItemChange { - return ItemChange(itemId, amount.toDouble(), ChangeDirection.LOST) - } - - fun loseCoins(number: Double): ItemChange { - return lose(ItemId.COINS, number) - } - - fun from(result: ResultRow): ItemChange { - return ItemChange( - result[DBItemEntry.itemId], - result[DBItemEntry.size], - result[DBItemEntry.mode], - ) - } - } -}
\ No newline at end of file diff --git a/mod/src/main/kotlin/moe/nea/ledger/Ledger.kt b/mod/src/main/kotlin/moe/nea/ledger/Ledger.kt index bc667e4..043b83e 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/Ledger.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/Ledger.kt @@ -119,6 +119,7 @@ class Ledger { di.registerSingleton(gson) di.register(LedgerConfig::class.java, DIProvider { managedConfig.instance }) di.register(Config::class.java, DIProvider.fromInheritance(LedgerConfig::class.java)) + di.register(Database::class.java, DIProvider { Database(dataFolder) }) di.registerInjectableClasses( AccessorySwapperDetection::class.java, AllowanceDetection::class.java, @@ -129,7 +130,6 @@ class Ledger { BitsDetection::class.java, BitsShopDetection::class.java, ConfigCommand::class.java, - Database::class.java, DebugDataCommand::class.java, DragonEyePlacementDetection::class.java, DragonSacrificeDetection::class.java, diff --git a/mod/src/main/kotlin/moe/nea/ledger/NumberUtil.kt b/mod/src/main/kotlin/moe/nea/ledger/NumberUtil.kt index 438f342..fa295b0 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/NumberUtil.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/NumberUtil.kt @@ -115,3 +115,38 @@ fun Instant.formatChat(): IChatComponent { .setColor(EnumChatFormatting.AQUA)) return text } + +private val formatChatDirection = run { + fun ItemChange.ChangeDirection.formatChat0(): IChatComponent { + val (text, color) = when (this) { + ItemChange.ChangeDirection.GAINED -> "+" to EnumChatFormatting.GREEN + ItemChange.ChangeDirection.TRANSFORM -> "~" to EnumChatFormatting.YELLOW + ItemChange.ChangeDirection.SYNC -> "=" to EnumChatFormatting.BLUE + ItemChange.ChangeDirection.CATALYST -> "*" to EnumChatFormatting.DARK_PURPLE + ItemChange.ChangeDirection.LOST -> "-" to EnumChatFormatting.RED + } + return ChatComponentText(text) + .setChatStyle( + ChatStyle() + .setColor(color) + .setChatHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, + ChatComponentText(name).setChatStyle(ChatStyle().setColor(color))))) + } + ItemChange.ChangeDirection.entries.associateWith { it.formatChat0() } +} + +fun ItemChange.ChangeDirection.formatChat(): IChatComponent { + return formatChatDirection[this]!! +} + +fun ItemChange.formatChat(): IChatComponent { + return ChatComponentText(" ") + .appendSibling(direction.formatChat()) + .appendText(" ") + .appendSibling(ChatComponentText("$count").setChatStyle(ChatStyle().setColor(EnumChatFormatting.WHITE))) + .appendSibling(ChatComponentText("x").setChatStyle(ChatStyle().setColor(EnumChatFormatting.DARK_GRAY))) + .appendText(" ") + .appendSibling(ChatComponentText(itemId.string).setChatStyle(ChatStyle().setParentStyle(ChatStyle().setColor( + EnumChatFormatting.WHITE)))) +} + diff --git a/mod/src/main/kotlin/moe/nea/ledger/QueryCommand.kt b/mod/src/main/kotlin/moe/nea/ledger/QueryCommand.kt index 19bd5d0..abdc13a 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/QueryCommand.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/QueryCommand.kt @@ -103,7 +103,7 @@ class QueryCommand : CommandBase() { val timestamp = transactionId.getTimestamp() val items = DBItemEntry.selectAll(database.connection) .where(Clause { column(DBItemEntry.transactionId) eq string(transactionId.wrapped) }) - .map { ItemChange.from(it) } + .map { DBItemEntry.objMap(it) } val text = ChatComponentText("") .setChatStyle(ChatStyle().setColor(EnumChatFormatting.YELLOW)) .appendSibling( diff --git a/mod/src/main/kotlin/moe/nea/ledger/modules/AccessorySwapperDetection.kt b/mod/src/main/kotlin/moe/nea/ledger/modules/AccessorySwapperDetection.kt index 1c228ff..808ac5c 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/modules/AccessorySwapperDetection.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/modules/AccessorySwapperDetection.kt @@ -22,9 +22,9 @@ class AccessorySwapperDetection { swapperUsed.useMatcher(event.message) { logger.logEntry( LedgerEntry( - TransactionType.ACCESSORIES_SWAPPING, - event.timestamp, - listOf( + TransactionType.ACCESSORIES_SWAPPING, + event.timestamp, + listOf( ItemChange.lose(ItemIds.TALISMAN_ENRICHMENT_SWAPPER, 1) ) ) diff --git a/mod/src/main/kotlin/moe/nea/ledger/modules/BitsDetection.kt b/mod/src/main/kotlin/moe/nea/ledger/modules/BitsDetection.kt index f0f5369..f6dad12 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/modules/BitsDetection.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/modules/BitsDetection.kt @@ -50,8 +50,8 @@ class BitsDetection @Inject constructor(val ledger: LedgerLogger) { ledger.logEntry( LedgerEntry( TransactionType.BOOSTER_COOKIE_ATE, - Instant.now(), - listOf( + Instant.now(), + listOf( ItemChange.lose(ItemIds.BOOSTER_COOKIE, 1) ) ) diff --git a/mod/src/main/kotlin/moe/nea/ledger/modules/EyedropsDetection.kt b/mod/src/main/kotlin/moe/nea/ledger/modules/EyedropsDetection.kt index 1c36ae4..c90f8d9 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/modules/EyedropsDetection.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/modules/EyedropsDetection.kt @@ -22,9 +22,9 @@ class EyedropsDetection { capsaicinEyedropsUsed.useMatcher(event.message) { logger.logEntry( LedgerEntry( - TransactionType.CAPSAICIN_EYEDROPS_USED, - event.timestamp, - listOf( + TransactionType.CAPSAICIN_EYEDROPS_USED, + event.timestamp, + listOf( ItemChange.lose(ItemIds.CAPSAICIN_EYEDROPS_NO_CHARGES, 1) ) ) diff --git a/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionDetection.kt b/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionDetection.kt index e858a6b..56e2b69 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionDetection.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionDetection.kt @@ -22,9 +22,9 @@ class GodPotionDetection { godPotionDrank.useMatcher(event.message) { logger.logEntry( LedgerEntry( - TransactionType.GOD_POTION_DRANK, - event.timestamp, - listOf( + TransactionType.GOD_POTION_DRANK, + event.timestamp, + listOf( ItemChange.lose(ItemIds.GOD_POTION_2, 1) ) ) diff --git a/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionMixinDetection.kt b/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionMixinDetection.kt index b96a24a..072503f 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionMixinDetection.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/modules/GodPotionMixinDetection.kt @@ -26,9 +26,9 @@ class GodPotionMixinDetection { godPotionMixinDrank.useMatcher(event.message) { logger.logEntry( LedgerEntry( - TransactionType.GOD_POTION_MIXIN_DRANK, - event.timestamp, - listOf( + TransactionType.GOD_POTION_MIXIN_DRANK, + event.timestamp, + listOf( ItemChange.lose(itemIdProvider.findForName(group("what")) ?: ItemId.NIL, 1) ) ) diff --git a/settings.gradle.kts b/settings.gradle.kts index 0390203..19f404d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -28,6 +28,7 @@ plugins { rootProject.name = "ledger" include("dependency-injection") include("database:core") +include("database:impl") include("basetypes") include("mod") includeBuild("build-src") |