aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-02-15 18:05:28 +0100
committerLinnea Gräf <nea@nea.moe>2024-02-15 18:05:28 +0100
commitee5d205578117d6fab9b2f89871e5442e480644f (patch)
tree60753f880972e85e39ed9c1e98bb534253cbd535 /src/main/kotlin/moe
downloadLocalTransactionLedger-ee5d205578117d6fab9b2f89871e5442e480644f.tar.gz
LocalTransactionLedger-ee5d205578117d6fab9b2f89871e5442e480644f.tar.bz2
LocalTransactionLedger-ee5d205578117d6fab9b2f89871e5442e480644f.zip
Initial commit
Diffstat (limited to 'src/main/kotlin/moe')
-rw-r--r--src/main/kotlin/moe/nea/ledger/BankDetection.kt40
-rw-r--r--src/main/kotlin/moe/nea/ledger/ChatReceived.kt15
-rw-r--r--src/main/kotlin/moe/nea/ledger/Ledger.kt51
-rw-r--r--src/main/kotlin/moe/nea/ledger/LedgerLogger.kt34
-rw-r--r--src/main/kotlin/moe/nea/ledger/NumberUtil.kt32
5 files changed, 172 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/BankDetection.kt b/src/main/kotlin/moe/nea/ledger/BankDetection.kt
new file mode 100644
index 0000000..6e54539
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/BankDetection.kt
@@ -0,0 +1,40 @@
+package moe.nea.ledger
+
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import java.util.regex.Pattern
+
+class BankDetection(val ledger: LedgerLogger) {
+
+ /*
+ You have withdrawn 1M coins! You now have 518M coins in your account!
+ You have deposited 519M coins! You now have 519M coins in your account!
+ */
+
+
+ val withdrawPattern =
+ Pattern.compile("^You have withdrawn (?<amount>$SHORT_NUMBER_PATTERN) coins?! You now have (?<newtotal>$SHORT_NUMBER_PATTERN) coins? in your account!$")
+ val depositPattern =
+ Pattern.compile("^You have deposited (?<amount>$SHORT_NUMBER_PATTERN) coins?! You now have (?<newtotal>$SHORT_NUMBER_PATTERN) coins? in your account!$")
+ @SubscribeEvent
+ fun onChat(event: ChatReceived) {
+ withdrawPattern.useMatcher(event.message) {
+ ledger.logEntry(
+ LedgerEntry(
+ "BANK_WITHDRAW",
+ event.timestamp,
+ parseShortNumber(group("amount")),
+ )
+ )
+ }
+ depositPattern.useMatcher(event.message) {
+ ledger.logEntry(
+ LedgerEntry(
+ "BANK_DEPOSIT",
+ event.timestamp,
+ parseShortNumber(group("amount")),
+ )
+ )
+ }
+ }
+
+}
diff --git a/src/main/kotlin/moe/nea/ledger/ChatReceived.kt b/src/main/kotlin/moe/nea/ledger/ChatReceived.kt
new file mode 100644
index 0000000..5e19083
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/ChatReceived.kt
@@ -0,0 +1,15 @@
+package moe.nea.ledger
+
+import net.minecraftforge.client.event.ClientChatReceivedEvent
+import net.minecraftforge.fml.common.eventhandler.Event
+import java.time.Instant
+
+data class ChatReceived(
+ val message: String,
+ val timestamp: Instant = Instant.now()
+) : Event() {
+ constructor(event: ClientChatReceivedEvent) : this(
+ event.message.unformattedText
+ .replace("§.".toRegex(), "")
+ )
+} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt
new file mode 100644
index 0000000..63be626
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt
@@ -0,0 +1,51 @@
+package moe.nea.ledger
+
+import net.minecraftforge.client.event.ClientChatReceivedEvent
+import net.minecraftforge.common.MinecraftForge
+import net.minecraftforge.fml.common.Mod
+import net.minecraftforge.fml.common.event.FMLInitializationEvent
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+
+@Mod(modid = "ledger", useMetadata = true)
+class Ledger {
+ /*
+ You have withdrawn 1M coins! You now have 518M coins in your account!
+ You have deposited 519M coins! You now have 519M coins in your account!
+
+ // ORDERS:
+
+ [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!
+
+ // INSTABUY:
+
+ [Bazaar] Bought 64x Wheat for 377.6 coins!
+ [Bazaar] Sold 64x Wheat for 268.8 coins!
+
+ // AUCTION HOUSE:
+
+ You collected 8,712,000 coins from selling Ultimate Carrot Candy Upgrade to [VIP] kodokush in an auction!
+ You purchased 2x Walnut for 69 coins!
+ You purchased ◆ Ice Rune I for 4,000 coins!
+
+ TODO: TRADING, FORGE, COOKIE_EATEN, NPC_SELL, NPC_BUY
+ */
+
+ @Mod.EventHandler
+ fun init(event: FMLInitializationEvent) {
+ val ledger = LedgerLogger()
+ listOf(
+ this,
+ BankDetection(ledger),
+ ).forEach(MinecraftForge.EVENT_BUS::register)
+ }
+
+ @SubscribeEvent
+ fun onChat(event: ClientChatReceivedEvent) {
+ if (event.type != 2.toByte())
+ MinecraftForge.EVENT_BUS.post(ChatReceived(event))
+ }
+}
diff --git a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
new file mode 100644
index 0000000..4692a13
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt
@@ -0,0 +1,34 @@
+package moe.nea.ledger
+
+import net.minecraft.client.Minecraft
+import net.minecraft.util.ChatComponentText
+import java.time.Instant
+
+class LedgerLogger {
+ fun printOut(text: String) {
+ Minecraft.getMinecraft().ingameGUI?.chatGUI?.printChatMessage(ChatComponentText(text))
+ }
+
+ fun logEntry(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}
+ §e================= TRANSACTION END
+ """.trimIndent()
+ )
+ }
+
+}
+
+data class LedgerEntry(
+ val transactionType: String,
+ val timestamp: Instant,
+ val totalTransactionCoins: Double,
+ val itemId: String? = null,
+ val itemAmount: Int? = null,
+)
diff --git a/src/main/kotlin/moe/nea/ledger/NumberUtil.kt b/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
new file mode 100644
index 0000000..8aa9bb8
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
@@ -0,0 +1,32 @@
+package moe.nea.ledger
+
+import java.util.regex.Matcher
+import java.util.regex.Pattern
+
+// language=regexp
+val SHORT_NUMBER_PATTERN = "[0-9]+(?:,[0-9]+)*(?:\\.[0-9]+)?[kKmMbB]?"
+
+val siScalars = mapOf(
+ 'k' to 1_000.0,
+ 'K' to 1_000.0,
+ 'm' to 1_000_000.0,
+ 'M' to 1_000_000.0,
+ 'b' to 1_000_000_000.0,
+ 'B' to 1_000_000_000.0,
+)
+
+fun parseShortNumber(string: String): Double {
+ var k = string.replace(",", "")
+ val scalar = k.last()
+ var scalarMultiplier = siScalars[scalar]
+ if (scalarMultiplier == null) {
+ scalarMultiplier = 1.0
+ } else {
+ k = k.dropLast(1)
+ }
+ return k.toDouble() * scalarMultiplier
+}
+
+fun <T> Pattern.useMatcher(string: String, block: Matcher.() -> T): T? =
+ matcher(string).takeIf { it.matches() }?.let(block)
+