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

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.TransactionType
import moe.nea.ledger.events.BeforeGuiAction
import moe.nea.ledger.events.ChatReceived
import moe.nea.ledger.getInternalId
import moe.nea.ledger.getLore
import moe.nea.ledger.unformattedString
import moe.nea.ledger.useMatcher
import moe.nea.ledger.utils.Inject
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent


class KatDetection {
	@Inject
	lateinit var log: LedgerLogger

	@Inject
	lateinit var itemIdProvider: ItemIdProvider

	val giftNameToIdMap = mapOf(
		"flower" to ItemId("KAT_FLOWER"),
		"bouquet" to ItemId("KAT_BOUQUET"),
	)
	val katGift = "\\[NPC\\] Kat: A (?<gift>.*)\\? For me\\? How sweet!".toPattern()

	@SubscribeEvent
	fun onChat(event: ChatReceived) {
		katGift.useMatcher(event.message) {
			val giftName = group("gift")
			val giftId = giftNameToIdMap[giftName]
			log.logEntry(LedgerEntry(
				TransactionType.KAT_TIMESKIP,
				event.timestamp,
				listOf(
					ItemChange.lose(giftId ?: ItemId.NIL, 1)
				)
			))
		}
	}

	val confirmSlot = 9 + 9 + 4
	val petSlot = 9 + 4

	data class PetUpgrade(
		val beforePetId: ItemId,
		val cost: List<Pair<ItemId, Double>>
	)

	var lastPetUpgradeScheduled: PetUpgrade? = null

	@SubscribeEvent
	fun onClick(event: BeforeGuiAction) {
		val slots = event.chestSlots ?: return
		val petItem = slots.lowerChestInventory.getStackInSlot(petSlot) ?: return
		val beforePetId = petItem.getInternalId() ?: return
		val confirmItem = slots.lowerChestInventory.getStackInSlot(confirmSlot) ?: return
		val lore = confirmItem.getLore()
		val cost = lore.iterator().asSequence()
			.dropWhile { it.unformattedString() != "Cost" }.drop(1)
			.takeWhile { it != "" }
			.map { itemIdProvider.findFromLore(it) ?: Pair(ItemId.NIL, 1.0) }
			.toList()
		lastPetUpgradeScheduled = PetUpgrade(beforePetId, cost)
	}

	val petUpgradeDialogue = "\\[NPC\\] Kat: I'll get your (?<type>.*) upgraded to (?<tier>.*) in no time!".toPattern()
	fun upgradePetTier(itemId: ItemId): ItemId {
		val str = itemId.string.split(";", limit = 2)
		if (str.size == 2) {
			val (type, tier) = str
			val tierT = tier.toIntOrNull()
			if (tierT != null)
				return ItemId(type + ";" + (tierT + 1))
		}
		return itemId
	}

	@SubscribeEvent
	fun onPetUpgrade(event: ChatReceived) {
		petUpgradeDialogue.useMatcher(event.message) {
			val upgrade = lastPetUpgradeScheduled ?: return
			lastPetUpgradeScheduled = null
			log.logEntry(LedgerEntry(
				TransactionType.KAT_UPGRADE,
				event.timestamp,
				listOf(
					ItemChange.lose(upgrade.beforePetId, 1),
					ItemChange.gain(upgradePetTier(upgrade.beforePetId), 1),
				) + upgrade.cost.map { ItemChange.lose(it.first, it.second) },
			))
		}
	}

}