aboutsummaryrefslogtreecommitdiff
path: root/internal/game/correct_combine_skill_test.go
blob: 7d9c7256c61e697f15719030e9ef764983eef28d (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 game

// Regression: combine recipes declare type "combine", which is an action and
// not a skill. Resolving the recipe type straight to a player.SkillName made
// p.Level read a map miss (level 1) for the success roll and made awardSkillXP
// persist a phantom "combine" skill entry.

import (
	"path/filepath"
	"testing"

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

const correctCombineYAML = `id: correct_combine_out
name: correct combine out
stackable: false
value: 5
craft:
  - type: combine
    ingredients:
      - items:
          - correct_combine_part
        quantity: 1
    xp: 7
    output_qty: 1
    ticks_per_cycle: 1
`

func TestCombineSkillResolvesARealSkill(t *testing.T) {
	for _, tc := range []struct {
		name string
		typ  string
		want player.SkillName
	}{
		{"combine is an action, not a skill", "combine", player.Crafting},
		{"empty type defaults to combine", "", player.Crafting},
		{"a declared real skill is honored", "smithing", player.Smithing},
	} {
		t.Run(tc.name, func(t *testing.T) {
			got := combineSkill(&item.CraftDef{Type: tc.typ})
			if got != tc.want {
				t.Fatalf("combineSkill(type %q) = %q, want %q", tc.typ, got, tc.want)
			}
			if !player.ValidSkill(got) {
				t.Fatalf("combineSkill(type %q) resolved to unknown skill %q", tc.typ, got)
			}
		})
	}
}

func TestCombineXPGoesToCraftingNotAPhantomSkill(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice")
	p := h.seedCharacter("alice", "alice")
	writeSeedFile(t, filepath.Join(h.dir, "items", "correct_combine_out.yaml"), []byte(correctCombineYAML))
	writeSeedFile(t, filepath.Join(h.dir, "items", "correct_combine_part.yaml"),
		[]byte("id: correct_combine_part\nname: correct combine part\nstackable: false\nvalue: 1\n"))

	conn := newChanConn()
	sess := &net.Session{Conn: conn, Player: p}
	sess.SetState(net.StateGame)

	startCrafting := p.Skills[player.Crafting]
	h.g.RunOnOwner(func() {
		h.g.ItemStore.Reload(h.dir)
		p.SetInvSlot(0, &player.InventorySlot{ItemID: "correct_combine_part", Quantity: 1})
		def := mustLoadItem(t, h.g, "correct_combine_out")
		h.g.startCombine(sess, p, def, def.FirstCraft(), 1, 0)
		if p.Action == nil {
			t.Fatal("combine did not start")
		}
		h.g.advanceCombine(sess, p) // phase 0 -> 1
		h.g.advanceCombine(sess, p) // phase 1 -> 2
		h.g.advanceCombine(sess, p) // phase 2: consume + output

		if got := p.CountItem("correct_combine_out"); got != 1 {
			t.Fatalf("combine output = %d, want 1", got)
		}
		if got := p.Skills[player.Crafting]; got != startCrafting+7 {
			t.Errorf("crafting xp = %d, want %d", got, startCrafting+7)
		}
		if got, ok := p.Skills[player.SkillName("combine")]; ok {
			t.Errorf("combine recipes persist a phantom skill: combine = %d", got)
		}
	})

	saved, err := h.g.AccountStore.LoadCharacter("alice")
	if err != nil {
		t.Fatalf("load character: %v", err)
	}
	if got, ok := saved.Skills[player.SkillName("combine")]; ok {
		t.Errorf("phantom combine skill was persisted: combine = %d", got)
	}
	if got := saved.Skills[player.Crafting]; got != startCrafting+7 {
		t.Errorf("persisted crafting xp = %d, want %d", got, startCrafting+7)
	}
}