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

import moe.nea.ledger.DebouncedValue
import moe.nea.ledger.ItemChange
import moe.nea.ledger.ItemId
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.TransactionType
import moe.nea.ledger.events.ChatReceived
import moe.nea.ledger.parseShortNumber
import moe.nea.ledger.useMatcher
import moe.nea.ledger.utils.di.Inject
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import kotlin.time.Duration.Companion.seconds

class DragonSacrificeDetection {
	//SACRIFICE! You turned Holy Dragon Boots into 30 Dragon Essence!
	//BONUS LOOT! You also received 17x Holy Dragon Fragment from your sacrifice!
	@Inject
	lateinit var itemIdProvider: ItemIdProvider

	@Inject
	lateinit var logger: LedgerLogger

	val sacrificePattern =
		"SACRIFICE! You turned (?<sacrifice>.*) into (?<amount>$SHORT_NUMBER_PATTERN) Dragon Essence!".toPattern()
	val bonusLootPattern = "BONUS LOOT! You also received (?<bonus>.*) from your sacrifice!".toPattern()

	var lastSacrifice: DebouncedValue<LedgerEntry> = DebouncedValue.farFuture()


	@SubscribeEvent
	fun onChat(event: ChatReceived) {
		sacrificePattern.useMatcher(event.message) {
			val sacrifice = itemIdProvider.findForName(group("sacrifice")) ?: return
			val lootEssence = parseShortNumber(group("amount"))
			consume(lastSacrifice.replace())
			lastSacrifice = DebouncedValue(LedgerEntry(
				TransactionType.DRACONIC_SACRIFICE,
				event.timestamp,
				listOf(
					ItemChange.lose(sacrifice, 1),
					ItemChange.gain(ItemId.DRAGON_ESSENCE, lootEssence)
				)
			))
		}
		bonusLootPattern.useMatcher(event.message) {
			val bonusItem = itemIdProvider.findStackableItemByName(
				group("bonus"), true
			) ?: return
			lastSacrifice.replace()?.let {
				consume(
					it.copy(items = it.items + ItemChange.unpairGain(bonusItem))
				)
			}
		}
	}

	@SubscribeEvent
	fun onTick(event: TickEvent) {
		consume(lastSacrifice.consume(4.seconds))
	}

	fun consume(entry: LedgerEntry?) {
		if (entry != null)
			logger.logEntry(entry)
	}
}