diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/LedgerLogger.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/LedgerLogger.kt | 126 |
1 files changed, 106 insertions, 20 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt index c35d203..045c6b1 100644 --- a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt +++ b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt @@ -3,7 +3,11 @@ package moe.nea.ledger import com.google.gson.Gson import com.google.gson.JsonArray import com.google.gson.JsonObject +import moe.nea.ledger.database.DBItemEntry +import moe.nea.ledger.database.DBLogEntry +import moe.nea.ledger.database.Database import moe.nea.ledger.events.ChatReceived +import moe.nea.ledger.utils.Inject import net.minecraft.client.Minecraft import net.minecraft.util.ChatComponentText import net.minecraftforge.fml.common.eventhandler.SubscribeEvent @@ -11,7 +15,8 @@ import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent import java.io.File import java.text.SimpleDateFormat import java.time.Instant -import java.util.* +import java.util.Date +import java.util.UUID class LedgerLogger { fun printOut(text: String) { @@ -21,30 +26,32 @@ class LedgerLogger { val profileIdPattern = "Profile ID: (?<profile>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})".toPattern() - var currentProfile: String? = null + var currentProfile: UUID? = null var shouldLog by Ledger.managedConfig.instance.debug::logEntries + @Inject + lateinit var database: Database + @SubscribeEvent fun onProfileSwitch(event: ChatReceived) { profileIdPattern.useMatcher(event.message) { - currentProfile = group("profile") + currentProfile = UUID.fromString(group("profile")) } } fun printToChat(entry: LedgerEntry) { + val items = entry.items.joinToString("\n§e") { " - ${it.direction} ${it.count}x ${it.itemId}" } printOut( """ §e================= TRANSACTION START §eTYPE: §a${entry.transactionType} §eTIMESTAMP: §a${entry.timestamp} - §eTOTAL VALUE: §a${entry.totalTransactionCoins} - §eITEM ID: §a${entry.itemId} - §eITEM AMOUNT: §a${entry.itemAmount} + §e%s §ePROFILE: §a${currentProfile} §e================= TRANSACTION END - """.trimIndent() + """.trimIndent().replace("%s", items) ) } @@ -79,6 +86,21 @@ class LedgerLogger { if (shouldLog) printToChat(entry) Ledger.logger.info("Logging entry of type ${entry.transactionType}") + val transactionId = UUIDUtil.createULIDAt(entry.timestamp) + DBLogEntry.insert(database.connection) { + it[DBLogEntry.profileId] = currentProfile ?: UUIDUtil.NIL_UUID + it[DBLogEntry.playerId] = UUIDUtil.getPlayerUUID() + it[DBLogEntry.type] = entry.transactionType + it[DBLogEntry.transactionId] = transactionId + } + entry.items.forEach { change -> + DBItemEntry.insert(database.connection) { + it[DBItemEntry.transactionId] = transactionId + it[DBItemEntry.mode] = change.direction + it[DBItemEntry.size] = change.count + it[DBItemEntry.itemId] = change.itemId + } + } entries.add(entry.intoJson(currentProfile)) commit() } @@ -110,28 +132,92 @@ class LedgerLogger { } } +enum class TransactionType { + AUCTION_BOUGHT, + AUCTION_SOLD, + AUTOMERCHANT_PROFIT_COLLECT, + BANK_DEPOSIT, + BANK_WITHDRAW, + BAZAAR_BUY_INSTANT, + BAZAAR_BUY_ORDER, + BAZAAR_SELL_INSTANT, + BAZAAR_SELL_ORDER, + BITS_PURSE_STATUS, + BOOSTER_COOKIE_ATE, + COMMUNITY_SHOP_BUY, + DUNGEON_CHEST_OPEN, + KISMET_REROLL, + NPC_BUY, + NPC_SELL, +} + +@JvmInline +value class ItemId( + val string: String +) { + companion object { + val COINS = ItemId("SKYBLOCK_COIN") + val BITS = ItemId("SKYBLOCK_BIT") + val NIL = ItemId("SKYBLOCK_NIL") + val DUNGEON_CHEST_KEY = ItemId("DUNGEON_CHEST_KEY") + val BOOSTER_COOKIE = ItemId("BOOSTER_COOKIE") + val KISMET_FEATHER = ItemId("KISMET_FEATHER") + } +} + + +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 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) + } + } +} data class LedgerEntry( - val transactionType: String, + val transactionType: TransactionType, val timestamp: Instant, - val totalTransactionCoins: Double, - val itemId: String? = null, - val itemAmount: Int? = null, + val items: List<ItemChange>, ) { - fun intoJson(profileId: String?): JsonObject { + fun intoJson(profileId: UUID?): JsonObject { + val coinAmount = items.find { it.itemId == ItemId.COINS || it.itemId == ItemId.BITS }?.count + val nonCoins = items.find { it.itemId != ItemId.COINS && it.itemId != ItemId.BITS } return JsonObject().apply { - addProperty("transactionType", transactionType) + addProperty("transactionType", transactionType.name) addProperty("timestamp", timestamp.toEpochMilli().toString()) - addProperty("totalTransactionValue", totalTransactionCoins) - addProperty("itemId", itemId ?: "") - addProperty("itemAmount", itemAmount ?: 0) - addProperty("profileId", profileId) + addProperty("totalTransactionValue", coinAmount) + addProperty("itemId", nonCoins?.itemId?.string ?: "") + addProperty("itemAmount", nonCoins?.count ?: 0.0) + addProperty("profileId", profileId.toString()) addProperty( "playerId", - (Minecraft.getMinecraft().thePlayer?.uniqueID?.toString() ?: lastKnownUUID) - .also { lastKnownUUID = it }) + UUIDUtil.getPlayerUUID().toString() + ) } } } -var lastKnownUUID = "null" |