aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/modules/BazaarOrderDetection.kt
blob: 7e611ace0f8f5bb0b50f76d8e4c33864fa2fc821 (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
package moe.nea.ledger.modules

import moe.nea.ledger.events.ChatReceived
import moe.nea.ledger.ItemIdProvider
import moe.nea.ledger.LedgerEntry
import moe.nea.ledger.LedgerLogger
import moe.nea.ledger.SHORT_NUMBER_PATTERN
import moe.nea.ledger.mixin.AccessorGuiEditSign
import moe.nea.ledger.parseShortNumber
import moe.nea.ledger.useMatcher
import moe.nea.ledger.utils.Inject
import net.minecraft.client.gui.inventory.GuiEditSign
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.regex.Pattern

class BazaarOrderDetection @Inject constructor(val ledger: LedgerLogger, val ids: ItemIdProvider) {

    val buyOrderClaimed =
        Pattern.compile("\\[Bazaar] Claimed (?<amount>$SHORT_NUMBER_PATTERN)x (?<what>.*) worth (?<coins>$SHORT_NUMBER_PATTERN) coins? bought for $SHORT_NUMBER_PATTERN each!")
    val sellOrderClaimed =
        Pattern.compile("\\[Bazaar] Claimed (?<coins>$SHORT_NUMBER_PATTERN) coins? from selling (?<amount>$SHORT_NUMBER_PATTERN)x (?<what>.*) at $SHORT_NUMBER_PATTERN each!")
    val orderFlipped =
        Pattern.compile("\\[Bazaar] Order Flipped! (?<amount>$SHORT_NUMBER_PATTERN)x (?<what>.*) for (?<coins>$SHORT_NUMBER_PATTERN) coins? of total expected profit.")
    val previousPricePattern =
        Pattern.compile("(?<price>$SHORT_NUMBER_PATTERN)/u")
    var lastFlippedPreviousPrice = 0.0

    @SubscribeEvent
    fun detectSignFlip(event: GuiScreenEvent.InitGuiEvent) {
        val gui = event.gui
        if (gui !is GuiEditSign) return
        gui as AccessorGuiEditSign
        val text = gui.tileEntity_ledger.signText
        if (text[2].unformattedText != "Previous price:") return
        previousPricePattern.useMatcher(text[3].unformattedText) {
            lastFlippedPreviousPrice = parseShortNumber(group("price"))
        }
    }

    @SubscribeEvent
    fun detectBuyOrders(event: ChatReceived) {
        orderFlipped.useMatcher(event.message) {
            val amount = parseShortNumber(group("amount")).toInt()
            ledger.logEntry(
                LedgerEntry(
                    "BAZAAR_BUY_ORDER",
                    event.timestamp,
                    lastFlippedPreviousPrice * amount,
                    ids.findForName(group("what")),
                    amount,
                )
            )
        }
        buyOrderClaimed.useMatcher(event.message) {
            ledger.logEntry(
                LedgerEntry(
	                "BAZAAR_BUY_ORDER",
	                event.timestamp,
	                parseShortNumber(group("coins")),
	                ids.findForName(group("what")),
	                parseShortNumber(group("amount")).toInt(),
                )
            )
        }
        sellOrderClaimed.useMatcher(event.message) {
            ledger.logEntry(
                LedgerEntry(
	                "BAZAAR_SELL_ORDER",
	                event.timestamp,
	                parseShortNumber(group("coins")),
	                ids.findForName(group("what")),
	                parseShortNumber(group("amount")).toInt(),
                )
            )
        }
    }
}