aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-02-15 21:07:35 +0100
committerLinnea Gräf <nea@nea.moe>2024-02-15 21:07:35 +0100
commitb796c927550bfd4e6828cf58ec60f7070d00f7d1 (patch)
tree1aa4268255d450ba97dba7f5cf0eb38956ad5fae
parentb7aa6cddd2a4134ff3ee53c8c1bc707dc1cab89c (diff)
downloadmoney-ledger-b796c927550bfd4e6828cf58ec60f7070d00f7d1.tar.gz
money-ledger-b796c927550bfd4e6828cf58ec60f7070d00f7d1.tar.bz2
money-ledger-b796c927550bfd4e6828cf58ec60f7070d00f7d1.zip
Add json export
-rw-r--r--src/main/java/moe/nea/ledger/BazaarOrderDetection.kt7
-rw-r--r--src/main/kotlin/moe/nea/ledger/LedgerLogger.kt54
2 files changed, 52 insertions, 9 deletions
diff --git a/src/main/java/moe/nea/ledger/BazaarOrderDetection.kt b/src/main/java/moe/nea/ledger/BazaarOrderDetection.kt
index 99a4213..1cb23bc 100644
--- a/src/main/java/moe/nea/ledger/BazaarOrderDetection.kt
+++ b/src/main/java/moe/nea/ledger/BazaarOrderDetection.kt
@@ -4,13 +4,6 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.regex.Pattern
class BazaarOrderDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) {
- /*
- [Bazaar] Buy Order Setup! 160x Wheat for 720.0 coins.
- [Bazaar] Claimed 160x Wheat worth 720.0 coins bought for 4.5 each!
-
- [Bazaar] Sell Offer Setup! 160x Wheat for 933.4 coins.
- [Bazaar] Claimed 34,236,799 coins from selling 176x Hyper Catalyst at 196,741 each!
- */
val buyOrderClaimed =
Pattern.compile("\\[Bazaar] Claimed (?<amount>$SHORT_NUMBER_PATTERN)x (?<what>.*) worth (?<coins>$SHORT_NUMBER_PATTERN) coins? bought for $SHORT_NUMBER_PATTERN each!")
diff --git a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
index 4692a13..548b09d 100644
--- a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
+++ b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
@@ -1,15 +1,22 @@
package moe.nea.ledger
+import com.google.gson.Gson
+import com.google.gson.JsonArray
+import com.google.gson.JsonObject
import net.minecraft.client.Minecraft
import net.minecraft.util.ChatComponentText
+import java.io.File
+import java.text.SimpleDateFormat
import java.time.Instant
+import java.util.*
class LedgerLogger {
fun printOut(text: String) {
Minecraft.getMinecraft().ingameGUI?.chatGUI?.printChatMessage(ChatComponentText(text))
}
- fun logEntry(entry: LedgerEntry) {
+
+ fun printToChat(entry: LedgerEntry) {
printOut(
"""
§e================= TRANSACTION START
@@ -23,12 +30,55 @@ class LedgerLogger {
)
}
+ val entries = JsonArray()
+
+ fun logEntry(entry: LedgerEntry) {
+ entries.add(entry.intoJson())
+ commit()
+ }
+
+ fun commit() {
+ try {
+ file.writeText(gson.toJson(entries))
+ } catch (ex: Exception) {
+ ex.printStackTrace()
+ }
+ }
+
+ val gson = Gson()
+
+ val file: File = run {
+ val folder = File("money-ledger")
+ folder.mkdirs()
+ val date = SimpleDateFormat("yyyy.MM.dd").format(Date())
+
+ generateSequence(0) { it + 1 }
+ .map {
+ if (it == 0)
+ folder.resolve("$date.json")
+ else
+ folder.resolve("$date-$it.json")
+ }
+ .filter { !it.exists() }
+ .first()
+ }
}
+
data class LedgerEntry(
val transactionType: String,
val timestamp: Instant,
val totalTransactionCoins: Double,
val itemId: String? = null,
val itemAmount: Int? = null,
-)
+) {
+ fun intoJson(): JsonObject {
+ return JsonObject().apply {
+ addProperty("transactionType", transactionType)
+ addProperty("timestamp", timestamp.toEpochMilli().toString())
+ addProperty("totalTransactionValue", totalTransactionCoins)
+ addProperty("itemId", itemId)
+ addProperty("itemAmount", itemAmount)
+ }
+ }
+}