aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
blob: 56738c5ae1c1b61c19d0f199941e6abfdf079a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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 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
import java.util.*

class LedgerLogger {
    fun printOut(text: String) {
        Minecraft.getMinecraft().ingameGUI?.chatGUI?.printChatMessage(ChatComponentText(text))
    }

    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

    @SubscribeEvent
    fun onProfileSwitch(event: ChatReceived) {
        profileIdPattern.useMatcher(event.message) {
            currentProfile = group("profile")
        }
    }


    fun printToChat(entry: LedgerEntry) {
        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}
            §ePROFILE: §a${currentProfile}
            §e================= TRANSACTION END
            """.trimIndent()
        )
    }

    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) {
        printToChat(entry)
        Ledger.logger.info("Logging entry of type ${entry.transactionType}")
        entries.add(entry.intoJson(currentProfile))
        commit()
    }

    fun commit() {
        try {
            hasRecentlyMerged = false
            file.writeText(gson.toJson(entries))
        } catch (ex: Exception) {
            Ledger.logger.error("Could not save file", ex)
        }
    }

    val gson = Gson()

    val folder = File("money-ledger").also { it.mkdirs() }
    val file: File = run {
        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(profileId: String?): JsonObject {
        return JsonObject().apply {
            addProperty("transactionType", transactionType)
            addProperty("timestamp", timestamp.toEpochMilli().toString())
            addProperty("totalTransactionValue", totalTransactionCoins)
            addProperty("itemId", itemId ?: "")
            addProperty("itemAmount", itemAmount ?: 0)
            addProperty("profileId", profileId)
            addProperty(
                "playerId",
                (Minecraft.getMinecraft().thePlayer?.uniqueID?.toString() ?: lastKnownUUID).also { lastKnownUUID = it })
        }
    }
}

var lastKnownUUID = "null"