aboutsummaryrefslogtreecommitdiff
path: root/internal/game/hardening_pickup_quality_test.go
blob: 135b5a6f575d09020c95e9af2363d918f424f50d (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package game

// Regression: a picked-up stack of a quality-bearing stackable item may only
// merge into a slot with the same Quality/MaxQuality. Otherwise it takes a
// free slot, mirroring withdrawBankItem in cmd_bank.go, so a merge can never
// silently destroy the picked-up stack's quality.

import (
	"path/filepath"
	"testing"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

const hardeningGemID = "test_gem"

func seedQualityGem(t *testing.T, h *harness) {
	t.Helper()
	writeSeedFile(t, filepath.Join(h.dir, "items", hardeningGemID+".yaml"),
		[]byte("id: test_gem\nname: test gem\nstackable: true\nmax_quality: 5\n"))
}

// gemSlots returns every inventory slot holding the test gem.
func gemSlots(p *player.Player) map[int]*player.InventorySlot {
	found := make(map[int]*player.InventorySlot)
	for i := 0; i < 28; i++ {
		if slot := p.InvSlot(i); slot != nil && slot.ItemID == hardeningGemID {
			found[i] = slot
		}
	}
	return found
}

func TestPickupMismatchedQualityUsesFreeSlot(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice")
	p := h.seedCharacter("alice", "alice")
	seedQualityGem(t, h)
	sess := &net.Session{Conn: newChanConn(), Player: p}
	sess.SetState(net.StateGame)
	h.g.RunOnOwner(func() {
		h.g.ItemStore.Reload(h.dir)
		p.SetInvSlot(0, &player.InventorySlot{ItemID: hardeningGemID, Quantity: 10, Quality: 1, MaxQuality: 5})
		h.g.World.AddGroundItem(p.RoomID, hardeningGemID, 5)

		h.g.doGet(sess, "test gem")

		slots := gemSlots(p)
		if len(slots) != 2 {
			t.Fatalf("gem slots = %d, want the pickup in its own slot: %v", len(slots), slots)
		}
		if low := p.InvSlot(0); low.Quantity != 10 || low.Quality != 1 {
			t.Errorf("existing low-quality stack changed: %+v", low)
		}
		found := false
		for i, slot := range slots {
			if i == 0 {
				continue
			}
			found = true
			if slot.Quantity != 5 || slot.Quality != 5 || slot.MaxQuality != 5 {
				t.Errorf("picked-up stack lost its properties: %+v", slot)
			}
		}
		if !found {
			t.Error("pickup did not take a free slot")
		}
		if got := h.g.World.GroundItems(p.RoomID)[hardeningGemID]; got != 0 {
			t.Errorf("ground gems = %d, want 0", got)
		}
	})
}

func TestPickupMatchingQualityMergesIntoStack(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice")
	p := h.seedCharacter("alice", "alice")
	seedQualityGem(t, h)
	sess := &net.Session{Conn: newChanConn(), Player: p}
	sess.SetState(net.StateGame)
	h.g.RunOnOwner(func() {
		h.g.ItemStore.Reload(h.dir)
		p.SetInvSlot(0, &player.InventorySlot{ItemID: hardeningGemID, Quantity: 10, Quality: 5, MaxQuality: 5})
		h.g.World.AddGroundItem(p.RoomID, hardeningGemID, 5)

		h.g.doGet(sess, "test gem")

		slots := gemSlots(p)
		if len(slots) != 1 {
			t.Fatalf("matching-quality pickup must merge, slots = %v", slots)
		}
		if slot := p.InvSlot(0); slot.Quantity != 15 || slot.Quality != 5 || slot.MaxQuality != 5 {
			t.Errorf("merged stack = %+v, want quantity 15 at quality 5", slot)
		}
	})
}

func TestPickupMismatchedQualityFullInventoryRefused(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice")
	p := h.seedCharacter("alice", "alice")
	seedQualityGem(t, h)
	sess := &net.Session{Conn: newChanConn(), Player: p}
	sess.SetState(net.StateGame)
	h.g.RunOnOwner(func() {
		h.g.ItemStore.Reload(h.dir)
		for i := 0; i < 28; i++ {
			p.SetInvSlot(i, &player.InventorySlot{ItemID: "filler", Quantity: 1})
		}
		p.SetInvSlot(0, &player.InventorySlot{ItemID: hardeningGemID, Quantity: 10, Quality: 1, MaxQuality: 5})
		h.g.World.AddGroundItem(p.RoomID, hardeningGemID, 5)

		h.g.doGet(sess, "test gem")

		if slot := p.InvSlot(0); slot.Quantity != 10 || slot.Quality != 1 {
			t.Errorf("a refused pickup must leave the mismatched stack alone: %+v", slot)
		}
		if got := h.g.World.GroundItems(p.RoomID)[hardeningGemID]; got != 5 {
			t.Errorf("ground gems = %d, want the stack left in place", got)
		}
	})
}