D&D: wire the Open5e magic-item registry into live gameplay

Magic items now reach players through three surfaces: zone loot drops,
Luigi's "Curios" shelf, and combat effects. Effects are formulaic
(Rarity scalar x Kind), mirroring the bestiary tuning pass, with an
empty magicItemEffectOverlay as the hand-authored refinement path.

- magic_items_gameplay.go: rarity index, magic_item_equipped persistence
  (new table, DnDSlot-keyed), codified effect formula, applyMagicItemEffects
  combat hook, potion/scroll -> ConsumableDef bridge, !adventure equip-magic
- dropZoneLoot: 15% magic-item substitution roll by tier rarity
- Luigi's Curios category: daily UTC-seeded 8-item rotation
- combat_bridge / combat_session_build: applyMagicItemEffects after passives
- consumableDefByName falls through so loot/shop potions auto-resolve
- renderDnDSheet: new Magic Items section

Equippable items live entirely in the DnDSlot scheme, separate from the
legacy tier-gear. Attunement items equip inert until attuned (3-slot cap).
This commit is contained in:
prosolis
2026-05-14 18:38:57 -07:00
parent 297ce3d786
commit 0d666beea3
12 changed files with 996 additions and 10 deletions

View File

@@ -0,0 +1,216 @@
package plugin
import (
"testing"
"gogobee/internal/db"
"maunium.net/go/mautrix/id"
)
// TestMagicItemsByRarityComplete — every registry item lands in exactly one
// rarity bucket and the buckets sum back to the registry size.
func TestMagicItemsByRarityComplete(t *testing.T) {
idx := magicItemsByRarity()
total := 0
for _, items := range idx {
total += len(items)
}
if total != len(magicItemRegistry) {
t.Errorf("rarity buckets sum to %d, registry has %d", total, len(magicItemRegistry))
}
for r, items := range idx {
for _, mi := range items {
if mi.Rarity != r {
t.Errorf("item %s (rarity %s) bucketed under %s", mi.ID, mi.Rarity, r)
}
}
}
}
// TestPickMagicItemForRarity — picks respect the requested rarity, and the
// Epic bucket folds in VeryRare items (the §5 loot tiers never emit VeryRare).
func TestPickMagicItemForRarity(t *testing.T) {
for _, r := range []DnDRarity{RarityCommon, RarityUncommon, RarityRare, RarityEpic, RarityLegendary} {
mi, ok := pickMagicItemForRarity(r, nil)
if !ok {
continue // a rarity may legitimately be empty
}
if r == RarityEpic {
if mi.Rarity != RarityEpic && mi.Rarity != RarityVeryRare {
t.Errorf("Epic pick returned rarity %s", mi.Rarity)
}
} else if mi.Rarity != r {
t.Errorf("pick for %s returned rarity %s", r, mi.Rarity)
}
}
}
// TestMagicItemEffectFormula — the codified Rarity+Kind formula produces the
// expected shape of delta per kind, and rarer items hit harder.
func TestMagicItemEffectFormula(t *testing.T) {
weakWeapon := MagicItem{Kind: MagicItemWeapon, Rarity: RarityCommon}
strongWeapon := MagicItem{Kind: MagicItemWeapon, Rarity: RarityLegendary}
we, se := magicItemEffectFor(weakWeapon), magicItemEffectFor(strongWeapon)
if we.DamageBonus <= 0 || se.DamageBonus <= we.DamageBonus {
t.Errorf("weapon DamageBonus not monotonic: common=%v legendary=%v", we.DamageBonus, se.DamageBonus)
}
armor := magicItemEffectFor(MagicItem{Kind: MagicItemArmor, Rarity: RarityRare})
if armor.DamageReductMult >= 1.0 || armor.DamageReductMult <= 0 {
t.Errorf("armor DamageReductMult should be in (0,1), got %v", armor.DamageReductMult)
}
staff := magicItemEffectFor(MagicItem{Kind: MagicItemStaff, Rarity: RarityRare})
if staff.FlatDmgStart <= 0 {
t.Errorf("staff should grant FlatDmgStart, got %d", staff.FlatDmgStart)
}
potion := magicItemEffectFor(MagicItem{Kind: MagicItemPotion, Rarity: RarityRare})
if potion != (magicItemEffect{DamageReductMult: 1.0}) {
t.Errorf("potion should have a neutral equip effect, got %+v", potion)
}
}
// TestMagicItemEffectOverlayWins — the hand-authored overlay beats the formula.
func TestMagicItemEffectOverlayWins(t *testing.T) {
saved := magicItemEffectOverlay
defer func() { magicItemEffectOverlay = saved }()
magicItemEffectOverlay = map[string]magicItemEffect{
"test_item": {DamageBonus: 9.99, DamageReductMult: 1.0},
}
got := magicItemEffectFor(MagicItem{ID: "test_item", Kind: MagicItemWeapon, Rarity: RarityCommon})
if got.DamageBonus != 9.99 {
t.Errorf("overlay did not win: got DamageBonus %v", got.DamageBonus)
}
}
// TestMagicItemConsumableBridge — potion/scroll items classify onto the
// consumable pipeline; everything else returns nil.
func TestMagicItemConsumableBridge(t *testing.T) {
healPotion := MagicItem{Name: "Potion of Greater Healing", Kind: MagicItemPotion, Rarity: RarityUncommon, Value: 100}
def := magicItemConsumableDef(healPotion)
if def == nil || def.Effect != EffectHeal || def.Value <= 0 || def.Buyable {
t.Errorf("healing potion classified wrong: %+v", def)
}
scroll := MagicItem{Name: "Scroll of Fireball", Kind: MagicItemScroll, Rarity: RarityRare, Value: 200}
if d := magicItemConsumableDef(scroll); d == nil || d.Effect != EffectFlatDmg {
t.Errorf("fireball scroll should be FlatDmg: %+v", d)
}
if d := magicItemConsumableDef(MagicItem{Kind: MagicItemWeapon, Rarity: RarityRare}); d != nil {
t.Errorf("weapon should not produce a ConsumableDef, got %+v", d)
}
}
// TestConsumableDefByNameFallsThrough — a real registry potion resolves
// through consumableDefByName even though it's not in consumableDefs.
func TestConsumableDefByNameFallsThrough(t *testing.T) {
var sample MagicItem
for _, mi := range magicItemRegistry {
if mi.Kind == MagicItemPotion {
sample = mi
break
}
}
if sample.Name == "" {
t.Skip("no potion in registry to exercise the fall-through")
}
if def := consumableDefByName(sample.Name); def == nil {
t.Errorf("consumableDefByName(%q) returned nil — fall-through broken", sample.Name)
}
}
// TestDailyCuriosStock — the shelf is the right size, deterministic within a
// run, and only holds real registry items.
func TestDailyCuriosStock(t *testing.T) {
a := dailyCuriosStock()
b := dailyCuriosStock()
if len(a) != curiosStockSize {
t.Errorf("curios stock size = %d, want %d", len(a), curiosStockSize)
}
for i := range a {
if a[i].ID != b[i].ID {
t.Errorf("curios stock not deterministic at %d: %s vs %s", i, a[i].ID, b[i].ID)
}
if _, ok := magicItemRegistry[a[i].ID]; !ok {
t.Errorf("curios stock item %s not in registry", a[i].ID)
}
}
}
// TestMagicItemSellTyping — potions/scrolls land as "consumable", wearables as
// "magic_item", and Value carries the rarity-bracket coin baseline.
func TestMagicItemSellTyping(t *testing.T) {
potion := magicItemSell(MagicItem{Name: "P", Kind: MagicItemPotion, Rarity: RarityCommon, Value: 50})
if potion.Type != "consumable" {
t.Errorf("potion AdvItem type = %q, want consumable", potion.Type)
}
ring := magicItemSell(MagicItem{Name: "R", Kind: MagicItemRing, Rarity: RarityRare, Value: 500})
if ring.Type != "magic_item" || ring.Value != 500 {
t.Errorf("ring AdvItem wrong: %+v", ring)
}
}
// TestEquippedMagicItemRoundTrip exercises the DB persistence layer: equip,
// load, attunement counting, unequip.
func TestEquippedMagicItemRoundTrip(t *testing.T) {
dir := t.TempDir()
db.Close()
if err := db.Init(dir); err != nil {
t.Fatal(err)
}
t.Cleanup(db.Close)
user := id.UserID("@magic:test.invalid")
// Pick a real attunement item and a real non-attunement item.
var attItem, plainItem MagicItem
for _, mi := range magicItemRegistry {
if mi.Slot == "" {
continue
}
if mi.Attunement && attItem.ID == "" {
attItem = mi
}
if !mi.Attunement && plainItem.ID == "" {
plainItem = mi
}
}
if attItem.ID == "" || plainItem.ID == "" {
t.Skip("registry lacks both an attunement and a non-attunement slotted item")
}
if err := equipMagicItem(user, attItem.Slot, attItem.ID, true); err != nil {
t.Fatalf("equip attunement item: %v", err)
}
// Equip the plain item into a distinct slot so it doesn't overwrite the
// attunement item (the classifier may give both the same slot).
plainSlot := DnDSlotRing1
if plainSlot == attItem.Slot {
plainSlot = DnDSlotRing2
}
if err := equipMagicItem(user, plainSlot, plainItem.ID, false); err != nil {
t.Fatalf("equip plain item: %v", err)
}
equipped, err := loadEquippedMagicItems(user)
if err != nil {
t.Fatalf("load: %v", err)
}
if len(equipped) != 2 {
t.Fatalf("expected 2 equipped items, got %d", len(equipped))
}
if countAttunedMagicItems(equipped) != 1 {
t.Errorf("attuned count = %d, want 1", countAttunedMagicItems(equipped))
}
if err := unequipMagicItem(user, attItem.Slot); err != nil {
t.Fatalf("unequip: %v", err)
}
equipped, _ = loadEquippedMagicItems(user)
if countAttunedMagicItems(equipped) != 0 {
t.Errorf("attuned count after unequip = %d, want 0", countAttunedMagicItems(equipped))
}
}