aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-02-18 17:59:20 +0100
committerLinnea Gräf <nea@nea.moe>2024-02-18 17:59:20 +0100
commit640456c11d91ef1815c57ade5773a9d917ebcb71 (patch)
tree35a855c3cad7eb313a196493ab213f28fa8250b9 /src
parent4ea303d3e81f8c644d37d70744eaf0dc55b3f855 (diff)
downloadmoney-ledger-640456c11d91ef1815c57ade5773a9d917ebcb71.tar.gz
money-ledger-640456c11d91ef1815c57ade5773a9d917ebcb71.tar.bz2
money-ledger-640456c11d91ef1815c57ade5773a9d917ebcb71.zip
Add auto merger
Diffstat (limited to 'src')
-rw-r--r--src/main/kotlin/moe/nea/ledger/Ledger.kt1
-rw-r--r--src/main/kotlin/moe/nea/ledger/LedgerLogger.kt31
2 files changed, 30 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt
index 108c238..0164f3b 100644
--- a/src/main/kotlin/moe/nea/ledger/Ledger.kt
+++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt
@@ -47,6 +47,7 @@ class Ledger {
listOf(
this,
ids,
+ ledger,
BankDetection(ledger),
BazaarDetection(ledger, ids),
BazaarOrderDetection(ledger, ids),
diff --git a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
index 5265f2a..83268e7 100644
--- a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
+++ b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
@@ -5,6 +5,8 @@ import com.google.gson.JsonArray
import com.google.gson.JsonObject
import net.minecraft.client.Minecraft
import net.minecraft.util.ChatComponentText
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent
import java.io.File
import java.text.SimpleDateFormat
import java.time.Instant
@@ -31,6 +33,31 @@ class LedgerLogger {
}
val entries = JsonArray()
+ var hasRecentlyMerged = false
+ var lastMergeTime = System.currentTimeMillis()
+
+ fun doMerge() {
+ val allFiles = folder.listFiles()?.toList() ?: emptyList()
+ val mergedJson = allFiles
+ .filter { it.name != "merged.json" && it.extension == "json" }
+ .sortedDescending()
+ .map { it.readText().trim().removePrefix("[").removeSuffix("]") }
+ .joinToString(",", "[", "]")
+ folder.resolve("merged.json").writeText(mergedJson)
+ hasRecentlyMerged = true
+ }
+
+ init {
+ Runtime.getRuntime().addShutdownHook(Thread { doMerge() })
+ }
+
+ @SubscribeEvent
+ fun onTick(event: ClientTickEvent) {
+ if (!hasRecentlyMerged && (System.currentTimeMillis() - lastMergeTime) > 60_000L) {
+ lastMergeTime = System.currentTimeMillis()
+ doMerge()
+ }
+ }
fun logEntry(entry: LedgerEntry) {
Ledger.logger.info("Logging entry of type ${entry.transactionType}")
@@ -40,6 +67,7 @@ class LedgerLogger {
fun commit() {
try {
+ hasRecentlyMerged = false
file.writeText(gson.toJson(entries))
} catch (ex: Exception) {
Ledger.logger.error("Could not save file", ex)
@@ -48,9 +76,8 @@ class LedgerLogger {
val gson = Gson()
+ val folder = File("money-ledger").also { it.mkdirs() }
val file: File = run {
- val folder = File("money-ledger")
- folder.mkdirs()
val date = SimpleDateFormat("yyyy.MM.dd").format(Date())
generateSequence(0) { it + 1 }