aboutsummaryrefslogtreecommitdiff
path: root/go/game/action_test.go
blob: 400717e74e139c11e5dd79dbc1c17e3de30ffe1b (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package game

import (
	"errors"
	"strings"
	"testing"

	"muhq.space/muhqs-game/go/utils"
)

const PASS_STRING = "p\u00A0pass"

func TestUpkeepAction(t *testing.T) {
	s, _, p, o := newMockState()
	m, _ := GetMap("2P-ring-street")
	s.SetMap(m)

	archer := s.addNewUnit(NewCard("base/archer"), Position{0, 0}, p)
	farmer := s.addNewUnit(NewCard("misc/farmer"), Position{0, 1}, p)
	// Change controller
	knight := s.addNewUnit(NewCard("base/knight"), Position{0, 2}, o)
	knight.controller = p

	king := s.addNewUnit(NewCard("misc/king"), Position{2, 2}, o)

	n := newUpkeepPrompt(p)
	ctx := n.Context.(TargetSelectionCtx)
	if ctx.Action.Source().(*Player) != p {
		t.Fatal("unexpected player ", p)
	}

	trgts := ctx.Action.Targets()
	if !trgts.AllowSelection() {
		t.Fatal("upkeep action does not allow target selection")
	}

	if trgts.RequireSelection() {
		t.Fatal("upkeep action does require selection")
	}

	opts := trgts.Options()
	if !utils.InterfaceSliceContains(opts, archer) || !utils.InterfaceSliceContains(opts, farmer) || !utils.InterfaceSliceContains(opts, farmer) {
		t.Fatal("Upkeep action targets does not contain the controlled units")
	}

	if utils.InterfaceSliceContains(opts, king) {
		t.Fatal("Upkeep action targets contains uncontrolled units")
	}
}

func TestMarshalPass(t *testing.T) {
	_, _, p, _ := newMockState()
	exp := PASS_STRING
	a := NewPassPriority(p)
	is := string(MarshalAction(a))
	if is != exp {
		t.Fatalf("expected string %s does not match %s\n", exp, is)
	}
}

func TestUnmarshalPass(t *testing.T) {
	s, _, _, _ := newMockState()
	in := PASS_STRING
	a, err := UnmarshalAction(s, []byte(in))
	if err != nil {
		t.Fatal(err)
	}
	if _, ok := a.(*PassPriority); !ok {
		t.Fatalf("expexted PassPriority not %s", a)
	}
}

const PICK_ACTION_STRING = "p\u00A0pick\u00A0base/archer:[base/archer, magic/more!]"

func TestMarshalPickAction(t *testing.T) {
	_, _, p, _ := newMockState()
	exp := PICK_ACTION_STRING
	poc := NewPileOfCards()
	c := NewCard("base/archer")
	poc.AddCard(c)
	poc.AddCard(NewCard("magic/more!"))
	a := NewDraftPick(p, poc, c)
	is := string(MarshalAction(a))
	if is != exp {
		t.Fatalf("expected string %s does not match %s\n", exp, is)
	}
}

func TestUnmarshalPickAction(t *testing.T) {
	s, _, _, _ := newMockState()
	in := PICK_ACTION_STRING
	a, err := UnmarshalAction(s, []byte(in))
	if err != nil {
		t.Fatal(err)
	}
	var p *DraftPick
	var ok bool
	if p, ok = a.(*DraftPick); !ok {
		t.Fatalf("expexted DraftPick not %v", a)
	}
	if len(p.pack.Cards()) != 2 {
		t.Fatalf("expexted two cards in pack not %d", len(p.pack.Cards()))
	}
	if p.pick.Name != "Archer" {
		t.Fatalf("expexted archer beeing picked not %s", p.pick.Path())
	}
}

func TestAttackAction(t *testing.T) {
	mapDef := `map: |+1
 H T
 SSS
    
symbols:
  T: tower
  H: house
  S: street
`
	s, _, p, o := newMockState()
	m, _ := readMap(strings.NewReader(mapDef))
	s.SetMap(m)

	t1 := s.addNewUnit(NewCard("base/fighter"), Position{0, 0}, o)
	t2 := s.addNewUnit(NewCard("base/fighter"), Position{0, 1}, o)
	t3 := s.addNewUnit(NewCard("base/fighter"), Position{0, 2}, o)

	a := s.addNewUnit(NewCard("base/archer"), Position{2, 0}, p)
	ca := s.addNewUnit(NewCard("base/cavalry_archer"), Position{2, 1}, p)

	aa1 := NewAttackAction(a)
	err := aa1.Target().AddSelection(t1)
	if !errors.Is(err, ErrTargetRangeProtected) {
		t.Fatal("protected from range combat not detected")
	}

	aa2 := NewAttackAction(ca)
	err = aa2.Target().AddSelection(t2)
	if err != nil {
		t.Fatal("unexpected target error:", err)
	}
	s.ResolveAction(aa2)
	if t2.Damage() != 1 {
		t.Fatal("fighter did not took 1 damage")
	}
	if ca.Damage() > 0 {
		t.Fatal("cavalry archer took damage in range combat from melee unit")
	}

	aa3 := NewAttackAction(a)
	err = aa3.Target().AddSelection(t2)
	if err != nil {
		t.Fatal("unexpected target error:", err)
	}
	s.ResolveAction(aa3)
	if t2.Damage() != 2 {
		t.Fatal("fighter did not took 1 damage")
	}
	if !t2.IsDestroyed() {
		t.Fatal("fighter is still alive")
	}
	if ca.Damage() > 0 {
		t.Fatal("archer took damage in range combat from melee unit")
	}

	aa4 := NewAttackAction(a)
	err = aa4.Target().AddSelection(t3)
	if err != nil {
		t.Fatal("unexpected target error:", err)
	}
	s.ResolveAction(aa4)
	if t3.Damage() != 1 {
		t.Fatal("fighter did not took 1 damage")
	}
	if ca.Damage() > 0 {
		t.Fatal("archer took damage in range combat from melee unit")
	}
}

func TestStreetAction(t *testing.T) {
	mapDef := `map: |+1
 HSH
 HSH
 HSS
 HSH

symbols:
  H: house
  S: street
`
	s := NewLocalState()
	m, _ := readMap(strings.NewReader(mapDef))
	s.SetMap(m)

	p := s.AddNewPlayer("player", NewDeck())

	c := s.addNewUnit(NewCard("base/cavalry"), Position{1, 0}, p)

	a := NewStreetAction(c)
	opts := a.Target().Options()
	if len(opts) != 4 {
		t.Fatal("Unexpected amount of street targets", len(opts))
	}
}